Gui*_*ido 4 c x86 assembly gcc bootloader
我正在尝试编写一个bootloader.我想编译一些C代码,以便引导加载程序可以将其加载到内存中并跳转到那里.
我有两个问题:
小智 5
您可以使用链接描述文件使用gcc链接器创建纯二进制文件.关键是OUTPUT_FORMAT(二进制)指令:
//========================================
FILE: linker.ld
//========================================
OUTPUT_FORMAT(binary)
SECTIONS {
.text : { *(.text) }
.data : { *(.data) }
.bss : { *(.bss) }
}
//========================================
Run Code Online (Sandbox Code Playgroud)
我在makefile中调用了链接器,如下所示(而linker.ld是链接描述文件):
//========================================
ld -T linker.ld loaderEntry.o loaderMain.o -o EOSLOAD.BIN -L$(lib) -lsys16
//========================================
Run Code Online (Sandbox Code Playgroud)
我用它编译了目标代码
//========================================
gcc -nostdinc -nostdlib -ffreestanding -c <code files> -o theObjectCode.o
//========================================
Run Code Online (Sandbox Code Playgroud)
为了摆脱不能在16位模式下工作的标准库.
对于握手MBR加载器和引导加载程序,我使用了以下loaderMain.Scc汇编代码(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
//========================================
Run Code Online (Sandbox Code Playgroud)
汇编代码调用已在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();
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我只能编写普通的C代码 - 到目前为止,我没有成功使用C++代码,而且我只设法生成微小的内存模型(意味着CS,SS,DS和ES都是一样的).gcc仅使用偏移量作为指针地址,因此如果没有额外的汇编代码,似乎很难克服timny内存模型问题.(虽然我听说有些人在gcc中解决了这个问题)
调用约定是最后一个参数在堆栈上被推送,似乎所有值都是dword对齐的.可以在.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.
//=====================
Run Code Online (Sandbox Code Playgroud)
顺便说一下该函数的C头声明如下:
//=====================
void kbdread(char* pc, (unsigned char)* psc);
//=====================
Run Code Online (Sandbox Code Playgroud)
希望这在某种程度上有所帮助.干杯.
| 归档时间: |
|
| 查看次数: |
5033 次 |
| 最近记录: |