請寫出一個 CPU0 的組合語言副程式 isPrime,可以判斷暫存器 R2當中的值是否為質數,如果是就將 R1 設為 1 傳回,否則就將 R1 設為 0

組合語言程式: isPrime.asm0

        LDI R2, 12
        JSUB isPrime
        LDI R14, -1
        RET 
isPrime: 
        LDI R3, 2
        LDI R8, 1
FOR:    CMP R3, R2
        JGE EXIT
        DIV R4, R2, R3
        MUL R5, R4, R3
        CMP R5, R2
        JNE NEXT
        LDI R1, 0
        RET
NEXT:    ADD R3, R3, R8
        JMP FOR
EXIT:    LDI R1, 1
        RET

組譯結果:

D:\ccc\code\ch12>as0 isPrime.asm0 isPrime.obj0
Assembler:asmFile=isPrime.asm0 objFile=isPrime.obj0
===============Assemble=============
        LDI R2, 12
        JSUB isPrime
        LDI R14, -1
        RET
isPrime:
                LDI R3, 2
                LDI R8, 1
FOR:    CMP R3, R2
                JGE EXIT
                DIV R4, R2, R3
                MUL R5, R4, R3
                CMP R5, R2
                JNE NEXT
                LDI R1, 0
                RET
NEXT:   ADD R3, R3, R8
                JMP FOR
EXIT:   LDI R1, 1
                RET

=================PASS1================
0000          LDI  R2, 12         L  8 (NULL)
0004          JSUB ISPRIME        J 2B (NULL)
0008          LDI  R14, -1        L  8 (NULL)
000C          RET                 J 2C (NULL)
0010 ISPRIME:                       FF (NULL)
0010          LDI  R3, 2          L  8 (NULL)
0014          LDI  R8, 1          L  8 (NULL)
0018 FOR:     CMP  R3, R2         A 10 (NULL)
001C          JGE  EXIT           J 25 (NULL)
0020          DIV  R4, R2, R3     A 16 (NULL)
0024          MUL  R5, R4, R3     A 15 (NULL)
0028          CMP  R5, R2         A 10 (NULL)
002C          JNE  NEXT           J 21 (NULL)
0030          LDI  R1, 0          L  8 (NULL)
0034          RET                 J 2C (NULL)
0038 NEXT:    ADD  R3, R3, R8     A 13 (NULL)
003C          JMP  FOR            J 26 (NULL)
0040 EXIT:    LDI  R1, 1          L  8 (NULL)
0044          RET                 J 2C (NULL)
0048                                FF (NULL)
===============SYMBOL TABLE=========
0040 EXIT:    LDI  R1, 1          L  8 (NULL)
0038 NEXT:    ADD  R3, R3, R8     A 13 (NULL)
0010 ISPRIME:                       FF (NULL)
0018 FOR:     CMP  R3, R2         A 10 (NULL)
=============PASS2==============
0000          LDI  R2, 12         L  8 0820000C
0004          JSUB ISPRIME        J 2B 2B000008
0008          LDI  R14, -1        L  8 08E0FFFF
000C          RET                 J 2C 2C000000
0010 ISPRIME:                       FF
0010          LDI  R3, 2          L  8 08300002
0014          LDI  R8, 1          L  8 08800001
0018 FOR:     CMP  R3, R2         A 10 10320000
001C          JGE  EXIT           J 25 25000020
0020          DIV  R4, R2, R3     A 16 16423000
0024          MUL  R5, R4, R3     A 15 15543000
0028          CMP  R5, R2         A 10 10523000
002C          JNE  NEXT           J 21 21000008
0030          LDI  R1, 0          L  8 08100000
0034          RET                 J 2C 2C000000
0038 NEXT:    ADD  R3, R3, R8     A 13 13338000
003C          JMP  FOR            J 26 26FFFFD8
0040 EXIT:    LDI  R1, 1          L  8 08100001
0044          RET                 J 2C 2C000000
0048                                FF
==========Save to ObjFile:isPrime.obj0==========
0820000C2B00000808E0FFFF2C000000083000020880000110320000250000201642300015543000
1052300021000008081000002C0000001333800026FFFFD8081000012C000000
請按任意鍵繼續 . . .

執行結果:

D:\ccc\code\ch12>vm0 isPrime.obj0
===VM0:run isPrime.obj0 on CPU0===
PC=00000004 IR=0820000C SW=00000000 R[02]=0X0000000C=12
PC=00000010 IR=2B000008 SW=00000000 R[00]=0X00000000=0
PC=00000014 IR=08300002 SW=00000000 R[03]=0X00000002=2
PC=00000018 IR=08800001 SW=00000000 R[08]=0X00000001=1
PC=0000001C IR=10320000 SW=80000000 R[12]=0X80000000=-2147483648
PC=00000020 IR=25000020 SW=80000000 R[00]=0X00000000=0
PC=00000024 IR=16423000 SW=80000000 R[04]=0X00000006=6
PC=00000028 IR=15543000 SW=80000000 R[05]=0X0000000C=12
PC=0000002C IR=10523000 SW=40000000 R[12]=0X40000000=1073741824
PC=00000030 IR=21000008 SW=40000000 R[00]=0X00000000=0
PC=00000034 IR=08100000 SW=40000000 R[01]=0X00000000=0
PC=00000008 IR=2C000000 SW=40000000 R[00]=0X00000000=0
PC=0000000C IR=08E0FFFF SW=40000000 R[14]=0XFFFFFFFF=-1
PC=00000010 IR=2C000000 SW=40000000 R[00]=0X00000000=0

===CPU0 dump registers===
IR =0x2c000000=738197504
R[00]=0x00000000=0
R[01]=0x00000000=0
R[02]=0x0000000c=12
R[03]=0x00000002=2
R[04]=0x00000006=6
R[05]=0x0000000c=12
R[06]=0x00000000=0
R[07]=0x00000000=0
R[08]=0x00000001=1
R[09]=0x00000000=0
R[10]=0x00000000=0
R[11]=0x00000000=0
R[12]=0x40000000=1073741824
R[13]=0x00000000=0
R[14]=0xffffffff=-1
R[15]=0x00000010=16
請按任意鍵繼續 . . .
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License