300 likes | 704 Vues
Keyboard Status Byte. 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. Keyboard Input Using INT 16h.
E N D
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. 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 ah,11h ; key waiting? int 16h jnz keyWaiting ; yes: process it jmp L1 ; 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 function key 3Bh F2 function key 3Ch F3 function key 3Dh F4 function key 3Eh Home 47h End 4Fh PgUp 49h PgDn 51h Kip Irvine: Assembly Language for Intel-Based Computers
Common ASCII Control Characters Kip Irvine: Assembly Language for Intel-Based Computers
Video Attribute Layout (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. 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). Kip Irvine: Assembly Language for Intel-Based Computers
4-bit Foreground Colors Kip Irvine: Assembly Language for Intel-Based Computers
4-bit Background Colors Kip Irvine: Assembly Language for Intel-Based Computers
Table 9. Listing of INT 10h Functions (1 of 2) Kip Irvine: Assembly Language for Intel-Based Computers
Table 9. Listing of INT 10h Functions (2 of 2) 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 ah,6 ; scroll window up • mov al,5 ; scroll 5 lines • mov ch,0 ; upper left row • mov cl,0 ; upper left column • mov dh,24 ; lower right row • mov dl,79 ; lower right column • mov bh,7 ; attribute for blank lines • int 10h ; 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 ah,6 ; scroll window up • mov al,0 ; entire window • mov ch,0 ; upper left row • mov cl,0 ; upper left column • mov dh,24 ; lower right row • mov dl,79 ; lower right column • mov bh,7 ; attribute for blank lines • int 10h ; 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 ah,7 ; scroll window down • mov al,1 ; scroll one row • mov ch,0 ; upper left row • mov cl,0 ; upper left column • mov dh,24 ; lower right row • mov dl,79 ; lower right column • mov bh,0F1h ; blank line's attribute • int 10h ; 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 ah,2 ; set cursor position • mov bh,0 ; on video page 0 • mov dx,0501h ; at row 5,column 1 • int 10h • getchar: • 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 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 ah,9 ; write character and attribute • mov al,0Ah ; ASCII character 0Ah • mov bh,0 ; video page 0 • mov bl,2 ; color (attribute) = green • mov cx,1 ; display it one time • int 10h 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. 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 cx,5 ; counter • mov ax,0 ; holds the sum • call Sum ; find sum of 5+4+3+2+1 • L1: mov ax,4C00h • int 21h • Main 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 call • L2: ret • Sum endp Kip Irvine: Assembly Language for Intel-Based Computers
Table 11. Stack Frame for the Sum Program 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. • main proc • 0000 mov ax,8 ; calculate 8! • 0003 push ax • 0004 call Factorial ; return value in AX • 0007 mov ax,4C00h • 000A int 21h • main endp Kip Irvine: Assembly Language for Intel-Based Computers
Example 9. The Factorial Procedure (2 of 2) • Factorial proc • 000C push bp • 000D mov bp,sp • 000F mov ax,[bp+4] ; get n • 0012 cmp ax,1 ; n <= 1? • 0015 ja L1 ; no: continue • 0017 mov ax,1 ; yes: return 1 • 001A jmp L2 • 001D L1: dec ax • 001E push ax ; Factorial(n‑1) • 001F call Factorial • 0022 mov bx,[bp+4] ; get n • 0025 mul bx ; ax = ax * bx • 0027 L2: pop bp • 0028 ret 2 ; AX = result • Factorial endp Kip Irvine: Assembly Language for Intel-Based Computers
Figure 8. Stack Frame, Factorial Program 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. Kip Irvine: Assembly Language for Intel-Based Computers