您可以使用链接器脚本使用 gcc 链接器创建普通二进制文件。
关键是 OUTPUT_FORMAT(binary) 指令:
//========================================
FILE: linker.ld
//========================================
OUTPUT_FORMAT(binary)
SECTIONS {
.text : { *(.text) }
.data : { *(.data) }
.bss : { *(.bss) }
}
//========================================
我在 makefile 中调用链接器如下(而 linker.ld 是链接器脚本文件):
//========================================
ld -T linker.ld loaderEntry.o loaderMain.o -o EOSLOAD.BIN -L$(lib) -lsys16
//========================================
我已经编译了目标代码
//========================================
gcc -nostdinc -nostdlib -ffreestanding -c <code files> -o theObjectCode.o
//========================================
为了摆脱不能在 16 位模式下工作的标准库。
对于握手 MBR 加载程序和引导加载程序,我使用了以下 loaderMain.S gcc 汇编代码(如上所示,loaderMain.o 必须是传递给链接器的第一个文件,位于地址偏移 0x0000 处)。
我使用 -code16gcc 指令来生成 16 位代码。
但是,生成的代码可能无法在旧的 x86 机器上运行,因为我使用了不兼容的
仅适用于较新型号的代码指令(%esp、$ebp、leave 等)。
//========================================
FILE: loaderEntry.S
//========================================
.text
.code16gcc
// the entry point at 0x9000:0x0000 // this is where I did a far call to by the MBR
.globl loaderMain // loader C entry function name declaration
push %cs // initialize data segments with same value as code segment
pop %ax // (I managed only tiny model so far ...)
movw %ax, %ds
movw %ax, %es
movw %ax, %fs
movw %ax, %gs
movw %ax, %ss // initialize stack segment with same value as code segment
movl $0xffff, %esp // initialize stack pointers with 0xffff (usage of extended (dword) offsets does not work, so we're stuck in tiny model)
movl %esp, %ebp
call loaderMain // call C entry function
cli // halt the machine for the case the main function dares to return
hlt
//========================================
汇编代码调用一个已在 C 语言文件 loaderMain.c 中定义的符号。
为了生成 16 位模式兼容代码,您必须在您使用的每个 C 文件的第一行代码之前声明使用 16 位指令集。这只能通过内联汇编指令 AFAIK 来完成:
asm(".code16gcc\n"); // use 16bit real mode code set
/* ... some C code .. */
// ... and here is the C entry code ... //
void loaderMain() {
uint cmdlen = 0;
bool terminate = false;
print(NL);
print(NL);
print("*** EOS LOADER has taken over control. ***\r\n\r\n");
print("Enter commands on the command line below.\r\n");
print("Command are executed by pressing the <ENTER> key.\r\n");
print("The command \'help\' shows a list of all EOS LOADER commands.\r\n");
print("HAVE FUN!\r\n");
print(NL);
while (!terminate) {
print("EOS:>");
cmdlen = readLine();
buffer[cmdlen] = '\0';
print(NL);
terminate = command();
}
shutdown();
}
到目前为止,我只能编写纯 C 代码 - 到目前为止,我还没有成功编写 C++ 代码,
我只设法产生了微小的内存模型(意味着 CS、SS、DS 和 ES 都是一样的)。
gcc 仅使用偏移量作为指针地址,因此如果没有额外的汇编代码似乎很难克服内存模型问题。 (虽然我听说有人在 gcc 中解决了这个问题)
调用约定是最后一个参数首先被压入堆栈,并且似乎所有值都是双字对齐的。下面贴出可以在 .code16gcc C 代码中调用的汇编代码示例:
//======================
.text
.code16gcc
.globl kbdread // declares a global symbol so that the function can be called from C
.type kbdread, @function // declares the symbol as a function
kbdread: // the entry point label which has to the same as the symbol
// this is the conventional stack frame for function entry
pushl %ebp
movl %esp, %ebp
// memory space for local variables would be allocated by decrementing the stack pointer accordingly
// the parameter arguments are being addressed by the base pointer which points to the same address while bein within the function
pushw %ds // I'm paranoid, I know...
pushw %es
pushw %fs
pushl %eax
pushl %ebx
pushl %ecx
pushl %edx
pushl %esi
pushl %edi
xorl %eax, %eax // calls the keyboard interrupt in order to read char code and scan code
int $0x16
xorl %edi, %edi
movl 8(%ebp), %edi // moves the pointer to the memory location in which the char code will be stored into EDI
movb %al, (%edi) // moves the char code from AL to the memory location to which EDI points
xorl %edi, %edi // paranoid again (but who knows how well the bios handles extended registers??)..
movl 12(%ebp), %edi // moves the pointer to the memory location in which the scan code will be stored into EDI
movb %ah, (%edi) // moves the scan code from AH to the memory location to which EDI points
popl %edi // restoring the values from stack..
popl %esi
popl %edx
popl %ecx
popl %ebx
popl %eax
popw %fs
popw %es
popw %ds
leave // .. and the conventional end frame for functions.
ret // be aware that you are responsible to restore the stack when you have declared local variables on the stack ponter.
// the leave instruction is a convenience method to do that. but it is part of not early X86 instruction set (as well as extended registers)
// so be careful which instruftion you actually use if you have to stay compatible with older computer models.
//=====================
顺便说一句,函数的 C 头声明如下所示:
//=====================
void kbdread(char* pc, (unsigned char)* psc);
//=====================
希望这对您有所帮助。干杯。