使用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
writesyscall: Thewritesyscall (number 1) requires the first argument in%rdito be the file descriptor. For stdout, this should be1, 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, %raxandpushq %raxserve 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, %rbplines 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
%raxafter thesyscallinstruction
内容的提问来源于stack exchange,提问作者Elias Fyksen
相关产品推荐
相关产品推荐

