0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ilasmでstack machine その4

Last updated at Posted at 2019-12-27

概要

ilasmでstack machineやってみた。
先人に、教えを乞うた。

参考にしたページ

テストコード

int add(int a, int b) {
    return (a+b);
}

void main() {
    int sum, n = 1;
    int wa;

    wa = add(12, 23);

    sum = 0;
    while (n <= 10) { 
        sum = sum + n;
        n = n + 1;
    }
    printf("add(12,23)=%d sum=%d", wa, sum);
}

結果

C:\mh\mcc>ci0 test.c -code
0: func str add
1: label int 1
2: entry
3: addsp int 0
4: push stack-val 2
5: push stack-val 3
6: add
7: ret
8: func str main
9: label int 2
10: entry
11: addsp int -3
12: push stack-ref -2
13: push int 1
14: mov
15: push stack-ref -3
16: push int 23
17: push int 12
18: call int 1
19: addsp int 2
20: mov
21: push stack-ref -1
22: push int 0
23: mov
24: label int 3
25: push stack-val -2
26: push int 10
27: le
28: jz int 4
29: push stack-ref -1
30: push stack-val -1
31: push stack-val -2
32: add
33: mov
34: push stack-ref -2
35: push stack-val -2
36: push int 1
37: add
38: mov
39: jmp int 3
40: label int 4
41: push stack-val -1
42: push stack-val -3
43: push str "add(12,23)=%d sum=%d"
44: call str printf
45: addsp int 3
46: ret
add(12,23)=35 sum=55

ニーモニック

ニーモニック 意味 stackの状態
push stackに積む。 1個増える
entry エントリー 変化無し
pop stackから、取り出す。 1個減る
mov ファンクションに飛ぶ。 変化無し
add stack-1,stackの加算を求める。 1個減る
addsp stackの操作。 1個減る
sub stack-1,stackの減算を求める。 1個減る
mul stack-1,stackの乗算を求める。 1個減る
div stack-1,stackの除算を求める。 1個減る
mod stack-1,stackの剰余を求める。 1個減る
ret サブルーチンから戻る。 1個減る
call サブルーチンに行く。 1個増える
jz stack-1,stackが真ならtugiへ飛ぶ。 1個減る
jmp 無条件でtugiへ飛ぶ。 変化無し
cmp stack-1,stackを比較する。 1個減る
lt stack-1,stackが大ならtugiへ飛ぶ。 1個減る
gt stack-1,stackが小ならtugiへ飛ぶ。 1個減る
le stack-1,stackが真ならtugiへ飛ぶ。 1個減る
ge stack-1,stackが偽ならtugiへ飛ぶ。 1個減る
eq stack-1,stackが同じならtugiへ飛ぶ。 1個減る
ne stack-1,stackが違うならtugiへ飛ぶ。 1個減る
func ファンクション 変化無し
label ラベル 変化無し

書き直し

   func "add"
label1:
   entry
   addsp 0
   push stack-val 2
   push stack-val 3
   add
   ret

   func "main"
label2:
   entry
   addsp -3
   push stack-ref -2
   push 1
   mov
   push stack-ref -3
   push 23
   push 12
   call label1
   addsp 2
   mov
   push stack-ref -1
   push 0
   mov
label3:
   push stack-val -2
   push 10
   le
   jz label4

   push stack-ref -1
   push stack-val -1
   push stack-val -2
   add
   mov
   push stack-ref -2
   push stack-val -2
   push 1
   add
   mov
   jmp label3

label4:
   push stack-val -1
   push stack-val -3
   push "add(12,23)=%d sum=%d"
   call "printf"
   addsp 3
   ret


以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?