如何使用NASM调用位于后两个扇区的代码?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用NASM调用位于后两个扇区的代码?相关的知识,希望对你有一定的参考价值。

也许我很蠢,不知道什么,但我认为我做得对。我有两个文件一个boot.asm,第二个code.asm。在boot.asm我只是启用蓝色的视频模式填充屏幕,并尝试加载位于两秒扇区的代码,但当我尝试它没有什么只是发生,无法弄清楚我做错了,也许我读错了或code.asm编码错误或boot.asm编码错误。为什么我不能调用存储在两个第二扇区中的代码来完成它们。

有关更多信息,我将从MBR加载此代码,我希望我的代码调用代码在两个第二扇区,因为我不能将它们全部放在一个扇区中,所以我问这个问题。

boot.asm

;bootloader
  bits 16
  org 0x7c00

;Here i try to load code located at second two sectors
Load_Code:    
    ;---Setup segments
    xor ax, ax                  ; AX=0
    mov ds, ax                  ; DS=ES=0 because we use an org of 0x7c00 - Segment<<4+offset = 0x0000<<4+0x7c00 = 0x07c00
    mov ax, es
    mov ax, ss
    mov sp, 0x7c00              ; SS:SP= 0x0000:0x7c00 stack just below bootloader

  ;---Read 2 sectors
  mov bx, buffer              ; ES: BX must point to the buffer
  mov dh,0                    ; head number
  mov ch,0                    ; track number
  mov cl,2                    ; sector number
  mov al,2                    ; number of sectors to read
  mov ah,2                    ; read function number
  int 13h

;Graphics mode
Set_Video:
  mov ax, 12h        ; Graphics mode (640x480x16)
  int 10h            ; Bios draw function

  mov ah, 06h        ; Scroll up function
  xor al, al         ; Clear entire screen
  xor cx, cx         ; Upper left corner CH=row, CL=column
  mov dx, 3FFFh      ; fill whole screen
  mov bh, 11h        ; blue background 
  int 10h            ; BIOS draw function
  call buffer

;Endless loop
.endloop:
  hlt
  jmp .endloop


;Fake MBR signature
MBR_Signature:
  times 510 - ($ - $$) db 0
  dw 0xAA55

buffer:

code.asm - 包含此代码的第二个两个扇区 - (这只是示例代码)

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 0          ; x co-ordinate
mov dx, 0          ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 1          ; x co-ordinate
mov dx, 1          ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 2          ; x co-ordinate
mov dx, 2          ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 3          ; x co-ordinate
mov dx, 3          ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 4          ; x co-ordinate
mov dx, 4          ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 5          ; x co-ordinate
mov dx, 5          ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 6          ; x co-ordinate
mov dx, 6          ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 7          ; x co-ordinate
mov dx, 7          ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 8          ; x co-ordinate
mov dx, 8          ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 9          ; x co-ordinate
mov dx, 9          ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 10         ; x co-ordinate
mov dx, 10         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 11         ; x co-ordinate
mov dx, 11         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 12         ; x co-ordinate
mov dx, 12         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 13         ; x co-ordinate
mov dx, 13         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 14         ; x co-ordinate
mov dx, 14         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 15         ; x co-ordinate
mov dx, 15         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 16         ; x co-ordinate
mov dx, 16         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 17         ; x co-ordinate
mov dx, 17         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 18         ; x co-ordinate
mov dx, 18         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 19         ; x co-ordinate
mov dx, 19         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 20         ; x co-ordinate
mov dx, 20         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 21         ; x co-ordinate
mov dx, 21         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 22         ; x co-ordinate
mov dx, 22         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 23         ; x co-ordinate
mov dx, 23         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 24         ; x co-ordinate
mov dx, 24         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 25         ; x co-ordinate
mov dx, 25         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 26         ; x co-ordinate
mov dx, 26         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 27         ; x co-ordinate
mov dx, 27         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 28         ; x co-ordinate
mov dx, 28         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 29         ; x co-ordinate
mov dx, 29         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 30         ; x co-ordinate
mov dx, 30         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 31         ; x co-ordinate
mov dx, 31         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 32         ; x co-ordinate
mov dx, 32         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 33         ; x co-ordinate
mov dx, 33         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 34         ; x co-ordinate
mov dx, 34         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 35         ; x co-ordinate
mov dx, 35         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 36         ; x co-ordinate
mov dx, 36         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 37         ; x co-ordinate
mov dx, 37         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 38         ; x co-ordinate
mov dx, 38         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 39         ; x co-ordinate
mov dx, 39         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 40         ; x co-ordinate
mov dx, 40         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 41         ; x co-ordinate
mov dx, 41         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 42         ; x co-ordinate
mov dx, 42         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 43         ; x co-ordinate
mov dx, 43         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 44         ; x co-ordinate
mov dx, 44         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 45         ; x co-ordinate
mov dx, 45         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 46         ; x co-ordinate
mov dx, 46         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 47         ; x co-ordinate
mov dx, 47         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 48         ; x co-ordinate
mov dx, 48         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 49         ; x co-ordinate
mov dx, 49         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 50         ; x co-ordinate
mov dx, 50         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 51         ; x co-ordinate
mov dx, 51         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 52         ; x co-ordinate
mov dx, 52         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 53         ; x co-ordinate
mov dx, 53         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 54         ; x co-ordinate
mov dx, 54         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 55         ; x co-ordinate
mov dx, 55         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 56         ; x co-ordinate
mov dx, 56         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 57         ; x co-ordinate
mov dx, 57         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 58         ; x co-ordinate
mov dx, 58         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 59         ; x co-ordinate
mov dx, 59         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 60         ; x co-ordinate
mov dx, 60         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 61         ; x co-ordinate
mov dx, 61         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 62         ; x co-ordinate
mov dx, 62         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 63         ; x co-ordinate
mov dx, 63         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 64         ; x co-ordinate
mov dx, 64         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 65         ; x co-ordinate
mov dx, 65         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 66         ; x co-ordinate
mov dx, 66         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 67         ; x co-ordinate
mov dx, 67         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 68         ; x co-ordinate
mov dx, 68         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 69         ; x co-ordinate
mov dx, 69         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 70         ; x co-ordinate
mov dx, 70         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 71         ; x co-ordinate
mov dx, 71         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 72         ; x co-ordinate
mov dx, 72         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 73         ; x co-ordinate
mov dx, 73         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 74         ; x co-ordinate
mov dx, 74         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 75         ; x co-ordinate
mov dx, 75         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 76         ; x co-ordinate
mov dx, 76         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 77         ; x co-ordinate
mov dx, 77         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 78         ; x co-ordinate
mov dx, 78         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 79         ; x co-ordinate
mov dx, 79         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 80         ; x co-ordinate
mov dx, 80         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 81         ; x co-ordinate
mov dx, 81         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 82         ; x co-ordinate
mov dx, 82         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 83         ; x co-ordinate
mov dx, 83         ; y co-ordinate
int 10h            ; BIOS draw function

mov al, 15         ; color - white
mov ah, 0ch        ; set pixel
mov cx, 84         ; x co-ordinate
mov dx, 84         ; y co-ordinate
int 10h            ; BIOS draw function

ret
times 1024 - ($ - $$) db 0x00

How it look like in hex.

答案

问题的最可能原因是当您清除屏幕时,您正在破坏dl的内容。

当BIOS加载引导扇区然后调用它时,它会将驱动器号放入dl。当您稍后尝试将后续扇区加载到内存中时,必须保留此值以供使用。由于您没有这样做,当您尝试读取驱动器时,BIOS正在尝试从(可能)不存在的设备读取。

另一答案
xor ax, ax                  ; AX=0
mov ds, ax                  ; DS=ES=0 because we use an org of 0x7c00 - Segment<<4+offset = 0x0000<<4+0x7c00 = 0x07c00
mov ax, es
mov ax, ss
mov sp, 0x7c00 

设置段寄存器失败!

你需要将AX存储在其中而不是其他方式。

xor ax, ax
mov ds, ax
mov es, ax                    <---
mov ss, ax                    <---
mov sp, 0x7c00 

mov ah, 0x07        ; function to call with interrupt
mov bh, 0x01        ; blue background 
mov dx, 0x3FFF      ; fill whole screen
int 0x10            ; BIOS draw function

你如何画12h的画面是相当奇怪的!

此屏幕有80列和30行,因此右下角位于(79,29)。 你也忘了指定左上角是(0,0)。你的bootloader程序永远不应该依赖于程序启动时具有零值的CX

mov ah, 0x07        ; function to call with interrupt
mov bh, 0x01        ; blue background 
xor cx, cx          ; (0,0)                       <-----
mov dx, 0x1D4F      ; (79,29)                     <-----
int 0x10            ; BIOS draw function

以上是关于如何使用NASM调用位于后两个扇区的代码?的主要内容,如果未能解决你的问题,请参考以下文章

NASM:如何正确访问 ssd 驱动器?

nasm 引导扇区中可能存在语法问题

计算Adobe Flex饼图中每个扇区的百分比

NASM ctypes SIMD - 如何访问返回到ctypes的128位数组?

读取磁盘:CHS方式

MBR的位置是不是位于C盘?