Skip to content

Instantly share code, notes, and snippets.

@scottt
Created May 6, 2013 13:09
Show Gist options
  • Save scottt/5524997 to your computer and use it in GitHub Desktop.
Save scottt/5524997 to your computer and use it in GitHub Desktop.
; File: strrev.asm
; A subroutine called from C programs.
; Parameters: string A
; Result: String is reversed and returned.
SECTION .text
global strrev
_strrev: nop
strrev:
push ebp
mov ebp, esp
; registers ebx,esi, and edi must be saved if used
push ebx
push edi
xor esi, esi
xor eax, eax
mov ecx, [ebp+8] ; load the start of the array into ecx
jecxz end ; jump if [ecx] is zero
mainLoop:
add eax, 1 ; icn eax would work as well
add ecx, 1
mov dl, [ecx] ; load ecx
cmp dl, 0 ; compare with 0
je reverseLoop ; if ecx is zero, we're done
jmp mainLoop ; if ecx isn't zero, keep looping
reverseLoop:
mov ecx, [ebp + 8] ; reload the start of the array into ecx
mov esi, ecx ; esi points to start of array
add ecx, eax
mov edi, ecx
dec edi ;edi points to end of array
shr eax, 1 ;eax is the count
jz end ; if string is 0 or 1 chars long, jump to end
reverseLoop_1:
mov cl, [esi] ;load initial array
mov bl, [edi] ;load end of array
mov [esi], bl ;swap
mov [edi], cl
inc esi
dec edi
dec eax ;loop
jnz reverseLoop_1
end:
pop edi ; restore registers
pop ebx
mov esp, ebp ; take down stack frame
pop ebp
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment