Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern printf
- segment .data
- n_pt_s db `asm: x%.8X `,10,0
- s_pt_s db `asm: x%.8X %s `,10,` x%.8X %s `,10,0
- segment .txt
- global organize_strings
- global compare2strs
- organize_strings:
- enter 4,0
- mov esi, [ebp+08] ;pointer to string_pointers
- ;;mov ecx, [ebp+12] ;length rests in here
- ;;for(i2=0;i2<length-1;i2++)
- ;;for(i1=i2+1;i1<length;i1++)
- ;;lowest_string< is eax >=string_pointer(i2)
- ;;get_smallest(lowest_string,string_i1) ;; this function will put the smallest of the two strings into eax
- ;;end for (of i1)
- ;;now swap the two addresses in the string_pointers array
- ;;end for (of i2)
- mov ecx, 0
- mov [ebp-04], ecx ;; ebp-4 is my big counter, i2.
- o_s_loop2:
- mov ebx, [ebp+12] ;; ebx to hold constant value of length of strlist
- mov edx, ecx ;; setting edx to i2
- o_s_loop1:
- inc ecx ;; setting i1 to i2+1, or just incrementing i1
- cmp ecx, ebx ;; loop1 comparison done here
- jge o_s_1done ;; if ecx has reached length, we're done with loop1
- ;mov eax, edx ;; DEBUG
- ;call num_print ;; DEBUG
- ;mov eax, ecx ;; DEBUG
- ;call num_print ;; DEBUG
- push ebx ;; not all of these have to be pushed
- push ecx ;; but I know that esi needs to
- push edx ;; & maybe ebx
- push esi ;; but we're extra safe like this
- mov eax, [esi+ecx*4] ;; string which ecx corresponds to
- push eax ;; argument 2 of compare2strs
- ;call num_print ;; DEBUG
- mov eax, [esi+edx*4] ;; string which edx corresponds to
- push eax ;; argument 1 of compare2strs
- ;call num_print ;; DEBUG
- call compare2strs ;; compare2strs(tempminstr,strlist[i1])
- add esp, 8 ;; pop eax pop eax
- ;push ecx ;; DEBUG
- ;mov ecx, [ebp-4] ;; DEBUG
- ;cmp ecx, 2 ;; DEBUG
- ;jge o_s_2done ;; DEBUG
- ;pop ecx
- pop esi ;; gettin my 4 registers back
- pop edx ;; just in case they changed
- pop ecx ;;
- pop ebx ;;
- ;call num_print ;; DEBUG
- cmp eax, 0 ;; was my smallest in edx really the smallest
- je o_s_no_change ;; if so, I'm done with this loop
- mov edx, ecx ;; if not, then ecx is the new edx
- o_s_no_change:
- ;mov eax, ecx ;; DEBUG
- ;call num_print ;; DEBUG
- ;mov eax, ebx ;; DEBUG
- ;call num_print ;; DEBUG
- jmp o_s_loop1 ;; jump beginning of loop (comparison done up there)
- ;mov edx, eax ;; DEBUG
- ;push eax ;; DEBUG
- ;call num_print ;; DEBUG
- ;add esp, 4 ;; DEBUG
- o_s_1done:
- mov eax, [esi+4*edx] ;; this is the smallest string inside the i1 loop
- mov ecx, [ebp-04] ;; loading i2
- mov ebx, [esi+4*ecx] ;; this is where smallest string needs to go to
- mov [esi+4*ecx], eax ;; writing the smallest string to firstest position
- mov [esi+4*edx], ebx ;; moving the larger string to the empty slot (swap sort)
- ;call num_print ;; DEBUG ;; should be a valid pointer address
- ;mov eax, ebx ;; DEBUG ;; these two prints are the two swapped string pointers
- ;call num_print ;; DEBUG ;; should be a valid pointer address
- ;mov ecx, [ebp-04] ;; loading big counter i2 ;; I just did this 4 lines earlier
- add ecx, 2 ;; increment i2 by 1 (yes 1, look at lines below)
- cmp ecx, ebx ;; comparing i2 to length-1
- jge o_s_2done ;; if they are equal, we're out of here, if it's greater
- ;; there is an error & we need to get out of this anyway
- dec ecx ;; see, only incrementing i2 by 1
- mov [ebp-04], ecx ;; storing big counter i2
- ;; (i1 starts from where i2 left off
- ;; (well, the initial comparison is the val i1,
- ;; which you see set up at the beginning of the loop))
- jmp o_s_loop2 ;; if I made it here, I'm ready for another round of swapsort
- o_s_2done:
- leave
- ret
- compare2strs:
- enter 0,0
- mov esi, [ebp+08]
- mov edi, [ebp+12]
- ;mov eax, esi ;; DEBUG
- ;call num_print ;; DEBUG
- ;mov eax, edi ;; DEBUG
- ;call num_print ;; DEBUG
- ;call string_print ;; DEBUG
- ;; comparing 2 strings & putting a 0 in if strptr1 is lowest, or 1 if strptr2 is lowest
- ;; 0 EAX [???] ;; using al as character of esi
- ;; 1 EBX [???] ;; using bl as character of edi
- ;; 2 ECX [???] ;; will be used as counter
- ;; 3 EDX [???] ;; keeping a const int length in here
- ;; 4 ESI [strptr1] ;; string 1
- ;; 5 EDI [strptr2] ;; string 2
- ;; 6 EBP [???] ;;
- ;; 7 ESP [???] ;;
- ;; load up characters of string into eax & ebx
- cmp_lp: mov al, [esi] ;;
- mov bl, [edi] ;;
- cmp al, bl ;;
- jg cmp_gre
- jl cmp_les
- ;;je cmp_equ
- cmp al, 0
- je cmp_les
- inc esi
- inc edi
- jmp cmp_lp
- cmp_gre: mov eax, 1
- leave
- ret
- cmp_les: mov eax, 0
- leave
- ret
- ;;-----------------------------------------------------------------------------------------------------
- ;; everything below here is used for debug purposes only
- ;;-----------------------------------------------------------------------------------------------------
- string_print: pusha
- push edi
- push edi
- push esi ;;remember kids, push your friends
- push esi ;;over backwards for accurate results
- push s_pt_s
- call printf
- add esp, 14h ;
- popa
- ret
- num_print: pusha
- push eax
- push n_pt_s
- call printf
- add esp, 8 ;remove 2 pushes on stack
- popa
- ret
Advertisement
Add Comment
Please, Sign In to add comment