extern printf
extern scanf
global main
section .text
main:
sub rsp, 0x10
mov rbx, rsp
add rbx, 0x08
mov rdi, format1
mov rsi, rbx
xor rax, rax
call scanf
mov rdi, format2
mov rsi, [rbx]
xor rax, rax
call printf
add rsp, 0x10
ret
format1:
db "%d", 0
format2:
db "%d", 0xa, 0
value:
dd 0xa
Above source is same with
#include <stdio.h>
int main(void)
{
int tmp;
scanf("%d", &tmp);
printf("%d\n", tmp);
}
It works well. But I have question. If I change my source code to
extern printf
extern scanf
global main
section .text
main:
mov rdi, format1
mov rsi, value
xor rax, rax
call scanf
mov rdi, format2
mov rsi, [value]
xor rax, rax
call printf
ret
format1:
db "%d", 0
format2:
db "%d", 0xa, 0
value:
dd 0xa
it makes segmentation fault. I think there are no difference between above source code and first one. Did I misunderstand?
gdb ./nameofprogramthen uselayout asmand thenlayout regand set a break point on main withb main, start the program withrunand then usenito step instruction by instruction? Or use a graphical frontend for GDB likeddd?call scanfsegmentation fault occur. However I passed format string tordiand address of value torsi. So I can't understand why scanf makes errorvaluein the.textsection..textis read only. What would happen if you read data into a memory location that is read only?