about summary refs log blame commit diff stats
path: root/apps/ex3.subx
blob: d75db65a7759259b70677cad3468f9344c0da45e (plain) (tree)
om> 2018-08-12 22:38:36 -0700 4512 - divide labels into two categories' href='/akkartik/mu/commit/subx/ex3.subx?h=main&id=f3901d906901e80588f2e3e56bbe3052e47e4d64'>f3901d90 ^
1
2
3
4
5
6
7
8
9
                                                                   
 
         

                                                                


                  
 
       


                                                                                                                                                 
 
      



                           
 
      
                             
                                                                                                                                                                   
                             
                       
                                                                                                                                                                      
               
                    

                        
 
      

                                
 
                            
# Add the first 10 numbers, and return the result in the exit code.
#
# To run:
#   $ ./bootstrap translate init.linux apps/ex3.subx -o apps/ex3
#   $ ./bootstrap run apps/ex3
# Expected result:
#   $ echo $?
#   55

== code
#   instruction                     effective address                                                   register    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

Entry:
    # result: ebx = 0
    bb/copy-to-ebx  0/imm32
    # counter: ecx = 1
    b9/copy-to-ecx  1/imm32

$loop:
    # if (counter > 10) break
    81          7/subop/compare     3/mod/direct    1/rm32/ecx    .           .             .           .           .               0xa/imm32         # compare ecx
    7f/jump-if->  $exit/disp8
    # result += counter
    01/add                          3/mod/direct    3/rm32/ebx    .           .             .           1/r32/ecx   .               .                 # add ecx to ebx
    # ++counter
    41/increment-ecx
    # loop
    eb/jump  $loop/disp8

$exit:
    # exit(ebx)
    e8/call  syscall_exit/disp32

# . . vim:nowrap:textwidth=0