about summary refs log blame commit diff stats
path: root/054string-equal.subx
blob: 073e7dbb03114430d0a396e5e0f57ce0cb394aa2 (plain) (tree)
1
2
3
4
5
6
7
8
9


                                              


                                                                                                                                                 
 
                       
                                                
                                                                                                                                         
                                      

                                                                                                                                                                                      
                         
 
                                                                                     
                 
                                                        

                               

                                  


                                    
                          
                   
     
                




                  
     
              

                                                                                                                                                                       
                      









                                                                                                                                                                             
                       

                                                                                                                                                                            
                                                    








                                                                                                                                                                      
                    
                                    
                                                                                                                                                                            
                                                                   
                 
                                                                                                                                                                               
                 
                                                                                                                                                                               
                                
                                                                                                                                                                           
                                                    
             
                    
             
                    

                                      
                           

                                     
                           
                   
                         



                 
              

                                                                                                                                                                       
             
 
         
 
                                     
                                 
                   

                     
              
                                 
                      

                                                                                                                                                                  
                   

                                                             
               
              
                                    
                      
                                                                                                                                                                  
             
 
                                                                                  
                                    
                   

                        
              
                                 
                      

                                                                                                                                                                  
                   

                                                                 
               
              
                                    
                      
                                                                                                                                                                  
             
 
                           
                                       
                   

                        
              
                                 
                      

                                                                                                                                                                  
                   

                                                   
               
              
                                    
                      
                                                                                                                                                                  
             
 
                                           
                                       
                   

                        
              
                                 
                      

                                                                                                                                                                  
                   

                                                                   
               
              
                                    
                      
                                                                                                                                                                  
             
 


                                                                                                

                                                                                                                                                                       
                      

                                      
                   

                                                                                                                                                                      


                                 

                                                                                                                                                                  
                   
                                                                                                                                                                      
                    
               


                                    
                                                                                                                                                                  

                         
                 
              

                                                                                                                                                                       










                                       

                                                                                                                                                                  


                                                
               


                                    
                                                                                                                                                                  

             
                            
# Comparing 'regular' length-prefixed strings.

== 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:  # run all tests
#?     e8/call test-compare-equal-strings/disp32
    e8/call  run-tests/disp32  # 'run-tests' is a function created automatically by SubX. It calls all functions that start with 'test-'.
    # syscall(exit, Num-test-failures)
    8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/ebx   Num-test-failures/disp32          # copy *Num-test-failures to ebx
    b8/copy-to-eax  1/imm32/exit
    cd/syscall  0x80/imm8

string-equal?:  # s : (address string), benchmark : (address string) -> eax : boolean
    # pseudocode:
    #   if (s->length != benchmark->length) return false
    #   currs = s->data
    #   currb = benchmark->data
    #   maxs = s->data + s->length
    #   while currs < maxs
    #     c1 = *currs
    #     c2 = *currb
    #     if (c1 != c2) return false
    #     ++currs, ++currb
    #   return true
    #
    # registers:
    #   currs: esi
    #   maxs: ecx
    #   currb: edi
    #   c1: eax
    #   c2: ebx
    #
    # . prolog
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # . save registers
    51/push-ecx
    52/push-edx
    56/push-esi
    57/push-edi
    # esi = s
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   8/disp8         .                 # copy *(ebp+8) to esi
    # edi = benchmark
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           7/r32/edi   0xc/disp8       .                 # copy *(ebp+12) to edi
    # ecx = s->length
    8b/copy                         0/mod/indirect  6/rm32/esi    .           .             .           1/r32/ecx   .               .                 # copy *esi to ecx
$string-equal?:lengths:
    # if (ecx != benchmark->length) return false
    39/compare                      0/mod/indirect  7/rm32/edi    .           .             .           1/r32/ecx   .               .                 # compare *edi and ecx
    75/jump-if-not-equal  $string-equal?:false/disp8
    # currs/esi = s->data
    81          0/subop/add         3/mod/direct    6/rm32/esi    .           .             .           .           .               4/imm32           # add to esi
    # maxs/ecx = s->data + s->length
    01/add                          3/mod/direct    1/rm32/ecx    .           .             .           6/r32/esi   .               .                 # add esi to ecx
    # currb/edi = benchmark->data
    81          0/subop/add         3/mod/direct    7/rm32/edi    .           .             .           .           .               4/imm32           # add to edi
    # c1/eax = c2/edx = 0
    31/xor                          3/mod/direct    0/rm32/eax    .           .             .           0/r32/eax   .               .                 # clear eax
    31/xor                          3/mod/direct    2/rm32/edx    .           .             .           2/r32/edx   .               .                 # clear edx
$string-equal?:loop:
    # if (currs >= maxs) return true
    39/compare                      3/mod/direct    6/rm32/esi    .           .             .           1/r32/ecx   .               .                 # compare esi with ecx
    73/jump-if-greater-or-equal-unsigned  $string-equal?:true/disp8
    # c1 = *currs
    8a/copy-byte                    0/mod/indirect  6/rm32/esi    .           .             .           0/r32/AL    .               .                 # copy byte at *esi to AL
    # c2 = *currb
    8a/copy-byte                    0/mod/indirect  7/rm32/edi    .           .             .           2/r32/DL    .               .                 # copy byte at *edi to DL
    # if (c1 != c2) return false
    39/compare                      3/mod/direct    0/rm32/eax    .           .             .           2/r32/edx   .               .                 # compare eax and edx
    75/jump-if-not-equal  $string-equal?:false/disp8
    # ++currs
    46/increment-esi
    # ++currb
    47/increment-edi
    eb/jump  $string-equal?:loop/disp8
$string-equal?:true:
    b8/copy-to-eax  1/imm32
    eb/jump  $string-equal?:end/disp8
$string-equal?:false:
    b8/copy-to-eax  0/imm32
$string-equal?:end:
    # . restore registers
    5f/pop-to-edi
    5e/pop-to-esi
    5a/pop-to-edx
    59/pop-to-ecx
    # . epilog
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

# - tests

test-compare-empty-with-empty-string:
    # eax = string-equal?("", "")
    # . . push args
    68/push  ""/imm32
    68/push  ""/imm32
    # . . call
    e8/call  string-equal?/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # check-ints-equal(eax, 1, msg)
    # . . push args
    68/push  "F - test-compare-empty-with-empty-string"/imm32
    68/push  1/imm32/true
    50/push-eax
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    c3/return

test-compare-empty-with-non-empty-string:  # also checks length-mismatch code path
    # eax = string-equal?("", "Abc")
    # . . push args
    68/push  "Abc"/imm32
    68/push  ""/imm32
    # . . call
    e8/call  string-equal?/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # check-ints-equal(eax, 0, msg)
    # . . push args
    68/push  "F - test-compare-empty-with-non-empty-string"/imm32
    68/push  0/imm32/false
    50/push-eax
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    c3/return

test-compare-equal-strings:
    # eax = string-equal?("Abc", "Abc")
    # . . push args
    68/push  "Abc"/imm32
    68/push  "Abc"/imm32
    # . . call
    e8/call  string-equal?/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # check-ints-equal(eax, 1, msg)
    # . . push args
    68/push  "F - test-compare-equal-strings"/imm32
    68/push  1/imm32/true
    50/push-eax
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    c3/return

test-compare-inequal-strings-equal-lengths:
    # eax = string-equal?("Abc", "Adc")
    # . . push args
    68/push  "Adc"/imm32
    68/push  "Abc"/imm32
    # . . call
    e8/call  string-equal?/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # check-ints-equal(eax, 0, msg)
    # . . push args
    68/push  "F - test-compare-inequal-strings-equal-lengths"/imm32
    68/push  0/imm32/false
    50/push-eax
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    c3/return

# helper for later tests
check-string-equal:  # s : (address string), expected : (address string), msg : (address string)
    # . prolog
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # . save registers
    50/push-eax
    # eax = string-equal?(s, expected)
    # . . push args
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
    # . . call
    e8/call  string-equal?/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # check-ints-equal(eax, 1, msg)
    # . . push args
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
    68/push  1/imm32
    50/push-eax
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
$check-string-equal:end:
    # . restore registers
    58/pop-to-eax
    # . epilog
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

# test the helper
test-check-string-equal:
    # check-string-equal?("Abc", "Abc")
    # . . push args
    68/push  "Abc"/imm32
    68/push  "Abc"/imm32
    # . . call
    e8/call  check-string-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # check-ints-equal(eax, 1, msg)
    # . . push args
    68/push  "F - test-check-string-equal"/imm32
    68/push  0/imm32/false
    50/push-eax
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    c3/return

# . . vim:nowrap:textwidth=0