1

我正在使用混合代码(包括 C 语言和汇编代码)在引导加载程序上创建简单的计算器应用程序。

我的 C 语言代码是 (addasm.c):#include

      int main() {

bootmain();
return 0 ;

  }
   int bootmain()
  {
 int arg1, arg2, add, sub, mul, quo, rem ;

printf( "Enter two integer numbers : " );
scanf( "%d%d", &arg1, &arg2 );

/* Perform Addition, Subtraction, Multiplication & Division */
__asm__ ( "addl %%ebx, %%eax;" : "=a" (add) : "a" (arg1) , "b" (arg2) );
__asm__ ( "subl %%ebx, %%eax;" : "=a" (sub) : "a" (arg1) , "b" (arg2) );
__asm__ ( "imull %%ebx, %%eax;" : "=a" (mul) : "a" (arg1) , "b" (arg2) );

__asm__ ( "movl $0x0, %%edx;"
          "movl %2, %%eax;"
          "movl %3, %%ebx;"
           "idivl %%ebx;" : "=a" (quo), "=d" (rem) : "g" (arg1), "g" (arg2) );

printf( "%d + %d = %d\n", arg1, arg2, add );
printf( "%d - %d = %d\n", arg1, arg2, sub );
printf( "%d * %d = %d\n", arg1, arg2, mul );
printf( "%d / %d = %d\n", arg1, arg2, quo );
printf( "%d %% %d = %d\n", arg1, arg2, rem );
return 0;
 }

我在 C 中创建了 bootmain() 函数,我需要在汇编代码中使用它。

我的汇编代码(ccode.asm)是:

 [BITS 16]   ; 16 bit code generation
 [ORG 0x7C00]    ; ORGin location is 7C00
 extern bootmain

  ;Main program
  main:      ; Main program label

  call bootmain

  ; End matter
   times 510-($-$$) db 0    ; Fill the rest of the sector with zeros
   dw 0xAA55        ; Boot signature

现在我正在编译这个

    nasm -f elf -o main.o ccode.asm  #assemble our asm file

但它给我的 ORG 关键字错误,它是未定义的关键字。

如果我将删除此关键字,那么它将给我无错误的输出。

删除 ORG 关键字后,我正在这样编译:

  nasm -f elf -o main.o ccode.asm  #assemble our asm file
  gcc addasm.c main.o -o add_asm     #compile and link in one step
  ./add_asm                       

所以我正在使用这个最终的 add_asm 文件,并通过使用磁盘资源管理器放置这个 add_asm 文件来使我的 USB 驱动器可启动。但是在启动时它显示消息:缺少操作系统所以这是在程序集文件中不使用 ORG 的问题。这主要是我与 NASM 一起使用的 ELF 的问题。但是对于外部 C 函数和 EXTERN 关键字,我需要使用 ELF。

ORG 的替代代码是:

  [Bits 16]
  extern bootmain
  start:
  mov ax, 07C0h ; Set up 4K stack space after this bootloader
  add ax, 288 ; (4096 + 512) / 16 bytes per paragraph
  mov ss, ax
  mov sp, 4096
 call bootmain
 mov ax, 07C0h ; Set data segment to where we're loaded
 mov ds, ax
 times 510-($-$$) db 0; Pad remainder of boot sector with 0s
 dw 0xAA55 ; The standard PC boot signature

但它也不起作用......它在引导时给了我与“缺少操作系统”相同的错误。

有没有其他方法可以在汇编文件 (*.asm) 中包含 C 函数?我被困在这里。如果有什么建议请给我。谢谢你。

4

1 回答 1

1

你不能像那样把一个普通的 C 程序变成一个引导加载程序。

  • 引导加载程序运行的环境与正常的可执行文件有很大不同。特别是,它不包含您可以链接的 C 库(或者,就此而言,任何链接器!),因此除非您链接到适当的版本,否则函数类似printf()并且scanf()不可用,而您没有这样做。

  • 您正在将程序编译为 32 位可执行文件。x86 系统以 16 位模式启动。必须进行大量初始化才能切换到该模式,这里没有任何初始化。

  • 您正在将程序编译为 Linux ELF(或者可能是 Windows PE?)可执行文件。这不是引导加载程序的正确格式。

于 2013-05-25T16:51:29.877 回答