kip irvine: assembly language for intel-based computers keyboard status byte

26
Kip Irvine: Assembly Language for Intel-Based Computers 7 6 5 4 3 2 1 0 Right shift key down Left shift key down Control key down Alt key down ScrollLock key down NumLock key down CapsLock key down Insert key down Keyboard Status Byte

Post on 21-Dec-2015

245 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

7 6 5 4 3 2 1 0

Right shift key downLeft shift key downControl key downAlt key downScrollLock key downNumLock key downCapsLock key downInsert key down

Keyboard Status Byte

Page 2: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

Keyboard Input Using INT 16h

mov ah,10h ; wait for key

int 16h ; AH=scan code, AL=ASCII code

Use INT 16h to input any key, including function keys, arrow keys, and other extended keys.

Page 3: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

Keyboard Input Using INT 16h

L1:

mov ah,11h ; key waiting?

int 16h

jnz keyWaiting ; yes: process it

jmp L1 ; no: continue loop

keyWaiting:

mov scanCode,ah

mov ASCIICode,al

INT 16h function 11h detects the presence of a key in the keyboard typeahead buffer. The following loop uses a conditional jump (JNZ), explained in Chapter 6.

Page 4: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

Keyboard Scan Codes

F1 function key 3Bh

F2 function key 3Ch

F3 function key 3Dh

F4 function key 3Eh

• A keyboard scan code is a unique 8-bit binary number associated with a particular keyboard key.

• A list of frequently used codes is inside the front cover of the book. Here are samples:

Home 47h

End 4Fh

PgUp 49h

PgDn 51h

Page 5: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

Hexadecimal Decimal Description

08 08 Backspace

09 09 Horizontal tab

0A 10 Line feed

0C 12 Form feed (printer only)

0D 13 Carriage return (Enter key)

1B 27 Escape

Common ASCII Control Characters

Page 6: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

0 0 0 0 0 1 1 1 07h (normal attribute)=

blink background foreground

(MSDOS mode only)

If your program is running in an MS-DOS window under Windows/NT, your background color is stored in bits 4-7 of the attribute bit, and blinking is disabled.

Video Attribute Layout

Page 7: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

Binary Hex Color

000 00 black

001 01 blue

010 02 green

011 03 cyan

100 04 red

101 05 magenta

110 06 brown

111 07 white

The following background colors are used only when running in full-screen mode or in pure MSDOS mode (by rebooting).

3-bit Background Colors

Page 8: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

Binary Hex Color Binary Hex Color

0000 00 black 1000 08 gray

0001 01 blue 1001 09 light blue

0010 02 green 1010 0A light green

0011 03 cyan 1011 0B light cyan

0100 04 red 1100 0C light red

0101 05 magenta 1101 0D light magenta

0110 06 brown 1110 0E yellow

0111 07 white 1111 0F bright white

4-bit Foreground Colors

Page 9: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

Binary Hex Color Binary Hex Color

0000 00 black 1000 08 gray

0001 01 blue 1001 09 light blue

0010 02 green 1010 0A light green

0011 03 cyan 1011 0B light cyan

0100 04 red 1100 0C light red

0101 05 magenta 1101 0D light magenta

0110 06 brown 1110 0E yellow

0111 07 white 1111 0F bright white

4-bit Background Colors

Page 10: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

FunctionNumber (in

AH)Description

0 Set Video Mode. Set the video display to monochrome, text, graphics,or color mode.

1 Set Cursor Lines. Identify the starting and ending scan lines for thecursor.

2 Set Cursor Position. Position the cursor on the screen.

3 Get Cursor Position. Get the cursor's screen position and size.

4 Read Light Pen. Read the position and status of the light pen.

5 Set Display Page. Select the video page to be displayed.

6 Scroll Window Up. Scroll a window on the current video page upward,replacing scrolled lines with blanks.

7 Scroll Window Down. Scroll a window on the current video pagedownward, replacing scrolled lines with blanks.

8 Read Character and Attribute. Read the character and its attribute atthe current cursor position.

9 Write Character and Attribute. Write a character and its attribute at thecurrent cursor position.

0Ah Write Character. Write a character only (no attribute) at the currentcursor position.

0Bh Set Color Pallete. Select a group of available colors for the videoadapter.

0Ch Write Graphics Pixel. Write a graphics pixel when in graphics mode.

0Dh Read Graphics Pixel. Read the color of a single graphics pixel at agiven location.

0Eh Write Character. Write a character to the screen and advance thecursor.

0Fh Get Video Mode. Get the current video mode.

11h Load Default ROM Font. While in text mode, load one of three defaultROM fonts and display on the EGA and VGA displays.

Table 9. Listing of INT 10h Functions (1 of 2)

Page 11: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

FunctionNumber (in

AH)Description

0 Set Video Mode. Set the video display to monochrome, text, graphics,or color mode.

1 Set Cursor Lines. Identify the starting and ending scan lines for thecursor.

2 Set Cursor Position. Position the cursor on the screen.

3 Get Cursor Position. Get the cursor's screen position and size.

4 Read Light Pen. Read the position and status of the light pen.

5 Set Display Page. Select the video page to be displayed.

6 Scroll Window Up. Scroll a window on the current video page upward,replacing scrolled lines with blanks.

7 Scroll Window Down. Scroll a window on the current video pagedownward, replacing scrolled lines with blanks.

8 Read Character and Attribute. Read the character and its attribute atthe current cursor position.

9 Write Character and Attribute. Write a character and its attribute at thecurrent cursor position.

0Ah Write Character. Write a character only (no attribute) at the currentcursor position.

0Bh Set Color Pallete. Select a group of available colors for the videoadapter.

0Ch Write Graphics Pixel. Write a graphics pixel when in graphics mode.

0Dh Read Graphics Pixel. Read the color of a single graphics pixel at agiven location.

0Eh Write Character. Write a character to the screen and advance thecursor.

0Fh Get Video Mode. Get the current video mode.

11h Load Default ROM Font. While in text mode, load one of three defaultROM fonts and display on the EGA and VGA displays.

Table 9. Listing of INT 10h Functions (2 of 2)

Page 12: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

INT 10h (06h) Scroll Window Up

mov ah,6 ; scroll window upmov al,5 ; scroll 5 linesmov ch,0 ; upper left rowmov cl,0 ; upper left column mov dh,24 ; lower right rowmov dl,79 ; lower right columnmov bh,7 ; attribute for blank linesint 10h ; call BIOS

When you scroll a window up, existing lines of text are moved upward and one or more blank lines are created. You can assign a color to the blank lines.

Page 13: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

Scroll (clear) Entire Window

mov ah,6 ; scroll window upmov al,0 ; entire windowmov ch,0 ; upper left rowmov cl,0 ; upper left column mov dh,24 ; lower right rowmov dl,79 ; lower right columnmov bh,7 ; attribute for blank linesint 10h ; call BIOS

If you set AL to zero, all lines in the window are scrolled. This clears the window.

Page 14: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

INT 10h (07h) Scroll Window Down

mov ah,7 ; scroll window downmov al,1 ; scroll one row mov ch,0 ; upper left rowmov cl,0 ; upper left column mov dh,24 ; lower right rowmov dl,79 ; lower right columnmov bh,0F1h ; blank line's attribute int 10h ; call BIOS

The following scrolls all lines within a window in the downward direction by one row. The blank line's attribute is blue text on a white background (11110001):

Page 15: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

INT 10h (2h) Set Cursor Position, and INT 10h (08h) Read Character and Attribute

locate: mov ah,2 ; set cursor position mov bh,0 ; on video page 0 mov dx,0501h ; at row 5,column 1 int 10hgetchar: mov ah,8 ; read char/attribute mov bh,0 ; on video page 0 int 10h mov char,al ; save the character mov attrib,ah ; save the attribute

Page 16: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

Advance the Screen Cursor

AdvanceCursor proc pusha

mov ah,3 ; get cursor positionmov bh,0int 10hinc dl ; increment columnmov ah,2 ; set cursor positionint 10hpoparet

AdvanceCursor endp

Strategy: Get the current cursor position, add 1 to DL, and set the new cursor position.

Page 17: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

INT 10h (09h) Write Character and Attribute

mov ah,9 ; write character and attributemov al,0Ah ; ASCII character 0Ahmov bh,0 ; video page 0mov bl,2 ; color (attribute) = greenmov cx,1 ; display it one timeint 10h

This function does not advance the cursor, so you have to do that separately

Page 18: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

Example: Write a Color String

string db "ABCDEFGHIJKLMOP"count = ($-string)color db 1 . mov cx,count mov si,offset stringL1: push cx ; save loop counter mov ah,9 ; write character and attribute mov al,[si] ; character to display mov bh,0 ; video page 0 mov bl,color ; get the color mov cx,1 ; display it one time int 10h call AdvanceCursor inc color ; next color inc si ; next character position pop cx ; restore loop counter Loop L1

Page 19: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

Table 10. Direct Video Procedures in the Link Library

Procedure Description

Set_videoseg

Set the current video segment address. The default is B800h,which is appropriate for a color display, including all types ofVGA. The alternative is B000h, the default for the oldermonochrome display. Input: AX contains the segment value.

Writechar_direct Write a single character to VRAM. Input: AL = character, AH =attribute, DH/DL = row (0-24) and column (0-79) on screen.

Writestring_directWrite a null-terminated string to VRAM, all characters in thesame color. Input: DS:SI points to the string, AH = attribute,DH/DL = row (0-24) and column (0-79) on screen.

Under Windows 2000, you can see the output of these functions while debugging in CodeView, but if you run the program in a Command window, they do not generate any output.

Page 20: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

Recursion - A Procedure Calling Itself

Recursion happens under the following circumstances:

• A procedure directly calls itself

• Procedure A calls one or more other procedures, and somewhere in the execution of these, one of them calls Procedure A. (indirect recursion)

There must be a way to stop the recursion, or it will run out of control. A conditional jump is usually used to accomplish this.

Page 21: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

Recursion Example: Sum of Integers

Main proc mov cx,5 ; counter mov ax,0 ; holds the sum call Sum ; find sum of 5+4+3+2+1L1: mov ax,4C00h int 21hMain endp

Sum proc or cx,cx ; check counter value jz L2 ; quit if zero add ax,cx ; otherwise, add to sum dec cx ; decrement counter call Sum ; recursive callL2: retSum endp

Page 22: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

Table 11. Stack Frame for the Sum Program

Pushed On Stack CX AX

L1 5 0

L2 4 5

L2 3 9

L2 2 12

L2 1 14

L2 0 15

Page 23: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

Example 9. The Factorial Procedure (1 of 2)

main proc0000 mov ax,8 ; calculate 8!0003 push ax0004 call Factorial ; return value in AX0007 mov ax,4C00h000A int 21h main endp

The factorial procedure calculates the factorial of the number passed to it in the AX register. The calculated value is returned in AX.

Page 24: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

Example 9. The Factorial Procedure (2 of 2)

Factorial proc000C push bp000D mov bp,sp000F mov ax,[bp+4] ; get n0012 cmp ax,1 ; n <= 1?0015 ja L1 ; no: continue0017 mov ax,1 ; yes: return 1001A jmp L2001D L1: dec ax001E push ax ; Factorial(n‑1)001F call Factorial0022 mov bx,[bp+4] ; get n0025 mul bx ; ax = ax * bx0027 L2: pop bp0028 ret 2 ; AX = result

Factorial endp

Page 25: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

Figure 8. Stack Frame, Factorial Program

000800070000

0007002200FA

0006002200F4

0005002200EE

STACK

0004(etc.)

nIP (return address)BP

(n-1)IP (return address)BP

(n-1)IPBP

(n-1)IPBP

(n-1)

Page 26: Kip Irvine: Assembly Language for Intel-Based Computers Keyboard Status Byte

Kip Irvine: Assembly Language for Intel-Based Computers

Table 12. Examples for Question 23

Blink Background Foreground Bit Pattern

Off Brown Yellow 0 1 1 0 1 1 1 0

Off White Blue

Off Blue White

On Cyan Gray

Off Black Light magenta

On Black Bright white

Create the required bit pattern for each of the following colors.