chapter 15 examples

49
Chapter 15 examples BIOS-level programming

Upload: aquarius

Post on 13-Jan-2016

58 views

Category:

Documents


0 download

DESCRIPTION

Chapter 15 examples. BIOS-level programming. What are scan codes?. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 15 examples

Chapter 15 examples

BIOS-level programming

Page 2: Chapter 15 examples

What are scan codes?

• Scancodes on IBM PC compatible keyboards are sets of 1 to 3 bytes which are sent by the keyboard. Most character keys have a single byte scancode; keys that perform special functions have 2-byte or 3-byte scancodes, usually beginning with the byte (in hexadecimal) E0, E1, or E2. In addition, a few keys send longer scancodes, effectively emulating a series of keys to make it easier for different types of software to process.

Page 3: Chapter 15 examples

Scancodes… continued

• PC-compatibles have used three scancode sets. The most commonly encountered are the "XT" ("set 1") scancodes, used by the IBM PC XT and earlier. These mostly consist of a single byte; the low 7 bits identify the key, and the most significant bit is clear for a key press or set for a key release. Some additional keys have an E0 (or rarely, E1 or E2) prefix. These were initially assigned so that ignoring the E0 prefix (which is in the key-up range and thus would have no effect on an operating system that did not understand them) would produce reasonable results. For example the numeric keypad's Enter key produces a scancode of E0 1C, which corresponds to the regular Enter key's scancode of 1C.

Page 4: Chapter 15 examples

Scancodes… continued

• Later additions (such as the Windows keys on many keyboards) have not followed this pattern.

• The IBM PC AT introduced the "AT" ("set 2") scancodes, with a different key numbering and where a key release is indicated by an F0 prefix. For backward compatibility, the keyboard controller on the motherboard translates these into XT (set 1) scancodes.[2] This translation can be disabled, allowing the raw scancodes to be seen.[3] Therefore, whether an engineer will encounter AT scancodes or XT scancodes on a modern PC-compatible depends on how the keyboard is being accessed.

Page 5: Chapter 15 examples

Variation on keybd.asm

Page 6: Chapter 15 examples

Keybd2.asm code…

.data;;;I added these messagescharis byte "char is=",0scan byte "scan code=",0ascii byte "ascii code=",0.codemain PROC

mov ax,@datamov ds,axcall ClrScr ; clear screen

L1: mov ah,10h ; keyboard inputint 16h ; using BIOSpush axpush axmov edx, offset chariscall writestringmov dl,alpush axmov ah,2int 21h

Page 7: Chapter 15 examples

exe in p drivecall ClrScr ; clear screenpop axcall crlfand eax,0ff00hmov edx, offset scancall writestringshl ax,8call writeIntcall crlfmov edx, offset asciicall writestringpop axand ax,0ffhcall writeIntcall crlfpop axcmp al,1Bh ; ESC key pressed?jne L1 ; no: repeat the loop

Page 8: Chapter 15 examples

Clearing the type-ahead buffer

• If the user is typing keys in some loop (in a game situation, for example) and the program is waiting for a particular key press, you may need to clear the type-ahead buffer

Page 9: Chapter 15 examples

INCLUDE Irvine16.incClearKeyboard PROTO, scanCode:BYTEESC_key = 1 ; scan code

.codemain PROCL1: ;------------------------- LOOP BODY

; Display a dot, to show program's progressmov ah,2mov dl,'.'int 21hmov eax,300 ; delay for 300 mscall Delay;-------------------------INVOKE ClearKeyboard,ESC_key ; check for Esc keyjnz L1 ; continue loop if ZF=0

quit:call Clrscrexit

main ENDP

Page 10: Chapter 15 examples

clearkbd;---------------------------------------------------ClearKeyboard PROC,

scanCode:BYTE;; Clears the keyboard, while checking for a; particular scan code.; Receives: keyboard scan code; Returns: Zero flag set if the ASCII code is; found; otherwise, Zero flag is clear.;---------------------------------------------------

push axL1:

mov ah,11h ; check keyboard bufferint 16h ; any key pressed?jz noKey ; no: exit now (ZF=0)mov ah,10h ; yes: remove from bufferint 16hcmp ah,scanCode ; was it the exit key?je quit ; yes: exit now (ZF=1)jmp L1 ; no: check buffer again

noKey: ; no key pressedor al,1 ; clear zero flag

quit:pop axret

ClearKeyboard ENDPEND main

Page 11: Chapter 15 examples

Not too impressive

Page 12: Chapter 15 examples

getting keyboard flags directlykbdseg dw 0040h;segment of memory where keyboard flags are locatedkbdofs dw ;ofs of memory where keyboard flags are located

string byte 'the keyboard settings',0ah,0dh,0flags word ?.codemain PROC

mov ax,@datamov ds,axmov ax,kbdsegmov es,axmov di,kbdofs

xor eax,eaxmov al, byte ptr ES:[di]inc dimov ah,byte ptr ES:[di]mov flags,axcall writebin

Page 13: Chapter 15 examples

getting keyboard flags indirectly

xor eax,eax

mov ah,12h

int 16h

mov flags,ax

call writebin

Page 14: Chapter 15 examples

Keyboard flags: note 1s for caps and numlock toggle keys (table pg 496)

C:\Masm615\Examples\ch15>FLAGS

0000 0000 0110 0000

C:\Masm615\Examples\ch15>FLAGS2

0000 0000 0110 0000

C:\Masm615\Examples\ch15>

Page 15: Chapter 15 examples

Keyboard flags

Page 16: Chapter 15 examples

Video display: 3 levels

1. Use DOS int 21h to put stuff on the screen

2. Use BIOS int 10h to put stuff on screen (no output redirect allowed)

3. Direct video access…write chars directly onto screen (see keyboard flags version for example of how to set seg and ofset)

Page 17: Chapter 15 examples

Video using int 10h

• Each color pixel is generated by an electron beam with r-g-b components A fourth channel controls intensity, in the format:

• I-R-G-B• Text table pg 500 shows bit setting for various

colors.• In color text mode, each character has an

attribute byte consisting of 2 4-bit color settings for background and foreground. (bottom pg 500)

Page 18: Chapter 15 examples

Int 10h

• Text pg 501 has a list of int 10h function codes

• These include reading characters from the screen, writing chars or pixels onto the screen

Page 19: Chapter 15 examples

Txt pg 502 lists (text) video modes

Mode Resolution colors

0 40X25 1

1 40X25 16

2 80X25 2

3 80X25 16

7 80X25 2

14h 132X25 16

Page 20: Chapter 15 examples

Int 10h functions

• Get current video mode and save it before setting a new video mode:

Int10h, function ah=0 sets mode in al.

• Function 1 sets the cursor size

• Function 2 sets cursor position (dh,dl=row,col; bh=videopage)

Page 21: Chapter 15 examples

Int 10h functions

• Function 3=get curpos & size

• Text has sample routines pg 545 to show/hide the cursor.. Here’s an example:Showcur procMov ah,1Mov cx,0607h;default sizeint 10hRetShowcur endp

Page 22: Chapter 15 examples

Writing (color) text to a window… text example

Page 23: Chapter 15 examples

TextWin.asm (left out a few lines); Scroll a window.

mov ax,0600h ; scroll windowmov bh,00011110b ; yellow on bluemov cx,050Ah ; upper-left cornermov dx,0A30h ; lower-right cornerint 10h

; Position the cursor inside the window.mov ah,2 ; set cursor positionmov dx,0714h ; row 7, col 20mov bh,0 ; video page 0int 10h

; Write some text in the window.mov dx,OFFSET messagecall WriteString

; Wait for a keypress.mov ah,10hint 16hexit

Page 24: Chapter 15 examples

Text example… colorstring main proccall ClrScr

call EnableBlinkingmov cx,SIZEOF stringmov si,OFFSET string

L1: push cx ; save loop countermov ah,9 ; write character/attributemov al,[si] ; character to displaymov bh,0 ; video page 0mov bl,color ; attributeor bl,ATTRIB_HI ; set blink/intensity bitmov cx,1 ; display it one timeint 10hmov cx,1 ; advance cursor tocall AdvanceCursor ; next screen columninc color ; next colorinc si ; next characterpop cx ; restore loop counterLoop L1

call Crlfexit

Page 25: Chapter 15 examples

EnableBlinking PROC

EnableBlinking PROC;; Enable blinking (using the high bit of color; attributes). In MS-Windows, this only works if; the program is running in full-screen mode.; Receives: nothing.; Returns: nothing;--------------------------------------------------

push axpush bxmov ax,1003hmov bl,1 ; blinking is enabledint 10hpop bxpop axret

EnableBlinking ENDP

Page 26: Chapter 15 examples

AdvanceCursor PROC

AdvanceCursor PROC;; Advances the cursor n columns to the right.; Receives: CX = number of columns; Returns: nothing;--------------------------------------------------

pushaL1:

push cx ; save loop countermov ah,3 ; get cursor positionmov bh,0 ; into DH, DLint 10h ; changes CX register!inc dl ; increment columnmov ah,2 ; set cursor positionint 10hpop cx ; restore loop counterloop L1 ; next column

poparet

Page 27: Chapter 15 examples

Run of colorstr

Page 28: Chapter 15 examples

Graphics in int 10h: table of video graphics modes on pg 512

Mode Resolution # colors

6 640X200 2

0dh 320X200 16

0eh 640X200 16

0fh 640X350 2

10h 640X350 16

11h 640X480 2

12h 640X480 16

13h 320X200 256

6ah 800X600 16

Page 29: Chapter 15 examples

functions

• 0Ch write a pixel

• 0dh read a pixel

• See sample calls and parameters in text

Page 30: Chapter 15 examples

Drawline example: Changes mode to full screen and draws a tiny line segment:

INCLUDE Irvine16.inc

;------------ Video Mode Constants -------------------

Mode_06 = 6 ; 640 X 200, 2 colors

Mode_0D = 0Dh ; 320 X 200, 16 colors

Mode_0E = 0Eh ; 640 X 200, 16 colors

Mode_0F = 0Fh ; 640 X 350, 2 colors

Mode_10 = 10h ; 640 X 350, 16 colors

Mode_11 = 11h ; 640 X 480, 2 colors

Mode_12 = 12h ; 640 X 480, 16 colors

Mode_13 = 13h ; 320 X 200, 256 colors

Mode_6A = 6Ah ; 800 X 600, 16 colors

Page 31: Chapter 15 examples

More….datasaveMode BYTE ? ; save the current video modecurrentX WORD 100 ; column number (X-coordinate)currentY WORD 100 ; row number (Y-coordinate)color BYTE 1 ; default color

; In 2-color modes, white = 1; In 16-color modes, blue = 1.codemain PROC

mov ax,@datamov ds,ax

; Save the current video modemov ah,0Fhint 10hmov saveMode,al

; Switch to a graphics modemov ah,0 ; set video modemov al,Mode_11int 10h

Page 32: Chapter 15 examples

And the rest; Draw a straight line

LineLength = 100

mov dx,currentYmov cx,LineLength ; loop counter

L1:push cxmov ah,0Ch ; write pixelmov al,color ; pixel colormov bh,0 ; video page 0mov cx,currentXint 10hinc currentX;inc color ; try this for multi-color modespop cxLoop L1

; Wait for a keystrokemov ah,0int 16h

; Restore the starting video modemov ah,0 ; set video mode… this part doesn’t seem to workmov al,saveMode ; saved video modeint 10hexit

main ENDP

Page 33: Chapter 15 examples

Cartesian coordinates

• Need to switch command prompt to full screen

• Even so, it worked ok when I used mode11 but not mode6

Page 34: Chapter 15 examples

Cartesian example from txt.datasaveMode BYTE ? ; save the current video modecurrentX WORD 100 ; column number (X-coordinate)currentY WORD 100 ; row number (Y-coordinate)color BYTE 1 ; default color

; In 2-color modes, white = 1; In 16-color modes, blue = 1

.codemain PROC

mov ax,@datamov ds,ax

; Save the current video modemov ah,0Fhint 10hmov saveMode,al

; Switch to a graphics modemov ah,0 ; set video modemov al,Mode_11;;;;;note mode6 did not work for meint 10h

Page 35: Chapter 15 examples

Cartesian example;mov cx,X_axisXmov dx,x_axisYmov ax,x_axislenmov bl,whitecall horizontal

mov cx,y_axisXmov dx,y_axisYmov ax,y_axislenmov bl,whitecall vertical

; Wait for a keystrokemov ah,0int 16h

; Restore the starting video modemov ah,0 ; set video modemov al,saveMode ; saved video modeint 10hexit

main ENDP

Page 36: Chapter 15 examples

Cartesian example: horizontal line

• horizontal proc• .data• currx word ?• .code• pusha• mov currx,cx• mov cx,ax• H1:• push cx• mov al,bl• mov ah,0ch• mov bh,0 ;;page

• mov cx,currx• int 10h• inc currx• pop cx• loop h1• popa• ret• horizontal endp

Page 37: Chapter 15 examples

Cartesian example: vertical linevertical proc.datacurry word ?.codepushamov curry,dxmov currx,cxmov cx,axV1:push cxmov al,blmov ah,0chmov bh,0mov cx,currxmov dx,curryint 10hinc currypop cxloop V1poparetvertical endp

END main

Page 38: Chapter 15 examples

Section 15.5 memory mapped graphics

• Mode 13h 320X200, 256 color

• Text example Mode13.asm

• Draws a few blue dots on the screen…

• Need to run this in full screen

Page 39: Chapter 15 examples

boatgame

• Code in slide and posted

• Draws a boat and sub

• Uses old medium resolution graphics mode.

Page 40: Chapter 15 examples

Medium resolution examples

• These examples are based on an older graphics card but they still work:

• Moveball---in slide notes

• Medres plays cellular automata in medium resolution

• Boatgame ---ask for source

Page 41: Chapter 15 examples

moveball

• Draws a cyan ball on a blue screen.

• Hit or hold key to move ball rightwards (it wraps)

• Type ‘x’ to exit

Page 42: Chapter 15 examples

Hr examples from another text: draws a series of lines in hr –very pretty but can’t get screenshot

.model small

.stack 100h

.386

.codemain proc near

mov ax,@datamov ds,axmov es,ax

mov ah,0fhint 10hpush axcall b10modecall c10displaymov ah,10hint 16hpop axmov ah,0int 10hmov ax,4c00hint 21h

main endpb10mode proc near

mov ax,12hint 10hmov ah,0bhmov bx,7int 10hret

b10mode endp

Page 43: Chapter 15 examples

Hr2 continuedc10display proc near

pushaxor bx,bxmov cx,64mov dx,70

@@20: mov ah,0ch mov al,bl int 10h inc cx cmp cx, 576;;;row/col delimiters in cx,dc jne @@20 mov cx,64 inc bl inc dx cmp dx,280 jne @@20 popa retc10display endpend main

Page 44: Chapter 15 examples

Hr4…similar…writes letters .model small.stack 100h.386video segment at 0b900h;;;page 1vid db 1000H dup (?)video ends

.codemain proc near

mov ax,videomov es,axassume es:videomov ah,0fhint 10hpush ax;;;save current modepush bxmov ax,3int 10hmov ax,501h;;page 1int 10hcall b10displaymov ah,10h;;wait for keyint 16h

Page 45: Chapter 15 examples

hr4mov ah,5pop bxmov al,bhint 10hpop axxor ah,ahint 10hmov ax,4c00hint 21h

main endpb10display proc

pushamov al,41hmov ah,1mov di,820

@@10: mov cx,60@@20: mov es:word ptr[di],ax

add di,2loop @@20inc ahinc aladd di,40cmp al,51hjne @@10poparet

b10display endpend main

Page 46: Chapter 15 examples

hr4

Page 47: Chapter 15 examples

Hr5 draws a block.model small.stack 100h.386

.datacolor db ?back dw 50front dw 250cstart dw 50cstop dw 250rstart dw 50rstop dw 250red equ 12grey equ 7

.codemain proc near

mov ax,@datamov ds,axmov es,ax

mov ah,0fhint 10hpush axcall b10mode

Page 48: Chapter 15 examples

Hr5 continued (next slide also)mov color,redcall putblock;;;code in notes

down:mov ah,10hint 16hpop axmov ah,0int 10hmov ax,4c00hint 21h

main endp

• b10mode proc near• mov ax,12h• int 10h• mov ah,0bh• mov bx,7• int 10h• ret• b10mode endp

Page 49: Chapter 15 examples

Other examples

• Game of life (life.exe/.asm) implements a not too interesting one-dimensional cellular automata in hr graphics

• Plotdot3 seems to be the same as hr2