sys_read syscall vs. int 0x80 in GNU Assembler [duplicate]

房东的猫 提交于 2019-12-11 08:42:32

问题


I'm attempting to write a simple program which grabs a number of characters from stdin. For the sake of brevity, the relevant code is:

mov $3, %rax    # sys_read = 3
mov $0, %rbx    # stdin fd = 0
mov $b, %rcx    # '.lcomm b, 32' declared in .bss section
mov $32,%rdx    # size_t
# syscall
int $0x80

When I use int $0x80 the program functions as intended, however with syscall it segfaults. I read that it has something to do with the fact that using an interrupt requires the kernel to remember the state of the machine, while syscall does not honour that requirement, i.e., the kernel handles it in its own time. I'm not sure if this is the real reason - I would assume that syscall does something to the registers such that sys_read fails.

I also read from a previous question posted here that "syscall is the default way of entering the kernel" and that "int 0x80 is the legacy way to invoke a system call and should be avoided." (Link)

I can't really find any good documentation on this, so any input would be appreciated.

Edit: typo


回答1:


Check this question. On x86_64 the correct exit system call is $60 which should be in %rax.

 mov $60, %rax
 mov $0, %rdi 
 syscall


来源:https://stackoverflow.com/questions/14033028/sys-read-syscall-vs-int-0x80-in-gnu-assembler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!