相关疑难解决方法(0)

如何在64位汇编程序中使用RIP相对寻址?

如何在AMD64架构的Linux汇编程序中使用RIP相对寻址?我正在寻找一个使用AMD64 RIP相对地址模式的简单示例(Hello world程序).

例如,以下64位汇编程序将与普通(绝对寻址)一起使用:

.text
    .global _start

_start:
    mov $0xd, %rdx

    mov $msg, %rsi
    pushq $0x1
    pop %rax
    mov %rax, %rdi
    syscall

    xor %rdi, %rdi
    pushq $0x3c
    pop %rax
    syscall

.data
msg:
    .ascii    "Hello world!\n"
Run Code Online (Sandbox Code Playgroud)

我猜测使用RIP相对寻址的相同程序将是这样的:

.text
    .global _start

_start:
    mov $0xd, %rdx

    mov msg(%rip), %rsi
    pushq $0x1
    pop %rax
    mov %rax, %rdi
    syscall

    xor %rdi, %rdi
    pushq $0x3c
    pop %rax
    syscall

msg:
    .ascii    "Hello world!\n"
Run Code Online (Sandbox Code Playgroud)

编译时,正常版本运行正常:

as -o hello.o hello.s && ld -s -o hello hello.o && ./hello
Run Code Online (Sandbox Code Playgroud)

但我无法使RIP版本正常工作. …

64-bit assembly x86-64 gnu-assembler

27
推荐指数
1
解决办法
2万
查看次数

标签 统计

64-bit ×1

assembly ×1

gnu-assembler ×1

x86-64 ×1