Keyboard Status Byte

advertisement
Keyboard Status Byte
Insert key down
CapsLock key down
NumLock key down
ScrollLock key down
Alt key down
Control key down
Left shift key down
Right shift key down
7
6
5
4
3
2
1
0
Kip Irvine: Assembly Language for Intel-Based Computers
Keyboard Input Using INT 16h
Use INT 16h to input any key, including function
keys, arrow keys, and other extended keys.
mov
int
ah,10h
16h
; wait for key
; AH=scan code, AL=ASCII code
Kip Irvine: Assembly Language for Intel-Based Computers
Keyboard Input Using INT 16h
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.
L1:
mov
int
jnz
jmp
ah,11h
16h
keyWaiting
L1
; key waiting?
; yes: process it
; no: continue loop
keyWaiting:
mov scanCode,ah
mov ASCIICode,al
Kip Irvine: Assembly Language for Intel-Based Computers
Keyboard Scan Codes
• 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:
F1
F2
F3
F4
function
function
function
function
key
key
key
key
3Bh
3Ch
3Dh
3Eh
Home
End
PgUp
PgDn
47h
4Fh
49h
51h
Kip Irvine: Assembly Language for Intel-Based Computers
Common ASCII Control Characters
Hexadecimal
Decimal
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
Description
Kip Irvine: Assembly Language for Intel-Based Computers
Video Attribute Layout
(MSDOS mode only)
0
blink
0
0
0
background
0
1
1
1
= 07h (normal attribute)
foreground
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.
Kip Irvine: Assembly Language for Intel-Based Computers
3-bit Background Colors
The following background colors are used only when running
in full-screen mode or in pure MSDOS mode (by rebooting).
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
Kip Irvine: Assembly Language for Intel-Based Computers
4-bit Foreground Colors
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
Kip Irvine: Assembly Language for Intel-Based Computers
4-bit Background Colors
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
Kip Irvine: Assembly Language for Intel-Based Computers
Table 9. Listing of INT 10h Functions (1 of 2)
Function
Number (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 the
cursor.
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 page
downward, replacing scrolled lines with blanks.
8
Read Character and Attribute. Read the character and its attribute at
the current cursor position.
9
Write Character and Attribute. Write a character and its attribute at the
Kip Irvine:
Language for Intel-Based Computers
currentAssembly
cursor position.
7
Scroll Window Down. Scroll a window on the current video page
downward, replacing scrolled lines with blanks.
8
Read Character and Attribute. Read the character and its attribute at
the current cursor position.
9
Write Character and Attribute. Write a character and its attribute at the
current cursor position.
Table 9. Listing of INT 10h Functions (2 of 2)
0Ah
Write Character. Write a character only (no attribute) at the current
cursor position.
0Bh
Set Color Pallete. Select a group of available colors for the video
adapter.
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 a
given location.
0Eh
Write Character. Write a character to the screen and advance the
cursor.
0Fh
Get Video Mode. Get the current video mode.
11h
Load Default ROM Font. While in text mode, load one of three default
ROM fonts and display on the EGA and VGA displays.
Kip Irvine: Assembly Language for Intel-Based Computers
INT 10h (06h) Scroll Window Up
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.
mov
mov
mov
mov
mov
mov
mov
int
ah,6
al,5
ch,0
cl,0
dh,24
dl,79
bh,7
10h
;
;
;
;
;
;
;
;
scroll window up
scroll 5 lines
upper left row
upper left column
lower right row
lower right column
attribute for blank lines
call BIOS
Kip Irvine: Assembly Language for Intel-Based Computers
Scroll (clear) Entire Window
If you set AL to zero, all lines in the window are
scrolled. This clears the window.
mov
mov
mov
mov
mov
mov
mov
int
ah,6
al,0
ch,0
cl,0
dh,24
dl,79
bh,7
10h
;
;
;
;
;
;
;
;
scroll window up
entire window
upper left row
upper left column
lower right row
lower right column
attribute for blank lines
call BIOS
Kip Irvine: Assembly Language for Intel-Based Computers
INT 10h (07h) Scroll Window Down
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):
mov
mov
mov
mov
mov
mov
mov
int
ah,7
al,1
ch,0
cl,0
dh,24
dl,79
bh,0F1h
10h
;
;
;
;
;
;
;
;
scroll window down
scroll one row
upper left row
upper left column
lower right row
lower right column
blank line's attribute
call BIOS
Kip Irvine: Assembly Language for Intel-Based Computers
INT 10h (2h) Set Cursor Position, and
INT 10h (08h) Read Character and Attribute
locate:
mov
mov
mov
int
getchar:
mov
mov
int
mov
mov
ah,2
bh,0
dx,0501h
10h
; set cursor position
; on video page 0
; at row 5,column 1
ah,8
bh,0
10h
char,al
attrib,ah
; read char/attribute
; on video page 0
; save the character
; save the attribute
Kip Irvine: Assembly Language for Intel-Based Computers
Advance the Screen Cursor
Strategy: Get the current cursor position, add 1
to DL, and set the new cursor position.
AdvanceCursor proc
pusha
mov ah,3
; get cursor position
mov bh,0
int 10h
inc dl
; increment column
mov ah,2
; set cursor position
int 10h
popa
ret
AdvanceCursor endp
Kip Irvine: Assembly Language for Intel-Based Computers
INT 10h (09h) Write Character and Attribute
This function does not advance the cursor, so
you have to do that separately
mov
mov
mov
mov
mov
int
ah,9
al,0Ah
bh,0
bl,2
cx,1
10h
;
;
;
;
;
write character and attribute
ASCII character 0Ah
video page 0
color (attribute) = green
display it one time
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 string
L1:
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
Kip Irvine: Assembly Language for Intel-Based Computers
Table 10. Direct Video Procedures in the Link Library
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.
Procedure
Description
Set_videoseg
Set the current video segment address. The default is B800h,
which is appropriate for a color display, including all types of
VGA. The alternative is B000h, the default for the older
monochrome 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_direct
Write a null-terminated string to VRAM, all characters in the
same color. Input: DS:SI points to the string, AH = attribute,
DH/DL = row (0-24) and column (0-79) on screen.
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.
Kip Irvine: Assembly Language for Intel-Based Computers
Recursion Example: Sum of Integers
Main proc
mov
mov
call
L1: mov
int
Main endp
Sum proc
or
jz
add
dec
call
L2: ret
Sum endp
cx,5
ax,0
Sum
ax,4C00h
21h
; counter
; holds the sum
; find sum of 5+4+3+2+1
cx,cx
L2
ax,cx
cx
Sum
;
;
;
;
;
check counter value
quit if zero
otherwise, add to sum
decrement counter
recursive call
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
Kip Irvine: Assembly Language for Intel-Based Computers
Example 9. The Factorial Procedure (1 of 2)
The factorial procedure calculates the factorial of the number
passed to it in the AX register. The calculated value is returned in
AX.
0000
0003
0004
0007
000A
main proc
mov ax,8
; calculate 8!
push ax
call Factorial ; return value in AX
mov ax,4C00h
int 21h
main endp
Kip Irvine: Assembly Language for Intel-Based Computers
Example 9. The Factorial Procedure (2 of 2)
000C
000D
000F
0012
0015
0017
001A
001D
001E
001F
0022
0025
0027
0028
Factorial proc
push bp
mov bp,sp
mov ax,[bp+4]
cmp ax,1
ja
L1
mov ax,1
jmp L2
L1: dec ax
push ax
call Factorial
mov bx,[bp+4]
mul bx
L2: pop bp
ret 2
Factorial endp
;
;
;
;
get n
n <= 1?
no: continue
yes: return 1
; Factorial(n-1)
; get n
; ax = ax * bx
; AX = result
Kip Irvine: Assembly Language for Intel-Based Computers
Figure 8. Stack Frame, Factorial Program
STACK
0008
0007
0000
n
IP (return address)
BP
0007
0022
00FA
(n-1)
IP (return address)
BP
0006
0022
00F4
(n-1)
IP
BP
0005
0022
00EE
(n-1)
IP
BP
0004
(etc.)
(n-1)
Kip Irvine: Assembly Language for Intel-Based Computers
Table 12. Examples for Question 23
Create the required bit pattern for each of the
following colors.
Blink
Background
Foreground
Bit Pattern
Off
Brown
Yellow
01101110
Off
White
Blue
Off
Blue
White
On
Cyan
Gray
Off
Black
Light magenta
On
Black
Bright white
Kip Irvine: Assembly Language for Intel-Based Computers
Download