使用AT&T汇编编写程序出现Segmentation fault(core dumped)错误求助
Hey there! Segmentation faults (core dumped) almost always boil down to accessing memory your program doesn't have permission to touch. Given your program's flow—print a prompt, read a number, then output it—here are the most likely culprits to check:
1. Invalid Memory Access During Input
If you're using C library functions like scanf, the biggest mistake here is passing the wrong type of argument for your variable:
- In AT&T syntax,
$nrefers to the immediate value ofn(its literal numeric value), while justnrefers to the memory address ofn. For the equivalent of C'sscanf("%d", &n), you need to push the address of your integer variable to the stack—not the value. If you accidentally push$ninstead ofn,scanfwill try to write input to a random memory location (treating the immediate value as an address), which guarantees a segfault. - If you're using raw
readsystem calls, double-check that your buffer is properly allocated (e.g., defined in the.dataor.bsssection) and that you're passing a valid buffer address—not an uninitialized register or invalid value.
2. Mismatched Format String and Arguments During Output
When printing the value with printf, mixing up values and addresses will cause problems:
- If your format string is
%d(for integers), you need to push the value ofn(usingpushl n, which loads the value from memory) to the stack. If you push the addressninstead,printfwill interpret that address as an integer (which might work for%d, but if you used the wrong format string like%s, it would try to read memory starting at that address—leading to a segfault).
3. Unbalanced Stack After C Library Calls
C library functions (like printf and scanf) use the cdecl calling convention, which means the caller is responsible for cleaning up the stack after the call. For example:
- If you push two arguments to call
scanf, you need to add8to%espafterward (since each push is 4 bytes on 32-bit systems) to restore the stack pointer. If you skip this, subsequent instructions or function calls will read/write to the wrong stack memory, causing a segfault.
Example of Working Code for Reference
Here's a minimal, correct AT&T assembly program that does what you're trying to do—you can compare it to your code to spot differences:
.data prompt: .string "Enter the value for n: " fmt_in: .string "%d" fmt_out: .string "You entered: %d\n" n: .long 0 # Integer variable initialized to 0 .text .globl main main: # Print prompt pushl $prompt call printf addl $4, %esp # Clean up 1 argument from stack # Read input into n pushl $n # Push address of n (correct for scanf) pushl $fmt_in call scanf addl $8, %esp # Clean up 2 arguments # Print the value of n pushl n # Push the value of n (loaded from memory) pushl $fmt_out call printf addl $8, %esp # Clean up 2 arguments # Exit program with status 0 movl $0, %eax ret
Go through your code and verify these three areas—chances are you'll find the issue there!
内容的提问来源于stack exchange,提问作者Anvar Abdulsatarov

