【问题标题】:linux assembly: how to call syscall?linux 程序集:如何调用系统调用?
【发布时间】:2013-12-18 00:38:55
【问题描述】:

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

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 位应用程序,您应该使用64-bit way of doing syscalls 吗?
  • 比特的奇怪组合是怎么回事?我感觉到有人尝试将 32 位样本合并到 64 位项目中...

标签: linux assembly x86-64 system-calls


【解决方案1】:

要在 64 位 Linux 中进行系统调用,请将系统调用号放入 rax,并将其参数依次放入 rdi、rsi、rdx、r10、r8 和 r9,然后调用 syscall。

请注意,64 位索书号与 32 位索书号不同。

这是 GAS 语法的示例。将地址放入寄存器的 NASM 语法是 lea rsi, [rel message] 使用 RIP 相对 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"

另见What happens if you use the 32-bit int 0x80 Linux ABI in 64-bit code?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-01
    • 2021-10-13
    • 2013-02-06
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    • 2015-03-23
    相关资源
    最近更新 更多