1 ## add the first 10 numbers, and return the result in the exit code
 2 #
 3 # To run:
 4 #   $ subx translate ex3.subx ex3
 5 #   $ subx run ex3
 6 # Expected result:
 7 #   $ echo $?
 8 #   55
 9 
10 == code
11 # instruction                     effective address                                                   operand     displacement    immediate
12 # op          subop               mod             rm32          base        index         scale       r32
13 # 1-3 bytes   3 bits              2 bits          3 bits        3 bits      3 bits        2 bits      2 bits      0/1/2/4 bytes   0/1/2/4 bytes
14   # result: EBX = 0
15   bb/copy                                                                                                                         0/imm32           # copy 0 to EBX
16   # counter: ECX = 1
17   b9/copy                                                                                                                         1/imm32           # copy 1 to ECX
18 
19 $loop:
20   # while (counter <= 10)
21   81          7/subop/compare     3/mod/direct    1/rm32/ECX                                                                      0xa/imm32         # compare ECX, 10/imm
22   7f/jump-if-greater  $exit/disp8
23   # result += counter
24   01/add                          3/mod/direct    3/rm32/EBX                                          1/r32/ECX                                     # add ECX to EBX
25   # ++counter
26   81          0/subop/add         3/mod/direct    1/rm32/ECX                                                                      1/imm32           # add 1 to ECX
27   # loop
28   eb/jump  $loop/disp8
29 
30 $exit:
31   # exit(EBX)
32   b8/copy                                                                                                                         1/imm32           # copy 1 to EAX
33   cd/syscall  0x80/imm8
34 
35 # vim:ft=subx:nowrap