Skip to content

Instantly share code, notes, and snippets.

@astarasikov
Created September 16, 2015 17:07
Show Gist options
  • Save astarasikov/bf0601f1cdcc7c86e769 to your computer and use it in GitHub Desktop.
Save astarasikov/bf0601f1cdcc7c86e769 to your computer and use it in GitHub Desktop.
assembly stub to clear caller's stack frame on i386 linux/gcc
.globl mcount
mcount:
push %eax
push %ebx
push %ecx
mov %esp,%eax
add $0x10,%eax //retval + rax + rbx + rcx
mov %ebp,%ebx
sub %eax,%ebx //caller thread stack frame size = ebp - prev.sp
mov $0,%ecx //stack filler value
sub $4,%ebx //subtract stored sp offset
//first iteration of the loop also decrements the %ebx
//thus preventing overwriting the return address
loop:
sub $4,%ebx
cmp $0,%ebx
jle done
mov %ecx,(%eax,%ebx,1)
jmp loop
done:
pop %ecx
pop %ebx
pop %eax
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment