Megidd
Megidd

Reputation: 7938

When I do a push instruction, I receive: Segmentation fault (core dumped)

At the following code, when I un-comment any of the push instructions, I receive the error Segmentation fault (core dumped) while running the executable. I tried to pin-point the error cause, but the cause is not found yet.

section .data
; Message contains app purpose
msg db 'This app calculates 2^3+5^2',0x0a
mlen equ $-msg

msg1 db 'Computation is done',0x0a
lmsg1 equ $-msg1

num1 dd 2
lnum1 equ $-num1

pow1 dd 3
lpow1 equ $-pow1

num2 dd 5
lnum2 equ $-num2

pow2 dd 2
lpow2 equ $-pow2

section .text
global _start
_start:

    xor edx, edx ; clear registers
    xor ecx, ecx
    xor ebx, ebx
    xor eax, eax
    xor esi, esi
    xor edi, edi
    xor esp, esp
    xor ebp, ebp

    mov edx, mlen
    mov ecx, msg
    mov ebx, 1
    mov eax, 4
    int 0x80 ; print message

    mov edx, dword [pow2]
    mov ecx, dword [num2]
    mov ebx, dword [pow1]
    mov eax, dword [num1]
    ;push edx
    ;push ecx ; --> When I un-comment any push command,
    ;push ebx ; --> I receive: Segmentation fault (core dumped)
    ;push eax ;

    jmp end

end:
    mov edx, lmsg1      ; length
    mov ecx, msg1       ; memory location
    mov ebx, 1
    mov eax, 4
    int 0x80            ; print a newline i.e. 0x0a 

    mov ebx, 0
    mov eax, 1
    int 0x80

Upvotes: 0

Views: 1115

Answers (1)

Johan
Johan

Reputation: 76537

What did you expect?
If you mess with the stackpointer (esp) obviously you are going to be in trouble.

On x86 protected mode address 0 is never a valid destination.
In addition all 'negative' addresses (0x80000000-0xFFFFFFFF) are in kernel space.

You reset esp and then you push, meaning you store a register in address 0-4 = 0xFFFFFFFC. This will fail because your process in user space does not have access to kernel space.

Regardless of the details, you can never mess with ESP like that. Only ever increment or decrement ESP as needed to clear or create stack space.
Never set esp to an absolute address.

Upvotes: 1

Related Questions