首页 > 解决方案 > 在 Linux x86 程序集(64 位)中打印 argv[0]

问题描述

以下代码按预期工作,打印“Hello”。

.section .data

string:
    .ascii "Hello, World!\n\0"

.section .text

.globl _start

_start:
    mov $5, %rdx            # num characters to print
    mov $string, %rcx       # address of string to print
    mov $1, %rbx            # print to stdout      
    mov $4, %rax            # print syscall
    int $0x80

    mov $1, %rax            # exit syscall
    int $0x80

我正在进行单行更改以尝试打印 的前几个字符argv[0],这应该是程序的名称(例如:“/home/sam/Documents/print”)。但是,实际上似乎没有打印任何内容。为什么会这样?

.section .data

string:
    .ascii "Hello, World!\n\0"

.section .text

.globl _start

_start:
    mov $5, %rdx            # num characters to print
    mov 8(%rsp), %rcx       # <-- attempt to print argv[0]
    mov $1, %rbx            # print to stdout      
    mov $4, %rax            # print syscall
    int $0x80

    mov $1, %rax            # exit syscall
    int $0x80

当我尝试在 gdb 中调试时,似乎 rcx 寄存器具有正确的字符串地址。

x/s $rcx // 0x7fffffffe37f: "/home/sam/Documents/print"

已编辑

代码已更新并按预期工作。

.section .text

.globl _start

_start:
    movq $1, %rax             # print syscall
    movq $1, %rdi             # print to stdout      
    movq 8(%rsp), %rsi        # <-- attempt to print argv[0]
    movq $5, %rdx             # num characters to print
    syscall

    movq $60, %rax            # exit syscall
    syscall

标签: assemblyx86

解决方案


推荐阅读