Linux程序集:如何调用syscall? [英] linux assembly: how to call syscall?

查看:170
本文介绍了Linux程序集:如何调用syscall?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在程序集中调用syscall.问题是我不能mov ecx,rsp. rsp是64位寄存器,ecx是32位寄存器.我想将缓冲区地址作为此系统调用的参数传递.我能做些什么?谢谢.

I want to call a syscall in assembly. The problem is I can't mov ecx,rsp. rsp is 64-bit register, ecx is a 32-bit register. I want to pass the buffer addr as a parameter of this syscall. What can I do? Thanks.

section .data 
s0: db "Largest basic function number supported:%s\n",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, 4
mov ebx, 1
mov ecx, s0
mov edx, s0len 
int 80h

mov eax, 1
int 80h

推荐答案

要在64位Linux上进行系统调用,请将系统调用号依次置于rax及其参数中,依次按rdi,rsi,rdx, r10,r8和r9,然后调用syscall.

To make a system call in 64-bit Linux, place the system call number in rax, and its arguments, in order, in rdi, rsi, rdx, r10, r8, and r9, then invoke syscall.

请注意,64位电话号码与32位电话号码不同.

Note that 64-bit call numbers are different from 32-bit call numbers.

这是GAS语法的示例.使用RIP相对LEA,将地址放入寄存器的NASM语法为lea rsi, [rel message].

Here is an example in GAS syntax. NASM syntax for putting an address in a register is lea rsi, [rel message] using a RIP-relative LEA.

        .global _start

        .text
_start:
        # write(1, message, 13)
        mov     $1, %rax                # system call 1 is write
        mov     $1, %rdi                # file handle 1 is stdout
        lea     message(%rip), %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

.section .rodata           # read-only data section
message:
        .ascii  "Hello, World\n"


另请参见 查看全文

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