简体   繁体   English

Segmentation Fault: 11 运行 C 程序时

[英]Segmentation Fault: 11 when running C program

To try to display graphics using C, I am trying to take advantage of C's "inline assembly" feature.为了尝试使用 C 显示图形,我正在尝试利用 C 的“内联汇编”功能。 I get no errors during compilation, but when I try to run the program, I get this error:在编译过程中我没有收到任何错误,但是当我尝试运行程序时,我收到了这个错误:

Segmentation Fault: 11

Here is my code:这是我的代码:

int main(){
asm("movb 0xc,%ah");
asm("movb $1,%al");
asm("movw $5,%cx");
asm("movw $5,%dx");
asm("int $0xc");
return 0;
}

Constructive criticism appreciated, insults not.建设性的批评赞赏,侮辱不。 Thanks!谢谢!

First, it looks like you're trying to use BIOS interrupts to do the graphics, but the graphics interrupt is int 10h (0x10) , not 0xc, so you want to call int $0x10.首先,看起来您正在尝试使用 BIOS 中断来执行图形,但图形中断是int 10h (0x10) ,而不是 0xc,因此您想调用 int $0x10。

Second, you can't call most BIOS interrupts from within 32-bit or 64-bit Linux or Windows programs, so make sure you're compiling this for DOS.其次,您不能从 32 位或 64 位 Linux 或 Windows 程序中调用大多数 BIOS 中断,因此请确保您正在为 DOS 编译它。 Otherwise, calling the invoke interrupt opcode on a BIOS interrupt will crash your program.否则,在 BIOS 中断上调用调用中断操作码会使您的程序崩溃。 And if you run a newer version of Windows, you'll probably still have to run your compiled program inside of an emulator like DOSBox for it to work properly.如果您运行较新版本的 Windows,您可能仍然需要在 DOSBox 等模拟器中运行已编译的程序才能使其正常工作。

Finally, GCC inline assembly has a certain format to it:最后,GCC 内联汇编具有一定的格式:

   __asm__ __volatile__ ( 
         assembler template 
       : output operands                  /* optional */
       : input operands                   /* optional */
       : list of clobbered registers      /* optional */
       );

So for example:例如:

int main()
{
  /* Set video mode: */
  __asm__ __volatile__ (
    "movb $0x0, %%ah \n\
     movb $0x13, %%al \n\
     int $0x10"
    :
    :
    :"ax"
  );

  /* Draw pixel of color 1 at 5,5: */
  __asm__ __volatile__ (
    "movb $0xC,%%ah \n\
     movb $1, %%al \n\
     movw $5, %%cx \n\
     movw $5, %%dx \n\
     int $0x10"
   :
   :
   :"ax","cx","dx"
  );

  /* Reset video mode: */
  __asm__ __volatile__ (
    "movb $0x0, %%ah \n\
     movb $0x03, %%al \n\
     int $0x10"
    :
    :
    :"ax"
  );

  return 0;
}

But the optional fields are only really useful if you're writing functions in assembly language and want to pass in arguments from your C code.但是,只有在您使用汇编语言编写函数并希望从 C 代码中传入 arguments 时,可选字段才真正有用。

Also, I don't have DJGPP and a DOS installation handy, so I can't test any of this code to make sure it works with the 32-bit protected mode binaries it generates, but hopefully I've hit the nail close enough on the head that you can handle the rest yourself.另外,我手边没有 DJGPP 和 DOS 安装,所以我无法测试任何代码以确保它可以与它生成的 32 位保护模式二进制文件一起使用,但希望我已经足够接近钉子了您可以自己处理 rest。 Good luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM