NASM - The Netwide Assembler
NASM Forum => Programming with NASM => Topic started by: pedrodemargomes on April 24, 2020, 03:16:56 PM
-
This is my code:
global main
extern printf
extern scanf
section .text
main:
push rbp
mov rbp, rsp
mov rax, 42
push rax
lea rsi, [rbp-8]
mov rdi, formatNumScanf
xor rax, rax
call scanf wrt ..plt
mov rax, [rbp-8]
mov rdi, formatNumPrintf
mov rsi, rax
xor rax, rax
call printf wrt ..plt
mov rsp, rbp
pop rbp
section .rodata
formatNumPrintf: db '%ld', 10, 0
formatNumScanf: db '%ld', 0
It erros with a seg fault when calling scanf with [rbp-8] address, that is the top of the stack.
It is strange that if i push two registers onto the stack at the beggining, it works as expected.
What am i doing wrong?
-
Hi Pedro,
Welcome to the forum.
Curiously, your code works for me, just as posted. You don't seem to "ret" from "main"... but it works anyway. I am confused!
Best,
Frank
-
That is awkward.
I am using Ubuntu in a x86-64 pc and compiling with this command line: nasm -felf64 teste.s && gcc teste.o
Is there something wrong with it?
-
Hi again...
I am using Debian. Shouldn't make any difference.
As I said, I tried your code exactly as posted. It worked. Then I added a "ret" as the last thing on your ".text" section. I really think that should be there. No difference. Try it and see if it makes any difference for you. This is a real puzzle!
Best,
Frank
-
I added the ret instruction and it continues to give seg fault.
I runned the program with gdb, and discovered that the seg fault occurs in scanf.
Here is the gdb output:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a4f4d6 in char_buffer_rewind (buffer=0x7fffffffd8c8) at vfscanf.c:224
Strangely, when i push two registers onto the stack before calling scanf it works.
I think that it has something to do with stack aligment, maybe the stack must be align in 16 byte boundaries.
-
I am not good at 64-bit code. It does have some stack alignment requirements. I think you've probably got it.
Best,
Frank