NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: iAnzu on June 02, 2019, 05:43:07 AM

Title: segmentation fault when using ebp-4/ebp-8
Post by: iAnzu on June 02, 2019, 05:43:07 AM
Hello and thanks for reading!

Windows 64 / Nasm x86 / gcc

A friend helped me with some code and he used ebp-4/ebp-8 to store temporal variables, I thought it would make it more readable to allocate variables in .bss section and use them. Later, I thought I didn't want to have too many variables in .bss section and decided to go back to my friends implementation, also, at that time, my brain already started to like to use ebp-X to store temporal variables.

Linux 64 / Nasm x86 / gcc

Segmentation fault, and I didn't know why for a really long period of time. it occurred to me, as a last resort, to create variables for ebp-4 and others. That made it, no more segmentation fault; but I'm still curious about why did this happened? Why couldn't I use ebp-4 to store temporal variables on Linux when on Windows it was just fine?

nasm -f elf32 -o
gcc  -m32 -o

%macro HexToBin 3 
        ...;code
        ...
        mov         dword [ebp-4], dword 0  ; Segmentation fault
        ...
        ...;code
%%hextobinE:
%endmacro
Title: Re: segmentation fault when using ebp-4/ebp-8
Post by: fredericopissarra on June 02, 2019, 11:42:11 AM
Are you allocating space on stack for local vars?

Code: [Select]
  push ebp
  mov  ebp,esp
  ...
  sub  esp,4   ; allocate 4 bytes of "local" stack space.
  ...
  mov dword [ebp-4],0
  ...
  add  esp,4  ; deallocate
  ...
  pop ebp
  ret
Title: Re: segmentation fault when using ebp-4/ebp-8
Post by: iAnzu on June 04, 2019, 02:00:30 PM
Thanks a lot, that solved it! I'm still curious, why windows didn't catch/notice this problem?