請寫出一個 CPU0 的組合語言副程式 swap,可以將暫存器 R1 與 R2 的內容交換

組合語言程式: swap.asm0

        LDI R1, 5
        LDI R2, 3
        JSUB SWAP
        LDI R14, -1
        RET

SWAP:   
    MOV R3, R1
        MOV R1, R2
        MOV R2, R3
        RET

組譯結果:

D:\ccc\code\ch12>as0 swap.asm0 swap.obj0
Assembler:asmFile=swap.asm0 objFile=swap.obj0
===============Assemble=============
        LDI R1, 5
        LDI R2, 3
        JSUB SWAP
        LDI R14, -1
        RET

SWAP:
                MOV R3, R1
        MOV R1, R2
        MOV R2, R3
        RET

=================PASS1================
0000          LDI  R1, 5          L  8 (NULL)
0004          LDI  R2, 3          L  8 (NULL)
0008          JSUB SWAP           J 2B (NULL)
000C          LDI  R14, -1        L  8 (NULL)
0010          RET                 J 2C (NULL)
0014                                FF (NULL)
0014 SWAP:                          FF (NULL)
0014          MOV  R3, R1         A 12 (NULL)
0018          MOV  R1, R2         A 12 (NULL)
001C          MOV  R2, R3         A 12 (NULL)
0020          RET                 J 2C (NULL)
0024                                FF (NULL)
===============SYMBOL TABLE=========
0014 SWAP:                          FF (NULL)
=============PASS2==============
0000          LDI  R1, 5          L  8 08100005
0004          LDI  R2, 3          L  8 08200003
0008          JSUB SWAP           J 2B 2B000008
000C          LDI  R14, -1        L  8 08E0FFFF
0010          RET                 J 2C 2C000000
0014                                FF
0014 SWAP:                          FF
0014          MOV  R3, R1         A 12 12310000
0018          MOV  R1, R2         A 12 12120000
001C          MOV  R2, R3         A 12 12230000
0020          RET                 J 2C 2C000000
0024                                FF
==========Save to ObjFile:swap.obj0==========
08100005082000032B00000808E0FFFF2C0000001231000012120000122300002C000000
請按任意鍵繼續 . . .

執行結果:

D:\ccc\code\ch12>vm0 swap.obj0
===VM0:run swap.obj0 on CPU0===
PC=00000004 IR=08100005 SW=00000000 R[01]=0X00000005=5
PC=00000008 IR=08200003 SW=00000000 R[02]=0X00000003=3
PC=00000014 IR=2B000008 SW=00000000 R[00]=0X00000000=0
PC=00000018 IR=12310000 SW=00000000 R[03]=0X00000005=5
PC=0000001C IR=12120000 SW=00000000 R[01]=0X00000003=3
PC=00000020 IR=12230000 SW=00000000 R[02]=0X00000005=5
PC=0000000C IR=2C000000 SW=00000000 R[00]=0X00000000=0
PC=00000010 IR=08E0FFFF SW=00000000 R[14]=0XFFFFFFFF=-1
PC=00000014 IR=2C000000 SW=00000000 R[00]=0X00000000=0

===CPU0 dump registers===
IR =0x2c000000=738197504
R[00]=0x00000000=0
R[01]=0x00000003=3
R[02]=0x00000005=5
R[03]=0x00000005=5
R[04]=0x00000000=0
R[05]=0x00000000=0
R[06]=0x00000000=0
R[07]=0x00000000=0
R[08]=0x00000000=0
R[09]=0x00000000=0
R[10]=0x00000000=0
R[11]=0x00000000=0
R[12]=0x00000000=0
R[13]=0x00000000=0
R[14]=0xffffffff=-1
R[15]=0x00000014=20
請按任意鍵繼續 . . .
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License