为什么我的程序在Ubuntu gcc上运行但不在OSX gcc上运行?

所以我的作业,我在Ubuntu中运行它,它编译得很好并且运行方式应该如此。 但是当我在Mac OSX中运行它时,会出现总线错误。 这是为什么?

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

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

 #include  #include  #include  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调试器中运行它,这是我得到的错误。

 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 : mov (%eax),%eax 

此外,我正在删除Ubuntu版本,因此滚动较少。

你的一些指示,如…

 mov (%esi), %eax 

…一次从字符缓冲区复制多个字节。 我认为这是偶然的? 你最好用C语言编写代码,然后使用gcc -S并与你手写的代码进行比较。 即使缓冲区与字边界对齐,您也要将指针递增一个字节 ,因此必须尝试对齐未对齐的内存。 sigbus基本上意味着你试图从一个地址中读取一个字的内存值,该地址指向一个不在对齐字开头的字节,但是一些CPU默默地如果慢慢地进行战斗而其他人挽救。 我不知道你的主机之间的硬件差异。