I'm trying to understand why as behaves differently than nasm when doing syscalls on the assembly level. Because I'm a glutton for punishment, I'm using Intel syntax. Here's my program:
.intel_syntax noprefix
.section .rodata
.LC0:
.string "Hello world!\n"
.text
.globl _start
.type _start, @function
_start:
mov edx, 13
mov ecx, OFFSET FLAT:.LC0
mov eax, 4
int 0x80
ret
I assemble with as -o prog.o prog.s and link with ld -s -o prog prog.o.
But when I run it, I get:
$ ./prog
Hello world!
Segmentation fault (core dumped)
GDB is not particularly helpful here. When I stepi on ret, it says Cannot access memory at address 0x1. Which is puzzling, because the value of ESP is:
(gdb) info registers esp
info registers esp
esp 0xbffff660 0xbffff660
Why does this program segfault?
retfrom your program, you need anexitsystem call. The1is the number of arguments (argc) which happens to be on the top of the stack, sorettries to use it as an address with obvious consequences.