about summary refs log blame commit diff stats
path: root/subx/examples/ex3.subx
blob: eb4d6c01b6b91ad147c8a376be1c826c66bc31dc (plain) (tree)
1
2
3
4
5
6
7
8
9
                                                                   



                                 


                  
 
       


                                                                                                                                               
                   
                                                                                                                                                                   
                    
                                                                                                                                                                   
 
      
                         
                                                                                                                                                                         
                                                                                                                                                                           
                     
                                                                                                                                                                    
             
                                                                                                                                                                  
        
                                                                                                                                                                
 
      
             

                                                                                                                                                                   

                    
## add the first 10 numbers, and return the result in the exit code
#
# To run:
#   $ subx translate ex3.subx ex3
#   $ subx run ex3
# Expected result:
#   $ echo $?
#   55

== code
# instruction                     effective address                                                   operand     displacement    immediate
# op          subop               mod             rm32          base        index         scale       r32
# 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
  # result: EBX = 0
  bb/copy                                                                                                                         0/imm32           # copy 0 to EBX
  # counter: ECX = 1
  b9/copy                                                                                                                         1/imm32           # copy 1 to ECX

$loop:
  # while (counter <= 10)
  81          7/subop/compare     3/mod/direct    1/rm32/ECX                                                                      0xa/imm32         # compare ECX, 10/imm
  7f/jump-if                                                                                                      $exit/disp8                       # jump-if-greater $exit
  # result += counter
  01/add                          3/mod/direct    3/rm32/EBX                                          1/r32/ECX                                     # add ECX to EBX
  # ++counter
  81          0/subop/add         3/mod/direct    1/rm32/ECX                                                                      1/imm32           # add 1 to ECX
  # loop
  eb/jump                                                                                                         $loop/disp8                       # jump $loop

$exit:
  # exit(EBX)
  b8/copy                                                                                                                         1/imm32           # copy 1 to EAX
  cd/syscall                                                                                                                      0x80/imm8         # int 80h

# vim:ft=subx:nowrap