0

I have trying to write a program in x86 and it's giving me segmentation fault. I have narrowed it down to this segment. Why is this giving a segmentation fault? I did the same thing without the program (that is write the same control flow) and there was no error

 segment .bss

 a resb 4
 b resb 4
 m resb 4
section .text
global _start    ;must be declared for using gcc

toh :

pop eax
mov [a],eax
mov eax, 1
mov [b],eax
mov eax, [b]
push eax
ret

_start:             ;tell linker entry point

mov eax,2
push eax
call toh
pop eax
mov [m],eax

mov eax,1
int 0x80               
4
  • 3
    push eax; ret; - you're literally setting eip to eax here. This is not what you want. The stack is not used to return values this way. Commented Apr 28, 2017 at 23:40
  • Be sure to use a debugger to help you visualize what's going on. Commented Apr 29, 2017 at 2:20
  • As @DanielKamilKozar indicated, the Return from Procedure (RET) is corrupted by the function's push instruction (as well as the pop instruction). So instead of the program returning to the location, following the function call, after RET, it is trying to return to location 0x1 or 0x2; which results in "Cannot access memory at address 0x2" then "SIGSEGV, Segmentation fault". Commented Apr 29, 2017 at 4:24
  • Thank you guys. I have figured it out. The call command puts the return address on the stack, which I am popping during the function execution. So first I pop the return value store it somewhere, and before ret I push it back on to the stack
    – Gopick
    Commented Apr 30, 2017 at 4:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.