BIOS 调用

如何与 BIOS 交互

基本输入/输出系统或 BIOS 是在任何操作系统运行之前控制计算机的原因。要访问 BIOS 提供的服务,汇编代码使用中断。中断采用的形式

int <interrupt> ; interrupt must be a literal number, not in a register or memory

中断号必须介于 0 到 255(0x00 - 0xFF)之间。

大多数 BIOS 调用使用 AH 寄存器作为函数选择参数,并使用 AL 寄存器作为数据参数。AH 选择的功能取决于所调用的中断。某些 BIOS 调用在 AX 中需要一个 16 位参数,或者根本不接受参数,并且只是由中断调用。有些参数甚至更多,在其他寄存器中传递。

用于 BIOS 调用的寄存器是固定的,不能与其他寄存器互换。

使用带有功能选择的 BIOS 调用

使用函数选择参数的 BIOS 中断的一般语法是:

mov ah, <function>
mov al, <data>
int <interrupt>

例子

如何将字符写入显示:

mov ah, 0x0E             ; Select 'Write character' function
mov al, <char>           ; Character to write
int 0x10                 ; Video services interrupt

如何从键盘读取字符(阻止):

mov ah, 0x00             ; Select 'Blocking read character' function
int 0x16                 ; Keyboard services interrupt
mov <ascii_char>, al     ; AL contains the character read
mov <scan_code>, ah      ; AH contains the BIOS scan code

如何从外部驱动器读取一个或多个扇区(使用 CHS 寻址):

mov ah, 0x02             ; Select 'Drive read' function
mov bx, <destination>    ; Destination to write to, in ES:BX
mov al, <num_sectors>    ; Number of sectors to read at a time
mov dl, <drive_num>      ; The external drive's ID
mov cl, <start_sector>   ; The sector to start reading from
mov dh, <head>           ; The head to read from
mov ch, <cylinder>       ; The cylinder to read from
int 0x13                 ; Drive services interrupt
jc <error_handler>       ; Jump to error handler on CF set

如何读取系统 RTC(实时时钟):

mov ah, 0x00             ; Select 'Read RTC' function
int 0x1A                 ; RTC services interrupt
shl ecx, 16              ; Clock ticks are split in the CX:DX pair, so shift ECX left by 16...
or cx, dx                ; and add in the low half of the pair
mov <new_day>, al        ; AL is non-zero if the last call to this function was before midnight
                         ; Now ECX holds the clock ticks (approx. 18.2/sec) since midnight
                         ; and <new_day> is non-zero if we passed midnight since the last read

如何从 RTC 读取系统时间:

mov ah, 0x02             ; Select 'Read system time' function
int 0x1A                 ; RTC services interrupt
                         ; Now CH contains hour, CL minutes, DH seconds, and DL the DST flag,
                         ; all encoded in BCD (DL is zero if in standard time)
                         ; Now we can decode them into a string (we'll ignore DST for now)

mov al, ch               ; Get hour
shr al, 4                ; Discard one's place for now
add al, 48               ; Add ASCII code of digit 0
mov [CLOCK_STRING+0], al ; Set ten's place of hour
mov al, ch               ; Get hour again
and al, 0x0F             ; Discard ten's place this time
add al, 48               ; Add ASCII code of digit 0 again
mov [CLOCK_STRING+1], al ; Set one's place of hour

mov al, cl               ; Get minute
shr al, 4                ; Discard one's place for now
add al, 48               ; Add ASCII code of digit 0
mov [CLOCK_STRING+3], al ; Set ten's place of minute
mov al, cl               ; Get minute again
and al, 0x0F             ; Discard ten's place this time
add al, 48               ; Add ASCII code of digit 0 again
mov [CLOCK_STRING+4], al ; Set one's place of minute

mov al, dh               ; Get second
shr al, 4                ; Discard one's place for now
add al, 48               ; Add ASCII code of digit 0
mov [CLOCK_STRING+6], al ; Set ten's place of second
mov al, dh               ; Get second again
and al, 0x0F             ; Discard ten's place this time
add al, 48               ; Add ASCII code of digit 0 again
mov [CLOCK_STRING+7], al ; Set one's place of second
...
db CLOCK_STRING "00:00:00", 0   ; Place in some separate (non-code) area

如何从 RTC 读取系统日期:

mov ah, 0x04             ; Select 'Read system date' function
int 0x1A                 ; RTC services interrupt
                         ; Now CH contains century, CL year, DH month, and DL day, all in BCD
                         ; Decoding to a string is similar to the RTC Time example above

如何获得连续的低内存大小:

int 0x12                 ; Conventional memory interrupt (no function select parameter)
and eax, 0xFFFF          ; AX contains kilobytes of conventional memory; clear high bits of EAX
shl eax, 10              ; Multiply by 1 kilobyte (1024 bytes = 2^10 bytes)
                         ; EAX contains the number of bytes available from address 0000:0000

如何重启计算机:

int 0x19                 ; That's it! One call. Just make sure nothing has overwritten the
                         ; interrupt vector table, since this call does NOT restore them to the
                         ; default values of normal power-up. This means this call will not
                         ; work too well in an environment with an operating system loaded.

错误处理

某些 BIOS 调用可能无法在每台计算机上实现,并且无法保证可以正常工作。通常,未实现的中断将返回寄存器 AH 中的 0x860x80。**几乎每个中断都会在错误条件下设置进位标志(CF)。**这使得使用 jc 条件跳转很容易跳转到错误处理程序。 (参见条件跳转

参考

一个相当详尽的 BIOS 调用和其他中断列表Ralf Brown 的中断列表 。可以在此处找到 HTML 版本。

通常假设可用的中断位于维基百科的列表中。

可以在 osdev.org 上找到有关常用中断的更深入概述