2012-06-17 69 views
3

至少在他/她的生活中至少有一次程序員,我正在嘗試創建我的「革命性」,即新的,唯一的操作系統。 :DAssembly - 讀取虛擬磁盤的下一個扇區

那麼,我正在使用一個虛擬仿真器(Oracle VM Virtual Box),爲此我創建一個新的unknwon操作系統,並帶有一個vmdk磁盤。我喜歡vmdk,因爲它們只是純文件,所以我可以將我的啓動加載器粘貼到虛擬硬盤的前512個字節。

現在,我正在嘗試讀取此虛擬磁盤的下一個扇區,我將在該扇區上粘貼一個顯示消息的簡單內核。

我有兩個問題:

  • 難道我讀第二段(第一-512 bytes-由引導程序佔用)是否正確? CODE:

    ReadDisk: 
        mov bx, 0x8000 ; segment 
        mov es, bx 
        mov bx, 0x0000 ; offset 
    
        mov ah, 0x02 ; read function 
        mov al, 0x01 ; sectors - this might be wrong, trying to read from hd 
        mov ch, 0x00 ; cylinder 
        mov cl, 0x02 ; sector 
        mov dh, 0x00 ; head 
        mov dl, 0x80 ; drive - trying to read from hd 
        int 0x13 ; disk int 
        jc ReadDisk 
        jmp [es:bx] ; buffer 
    

    在這裏,我得到錯誤信息,檢查後CF.但是,如果我使用INT 13,1來獲取最後的 狀態消息,則AL爲0 - 因此不會保存錯誤。

  • 我粘貼我的簡單內核在vmdk裏的正確位置?我所做的是在文件的第512個字節之後粘貼它,前面的512個字節,正如我所說的,是boot-loader。 該文件是這樣的:

    BE 45 7C E8 16 00 EB FE B4 0E B7 00 B3 07 CD 10 <- First sector 
    C3 AC 08 C0 74 05 E8 EF FF EB F6 C3 B4 00 B2 80 
    CD 13 BE 5D 7C 72 F5 BB 00 80 8E C3 BB 00 00 B4 
    02 B0 06 B5 00 B1 01 B6 00 B2 07 CD 13 BE 4E 7C 
    72 CF 26 FF 27 57 65 6C 63 6F 6D 65 21 00 52 65 
    61 64 69 6E 67 20 65 72 72 6F 72 21 00 52 65 73 
    65 74 74 69 6E 67 20 65 72 72 6F 72 21 00 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 AA <- Boot-loader signature 
    B4 0E B0 2E CD 10 EB FE 00 00 00 00 00 00 00 00 <- Start of the second sector 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    

所以,這是我想給內核添加到第二個扇區的方式。 你認爲這是什麼錯誤?謝謝!

UPDATE

O.K.現在我沒有收到任何錯誤,但是我沒有看到正在執行的加載代碼。它應該在窗口上顯示一個點:

;-------------------------------------------- 
; 'load.asm' 
; loaded from 'boot.asm' 

[org 0x8000] 
[bits 16] 

;-------------------------------------------- 

main: 
mov ah, 0x0E ; print function 
mov al, '.' ; ascii char 
int 0x10 ; IO int 

jmp $ ; hang 
+0

你應該真的去http://osdev.org/並在那裏問它。並閱讀其wiki上的一些文章。 – Griwes

回答

1

失敗後再試一次。我想你會收到一個錯誤指示,當磁盤旋轉時沒有消息,所以仿真器可能故意第一次失敗。在bochs和qemu中,四次嘗試對我都很好,但我沒有嘗試過其他任何事情。在讀取之前,您可能還想重置驅動器控制器以清除以前的任何錯誤。使用中斷0x13,清除al,驅動器號碼爲dl


注:硬編碼的驅動器號可能適用於現在,但不會讓你支持從其它驅動器啓動。啓動引導加載程序時,BIOS應在dl中保留驅動器號,以便保存該信息。

+0

我仍然無法得到我想要的。沒有錯誤顯示,但加載到內存中的代碼未執行。查看更新! – ali

+0

'bh'和'bl'也會影響打印功能的結果。就我個人而言,我會使用中斷0x10函數0設置視頻模式並直接寫入視頻內存。另外,如果使用0x8000作爲段,則線性地址爲0x80000,因此'org'指令是錯誤的。對於那個程序來說,如果那個位置的RAM不存在,這隻會是一個問題。如果可以的話,你應該連接一個調試器並確保處理器跳到正確的位置。 – ughoavgfhw

2

的一個問題是在這裏:

jmp [es:bx] 

這將讀出的地址,一個16位在bx包含在寄存器es(段部分)的地址和(偏移,事實上,從所述存儲器位置偏移部分),然後將ip設置爲該16位偏移量。

你可能要改爲使用方法是:

jmp some_constant1:some_constant2 

這將設置cssome_constant1ipsome_constant2。不出所料,這兩個常量的最佳候選對象分別是0x8000和0,因爲這是您的代碼被加載的位置。

現在,第二個問題是在這裏:

[org 0x8000] 

org告訴NASM以這樣的方式,如果在加載偏移爲0x8000,將工作來生成代碼。現在,偏移量0x8000與段0x8000不同。如果你使用jmp 0x8000:0,那麼你也應該使用:

[org 0]