加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux汇编:如何调用syscall?

发布时间:2020-12-14 00:05:42 所属栏目:Linux 来源:网络整理
导读:我想在汇编中调用一个系统调用.问题是我不能运行ecx,rsp. rsp是64位寄存器,ecx是32位寄存器.我想将缓冲区addr作为此系统调用的参数传递.我能做什么?谢谢. section .data s0: db "Largest basic function number supported:%sn",0s0len: equ $-s0section .t
我想在汇编中调用一个系统调用.问题是我不能运行ecx,rsp. rsp是64位寄存器,ecx是32位寄存器.我想将缓冲区addr作为此系统调用的参数传递.我能做什么?谢谢.

section .data 
s0: db "Largest basic function number supported:%sn",0
s0len: equ $-s0

section .text 
global main
extern write
main: 
sub rsp,16
xor eax,eax
cpuid

mov [rsp],ebx
mov [rsp+4],edx
mov [rsp+8],ecx 
mov [rsp+12],word 0x0

mov eax,4
mov ebx,1
mov ecx,rsp
mov edx,4 
int 80h

mov eax,s0
mov edx,s0len 
int 80h

mov eax,1
int 80h

解决方法

要在64位Linux中进行系统调用,请将系统调用号放在rax中,然后将其参数按顺序放在rdi,rsi,rdx,r10,r8和r9中,然后调用syscall.

这是一个例子

.global _start

        .text
_start:
        # write(1,message,13)
        mov     $1,%rax                # system call 1 is write
        mov     $1,%rdi                # file handle 1 is stdout
        mov     $message,%rsi          # address of string to output
        mov     $13,%rdx               # number of bytes
        syscall

        # exit(0)
        mov     $60,%rax               # system call 60 is exit
        xor     %rdi,%rdi              # return code 0
        syscall
message:
        .ascii  "Hello,Worldn"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读