我正在使用混合代码(包括 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 函数?我被困在这里。如果有什么建议请给我。谢谢你。