patrickjp93
patrickjp93

Reputation: 65

x86 GNU Assembler Strange Change Seg Fault

The following x86 assembly code assembles fine, and it used to run flawlessly on my school's linux server, but when applying the same code to my linux virtual machine (ubuntu 14.04, all of a sudden it causes a segmentation fault.

Did stack conventions change, is this a GNU assembler problem? What memo did I miss? I am running on a 64-bit machine, and this is a warm-up to building the backbone of an OS, so I need to be able to use the 16-bit real, 32-bit protected, and the 64-bit mode all in the same program. So I suppose what I really need is the little details about making all modes valid in the same program. I know to use .code16/32/64 when changing modes, but I guess what I'm missing (and can't seem to find in any OS tutorial, is how to do this on 64-bit architecture.

.code32
.text 
.global _start

_start:

    pushl $str1
    pushl $len1
    call print
    addl $8, %esp <-cleans up the stack pointer

exit:

    movl $1, %eax
    movl $0, %ebx
    int $0x80

print:

    pushl %ebp
    movl %esp, %ebp

    movl $4, %eax
    movl $1, %ebx
    movl 12(%ebp), %ecx <- This is where the Seg Fault occurs according to GDB
    movl 8(%ebp), %edx
    int $0x80
    popl %ebp
    ret

.data

str1 : .ascii "String1\n"

len1 =  . - str1

Upvotes: 3

Views: 219

Answers (1)

rodrigo
rodrigo

Reputation: 98328

I'm guessing that you have a 64-bit machine, while your program is obviously 32-bit.

I have a 64-bit machine, if I compile it with this command, it fails, same line as you:

$ gcc -nostdlib test.s

However, if I compile a 32-bit executable:

$ gcc -nostdlib -m32 test.s

And all is fine.

Note that you may need some packages to be able to compile a 32-bit program in a 64-bit machine (g++-multilib or whatever they call it these days).

Upvotes: 1

Related Questions