You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用AT&T语法汇编调用syscall无法输出到控制台求助

Fixing Your x86_64 AT&T Syntax Syscall Code

Let's walk through the issues in your code and fix them step by step:

Key Problems in Your Original Code

  • Missing file descriptor for write syscall: The write syscall (number 1) requires the first argument in %rdi to be the file descriptor. For stdout, this should be 1, but your code never sets this register—this is the primary reason your print operation wasn't working.
  • Unnecessary stack operations: The lines mov $44, %rax and pushq %rax serve no purpose here. They clutter the stack and don't contribute to your intended syscalls.
  • Redundant stack frame setup: Since you're not using local variables or complex stack operations, the pushq %rbp/movq %rsp, %rbp lines add unnecessary overhead (they don't break the code, but they're unnecessary for this simple program).

Corrected Code

.section .rodata
.LC0:
.string "abc\n"  ; Our string to print

.text
.globl _start
_start:
    ; write syscall: fd=1(stdout), buf=.LC0, count=4
    movq $1, %rdi    ; 1st arg: file descriptor (stdout)
    movq .LC0(%rip), %rsi  ; 2nd arg: pointer to our string
    movq $4, %rdx    ; 3rd arg: length of "abc\n" (4 bytes total)
    movq $1, %rax    ; Syscall number for write
    syscall

    ; exit syscall: exit code=0
    movq $0, %rdi    ; 1st arg: exit status code (0 = success)
    movq $60, %rax   ; Syscall number for exit
    syscall

How to Compile and Run

Use your existing compile command:

gcc -nostdlib file.s -o file

Then execute the binary:

./file

You’ll see abc printed to your terminal, followed by a clean exit with no errors.

Quick x86_64 Linux Syscall Reminder

For reference, x86_64 Linux syscalls follow these rules:

  • Syscall number is stored in %rax
  • Arguments are passed in registers in this order: %rdi, %rsi, %rdx, %r10, %r8, %r9
  • Return values are stored in %rax after the syscall instruction

内容的提问来源于stack exchange,提问作者Elias Fyksen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:05:23