86 GNU汇编奇怪的变化赛格故障 [英] x86 GNU Assembler Strange Change Seg Fault

查看:146
本文介绍了86 GNU汇编奇怪的变化赛格故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下x86汇编code组装好,并用它我校的Linux服务器上完美运行,但应用相同的code到我的Linux虚拟机的时候(Ubuntu的14.04,突然它会导致分割故障。

难道栈约定发生变化,这是一个GNU汇编问题?我错过了什么备忘录?
我是一个64位的机器上运行,这是一场热身赛,以建立一个操作系统的骨干,所以我需要能够使用16位实,32位保护,以及64位模式都在相同的程序。所以我想我真正需要的是有关使在同一个程序的有效模式,所有的小细节。我知道使用。code16 / 32/64改变模式的时候,但我想我错过了什么(似乎并不能在任何操作系统教程中找到的,是如何做到这一点的64位架构。

  code32
。文本
。全球_start_开始:    pushl $ STR1
    pushl $ LEN1
    调用打印
    ADDL $ 8%ESP< -cleans堆栈指针出口:    MOVL $ 1,%eax中
    MOVL $ 0%EBX
    INT 0x80的$打印:    pushl%EBP
    MOVL%ESP,EBP%    MOVL $ 4,%EAX
    MOVL $ 1,EBX%
    MOVL 12(%EBP),ECX%LT - ; - 这是赛格故障按照发生GDB
    MOVL 8(EBP%),EDX%
    INT 0x80的$
    popl%EBP
    RET。数据STR1:.ascii字符串1 \\ nLEN1 =。 - STR1


解决方案

我猜你有一个64位的机器,而你的程序显然是32位的。

我有一个64位的机器,如果我用这个命令编译它,它失败了,同一行你:

  $ GCC -nostdlib test.s

不过,如果我编译一个32位可执行文件:

  $ GCC -nostdlib -m32 test.s

所有的罚款。

请注意,您可能需要一些程序包能够在64位机上编译32位程序( G ++ - multilib的或不管他们叫这些天)。

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

解决方案

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

这篇关于86 GNU汇编奇怪的变化赛格故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆