Skip to content

Instantly share code, notes, and snippets.

/sufsort.asm Secret

Created December 14, 2016 17:14
Show Gist options
  • Save anonymous/1e5d786edce17382c5b6d266cfc73561 to your computer and use it in GitHub Desktop.
Save anonymous/1e5d786edce17382c5b6d266cfc73561 to your computer and use it in GitHub Desktop.
%include "asm_io.inc"
SECTION .data
argErr: db "incorrect number of command line arguments", 0
lenErr: db "incorrect length of input", 0
compErr: db "incorrect command line arguments must be composed of 0,1,2", 0
SECTION .bss
X: resb 30
input: resd 1
charCount: resd 1
counter: resd 1
i: resd 1
j: resd 1
SECTION .text
global asm_main
sufcmp:
enter 0,0
pusha
; init
mov [p], dword 1 ;p
mov ebx, dword[ebp+8] ;X
mov edx, dword[ebp+12] ;i
mov edx, dword[ebp+16] ;j
mov [i], edx
mov [j], edx
RETURN:
popa
mov eax, dword[p]
leave
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
asm_main:
enter 0,0
pusha
mov eax, dword [ebp+8] ;argc
cmp eax,2 ;argc should be 2
jne argumentError
mov ebx, dword [ebp+12] ; address of arg[v]
mov ecx, dword [ebx+4] ; arg[]
mov [input], dword ecx
mov [charCount], dword 0
mov edx, 0
jmp loop1
loop1:
mov ecx, dword[input]
cmp byte[ecx], 0
je checkLength
add [input], dword 1
inc edx
jmp loop1
checkLength:
cmp edx, 1
jb lengthError
cmp edx, 30
ja lengthError
sub [input], dword edx ;resetting input to beginning
jmp checkComp
checkComp:
mov eax, 0
mov ebx, dword [input]
cmp byte[ebx], 0
je goodInput
cmp byte[ebx], '0'
jb compositionError
cmp byte[ebx], '2'
ja compositionError
mov ecx, X
add ecx, dword[charCount]
mov al, byte[ebx]
mov [ecx], al
add [input], dword 1
add [charCount], dword 1
jmp checkComp
goodInput:
mov eax,X
call print_string
call print_nl
jmp stillSort
; stillSort:
; call sufcmp
; mov bl, byte ptr[eax] ;get 1 byte
; mov dl, byte ptr[eax+1] ;and the byte to its right
; cmp bl, dl ;compare the 2
; jg notdone ;if byte 2 > byte 1, not sorted, go sort
; push eax ;save where we are
; push ecx ;save counter
; lea eax, input ;go back to start (for test)
; xor ecx, ecx ;reset counter
; jmp test_sorted ;go test the whole list
;notDone:
; mov byte ptr[eax], dl ;put byte 2 in byte 1 position
; mov byte ptr[eax+1], bl ;put byte 1 in byte 2 position
; inc eax ;go to next byte
; inc ecx ;count
; cmp ecx, dword ptr[charCount] ;10 elements (0-9)
; jnz stillsort ;still sorting (no reset)
; lea eax, input ;did all 10 elements, go again from start
; xor ecx, ecx ;reset counter
; jmp stillsort ;back to sort code
;
; testSorted:
; mov bl, byte ptr[eax] ;get 1 byte
; mov dl, byte ptr[eax+1] ;and the byte to its right
; cmp bl, dl ;compare
; jg nope ;if byte 2 > byte 1 the whole list isnt sorted
; inc eax ;try next byte
; inc ecx ;count
; cmp ecx, dword ptr[charCount] ;10 elements (0-9)
; jz done ;all 10 elements are sorted
; jmp test_sorted ;or loop
;nope:
;pop ecx ;get the back the old count
;pop eax ;back to the last byte we were on
;inc eax ;but 1 more now
;inc ecx ;increase counter
;cmp ecx, dword ptr[charCount] ;10 elements (0-9)
;jnz stillsort ;sorting
;lea eax, input ;it was the last element, back to start
;xor ecx, ecx ;reset counter
;jmp stillsort ;sorting
loop2:
mov ecx, dword[charCount]
cmp byte[ecx],0
ja loop3
jb asm_main_end
sub [charCount],dword 1
dec edx
jmp loop2
loop3:
mov ecx,X
cmp
argumentError:
mov eax, argErr
call print_string
call print_nl
jmp asm_main_end
lengthError:
mov eax, lenErr
call print_string
call print_nl
jmp asm_main_end
compositionError:
mov eax, compErr
call print_string
call print_nl
jmp asm_main_end
asm_main_end:
popa ; restore all registers
leave
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment