Vulpes

225MP4_asm

Oct 29th, 2011
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. extern printf
  3.  
  4. segment .data
  5.         n_pt_s      db `asm: x%.8X `,10,0
  6.         s_pt_s      db `asm: x%.8X %s `,10,`     x%.8X %s `,10,0
  7.  
  8. segment .txt
  9.         global      organize_strings
  10.         global      compare2strs
  11.  
  12. organize_strings:
  13.    
  14.         enter       4,0
  15.         mov     esi,        [ebp+08]    ;pointer to string_pointers
  16.         ;;mov       ecx,        [ebp+12]    ;length rests in here
  17.  
  18.         ;;for(i2=0;i2<length-1;i2++)
  19.  
  20.         ;;for(i1=i2+1;i1<length;i1++)
  21.         ;;lowest_string< is eax >=string_pointer(i2)
  22.         ;;get_smallest(lowest_string,string_i1)     ;; this function will put the smallest of the two strings into eax
  23.         ;;end for (of i1)
  24.  
  25.         ;;now swap the two addresses in the string_pointers array
  26.  
  27.         ;;end for (of i2)
  28.        
  29.         mov     ecx,        0
  30.         mov     [ebp-04],   ecx     ;; ebp-4 is my big counter, i2.
  31.  
  32. o_s_loop2:
  33.         mov     ebx,        [ebp+12]    ;; ebx to hold constant value of length of strlist
  34.         mov     edx,        ecx     ;; setting edx to i2
  35. o_s_loop1: 
  36.         inc     ecx             ;; setting i1 to i2+1, or just incrementing i1
  37.         cmp     ecx,        ebx     ;; loop1 comparison done here
  38.         jge     o_s_1done           ;; if ecx has reached length, we're done with loop1
  39.         ;mov        eax,        edx     ;; DEBUG
  40.         ;call       num_print           ;; DEBUG
  41.         ;mov        eax,        ecx     ;; DEBUG
  42.         ;call       num_print           ;; DEBUG
  43.         push        ebx             ;; not all of these have to be pushed
  44.         push        ecx             ;; but I know that esi needs to
  45.         push        edx             ;; & maybe ebx
  46.         push        esi             ;; but we're extra safe like this
  47.  
  48.         mov     eax,        [esi+ecx*4] ;; string which ecx corresponds to
  49.         push        eax             ;; argument 2 of compare2strs
  50.         ;call       num_print           ;; DEBUG
  51.         mov     eax,        [esi+edx*4] ;; string which edx corresponds to
  52.         push        eax             ;; argument 1 of compare2strs
  53.         ;call       num_print           ;; DEBUG
  54.         call        compare2strs            ;; compare2strs(tempminstr,strlist[i1])
  55.         add     esp,        8       ;; pop eax pop eax
  56.  
  57.         ;push       ecx             ;; DEBUG
  58.         ;mov        ecx,        [ebp-4]     ;; DEBUG
  59.         ;cmp        ecx,        2       ;; DEBUG
  60.         ;jge        o_s_2done           ;; DEBUG
  61.         ;pop        ecx
  62.  
  63.  
  64.  
  65.         pop     esi             ;; gettin my 4 registers back
  66.         pop     edx             ;; just in case they changed
  67.         pop     ecx             ;;
  68.         pop     ebx             ;;
  69.    
  70.         ;call       num_print           ;; DEBUG
  71.         cmp     eax,        0       ;; was my smallest in edx really the smallest
  72.         je      o_s_no_change           ;; if so, I'm done with this loop
  73.         mov     edx,        ecx     ;; if not, then ecx is the new edx
  74. o_s_no_change: 
  75.         ;mov        eax,        ecx     ;; DEBUG
  76.         ;call       num_print           ;; DEBUG
  77.         ;mov        eax,        ebx     ;; DEBUG
  78.         ;call       num_print           ;; DEBUG
  79.         jmp     o_s_loop1           ;; jump beginning of loop (comparison done up there)
  80.    
  81.         ;mov        edx,        eax     ;; DEBUG
  82.         ;push       eax             ;; DEBUG
  83.         ;call       num_print           ;; DEBUG
  84.         ;add        esp,        4       ;; DEBUG
  85. o_s_1done: 
  86.         mov     eax,        [esi+4*edx] ;; this is the smallest string inside the i1 loop
  87.         mov     ecx,        [ebp-04]    ;; loading i2
  88.         mov     ebx,        [esi+4*ecx] ;; this is where smallest string needs to go to
  89.         mov     [esi+4*ecx],    eax     ;; writing the smallest string to firstest position
  90.         mov     [esi+4*edx],    ebx     ;; moving the larger string to the empty slot (swap sort)
  91.  
  92.         ;call       num_print           ;; DEBUG ;; should be a valid pointer address
  93.         ;mov        eax,        ebx     ;; DEBUG ;; these two prints are the two swapped string pointers
  94.         ;call       num_print           ;; DEBUG ;; should be a valid pointer address
  95.    
  96.         ;mov        ecx,        [ebp-04]    ;; loading big counter i2 ;; I just did this 4 lines earlier
  97.         add     ecx,        2       ;; increment i2 by 1 (yes 1, look at lines below)
  98.         cmp     ecx,        ebx     ;; comparing i2 to length-1
  99.         jge     o_s_2done           ;; if they are equal, we're out of here, if it's greater
  100.                                 ;; there is an error & we need to get out of this anyway
  101.         dec     ecx             ;; see, only incrementing i2 by 1
  102.         mov     [ebp-04],   ecx     ;; storing big counter i2
  103.                                 ;; (i1 starts from where i2 left off
  104.                                 ;; (well, the initial comparison is the val i1,
  105.                                 ;; which you see set up at the beginning of the loop))
  106.         jmp     o_s_loop2           ;; if I made it here, I'm ready for another round of swapsort
  107. o_s_2done:
  108.         leave      
  109.         ret
  110.  
  111.  
  112. compare2strs:  
  113.         enter       0,0
  114.         mov     esi,        [ebp+08]
  115.         mov     edi,        [ebp+12]
  116.        
  117.         ;mov        eax,        esi     ;; DEBUG
  118.         ;call       num_print           ;; DEBUG
  119.         ;mov        eax,        edi     ;; DEBUG
  120.         ;call       num_print           ;; DEBUG
  121.         ;call       string_print            ;; DEBUG
  122.  
  123.         ;; comparing 2 strings & putting a 0 in if strptr1 is lowest, or 1 if strptr2 is lowest
  124.         ;; 0 EAX [???]                  ;; using al as character of esi
  125.         ;; 1 EBX [???]                  ;; using bl as character of edi
  126.         ;; 2 ECX [???]                  ;; will be used as counter
  127.         ;; 3 EDX [???]                  ;; keeping a const int length in here
  128.         ;; 4 ESI [strptr1]              ;; string 1
  129.         ;; 5 EDI [strptr2]              ;; string 2
  130.         ;; 6 EBP [???]                  ;;
  131.         ;; 7 ESP [???]                  ;;
  132.  
  133.         ;; load up characters of string into eax & ebx
  134. cmp_lp:     mov     al,     [esi]   ;;
  135.         mov     bl,     [edi]   ;;
  136.         cmp     al,     bl  ;;
  137.         jg      cmp_gre    
  138.         jl      cmp_les    
  139.         ;;je        cmp_equ    
  140.         cmp     al,     0
  141.         je      cmp_les
  142.         inc     esi
  143.         inc     edi
  144.         jmp     cmp_lp
  145.  
  146. cmp_gre:    mov     eax,        1
  147.         leave
  148.         ret
  149. cmp_les:    mov     eax,        0
  150.         leave
  151.         ret
  152.  
  153. ;;-----------------------------------------------------------------------------------------------------
  154. ;; everything below here is used for debug purposes only
  155. ;;-----------------------------------------------------------------------------------------------------
  156.  
  157. string_print:   pusha
  158.  
  159.         push        edi
  160.         push        edi
  161.         push        esi ;;remember kids, push your friends
  162.         push        esi ;;over backwards for accurate results
  163.         push        s_pt_s
  164.        
  165.         call        printf
  166.         add     esp,        14h  ;     
  167.         popa
  168.         ret
  169.  
  170.  
  171.  
  172.  
  173. num_print:  pusha
  174.  
  175.         push        eax
  176.         push        n_pt_s
  177.            
  178.         call        printf
  179.         add     esp,    8  ;remove 2 pushes on stack
  180.        
  181.         popa
  182.         ret
Advertisement
Add Comment
Please, Sign In to add comment