我不知道为什么全局变量不起作用。我认为 .rdata 部分的地址存在问题。我不知道如何链接我的内核以使该部分在特定的内存区域可见。
我正在使用Windows 7
注意:我不想使用GRUB Bootloader,而是想使用我自己的bootloader。
这是我的档案:
bootsect.asm
[org 0x7c00]
KERNEL_OFFSET equ 0x1000 ; The same one we used when linking the kernel
mov [BOOT_DRIVE], dl ; Remember that the BIOS sets us the boot drive in 'dl' on boot
mov bp, 0x9000
mov sp, bp
mov bx, KERNEL_OFFSET ; Read from disk and store in 0x1000
mov dh, 2
mov dl, [BOOT_DRIVE]
pusha
; reading from disk requires setting specific values in all registers
; so we will overwrite our input parameters from 'dx'. Let's save it
; to the stack for later use.
push dx
mov ah, 0x02 ; ah <- int 0x13 function. 0x02 = 'read'
mov al, dh ; al <- number of sectors to read (0x01 .. 0x80)
mov cl, 0x02 ; cl <- sector (0x01 .. 0x11)
; 0x01 is our boot sector, 0x02 is the first 'available' sector
mov ch, 0x00 ; ch <- cylinder (0x0 .. 0x3FF, upper 2 bits in 'cl')
; dl <- drive number. Our caller sets it as a parameter and gets it from BIOS
; (0 = floppy, 1 = floppy2, 0x80 = hdd, 0x81 = hdd2)
mov dh, 0x00 ; dh <- head number (0x0 .. 0xF)
; [es:bx] <- pointer to buffer where the data will be stored
; caller sets it up for us, and it is actually the standard location for int 13h
int 0x13 ; BIOS interrupt
jc $ ; if error (stored in the carry bit)
pop dx
cmp al, dh ; BIOS also sets 'al' to the # of sectors read. Compare it.
jne $
popa
;call switch_to_pm ; disable interrupts, load GDT, etc. Finally jumps to 'BEGIN_PM'
cli ; 1. disable interrupts
lgdt [gdt_descriptor] ; 2. load the GDT descriptor
mov eax, cr0
or eax, 0x1 ; 3. set 32-bit mode bit in cr0
mov cr0, eax
jmp CODE_SEG:init_pm ; 4. far jump by using a different segment
jmp $ ; Never executed
gdt_start: ; don't remove the labels, they're needed to compute sizes and jumps
; the GDT starts with a null 8-byte
dd 0x0 ; 4 byte
dd 0x0 ; 4 byte
; GDT for code segment. base = 0x00000000, length = 0xfffff
; for flags, refer to os-dev.pdf document, page 36
gdt_code:
dw 0xffff ; segment length, bits 0-15
dw 0x0 ; segment base, bits 0-15
db 0x0 ; segment base, bits 16-23
db 10011010b ; flags (8 bits)
db 11001111b ; flags (4 bits) + segment length, bits 16-19
db 0x0 ; segment base, bits 24-31
; GDT for data segment. base and length identical to code segment
; some flags changed, again, refer to os-dev.pdf
gdt_data:
dw 0xffff
dw 0x0
db 0x0
db 10010010b
db 11001111b
db 0x0
gdt_end:
; GDT descriptor
gdt_descriptor:
dw gdt_end - gdt_start - 1 ; size (16 bit), always one less of its true size
dd gdt_start ; address (32 bit)
; define some constants for later use
CODE_SEG equ gdt_code - gdt_start
DATA_SEG equ gdt_data - gdt_start
[bits 32] ; using 32-bit protected mode
; this is how constants are defined
VIDEO_MEMORY equ 0xb8000
WHITE_ON_BLACK equ 0x0f ; the color byte for each character
[bits 32]
init_pm: ; we are now using 32-bit instructions
mov ax, DATA_SEG ; 5. update the segment registers
mov ss, ax
mov es, ax
mov fs, ax
mov gs, ax
;mov ax, 11c4h
mov ds, ax
mov ebp, 0x90000 ; 6. update the stack right at the top of the free space
mov esp, ebp
call KERNEL_OFFSET ; Give control to the kernel
jmp $ ; Stay here when the kernel returns control to us (if ever)
BOOT_DRIVE db 0 ; It is a good idea to store it in memory because 'dl' may get overwritten
; padding
times 510 - ($-$$) db 0
dw 0xaa55
kernel.c
void write_string( int colour, const char *string )
{
volatile char *video = (volatile char*)0xB8000;
while( *string != 0 )
{
*video++ = *string++;
*video++ = colour;
}
}
void ClearScreen()
{
char* video = (char*) 0xB8000;
int i = 0;
while(i < 80*25)
{
*video++ = 0;
*video++ = 0;
i++;
}
}
char* str1 = "from global";
void start()
{
ClearScreen();
char str2[] = "from stack";
write_string(7, str1); //This is not working
write_string(7, str2); //This is working but I don't want that
}
kernel_entry.asm
[bits 32]
[extern _start]
call _start
jmp $
生成文件
GCC = gcc.exe
NASM = nasm.exe
LD = ld.exe
OBJCOPY = objcopy.exe
QEMU = qemu-system-i386.exe
build:
$(GCC) -ffreestanding -c kernel.c -o kernel.o
$(NASM) kernel_entry.asm -f elf -o kernel_entry.o
$(LD) -T NUL -o kernel.tmp -Ttext 0x1000 kernel_entry.o kernel.o
$(OBJCOPY) -O binary -j .text kernel.tmp kernel.bin
$(NASM) bootsect.asm -f bin -o bootsect.bin
copy /b bootsect.bin + kernel.bin os-image.bin
$(QEMU) -fda os-image.bin
clean:
del kernel.o
del kernel_entry.o
del kernel.tmp
del kernel.bin
del bootsect.bin
del os-image.bin
答案 0 :(得分:0)
如果要编写自己的引导加载程序和应用程序的启动代码,则必须使用初始化代码执行rdata(或rodata)到内存的副本。 对于bss部分,同样的事情是你的启动代码,它有责任将整个内存重置为0,放置该部分的全局变量。