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
中的 0x86
或 0x80
。**幾乎每個中斷都會在錯誤條件下設定進位標誌(CF)。**這使得使用 jc
條件跳轉很容易跳轉到錯誤處理程式。 (參見條件跳轉 )
參考
一個相當詳盡的 BIOS 呼叫和其他中斷列表是 Ralf Brown 的中斷列表 。可以在此處找到 HTML 版本。
通常假設可用的中斷位於維基百科的列表中。
可以在 osdev.org 上找到有關常用中斷的更深入概述