x86 - assembly, segmentation fault -
this code results in segmentation fault, have no idea why it, code supposed pass current location of esp @ stack ebp , use indirect addressing mode on ebp value of address, don't know why os terminating results in segmentation fault
.section .data .section .text .globl _start _start: movl $50,%edx pushl, %edx movl %esp,%ebp movl (%ebp),%ebx ## causes problem reason, movl $1,%eax int $0x80 ## program should return exit status of %ebx value
as jester says, problem 64bit linux tools default making 64bit programs. has bad habit of writing answers in comments, i'll duplicate here:
as --32 test.s -o test.o; ld -melf_i386 test.o -o test
or
gcc -m32 foo.s -ffreestanding -nostdlib -o foo
you segfault at
movl (%ebp),%ebx
because %rsp
isn't all-zero in upper 32 bits, %esp
different address %rsp
. find problem gdb. you'd have noticed had 64bit registers. see https://stackoverflow.com/tags/x86/info info on using gdb asm.
i'm going pick on line more:
pushl, %edx
besides being syntax error (extra comma), _start
doesn't need save registers. x86-64 abi says should assume they're full of random garbage, except stack pointer. says %rdx
has address of function should register atexit()
, on current linux, %rdx
zeroed on process entry. (command line args on stack.)
i presume things pretty same x86 32bit process startup.
Comments
Post a Comment