x86 GNU Assembler Strange Change Seg Fault

时光毁灭记忆、已成空白 提交于 2019-12-01 06:29:33

问题


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

回答1:


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).



来源:https://stackoverflow.com/questions/23938907/x86-gnu-assembler-strange-change-seg-fault

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!