I'm trying to learn how to use the x86 DIV instruction. Here is my external function located in a .S file:
.intel_syntax noprefix
.text
.global divExtern
divExtern:
mov edx, 0
mov eax, [esp+4]
mov ebx, [esp+8]
div ebx
ret
Here is my C code:
#include <stdio.h>
int extern divExtern(int n, int m);
int main() {
printf("%d", divExtern(10, 5));
return 0;
}
As you can see I am testing to see if 10/5 = 2. Using GDB I verified that 2 is indeed in EAX right before executing RET.
0x565555eb <divExtern> mov $0x0,%edx 3
30x565555f0 <divExtern+5> mov 0x4(%esp),%eax 3
30x565555f4 <divExtern+9> mov 0x8(%esp),%ebx 3
30x565555f8 <divExtern+13> div %ebx 3
>30x565555fa <divExtern+15> ret
(gdb) p $eax
$2 = 2
However I am still getting a seg fault.
GDB has shown me that EAX changed in between executing the function and printf
. Why is this the case and what can I do to prevent this?
0x565555af <main+15> call 0x56555470 <__x86.get_pc_thunk.bx> 3
30x565555b4 <main+20> add $0x1a4c,%ebx 3
30x565555ba <main+26> sub $0x8,%esp 3
30x565555bd <main+29> push $0x5 3
30x565555bf <main+31> push $0xa 3
30x565555c1 <main+33> call 0x565555eb <divExtern> 3
30x565555c6 <main+38> add $0x10,%esp 3
30x565555c9 <main+41> sub $0x8,%esp 3
30x565555cc <main+44> push %eax 3
30x565555cd <main+45> lea -0x1980(%ebx),%eax 3
30x565555d3 <main+51> push %eax 3
>30x565555d4 <main+52> call 0x56555400 <printf@plt>
(gdb) p $eax
$3 = -6523
In case you need to know I compiled this by running gcc test.c divExtern.S -m32 -o test