0

I am new to nasm and have written this code:

section .text
global _start

; just experimenting with functions
do_something:
   mov DWORD [esp+0], 1;
   mov DWORD [esp+4], 1;
   mov DWORD [esp+0],0;
   mov DWORD [esp+4],0;
   ret ;

_start:
   mov ecx,.Const.0;
   mov edx,.Const.0.length;
   call log       ;
   xor ecx, ecx   ;
   xor edx, edx   ;
   call do_something
   xor ebx,ebx
   mov eax, 1
   int 0x80

.Const.0:
    DB "Hello world!", 0xA
.Const.0.length equ $-.Const.0

But when calling the "hello_world" function, I get a segmentation fault, it looks like the esp register is causing the problem when put in the function. Any help would be greatly appreciated :D

gearDev
  • 29
  • 1
  • 8
  • 2
    `[ESP+0]` is the return address and you're overwriting it. – Shift_Left Mar 09 '21 at 21:50
  • 1
    Also related: [Does the stack automatically get popped when leaving a function in x86 NASM assembly language?](https://stackoverflow.com/q/31206192) shows stack layout during a call. – Peter Cordes Mar 10 '21 at 06:39

0 Answers0