为什么我的程序在Ubuntu gcc上而不在OSX gcc上运行? [英] Why does my program run on Ubuntu gcc but not OSX gcc?

查看:106
本文介绍了为什么我的程序在Ubuntu gcc上而不在OSX gcc上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我的作业是在Ubuntu中运行的,它可以很好地编译并按应有的方式运行.但是,当我在Mac OSX中运行此命令时,会出现总线错误.这是为什么?

So my homework, I ran it in Ubuntu, and it compiles fine and runs like the way it should. But when I run this in Mac OSX, it gets a bus error. Why is that?

我正在使用gcc -m32 source.c -o test

这是Mac OSX版本(添加了下划线前缀):

Here's the Mac OSX version (added prefixed underscores):

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char phrase[] = "slow but sure";
int sz;
int phrasesz;
char *arg;
char *result;

// Add any extra variables you may need here.

int main(int argc, char* argv[]) {
   if (argc != 2) {
     printf("Usage: %s takes 1 string argument.\n", argv[0]);
     exit(1);
   }

   // Allocate memory and copy argument string into arg.

   sz = strlen(argv[1]) + 1;
   arg = malloc(sz);
   strcpy(arg, argv[1]);

   // Allocate lots of memory for the result.

   phrasesz = strlen(phrase) + 1;
   result = malloc(sz * phrasesz);

   // Now copy phrase into result, while replacing SPACE
   // with SPACE+arg+SPACE.

__asm__("\n\
    leal    _phrase, %esi\n\
    movl    _result, %ebx\n\
outerLoop:\n\
    cmpb    $0, (%esi)\n\
    je      finished\n\
forLoop:\n\
    cmpb    $32,(%esi)\n\
    je      endLoop\n\
    cmpb    $0, (%esi)\n\
    je      finished\n\
    mov     (%esi), %eax\n\
    mov     %eax, (%ebx)\n\
    incl    %ebx\n\
    incl    %esi\n\
    jmp     forLoop\n\
endLoop:\n\
    mov     (%esi), %eax\n\
    mov     %eax, (%ebx)\n\
    incl    %ebx\n\
    incl    %esi\n\
    movl    _arg, %edx\n\
copyArgv1IntoResult:\n\
    cmpb    $0, (%edx)\n\
    je      finishedCopyingArgv1\n\
    mov     (%edx), %ecx\n\
    mov     %ecx, (%ebx)\n\
    incl    %ebx\n\
    incl    %edx\n\
    jmp     copyArgv1IntoResult\n\
finishedCopyingArgv1:\n\
    movb    $32, (%ebx)\n\
    incl    %ebx\n\
    jmp     outerLoop\n\
finished:\n\
    movb    $0, (%ebx)\n\
");

   printf("%s\n", result);
   return 0;
}

更新:

我在gdb调试器中运行了它,这是我得到的错误.

I ran it in gdb debugger and this is the error I am getting.

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00000000
0x00001ee8 in finished ()
1: x/i $pc  0x1ee8 <finished+11>:   mov    (%eax),%eax

此外,我将删除Ubuntu版本,以减少滚动.

Also, I am removing the Ubuntu version so there's less scrolling.

推荐答案

您的某些说明,例如...

Some of your instructions, like...

mov     (%esi), %eax

...一次一次从字符缓冲区复制多个字节.我认为这是偶然的吗?您最好用C编写代码,然后使用gcc -S并与您的手写代码进行比较.即使缓冲区与字边界对齐,您也会将指针增加一个 byte ,因此一定要尝试读取未对齐的内存. sigbus基本上意味着您正在尝试从一个地址读取一个字的内存,该地址指向的字节不在对齐字的开头,但是如果某些CPU缓慢运行而另一些CPU却无法恢复,则它们将保持静默状态.我不知道您的主机之间的硬件差异.

...are copying more than a byte from the character buffer at a time. I assume that's accidental? You'd do well to write the code in C then use gcc -S and compare to your hand-written code. Even if the buffers are aligned to a word-boundary, you're incrementing the pointers by one byte, so certain to attempt an unaligned memory read. A sigbus basically means that you're trying to read a word's worth of memory from an address that points to a byte that's not at the start of an aligned word, but some CPUs silently if slowly battle on while others bail out. I've no idea of the hardware differences between your hosts.

这篇关于为什么我的程序在Ubuntu gcc上而不在OSX gcc上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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