1

I am getting a segmentation fault for the following assembly code which simply prints out a message though the printing is handled by a separate function so I'm quite sure I'm not allocating the right space on the stack for the message and the length.

Here is the code:

section .data
    print_msg:      DB "H", 10, 0
    len:            equ $-print_msg 
    print_msg2:     DB "BYE WORLD", 10, 0
    len2:           equ $-print_msg2


section .text

    global main

main:
    push ebp
    mov ebp, esp

    push DWORD len
    push print_msg
    call _write

    push DWORD len2
    push print_msg2
    call _write

    leave
    ret

_write:
    push ebp
    mov ebp, esp   

    push ebx
    mov eax, 4
    mov ebx, 1
    mov ecx, [ebp+8]
    mov edx, [ebp+12]
    int 80h
    pop ebx

    leave 
    ret
4
  • Won't push print_msg push a pointer, and not the chars?
    – James
    Apr 14, 2014 at 14:15
  • @James: Indeed it will. Another problem is that the ebp-relative offsets inside _write are wrong (the old ebp will be at [ebp+0], and the return address will be at [ebp+4]).
    – Michael
    Apr 14, 2014 at 14:21
  • @James Oh and so the pointer is only 4 bytes?
    – user28130
    Apr 14, 2014 at 14:21
  • @Michael so do you think I should get rid of pushing ebp within the _write and just sort out the offsets.
    – user28130
    Apr 14, 2014 at 14:25

1 Answer 1

0
  1. You don't need to allocate any space on the stack because push will do that for you.
  2. push DWORD [len] is wrong because it tries to dereference len which is just a number.
  3. mov ecx, [ebp+2] and mov edx, [ebp+6] are using wrong offsets, they should be +8 and +12, respectively. Stack layout from ebp is: saved_ebp, return address, arg1, arg2 (4 bytes each)

Like so:

section .data
    print_msg:      DB "H", 10, 0
    len:            equ $-print_msg

section .text

    global main
main:
    push ebp
    mov ebp, esp
    push DWORD len
    push print_msg
    call _write
    leave
    ret

_write:
    push ebp
    mov ebp, esp
    mov eax, 4
    mov ebx, 1
    mov ecx, [ebp+8]
    mov edx, [ebp+12]
    int 80h
    leave
    ret

PS: C calling convention mandates you should preserve ebx, so the _write function should save and restore it.

6
  • Thank you so much, I understand, I've been following tutorials where we need to physically allocate space for items to be pushed onto the stack.
    – user28130
    Apr 14, 2014 at 14:34
  • Oh so I should push ebx on the stack and then restore it outside the function.
    – user28130
    Apr 14, 2014 at 14:35
  • You can allocate space, but then you should use mov to write to this space and not push. Make sure you allocate 4 bytes for each argument, too. For saving/restoring ebx you should push and pop it inside _write itself, I recommend after the mov ebp, esp and before the leave, respectively.
    – Jester
    Apr 14, 2014 at 14:40
  • I have extended my code and tried to sort restore ebx values, have I done it right, it works just wondering about ebx restoration.
    – user28130
    Apr 14, 2014 at 14:48
  • You still have the unnecessary space allocation, and you haven't put the ebx save/restore where I told you. But it works :)
    – Jester
    Apr 14, 2014 at 14:50

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.