10

BIOSへの割り込みを使用して「Helloworld」を表示する単純な第1ステージのブートローダーを作成しました。次に、第2ステージを作成するための次の明らかなステップとして、そのためのコードはどこに存在する必要があり、第1ステージからどのようにロードするのでしょうか。

これが第一段階のプログラムです

[BITS 16]   ;Tells the assembler that its a 16 bit code
[ORG 0x7C00]    ;Origin, tell the assembler that where the code will
                ;be in memory after it is been loaded

MOV SI, HelloString ;Store string pointer to SI
CALL PrintString    ;Call print string procedure
JMP $       ;Infinite loop, hang it here.


PrintCharacter: ;Procedure to print character on screen
    ;Assume that ASCII value is in register AL
MOV AH, 0x0E    ;Tell BIOS that we need to print one charater on screen.
MOV BH, 0x00    ;Page no.
MOV BL, 0x07    ;Text attribute 0x07 is lightgrey font on black background

INT 0x10    ;Call video interrupt
RET     ;Return to calling procedure



PrintString:    ;Procedure to print string on screen
    ;Assume that string starting pointer is in register SI

next_character: ;Lable to fetch next character from string
MOV AL, [SI]    ;Get a byte from string and store in AL register
INC SI      ;Increment SI pointer
OR AL, AL   ;Check if value in AL is zero (end of string)
JZ exit_function ;If end then return
CALL PrintCharacter ;Else print the character which is in AL register
JMP next_character  ;Fetch next character from string
exit_function:  ;End label
RET     ;Return from procedure


;Data
HelloString db 'Hello World', 0 ;HelloWorld string ending with 0

TIMES 510 - ($ - $$) db 0   ;Fill the rest of sector with 0
DW 0xAA55           ;Add boot signature at the end of bootloader
4

3 に答える 3

8

x86 では、次のようにします (簡略化)。

  • ブートローダにディスク/フロッピーの n 番目のセクタ (どこからブートしても) をメモリにロードして実行させます (つまり、セグメント/オフセットをロードして実行しますretf)。より良い代替手段は、ファイル システムで特定のファイル名 (KERNEL.BIN など) を検索することですが、ファイル システムの種類 (フロッピー イメージからテストする場合は FAT12 など) を知る必要があります。
  • その後、カーネルはリアル モードで起動します。コード記述子、GDT などをセットアップし、32 ビット アドレス指定 (「A20」について聞いたことがあるはずです) をアクティブにし、最後に保護モードに入ります。次に、32 ビット コード セグメントへのファー ジャンプが必要です (カーネル ファイルは、32 ビット コードが絶対位置、たとえば 16 ビット リアル モードの直後のオフセット 512 になるようにリンクする必要があります)。 .
  • 32 ビット カーネル アセンブリは、EXTERN _mykernelそのシンボルを (たとえば) 定義して呼び出すだけです。
  • 次に、カーネルを C 関数として書き始めることができますmykernel

オーケー、これは私が数年前に行ったことの簡単な概要でした (インターネットからのコピー&ペーストがたくさんあります;)。それでも役に立たない場合は、OS 開発に関する優れた Web リソースを次に示します。

それが役立つことを願っています^^

于 2010-01-15T22:55:30.467 に答える
2

ステージ 2 をロードしてそれにジャンプする最小限の実行可能な NASM BIOS の例

use16
org 0x7C00

    ; You should do further initializations here
    ; like setup the stack and segment registers.

    ; Load stage 2 to memory.
    mov ah, 0x02
    ; Number of sectors to read.
    mov al, 1
    ; This may not be necessary as many BIOS set it up as an initial state.
    mov dl, 0x80
    ; Cylinder number.
    mov ch, 0
    ; Head number.
    mov dh, 0
    ; Starting sector number. 2 because 1 was already loaded.
    mov cl, 2
    ; Where to load to.
    mov bx, stage2
    int 0x13

    jmp stage2

    ; Magic bytes.    
    times ((0x200 - 2) - ($ - $$)) db 0x00
    dw 0xAA55

stage2:

    ; Print 'a'.
    mov ax, 0x0E61
    int 0x10

    cli
    hlt

    ; Pad image to multiple of 512 bytes.
    times ((0x400) - ($ - $$)) db 0x00

コンパイルして実行します。

nasm -f bin -o main.img main.asm
qemu-system-i386 main.img

期待される結果:a画面に出力され、プログラムが停止します。

Ubuntu 14.04 でテスト済み。

私の GitHub で、リンカー スクリプトとより正確な初期化 (セグメント レジスタ、スタック) を使用した Saner GAS の例。

于 2015-10-04T21:05:35.173 に答える
1

ここで GRUB の実装を見てください (ステージ 1):

http://src.illumos.org/source/xref/illumos-gate/usr/src/grub/grub-0.97/stage1/stage1.S

最初に、0x7c00 の開始点と、この最初のセクターの 0xaa55 の終了署名に気付きました。逆アセンブリ内から、次のことがわかります。

349 copy_buffer:
350   movw    ABS(stage2_segment), %es
351 
352   /*
353    * We need to save %cx and %si because the startup code in
354    * stage2 uses them without initializing them.
355    */
356   pusha
357   pushw   %ds
358 
359   movw    $0x100, %cx
360   movw    %bx, %ds
361   xorw    %si, %si
362   xorw    %di, %di
363 
364   cld
365 
366   rep
367   movsw
368 
369   popw    %ds
370   popa
371 
372   /* boot stage2 */
373   jmp *(stage2_address)
374 
375 /* END OF MAIN LOOP */
376

基本的にロジックは、ステージ 2 のコードをメモリの別の部分にコピーし、その後、そこに直接ジャンプすることです。これが「ブート ステージ 2」です。つまり、「ブート ステージ 1」は、セクターがメモリにロードされた後に BIOS から効果的にトリガーされますが、ステージ 2 はそこにジャンプする場所であり、どこにでもある可能性があります。

于 2012-02-18T12:59:17.407 に答える