Title: Lecture 20 BIOS-Level Programming
1Lecture 20BIOS-Level Programming
- Assembly Language for
- Intel-Based Computers,
- 4th edition
- Kip R. Irvine
2Chapter Overview
- Review Chapter 13
- Keyboard Input with INT 16h
- VIDEO Programming with INT 10h
- Drawing Graphics Using INT 10h
- Memory-Mapped Graphics
- Mouse Programming
3Interrupts
- Hardware interrupt in response to a request by
a hardware device that needs attention. Hardware
interrupts occur at "unexpected" times. - Software interrupt A call to DOS or BIOS in
response to a interrupt instruction (INT) in the
program being processed. - Exception An automatically generated trap in
response to an exceptional condition such as
division by zero.
4Hardware Interrupt Steps
- 0. The program is executing in the CPU
- 1. The hardware device needs attention and
requests an interrupt. - 2. The CPU finishes processing the current
instruction. It the saves its "state" (flags and
CSIP) on the stack. - 3. The address of the interrupt handler is
obtained from the Interrupt Vector Table.
5Hardware Interrupt Steps
- 4. The CPU loads the address of the interrupt
handler into the IP. - 5. The interrupt handler code is processed.
- 6. When the interrupt handler is finished, the
CPU pops the "state" (CSIP and Flags) back from
the stack. - 7. Execution of original program continues
beginning at the next instruction.
6Hardware Interrupts
- Device Program Interrupt table
Interrupt Code -
Stack
0
4
3
1
1
2
5
7
6
7Hardware Interrupt Comments
- Some interrupt operations are so critical that
the interrupt driver may disable other interrupts
until the critical operation is completed. - In IBM terminology, hardware interrupts are known
as an IRQ. Each device is assigned a number
which determines the Interrupt Vector entry of
the appropriate interrupt handler.
8Software Interrupt
- Software interrupt. Works much the same way but
steps 1 and 2 are replaced by an INT n
instruction in the code. The number n determines
the table entry specifying location of the
interrupt handler desired.
9Common Software Interrupts
- INT 10h Video services
- INT 16h Keyboard services
- INT 21h DOS services - "DOS function calls"
Most interrupts use AH to specify the desired
operation (function)
10INT 21h functions
INT 21 functions Example Program
4Ch terminate process mov ah,4Ch mov al,0 int 21h
02h character output advance cursor mov ah,02h mov dl,A int 21h
06h character output mov ah,06h mov dl,a int 21h
09h print terminated string mov ah,09h mov dx,OFFSET string int 21h
11INT 21h functions (continues)
INT 21 functions Example Program
01h read a char from stdin mov ah,01h int 21h mov char,al
06h character input no waiting mov ah,06h mov dl,0FFh int 21h
0Ah input string mov ah,0Ah mov dx,OFFSET kybdData int 21h
0Bh status of input buffer mov ah,0Bh int 21h (al 0/0FFh)
12Keyboard Input functions Summary
INT 1 6 0Ah 0Bh
Wait Y N Y N
Filters Y N Y Y
Echo Y N Y -
Ctrl-Break Y N Y Y
String - - Y -
13INT 21h functions (continues)
INT 21 functions Example Program
3Fh read from stdin or a disk file BX file or device handle (keyboard 0) CX number of bytes to read DSDX address of array mov ah,3Fh mov bx,0 mov cx,127 mov dx,OFFSET inBuffer int 21h mov bytesRead,ax
40h Write to stdout or a disk file BX file or device handle (console 1) CX number of bytes to write DSDX address of array mov ah,40h mov bx,1 mov cx,LENGTHOF message mov dx,OFFSET message int 21h mov bytesWritten,ax
14INT 21h functions (continues)
INT 21 functions Example Program
2Ah Get system date Year 1980 2099 Month 1 12 Day 1 - 31 mov ah,2Ah int 21h mov year,cx mov month,dh mov day,dl mov dayOfWeek,al
2Bh Set system date AL 0 if successful otherwise AL 0FFh mov ah,2Bh mov cx,year mov dh,month mov dl,day int 21h cmp al,0 jne failed
15INT 21h functions (continues)
INT 21 functions Example Program
2Ch Get system time Hour 0 23 Minute 0 59 Second 0 - 59 mov ah,2Ch int 21h mov hours,ch mov minutes,cl mov seconds,dh
2Dh Set system time AL 0 if successful otherwise AL 0FFh mov ah,2Dh mov ch,hours mov cl,minutes mov dh,seconds int 21h cmp al,0 jne failed
16INT 21h functions (continues)
INT 21 functions Example Program
716Ch open/create a file Set CF if fail Page 479
3Eh close a file Page 480
42h move file pointer Page 481
5706h file creation data/time Page 481
62h get program segment prefix Page 486
17BIOS I/O functions
- Advantages
- Faster
- More control
- "Understands" video monitors
- Disadvantages
- Cannot be redirected
- Somewhat harder to use
18Keyboard Input with INT 16h
- How the Keyboard Works
- INT 16h Functions
- Set Typematic Rate (03h)
- Push Key into Keyboard Buffer (05h)
- Wait for Key (10h)
- Check Keyboard Buffer (11h)
- Get Keyboard Flags
- Clearing the Keyboard Buffer
19Keyboard input ASCII and Scan Codes
- ASCII code one of the 127 characters in ASCII
alphabet. Values from 1 to 127. - Many keys do not have an ASCII code. Examples
All Alt keys like Alt A, F1, Shift F2, Control
F3, right arrow, Home, Insert, ... - Non-ASCII keys have an ASCII code of 00 or E0h.
- BIOS functions provide both ASCII code and Scan
code
20Scan Codes Examples
- Character Scan ASCII A 1E 41 a
1E 61 A 1E 01 Alt A 1E
00 - F1 3B 00 Shift F1 54 00 F1
5E 00 Alt F1 68 00 - Home 47 E0 ? 4D E0
21How the Keyboard Works
Input Port
sc
Keyboard
sc
sc, ac
INT 9h handler
Typeahead Buffer
ac
sc, ac
INT 16h handler
INT 21h handler
22INT 16h Functions
- Set Typematic Rate (03h)
- Push Key into Keyboard Buffer (05h)
- Wait for Key (10h)
- Check Keyboard Buffer (11h)
- Get Keyboard Flags
- Clearing the Keyboard Buffer
23Set Typematic Rate (03h)
AL Subservice 0 - Return to default keyboard
state 1 - Increase initial delay 2 - Slow
typematic rate by 1/2 3 - Perform Subservices 1
and 2 4 - Turns off typematic characters 5 -
Set typematic rate and delay
- Set Typematic repeat rate
- AH 3 AL 5
- BH repeat delay
- 0 250ms
- 1 500ms
- 2 750ms
- 3 1000ms
- BL repeat rate
- 0 fastest
- 1Fh slowest
Mov AX, 0305h Mov BH, 1 Mov BL, 0Fh INT 16h
24Push Key into Keyboard Buffer (05h)
- Push key into typeahead buffer
- A key consists of ac and sc
- AH 5, CH scan code, CL ASCII code
mov AH, 5 mov CH, 3Bh scan code for F1 key mov
CL, 0 ASCII code INT 16h
If buffer is full, CF1 and AL 1 otherwise CF
0, AL 0
25Wait for Key 10h
- Remove a key from the buffer
- If no key is in the buffer, wait for a key
- AH 10h
- Return sc in AH, ac in AL
- Every key has a scan code
- Keys for non-ASCII characters have 00h or E0h as
the ASCII code - Does not echo, can't be redirected.
mov AH, 10h INT 16h mov scanCode, AH mov
ASCIICode, AL
26Sample Program
- Get input keystrokes and display both the ASCII
code and the scan code of each key. Terminates as
Esc is pressed
TITLE Keyboard Display
(keybd.asm) Include Irvine16.inc .code main
PROC mov ax,_at_data mov ds,ax call ClrScr
clear screen L1 mov ah,10h keyboard
input int 16h using BIOS call DumpRegs look
at AH, AL cmp al,1Bh ESC key pressed? jne
L1 no repeat the loop call ClrScr clear
screen exit main ENDP END main
27Scan Codes BIOS vs. DOS
- BIOS You get scan code and ASCII value every
time! Special keys have a ASCII code of either
00h or E0h - DOS You only get the scan code if the ASCII
code is 00h. Codes come one at a time with the
ASCII code first.
28Processing scan codes in DOS
28
- mov ah, 01h Input char - DOSint 21h
Input the ASCII codecmp al, 00h is it
non-ASCII?jne processASCIIcharint 21h
read scancodemov aScanCode, al
29Processing scan codes in BIOS
- mov ah, 10h Input char BIOS
- int 16h get scan and ASCII
- cmp al, 0E0h is it an extended key?
- je scankey
- cmp al, 00h is it an ASCII key?
- jne ASCIIKey
- scankey process scancode in ah
30Check Keyboard Buffer 11h
- Check if any keys are waiting
- Returns the ac and sc of the next available key
- Not remove the key from the buffer
mov ah, 11h int 16h jz NoKeyWaiting no key in
buffer mov scanCode, ah mov ASCIICode, al
31Get Keyboard Flags 12h
- Returns current state of the keyboard flags
- Returns copy of the keyboard flags in AX
- Keyboard flags are in the BIOS data area
mov ah, 12h int 16h mov keyFlags, ax
32Get Keyboard Flags 12h
Bit Description Bit Description
0 R Shift ? 8 L Ctr ?
1 L Shift ? 9 L Alt ?
2 L/R Ctr ? 10 R Ctr ?
3 L/R Alt ? 11 R Alt ?
4 Scroll Lock on 12 Scroll key ?
5 Num Lock on 13 Num Lock ?
6 Caps Lock on 14 Caps Lock ?
7 Insert on 15 SysReq ?
33Three Levels of Video
- Level Compatibility Speed Redirection
- DOS high slow yes
- BIOS high medium no
- Direct medium high no
- DOS output is generic. There are "no" special
video effects - BIOS video "understands" video and allows special
effects - Direct video allows everything but do it yourself
34Text Attributes
- Color text
- Background Foreground 0 0
0 0 0 1 1 1
0111 White character 0000
Black background
35Pages
- Most adapters and most modes allow multiple pages
- BIOS allows writing to any of the pages even if
it is not currently displayed - Example uses
- Write to hidden page and then display that page
to create an "instantaneous" switch in the
display - Save old screen data while doing a special page
36Text Mode Colors
- Code Color Code Color0000 black 1000
gray0001 blue 1001 light
blue0010 green 1010 light
green0011 cyan 1011 light
cyan0100 red 1100 light
red0101 magenta 1101 light
magenta0110 brown 1110 yellow0111 light
gray 1111 bright white
37Function 0Fh Get Video ModeFunction 00h Set
Video Mode
- mov ah, 0Fh Get video modeint 10hmov
vmodeOld, almov columnOld, ah - mov pageOld, bhmov ah, 00h Set video
modemov al, vmodeNewint 10h
38Function 03h Get Cursor LocationFunction 02h
Set Cursor Location
- mov ah, 3 get cursor location
- mov bh, vpage
- int 10h
- mov cursor, CX CH starting scan line, CL
ending - mov position, DX DH row, DL col
- mov ah, 2 set cursor location
- mov dh, row
- mov dl, col
- mov bh, vpage
- int 10h
39Function 08h Read character and attribute
Function 09h Write character and attribute
- mov ah, 08h Read char. and attrmov bh,
videoPageint 10h - mov aChar, almov theAttribute, ah
- mov ah, 09h Write char. and attrmov al,
aCharmov bh, videoPagemov bl, theAttribute
mov cx, repeatCountint 10h
40Function 0Ah Write character
- Just like 09h except that the attribute is
unchanged -
- mov ah, 0Ah Write charmov al, aCharmov
bh, videoPagemov cx, repeatCountint 10h
41Functions 06h (07h) Scroll up (down)
- Specify a window by its corners
- Specify the number of lines to scroll in window
- Number lines 0 means all lines
- Row and columns numbering starts with 0
-
- mov ah, 06h scroll window upmov al,
numLinesmov ch, topRowmov cl, leftColumnmov
dh, bottomRowmov dl, rightColumnmov bh,
theAttributeint 10h
AB
Scroll up one line
AB
AB
42Restore the screen
- Recall that after changing the video mode,
pages, or attributes we should restore the screen
before terminating the program -
- mov ah, 0h Set video modemov al,
vmodeOldint 10h mov ah, 05h Set video
pagemov al, pageOldint 10h
43Toggle Blinking and Intensity Modes
- Set the highest bit of a color attribute to
either control the color intensity or blink the
character - AL 3 BL 0 intesity, 1 blinking
-
- mov ah, 10h blinking/intensitymov
al, 3 - mov bl, 1 enable blinkingint 10h
Video display must run in a full screen mode
44Function 0Eh Teletype output
- Print the character, move cursor to right, go to
beginning of next line if needed -
- mov ah, 0Eh Write char and advance
cursormov al, aCharmov bh, videoPageint 10h
45Function 13h String Teletype output
- Print the string at a given row and column
- String may contain both characters and attribute
values -
- .data
- colorStr byte A, 1Fh, B, 1Ch, C, 1Bh
- row byte 10
- col byte 20
- .code
- mov ax, SEG colorStr
- mov es, ax
- mov ah, 13h Write stringmov al, 2
write modemov bh, videoPagemov CX, (SIZEOF
colorStr)/2 - mov dh, row
- mov dl, col
- mov bp, OFFSET colorStr
- int 10h
46Function 0Ch Write Graphics Pixel
- Draw a pixel on the screen in graphics mode
- Slow, most write directly into video memory
-
- mov ah, 0Ch
- mov al, pixelValue
- mov bh, videoPage
- mov CX, x_coord
- mov DX, y_coord
- int 10h
47Function 0Dh Read Graphics Pixel
- Read a pixel from the screen at a given row and
column -
- mov ah, 0Dh
- mov bh, videoPage
- mov CX, x_coord
- mov DX, y_coord
- int 10h
- mov pixelValue, al
48Mouse ProgrammingINT 33h
- Requires device driver program to be installed
- Mouse movement are tracked in mickeys
- 1 mickey 1/200 of an inch of mouse travel
- 8 mickeys 1 horizontal pixel
- 16 mikeys 1 vertical pixel
49Reset Mouse and Get Status
- AX 0FFFFh BX number of mouse buttons if
mouse support is available - AX 0 if no mouse found
- Centered on the screen, video page 0
- Mouse pointer hidden
- Internal counter is set to -1
- mov ax, 0
- int 33h
- cmp ax, 0
- je MouseNotAvailable
50Showing/Hiding the Mouse Pointer
- mov ax, 1 show mouse pointer
- int 33h internal counter is incremented
- mov ax, 2 hide mouse pointer
- int 33h internal counter is decremented
51Get Mouse Position and Status
- mov ax, 3
- int 33h
- mov x_coord, CX x-coord in pixels
- mov y_coord, DX
- test bx, 1 test mouse button status
- jnz Left_Button_Down
- test bx, 2
- jnz Right_Button_Down
- test bx, 4
- jnz Middle_Button_Down
52Set Mouse Position
- mov ax, 4
- mov CX, x_coord
- mov DX, y_coord
- int 33h
-
53Get Button Presses (Drag)
- Return the status of all mouse buttons and the
position of the last button press - BX button ID 0 left, 1 right, 2 center
- CX X-coord of last button press
- DX Y-coord of last button press
- mov ax, 5
- mov bx, 0 button ID
- int 33h
- test ax, 1 left button?
- jz skip
- mov X_coord, CX
- mov Y_coord, DX
-
54Get Button Release (Click/End of Drag)
- BX button ID 0 left, 1 right, 2 center
- CX X-coord of last button release
- DX Y-coord of last button release
- mov ax, 6
- mov bx, 0 button ID
- int 33h
- test ax, 1 left button?
- jz skip
- mov X_coord, CX
- mov Y_coord, DX
-
55Setting Horizontal Limits
- CX Min X-coord (in pixels)
- DX Max X-coord
- mov ax, 7
- mov CX, Min_X
- mov DX, Max_X
- int 33h
56Setting Vertical Limits
- CX Min Y-coord (in pixels)
- DX Max Y-coord
- mov ax, 8
- mov CX, Min_Y
- mov DX, Max_Y
- int 33h
57Miscellaneous Mouse Functions
Function Description I/O parameters
0Fh Set Mickeys to 8 pixel ratio InCX h_mickey, DX v_mickey
10h Set exclusive area In CX/DX x/y_coord UL, SI/DI x/y_coord BR
13h Set double speed threshold In DX threshold speed m/s
1Ah Set mouse sensitivity In BX/CX h/v_speed, DX double speed threshold
1Bh Get mouse sensitivity Out BX, CX, DX
1Fh Disable mouse driver Out AX 0FFFFh if fails
20h Enable mouse driver
24h Get mouse information CH mouse type, CL IRQ number
58- CSCE 380
- Department of Computer Science and Computer
Engineering - Pacific Lutheran University
- 5/4/2003