sys_read syscall与GNU汇编程序中的int 0x80 [英] sys_read syscall vs. int 0x80 in GNU Assembler

查看:212
本文介绍了sys_read syscall与GNU汇编程序中的int 0x80的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个简单的程序,该程序可以从stdin中获取许多字符.为简洁起见,相关代码为:

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

当我使用int $0x80时,程序会按预期运行,但是使用syscall时,它会出现段错误.我读到它与以下事实有关:使用中断要求内核记住计算机的状态,而syscall不满足该要求,即内核在自己的时间内处理它.我不确定这是否是真正的原因-我会假设syscall对寄存器执行了某些操作,导致sys_read失败.

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.

我还从此处发布的上一个问题中了解到,"syscall是进入内核的默认方式",而"int 0x80是调用系统调用的传统方式,应避免使用." (链接)

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.

错字

推荐答案

检查这个问题.在x86_64上,正确的退出系统调用为$60,应位于%rax中.

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

这篇关于sys_read syscall与GNU汇编程序中的int 0x80的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆