Test -- CPU0 工具鏈的測試範例

檔案:test.c0

sum = 0;
for (i=0; i<=10; i++)
{
  sum = sum + i;
}
return sum;

編譯過程

D:\ch12>c0c test.c0 test.asm0
compile file:test.c0
======= tokenize =======
token=sum
token==
token=0
token=;
token=for
token=(
token=i
token==
token=0
token=;
token=i
token=<=
token=10
token=;
token=i
token=++
token=)
token={
token=sum
token==
token=sum
token=+
token=i
token=;
token=}
token=return
token=sum
token=;
tokens->count = 28
token=sum , type=id
token== , type==
token=0 , type=number
token=; , type=;
token=for , type=for
token=( , type=(
token=i , type=id
token== , type==
token=0 , type=number
token=; , type=;
token=i , type=id
token=<= , type=<=
token=10 , type=number
token=; , type=;
token=i , type=id
token=++ , type=++
token=) , type=)
token={ , type={
token=sum , type=id
token== , type==
token=sum , type=id
token=+ , type=+
token=i , type=id
token=; , type=;
token=} , type=}
token=return , type=return
token=sum , type=id
token=; , type=;
======= parsing ========
+PROG
 +BaseList
  +BASE
   +STMT
     idx=0, token=sum, type=id
     idx=1, token==, type==
    +EXP
      idx=2, token=0, type=number
    -EXP
   -STMT
    idx=3, token=;, type=;
  -BASE
  +BASE
   +FOR
     idx=4, token=for, type=for
     idx=5, token=(, type=(
    +STMT
      idx=6, token=i, type=id
      idx=7, token==, type==
     +EXP
       idx=8, token=0, type=number
     -EXP
    -STMT
     idx=9, token=;, type=;
    +COND
     +EXP
       idx=10, token=i, type=id
     -EXP
      idx=11, token=<=, type=<=
     +EXP
       idx=12, token=10, type=number
     -EXP
    -COND
     idx=13, token=;, type=;
    +STMT
      idx=14, token=i, type=id
      idx=15, token=++, type=++
    -STMT
     idx=16, token=), type=)
    +BLOCK
      idx=17, token={, type={
     +BaseList
      +BASE
       +STMT
         idx=18, token=sum, type=id
         idx=19, token==, type==
        +EXP
          idx=20, token=sum, type=id
          idx=21, token=+, type=+
          idx=22, token=i, type=id
        -EXP
       -STMT
        idx=23, token=;, type=;
      -BASE
     -BaseList
      idx=24, token=}, type=}
    -BLOCK
   -FOR
  -BASE
  +BASE
   +STMT
     idx=25, token=return, type=return
     idx=26, token=sum, type=id
   -STMT
    idx=27, token=;, type=;
  -BASE
 -BaseList
-PROG
=====PCODE=====
         =    0         sum
         =    0         i
FOR0:
         CMP  i    10
         J    >         _FOR0
         +    sum  i    T0
         =    T0        sum
         +    i    1    i
         J              FOR0
_FOR0:
         RET            sum
=====AsmFile:test.asm0======
         LDI  R1   0
         ST   R1   sum
         LDI  R1   0
         ST   R1   i
FOR0:
         LD   R1   i
         LDI  R2   10
         CMP  R1   R2
         JGT  _FOR0
         LD   R1   sum
         LD   R2   i
         ADD  R3   R1   R2
         ST   R3   T0
         LD   R1   T0
         ST   R1   sum
         LD   R1   i
         LDI  R2   1
         ADD  R3   R1   R2
         ST   R3   i
         JMP  FOR0
_FOR0:
         LD   R1   sum
         RET
sum:     RESW 1
i:       RESW 1
T0:      RESW 1

輸出的組合語言檔:test.asm0

         LDI  R1   0        
         ST   R1   sum      
         LDI  R1   0        
         ST   R1   i        
FOR0:                       
         LD   R1   i        
         LDI  R2   10       
         CMP  R1   R2       
         JGT  _FOR0          
         LD   R1   sum      
         LD   R2   i        
         ADD  R3   R1   R2  
         ST   R3   T0       
         LD   R1   T0       
         ST   R1   sum      
         LD   R1   i        
         LDI  R2   1        
         ADD  R3   R1   R2  
         ST   R3   i        
         JMP  FOR0          
_FOR0:                      
         LD   R1   sum      
         RET                
sum:     RESW 1             
i:       RESW 1             
T0:      RESW 1

組譯過程

D:\ch12>as0 test.asm0 test.obj0
Assembler:asmFile=test.asm0 objFile=test.obj0
===============Assemble=============
         LDI  R1   0
         ST   R1   sum
         LDI  R1   0
         ST   R1   i
FOR0:
         LD   R1   i
         LDI  R2   10
         CMP  R1   R2
         JGT  _FOR0
         LD   R1   sum
         LD   R2   i
         ADD  R3   R1   R2
         ST   R3   T0
         LD   R1   T0
         ST   R1   sum
         LD   R1   i
         LDI  R2   1
         ADD  R3   R1   R2
         ST   R3   i
         JMP  FOR0
_FOR0:
         LD   R1   sum
         RET
sum:     RESW 1
i:       RESW 1
T0:      RESW 1
=================PASS1================
0000          LDI  R1   0         L  8 (NULL)
0004          ST   R1   SUM       L  1 (NULL)
0008          LDI  R1   0         L  8 (NULL)
000C          ST   R1   I         L  1 (NULL)
0010 FOR0:                          FF (NULL)
0010          LD   R1   I         L  0 (NULL)
0014          LDI  R2   10        L  8 (NULL)
0018          CMP  R1   R2        A 10 (NULL)
001C          JGT  _FOR0          J 23 (NULL)
0020          LD   R1   SUM       L  0 (NULL)
0024          LD   R2   I         L  0 (NULL)
0028          ADD  R3   R1   R2   A 13 (NULL)
002C          ST   R3   T0        L  1 (NULL)
0030          LD   R1   T0        L  0 (NULL)
0034          ST   R1   SUM       L  1 (NULL)
0038          LD   R1   I         L  0 (NULL)
003C          LDI  R2   1         L  8 (NULL)
0040          ADD  R3   R1   R2   A 13 (NULL)
0044          ST   R3   I         L  1 (NULL)
0048          JMP  FOR0           J 26 (NULL)
004C _FOR0:                         FF (NULL)
004C          LD   R1   SUM       L  0 (NULL)
0050          RET                 J 2C (NULL)
0054 SUM:     RESW 1              D F0 (NULL)
0058 I:       RESW 1              D F0 (NULL)
005C T0:      RESW 1              D F0 (NULL)
===============SYMBOL TABLE=========
005C T0:      RESW 1              D F0 (NULL)
0010 FOR0:                          FF (NULL)
0054 SUM:     RESW 1              D F0 (NULL)
0058 I:       RESW 1              D F0 (NULL)
004C _FOR0:                         FF (NULL)
=============PASS2==============
0000          LDI  R1   0         L  8 08100000
0004          ST   R1   SUM       L  1 011F004C
0008          LDI  R1   0         L  8 08100000
000C          ST   R1   I         L  1 011F0048
0010 FOR0:                          FF
0010          LD   R1   I         L  0 001F0044
0014          LDI  R2   10        L  8 0820000A
0018          CMP  R1   R2        A 10 10120000
001C          JGT  _FOR0          J 23 2300002C
0020          LD   R1   SUM       L  0 001F0030
0024          LD   R2   I         L  0 002F0030
0028          ADD  R3   R1   R2   A 13 13312000
002C          ST   R3   T0        L  1 013F002C
0030          LD   R1   T0        L  0 001F0028
0034          ST   R1   SUM       L  1 011F001C
0038          LD   R1   I         L  0 001F001C
003C          LDI  R2   1         L  8 08200001
0040          ADD  R3   R1   R2   A 13 13312000
0044          ST   R3   I         L  1 013F0010
0048          JMP  FOR0           J 26 26FFFFC4
004C _FOR0:                         FF
004C          LD   R1   SUM       L  0 001F0004
0050          RET                 J 2C 2C000000
0054 SUM:     RESW 1              D F0 00000000
0058 I:       RESW 1              D F0 00000000
005C T0:      RESW 1              D F0 00000000
==========Save to ObjFile:test.obj0==========
08100000011F004C08100000011F0048001F00440820000A101200002300002C001F0030002F0030
13312000013F002C001F0028011F001C001F001C0820000113312000013F001026FFFFC4001F0004
2C000000000000000000000000000000

執行過程

D:\ch12>vm0 test.obj0
===VM0:run test.obj0 on CPU0===
PC=00000004 IR=08100000 SW=00000000 R[01]=0X00000000=0
PC=00000008 IR=011F004C SW=00000000 R[01]=0X00000000=0
PC=0000000C IR=08100000 SW=00000000 R[01]=0X00000000=0
PC=00000010 IR=011F0048 SW=00000000 R[01]=0X00000000=0
PC=00000014 IR=001F0044 SW=00000000 R[01]=0X00000000=0
PC=00000018 IR=0820000A SW=00000000 R[02]=0X0000000A=10
PC=0000001C IR=10120000 SW=80000000 R[12]=0X80000000=-2147483648
PC=00000020 IR=2300002C SW=80000000 R[00]=0X00000000=0
PC=00000024 IR=001F0030 SW=80000000 R[01]=0X00000000=0
PC=00000028 IR=002F0030 SW=80000000 R[02]=0X00000000=0
PC=0000002C IR=13312000 SW=80000000 R[03]=0X00000000=0
PC=00000030 IR=013F002C SW=80000000 R[03]=0X00000000=0
PC=00000034 IR=001F0028 SW=80000000 R[01]=0X00000000=0
PC=00000038 IR=011F001C SW=80000000 R[01]=0X00000000=0
PC=0000003C IR=001F001C SW=80000000 R[01]=0X00000000=0
PC=00000040 IR=08200001 SW=80000000 R[02]=0X00000001=1
PC=00000044 IR=13312000 SW=80000000 R[03]=0X00000001=1
PC=00000048 IR=013F0010 SW=80000000 R[03]=0X00000001=1
PC=00000010 IR=26FFFFC4 SW=80000000 R[15]=0X00000010=16
PC=00000014 IR=001F0044 SW=80000000 R[01]=0X00000001=1
PC=00000018 IR=0820000A SW=80000000 R[02]=0X0000000A=10
PC=0000001C IR=10120000 SW=80000000 R[12]=0X80000000=-2147483648
PC=00000020 IR=2300002C SW=80000000 R[00]=0X00000000=0
PC=00000024 IR=001F0030 SW=80000000 R[01]=0X00000000=0
PC=00000028 IR=002F0030 SW=80000000 R[02]=0X00000001=1
PC=0000002C IR=13312000 SW=80000000 R[03]=0X00000001=1
PC=00000030 IR=013F002C SW=80000000 R[03]=0X00000001=1
PC=00000034 IR=001F0028 SW=80000000 R[01]=0X00000001=1
PC=00000038 IR=011F001C SW=80000000 R[01]=0X00000001=1
PC=0000003C IR=001F001C SW=80000000 R[01]=0X00000001=1
PC=00000040 IR=08200001 SW=80000000 R[02]=0X00000001=1
PC=00000044 IR=13312000 SW=80000000 R[03]=0X00000002=2
PC=00000048 IR=013F0010 SW=80000000 R[03]=0X00000002=2
PC=00000010 IR=26FFFFC4 SW=80000000 R[15]=0X00000010=16
PC=00000014 IR=001F0044 SW=80000000 R[01]=0X00000002=2
PC=00000018 IR=0820000A SW=80000000 R[02]=0X0000000A=10
PC=0000001C IR=10120000 SW=80000000 R[12]=0X80000000=-2147483648
PC=00000020 IR=2300002C SW=80000000 R[00]=0X00000000=0
PC=00000024 IR=001F0030 SW=80000000 R[01]=0X00000001=1
PC=00000028 IR=002F0030 SW=80000000 R[02]=0X00000002=2
PC=0000002C IR=13312000 SW=80000000 R[03]=0X00000003=3
PC=00000030 IR=013F002C SW=80000000 R[03]=0X00000003=3
PC=00000034 IR=001F0028 SW=80000000 R[01]=0X00000003=3
PC=00000038 IR=011F001C SW=80000000 R[01]=0X00000003=3
PC=0000003C IR=001F001C SW=80000000 R[01]=0X00000002=2
PC=00000040 IR=08200001 SW=80000000 R[02]=0X00000001=1
PC=00000044 IR=13312000 SW=80000000 R[03]=0X00000003=3
PC=00000048 IR=013F0010 SW=80000000 R[03]=0X00000003=3
PC=00000010 IR=26FFFFC4 SW=80000000 R[15]=0X00000010=16
PC=00000014 IR=001F0044 SW=80000000 R[01]=0X00000003=3
PC=00000018 IR=0820000A SW=80000000 R[02]=0X0000000A=10
PC=0000001C IR=10120000 SW=80000000 R[12]=0X80000000=-2147483648
PC=00000020 IR=2300002C SW=80000000 R[00]=0X00000000=0
PC=00000024 IR=001F0030 SW=80000000 R[01]=0X00000003=3
PC=00000028 IR=002F0030 SW=80000000 R[02]=0X00000003=3
PC=0000002C IR=13312000 SW=80000000 R[03]=0X00000006=6
PC=00000030 IR=013F002C SW=80000000 R[03]=0X00000006=6
PC=00000034 IR=001F0028 SW=80000000 R[01]=0X00000006=6
PC=00000038 IR=011F001C SW=80000000 R[01]=0X00000006=6
PC=0000003C IR=001F001C SW=80000000 R[01]=0X00000003=3
PC=00000040 IR=08200001 SW=80000000 R[02]=0X00000001=1
PC=00000044 IR=13312000 SW=80000000 R[03]=0X00000004=4
PC=00000048 IR=013F0010 SW=80000000 R[03]=0X00000004=4
PC=00000010 IR=26FFFFC4 SW=80000000 R[15]=0X00000010=16
PC=00000014 IR=001F0044 SW=80000000 R[01]=0X00000004=4
PC=00000018 IR=0820000A SW=80000000 R[02]=0X0000000A=10
PC=0000001C IR=10120000 SW=80000000 R[12]=0X80000000=-2147483648
PC=00000020 IR=2300002C SW=80000000 R[00]=0X00000000=0
PC=00000024 IR=001F0030 SW=80000000 R[01]=0X00000006=6
PC=00000028 IR=002F0030 SW=80000000 R[02]=0X00000004=4
PC=0000002C IR=13312000 SW=80000000 R[03]=0X0000000A=10
PC=00000030 IR=013F002C SW=80000000 R[03]=0X0000000A=10
PC=00000034 IR=001F0028 SW=80000000 R[01]=0X0000000A=10
PC=00000038 IR=011F001C SW=80000000 R[01]=0X0000000A=10
PC=0000003C IR=001F001C SW=80000000 R[01]=0X00000004=4
PC=00000040 IR=08200001 SW=80000000 R[02]=0X00000001=1
PC=00000044 IR=13312000 SW=80000000 R[03]=0X00000005=5
PC=00000048 IR=013F0010 SW=80000000 R[03]=0X00000005=5
PC=00000010 IR=26FFFFC4 SW=80000000 R[15]=0X00000010=16
PC=00000014 IR=001F0044 SW=80000000 R[01]=0X00000005=5
PC=00000018 IR=0820000A SW=80000000 R[02]=0X0000000A=10
PC=0000001C IR=10120000 SW=80000000 R[12]=0X80000000=-2147483648
PC=00000020 IR=2300002C SW=80000000 R[00]=0X00000000=0
PC=00000024 IR=001F0030 SW=80000000 R[01]=0X0000000A=10
PC=00000028 IR=002F0030 SW=80000000 R[02]=0X00000005=5
PC=0000002C IR=13312000 SW=80000000 R[03]=0X0000000F=15
PC=00000030 IR=013F002C SW=80000000 R[03]=0X0000000F=15
PC=00000034 IR=001F0028 SW=80000000 R[01]=0X0000000F=15
PC=00000038 IR=011F001C SW=80000000 R[01]=0X0000000F=15
PC=0000003C IR=001F001C SW=80000000 R[01]=0X00000005=5
PC=00000040 IR=08200001 SW=80000000 R[02]=0X00000001=1
PC=00000044 IR=13312000 SW=80000000 R[03]=0X00000006=6
PC=00000048 IR=013F0010 SW=80000000 R[03]=0X00000006=6
PC=00000010 IR=26FFFFC4 SW=80000000 R[15]=0X00000010=16
PC=00000014 IR=001F0044 SW=80000000 R[01]=0X00000006=6
PC=00000018 IR=0820000A SW=80000000 R[02]=0X0000000A=10
PC=0000001C IR=10120000 SW=80000000 R[12]=0X80000000=-2147483648
PC=00000020 IR=2300002C SW=80000000 R[00]=0X00000000=0
PC=00000024 IR=001F0030 SW=80000000 R[01]=0X0000000F=15
PC=00000028 IR=002F0030 SW=80000000 R[02]=0X00000006=6
PC=0000002C IR=13312000 SW=80000000 R[03]=0X00000015=21
PC=00000030 IR=013F002C SW=80000000 R[03]=0X00000015=21
PC=00000034 IR=001F0028 SW=80000000 R[01]=0X00000015=21
PC=00000038 IR=011F001C SW=80000000 R[01]=0X00000015=21
PC=0000003C IR=001F001C SW=80000000 R[01]=0X00000006=6
PC=00000040 IR=08200001 SW=80000000 R[02]=0X00000001=1
PC=00000044 IR=13312000 SW=80000000 R[03]=0X00000007=7
PC=00000048 IR=013F0010 SW=80000000 R[03]=0X00000007=7
PC=00000010 IR=26FFFFC4 SW=80000000 R[15]=0X00000010=16
PC=00000014 IR=001F0044 SW=80000000 R[01]=0X00000007=7
PC=00000018 IR=0820000A SW=80000000 R[02]=0X0000000A=10
PC=0000001C IR=10120000 SW=80000000 R[12]=0X80000000=-2147483648
PC=00000020 IR=2300002C SW=80000000 R[00]=0X00000000=0
PC=00000024 IR=001F0030 SW=80000000 R[01]=0X00000015=21
PC=00000028 IR=002F0030 SW=80000000 R[02]=0X00000007=7
PC=0000002C IR=13312000 SW=80000000 R[03]=0X0000001C=28
PC=00000030 IR=013F002C SW=80000000 R[03]=0X0000001C=28
PC=00000034 IR=001F0028 SW=80000000 R[01]=0X0000001C=28
PC=00000038 IR=011F001C SW=80000000 R[01]=0X0000001C=28
PC=0000003C IR=001F001C SW=80000000 R[01]=0X00000007=7
PC=00000040 IR=08200001 SW=80000000 R[02]=0X00000001=1
PC=00000044 IR=13312000 SW=80000000 R[03]=0X00000008=8
PC=00000048 IR=013F0010 SW=80000000 R[03]=0X00000008=8
PC=00000010 IR=26FFFFC4 SW=80000000 R[15]=0X00000010=16
PC=00000014 IR=001F0044 SW=80000000 R[01]=0X00000008=8
PC=00000018 IR=0820000A SW=80000000 R[02]=0X0000000A=10
PC=0000001C IR=10120000 SW=80000000 R[12]=0X80000000=-2147483648
PC=00000020 IR=2300002C SW=80000000 R[00]=0X00000000=0
PC=00000024 IR=001F0030 SW=80000000 R[01]=0X0000001C=28
PC=00000028 IR=002F0030 SW=80000000 R[02]=0X00000008=8
PC=0000002C IR=13312000 SW=80000000 R[03]=0X00000024=36
PC=00000030 IR=013F002C SW=80000000 R[03]=0X00000024=36
PC=00000034 IR=001F0028 SW=80000000 R[01]=0X00000024=36
PC=00000038 IR=011F001C SW=80000000 R[01]=0X00000024=36
PC=0000003C IR=001F001C SW=80000000 R[01]=0X00000008=8
PC=00000040 IR=08200001 SW=80000000 R[02]=0X00000001=1
PC=00000044 IR=13312000 SW=80000000 R[03]=0X00000009=9
PC=00000048 IR=013F0010 SW=80000000 R[03]=0X00000009=9
PC=00000010 IR=26FFFFC4 SW=80000000 R[15]=0X00000010=16
PC=00000014 IR=001F0044 SW=80000000 R[01]=0X00000009=9
PC=00000018 IR=0820000A SW=80000000 R[02]=0X0000000A=10
PC=0000001C IR=10120000 SW=80000000 R[12]=0X80000000=-2147483648
PC=00000020 IR=2300002C SW=80000000 R[00]=0X00000000=0
PC=00000024 IR=001F0030 SW=80000000 R[01]=0X00000024=36
PC=00000028 IR=002F0030 SW=80000000 R[02]=0X00000009=9
PC=0000002C IR=13312000 SW=80000000 R[03]=0X0000002D=45
PC=00000030 IR=013F002C SW=80000000 R[03]=0X0000002D=45
PC=00000034 IR=001F0028 SW=80000000 R[01]=0X0000002D=45
PC=00000038 IR=011F001C SW=80000000 R[01]=0X0000002D=45
PC=0000003C IR=001F001C SW=80000000 R[01]=0X00000009=9
PC=00000040 IR=08200001 SW=80000000 R[02]=0X00000001=1
PC=00000044 IR=13312000 SW=80000000 R[03]=0X0000000A=10
PC=00000048 IR=013F0010 SW=80000000 R[03]=0X0000000A=10
PC=00000010 IR=26FFFFC4 SW=80000000 R[15]=0X00000010=16
PC=00000014 IR=001F0044 SW=80000000 R[01]=0X0000000A=10
PC=00000018 IR=0820000A SW=80000000 R[02]=0X0000000A=10
PC=0000001C IR=10120000 SW=40000000 R[12]=0X40000000=1073741824
PC=00000020 IR=2300002C SW=40000000 R[00]=0X00000000=0
PC=00000024 IR=001F0030 SW=40000000 R[01]=0X0000002D=45
PC=00000028 IR=002F0030 SW=40000000 R[02]=0X0000000A=10
PC=0000002C IR=13312000 SW=40000000 R[03]=0X00000037=55
PC=00000030 IR=013F002C SW=40000000 R[03]=0X00000037=55
PC=00000034 IR=001F0028 SW=40000000 R[01]=0X00000037=55
PC=00000038 IR=011F001C SW=40000000 R[01]=0X00000037=55
PC=0000003C IR=001F001C SW=40000000 R[01]=0X0000000A=10
PC=00000040 IR=08200001 SW=40000000 R[02]=0X00000001=1
PC=00000044 IR=13312000 SW=40000000 R[03]=0X0000000B=11
PC=00000048 IR=013F0010 SW=40000000 R[03]=0X0000000B=11
PC=00000010 IR=26FFFFC4 SW=40000000 R[15]=0X00000010=16
PC=00000014 IR=001F0044 SW=40000000 R[01]=0X0000000B=11
PC=00000018 IR=0820000A SW=40000000 R[02]=0X0000000A=10
PC=0000001C IR=10120000 SW=00000000 R[12]=0X00000000=0
PC=0000004C IR=2300002C SW=00000000 R[00]=0X00000000=0
PC=00000050 IR=001F0004 SW=00000000 R[01]=0X00000037=55
PC=00000054 IR=2C000000 SW=00000000 R[00]=0X00000000=0

===CPU0 dump registers===
IR =0x2c000000=738197504
R[00]=0x00000000=0
R[01]=0x00000037=55
R[02]=0x0000000a=10
R[03]=0x0000000b=11
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]=0x00000054=84
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License