Chapter 15 BIOS-Level Programming Assembly Language for Intel-Based Computers, 4th edition Kip R....

Preview:

Citation preview

Chapter 15Chapter 15

BIOS-Level ProgrammingBIOS-Level Programming

Assembly Language for

Intel-Based Computers,

4th edition

Kip R. Irvine

2

Chapter OverviewChapter Overview

• Review Chapter 13• Keyboard Input with INT 16h• VIDEO Programming with INT 10h• Drawing Graphics Using INT 10h• Memory-Mapped Graphics• Mouse Programming

3

Interrupts:Interrupts:

• 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.

4

Hardware Interrupt StepsHardware 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 CS:IP) on the stack.

• 3. The address of the interrupt handler is obtained from the Interrupt Vector Table.

5

Hardware Interrupt StepsHardware 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" (CS:IP and Flags) back from the stack.

• 7. Execution of original program continues beginning at the next instruction.

6

Hardware InterruptsHardware Interrupts

Device Program Interrupt table Interrupt Code

Stack

11 23

4

5

6

0

7

7

Hardware Interrupt CommentsHardware 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.

8

Software InterruptSoftware 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.

9

Common Software InterruptsCommon 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)

10

INT 21h functionsINT 21h functions

INT 21 functions Example Program

4Ch – terminate processmov ah,4Chmov al,0int 21h

02h – character output

advance cursor

mov ah,02hmov dl,’A’int 21h

06h – character outputmov ah,06h mov dl,’a’ int 21h

09h – print $ terminated stringmov ah,09hmov dx,OFFSET stringint 21h

11

INT 21h functions (continues)INT 21h functions (continues)

INT 21 functions Example Program

01h – read a char from stdinmov ah,01hint 21hmov char,al

06h – character input

no waiting

mov ah,06hmov dl,0FFhint 21h

0Ah – input stringmov ah,0Ahmov dx,OFFSET kybdDataint 21h

0Bh – status of input buffermov ah,0Bhint 21h(al = 0/0FFh)

max characters allowed (including <CR>)number of characters input (excluding <CR>)

characters input

0 1 2 …

12

Keyboard Input functions SummaryKeyboard 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 -

13

INT 21h functions (continues)INT 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;

DS:DX = address of array

mov ah,3Fhmov bx,0mov cx,127mov dx,OFFSET inBufferint 21hmov bytesRead,ax

40h – Write to stdout or a disk file;

BX = file or device handle (console = 1);

CX = number of bytes to write;

DS:DX = address of array

mov ah,40hmov bx,1mov cx,LENGTHOF messagemov dx,OFFSET messageint 21hmov bytesWritten,ax

14

INT 21h functions (continues)INT 21h functions (continues)

INT 21 functions Example Program

2Ah – Get system date

Year: 1980 – 2099

Month: 1 – 12

Day: 1 - 31

mov ah,2Ahint 21hmov year,cxmov month,dhmov day,dlmov dayOfWeek,al

2Bh – Set system date

AL = 0 if successful; otherwise AL = 0FFh

mov ah,2Bhmov cx,yearmov dh,monthmov dl,dayint 21hcmp al,0jne failed

15

INT 21h functions (continues)INT 21h functions (continues)

INT 21 functions Example Program

2Ch – Get system time

Hour: 0 – 23

Minute: 0 – 59

Second: 0 - 59

mov ah,2Chint 21hmov hours,chmov minutes,clmov seconds,dh

2Dh – Set system time

AL = 0 if successful; otherwise AL = 0FFh

mov ah,2Dhmov ch,hoursmov cl,minutesmov dh,secondsint 21hcmp al,0jne failed

16

INT 21h functions (continues)INT 21h functions (continues)

INT 21 functions Example Program

716Ch – open/create a file

Set CF if fail

Page 479

3Eh – close a filePage 480

42h – move file pointerPage 481

5706h – file creation data/timePage 481

62h – get program segment prefixPage 486

17

BIOS I/O functionsBIOS I/O functions

• Advantages:• Faster

• More control

• "Understands" video monitors

• Disadvantages• Cannot be redirected

• Somewhat harder to use

18

Keyboard Input with INT 16hKeyboard 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

19

Keyboard input: ASCII and Scan CodesKeyboard 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

20

Scan Codes: ExamplesScan 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

21

How the Keyboard WorksHow the Keyboard Works

Keyboard

INT 9h handlerTypeahead Buffer

INT 16h handler INT 21h handler

sc

Input Port

sc

sc, ac

acsc, ac

22

INT 16h FunctionsINT 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

23

Set Typematic Rate (03h)Set Typematic Rate (03h)

• 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, 0305hMov BH, 1Mov BL, 0FhINT 16h

24

Push Key into Keyboard Buffer (05h)Push 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

* If buffer is full, CF=1 and AL =1; otherwise CF = 0, AL = 0

mov AH, 5

mov CH, 3Bh ; scan code for F1 key

mov CL, 0 ; ASCII code

INT 16h

25

Wait for Key 10hWait 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, 10hINT 16hmov scanCode, AHmov ASCIICode, AL

26

Sample ProgramSample 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,@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

27

Scan Codes: BIOS vs. DOSScan 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.

28

Processing scan codes in DOSProcessing scan codes in DOS

mov ah, 01h ; Input char - DOSint 21h ; Input the ASCII codecmp al, 00h ; is it ASCII 0?jne processASCIIcharint 21h ; read scancodemov aScanCode, al

28

29

Processing scan codes in BIOSProcessing 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

30

Check Keyboard Buffer 11hCheck 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, 11hint 16hjz NoKeyWaiting ;no key in buffermov scanCode, ahmov ASCIICode, al

31

Get Keyboard Flags 12hGet 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, 12hint 16hmov keyFlags, ax

32

Get Keyboard Flags 12hGet 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

33

Level Compatibility Speed RedirectionDOS high slow yesBIOS high medium noDirect 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

Three Levels of VideoThree Levels of Video

34

• Color text

Background Foreground 0 0 0 0 0 1 1 1

0111 White character 0000 Black background

A

Text AttributesText Attributes

0 Do not blink

35

PagesPages

• 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

36

Text Mode ColorsText 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

37

Function 0Fh: Get Video ModeFunction 0Fh: Get Video ModeFunction 00h: Set Video ModeFunction 00h: Set Video Mode

mov ah, 0Fh ; Get video modeint 10hmov vmodeOld, almov columnOld, ah

mov pageOld, bh

mov ah, 00h ; Set video modemov al, vmodeNewint 10h

38

Function 03h: Get Cursor LocationFunction 03h: Get Cursor LocationFunction 02h: Set Cursor LocationFunction 02h: Set Cursor Location

mov ah, 3 ; get cursor locationmov bh, vpageint 10hmov cursor, CX ; CH = starting scan line, CL = endingmov position, DX ; DH = row, DL = col

mov ah, 2 ; set cursor locationmov dh, rowmov dl, colmov bh, vpageint 10h

39

Function 08h: Read character and attribute Function 08h: Read character and attribute Function 09h: Write character and attributeFunction 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

40

Function 0Ah: Write characterFunction 0Ah: Write character

• Just like 09h except that the attribute is unchanged

mov ah, 0Ah ; Write charmov al, aCharmov bh, videoPagemov cx, repeatCountint 10h

41

Functions 06h (07h): Scroll up (down)Functions 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

Scrool up one line

AB

ABAB

42

Restore the screenRestore 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

43

Toggle Blinking and Intensity ModesToggle 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

44

Function 0Eh: Teletype outputFunction 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

45

Function 13h: String Teletype outputFunction 13h: String Teletype output

• Print the string at a given row and column• String may contain both characters and attribute values

.datacolorStr byte ‘A’, 1Fh, ‘B’, 1Ch, ‘C’, 1Bhrow byte 10col byte 20.codemov ax, SEG colorStrmov es, axmov ah, 13h ; Write stringmov al, 2 ; write modemov bh, videoPagemov CX, (SIZEOF colorStr)/2mov dh, rowmov dl, colmov bp, OFFSET colorStrint 10h

46

Function 0Ch: Write Graphics PixelFunction 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

47

Function 0Dh: Read Graphics PixelFunction 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

48

Mouse ProgrammingMouse ProgrammingINT 33hINT 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

49

Reset Mouse and Get StatusReset 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, 0int 33hcmp ax, 0je MouseNotAvailable

50

Showing/Hiding the Mouse PointerShowing/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

51

Get Mouse Position and StatusGet 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

52

Set Mouse PositionSet Mouse Position

mov ax, 4

mov CX, x_coord

mov DX, y_coord

int 33h

53

Get Button Presses (Drag)Get 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, 5mov bx, 0; button IDint 33htest ax, 1 ; left button?jz skipmov X_coord, CXmov Y_coord, DX

54

Get Button Release (Click/End of Drag)Get 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

55

Setting Horizontal LimitsSetting 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

56

Setting Vertical LimitsSetting 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

57

Miscellaneous Mouse FunctionsMiscellaneous Mouse FunctionsFunction Description I/O parameters

0Fh Set Mickeys to 8 pixel ratio In:CX = 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• 11/24/2002

Recommended