linux - Why do I have a segfault when using cmp instruction? -
i using yasm on linux x86_64.
i following introduction assembly language on internet. i've covered basic data types. i'm diving loops contruction. good, first attempt failure. code segfault around use of cmd
instruction:
segment .data dd 0x01,0x02,0x03,0x04 b dd 0x03,0x03,0x03,0x03 product dd 0x00 segment .text global _start _start: mov rax,0 begin_while: cmp rax,4 jnl end_while inc rax jmp begin_while end_while: mov rax,1 xor rbx,rbx int 0x80
but when add few lines after _start
label, behaves expected. i.e. no segfault.
push rbp mov rbp,rsp sub rsp,16
the book reading uses construct time time. not everytime. understand has procedure follow when calling function. guess might related libc runtime. anyway, not understand why it's needed. far few simple programs wrote (not much) never had use it. today, using jmp
.
does have explanation it?
well, using x86
interrupt call
int 0x80
which should changed
syscall
for x86_64
. relying on x86
compatibility while using 64-bit
registers. pointed out jester, code compile , run without error on x86_64
. (i have confirmed both ways without error on amd64 linux) however, extent true platforms unclear.
in writing x86_64 code, should change:
mov rax,1
to
mov rax, 0x3c ; 60 decimal
to setup proper x86_64
exit syscall instead of relying on x86
compatibility. (in x86_64, syscall number 1
__nr_write 1
, see: /usr/include/asm/unistd_64.h
compared /usr/include/asm/unistd_32.h
)
Comments
Post a Comment