2015-10-11 45 views
0

我不知道爲什麼全局變量不起作用。我認爲地址.rdata部分存在問題。我不知道如何鏈接我的內核,使這部分在特定的內存區域可見。全局變量在C內核.rdata部分中不起作用

我使用Windows 7

注:我不想使用GRUB引導加載程序,而不是我想用我自己的引導程序。

這是我的文件:

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 $ 

的Makefile

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

'str1'不是堆分配。它是一個全局變量,並且將字符串插入放入「只讀數據」部分,該部分被映射到進程空間中爲只讀。順便說一句,你爲什麼寫'str1'不工作? – LPs

+0

因爲它沒有打印到屏幕 –

+0

您是否嘗試切換printfs?我的意思是先打印str2。 – LPs

回答

0

如果你想編寫自己的引導程序和應用程序的啓動代碼,RDATA(或RODATA)的複製到存儲必須由您的初始化代碼來執行。 bss部分的相同之處在於,您的啓動代碼具有將該部分的全局變量置於其中的整個內存重置爲0的責任。

+0

但是如何解決全局變量的地址......? –