genghiskhan
genghiskhan

Reputation: 1149

x86 NASM changes value at address not passed as parameter

I am noticing some peculiar behavior with scanf in NASM assembly code. I have two calls to scanf:

mov rdi, fmt
mov rsi, r14
call _scanf

and

mov rdi, fmt
mov rsi, r15
call _scanf

where fmt is declared in the data section as:

section .data
fmt: db "%d", 0

Before the first scanf, the addresses in r14 and r15 are:

r14 = 0x0000000000002104
r15 = 0x0000000000002105

In LLDB, executing me read -fd -c1 on either of these addresses outputs 0.

After entering "2" for the first scanf, the value in 0x0000000000002104 is thankfully 2.

After entering "2" for the second scanf, the value in 0x0000000000002105 is 2. However, now the value in 0x0000000000002104 is 514.

I am experiencing similar changes in memory after calls to scanf in other places and will reproduce them if needed but wanted to know if anyone has experienced this.

Upvotes: 0

Views: 179

Answers (1)

genghiskhan
genghiskhan

Reputation: 1149

Previously, I was using %d as the format for scanf which reads 4 bytes. However the addresses in r14 and r15 were reserved in the bss section for only 1 byte. I changed this so that they now reserve 4 bytes and the program works as expected.

Upvotes: 3

Related Questions