bdf30dec5b53a25ffa40be2fa7f08fff.ppt
- Количество слайдов: 58
Chapter 5: Procedures and Interrupts Slides to Accompany Assembly Language for Intel-Based Computers, Third Edition Kip Irvine: Assembly Language for Intel-Based Computers
Overview • • Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS (INT 21 h) Function Calls BIOS Keyboard Input (INT 16 h) BIOS Video Control (INT 10 h) Recursion Kip Irvine: Assembly Language for Intel-Based Computers
PUSH Instruction push 0006 h push 00 A 5 h Kip Irvine: Assembly Language for Intel-Based Computers
After pushing 0001 and 0002 Kip Irvine: Assembly Language for Intel-Based Computers
Before and After Popping from the Stack pop AX ; now, AX=0002 Kip Irvine: Assembly Language for Intel-Based Computers
Uses of the Stack • Save and restore registers • Save the return address when a CALL instruction is executed • Push parameters on the stack before calling a subroutine • Create local variables inside a procedure A procedure's stack frame includes passed parameters, the return address, and local variables. Kip Irvine: Assembly Language for Intel-Based Computers
Example: Calling a Procedure main proc mov ax, @data mov ds, ax call My. Sub mov ax, 4 c 00 h int 21 h main endp My. Sub proc. . ret My. Sub endp ; returns to here ; control transfers here Kip Irvine: Assembly Language for Intel-Based Computers
Nested Procedure Calls (1) main proc 000 A call sub 1 000 C mov ax, . . main endp 0050 sub 1 proc. call sub 2 ret sub 1 endp 0060 sub 2 proc. call sub 3 ret sub 2 endp sub 3 proc. . ret sub 3 endp Kip Irvine: Assembly Language for Intel-Based Computers
Nested Procedure Calls (2) Kip Irvine: Assembly Language for Intel-Based Computers
Avoid Overlapping Procedures! main proc. call subroutine 1 proc. main endp. . ret subroutine 1 endp Kip Irvine: Assembly Language for Intel-Based Computers
Procedure Calls (1) title Procedure Demonstration (SUBS. ASM) ; This program calls two procedures: one for ; keyboard input, another to add the elements ; in an array of integers. . model small. stack 100 h. data char db ? sum dw ? array dw 100 h, 200 h, 300 h, 400 h, 500 h array_size = ($-array)/(TYPE array) ; more. . . Kip Irvine: Assembly Language for Intel-Based Computers
Procedure Calls (2). code main proc mov ax, @data mov ds, ax call mov input. Char char, AL ; set up the DS register ; input char into AL ; store in a variable ; Prepare to call the calc. Sum procedure. mov call mov bx, offset array cx, array_size calc. Sum sum, ax mov ax, 4 C 00 h int 21 h main endp ; ; BX points to array CX = array count calculate sum store in a variable ; return to DOS Kip Irvine: Assembly Language for Intel-Based Computers
Procedure Calls (3) ; input character from keyboard input. Char mov int ret input. Char proc ah, 1 21 h ; DOS function #1: char input ; call DOS to do the work endp ; more. . . Kip Irvine: Assembly Language for Intel-Based Computers
Procedure Calls (4) ; Calculate the sum of an array of integers. ; Input: BX points to the array and CX contains ; the array size. Returns the SUM in AX. calc. Sum proc push bx push cx mov ax, 0 CS 1: add ax, [bx] add bx, 2 loop CS 1 pop cx pop bx ret calc. Sum endp ; save BX, CX ; point to next integer ; repeat for array size ; restore BX, CX ; sum stored in AX Kip Irvine: Assembly Language for Intel-Based Computers
Calling a NEAR Procedure main proc 0006: call sub 1 0009: inc ax. main endp sub 1 proc 0080: mov ax, 1. ret sub 1 endp Kip Irvine: Assembly Language for Intel-Based Computers
Calling a FAR Procedure main proc 2 FC 0: 0006: call far ptr sub 1 2 FC 0: 0009: inc ax. . main endp sub 1 proc 3 AB 6: 0080: mov ax, 1. ret sub 1 endp Kip Irvine: Assembly Language for Intel-Based Computers
Preserving Local Registers (1) It is common practice to save and restore any registers that a procedure plans to modify. Writeint push. . pop pop ret Writeint proc cx ; save registers that will change bx si si bx cx ; restore the same registers ; (in reverse order) endp Kip Irvine: Assembly Language for Intel-Based Computers
Preserving Local Registers (2) What would happen to the following program if Writeint did not preserve CX, BX, and SI? main proc. . . mov cx, LIST_COUNT mov bx, DECIMAL_RADIX mov si, offset a. List L 1: mov ax, [si] call Writeint add si, 2 Loop L 1. . . main endp Kip Irvine: Assembly Language for Intel-Based Computers
Interrupts • Hardware interrupts – occur as a response to a hardware device – routed through the Intel 8259 Interrupt Controller • Software interrupts – calls to operating system functions, located in BIOS and resident portion of DOS – activated by the INT instruction Kip Irvine: Assembly Language for Intel-Based Computers
Interrupt Vectoring Process Kip Irvine: Assembly Language for Intel-Based Computers
INT Instruction • The INT instruction is always followed by a hexadecimal number that identifies its type • Common examples: – – – – INT 10 h- video BIOS INT 14 h- Serial I/O INT 16 h- keyboard BIOS INT 17 h- printer services INT 1 Ah - Time of day INT 1 Ch - User timer INT 21 h- DOS services Kip Irvine: Assembly Language for Intel-Based Computers
DOS Function Calls (INT 21 h) • The INT 21 h instruction activates a DOS function call • The function number (0 -255) is placed in the AH register before invoking INT 21 h • Some functions require that you assign values to certain registers before invoking INT 21 h • Some functions return values in registers Kip Irvine: Assembly Language for Intel-Based Computers
Simple Console I/O mov int ah, 1 21 h ; single character input mov int ah, 2 dl, 'A' 21 h ; single character output mov int ah, 9 dx, offset message 21 h ; string output Kip Irvine: Assembly Language for Intel-Based Computers
INT 21 h: Standard Input • • 01 h 06 h 07 h 08 h 0 Ah 0 Bh 0 Ch 3 Fh Filtered Input With Echo Direct Input Without Waiting Direct Input, No Ctrl-Break Direct Input with Ctrl-Break Buffered Input Get Input Status Clear Input Buffer, Invoke Input Function Read From File or Device Kip Irvine: Assembly Language for Intel-Based Computers
Comparison of Standard Input Kip Irvine: Assembly Language for Intel-Based Computers
Keyboard Parameter Record (Function 0 Ah) Kip Irvine: Assembly Language for Intel-Based Computers
3 Fh: Read from File or Device When the user presses Enter at the end of the input, two bytes (0 Dh, 0 Ah) are appended to the string in the input buffer and the count in AX includes the extra characters. buffer db 127 dup(0). . mov ah, 3 Fh mov bx, 0 mov cx, 127 mov dx, offset buffer int 21 h ; read from file/device ; device = keyboard ; request 127 bytes maximum ; AX = number chars typed + 2 Kip Irvine: Assembly Language for Intel-Based Computers
40 h: Write to File or Device buffer db 127 dup(0) count dw ? . mov ah, 40 h mov bx, 1 mov cx, count mov dx, offset buffer int 21 h ; read from file/device ; device = console ; number of chars to write Kip Irvine: Assembly Language for Intel-Based Computers
2 Ah: Get Date, 2 Bh: Set Date mov int mov mov ah, 2 Ah 21 h year, cx month, dh day, dl day. Of. Week, al mov mov int cmp jne ah, 2 Bh cx, year dh, month dl, day 21 h al, 0 bad. Date Requires administrator privileges under Windows NT. Kip Irvine: Assembly Language for Intel-Based Computers
2 Ch: Get Time, 2 Dh: Set Time mov int mov mov ah, 2 Ch 21 h hours, ch minutes, cl seconds, dh mov mov int cmp jne ah, 2 Dh ch, hours cl, minutes dh, seconds 21 h al, 0 bad. Time Requires administrator privileges under Windows NT. Kip Irvine: Assembly Language for Intel-Based Computers
INT 16 h BIOS Keyboard Input Kip Irvine: Assembly Language for Intel-Based Computers
Keyboard Status Byte Kip Irvine: Assembly Language for Intel-Based Computers
Keyboard Input Using INT 16 h Use INT 16 h to input any key, including function keys, arrow keys, and other extended keys. mov int ah, 10 h 16 h ; wait for key ; AH=scan code, AL=ASCII code Kip Irvine: Assembly Language for Intel-Based Computers
Keyboard Input Using INT 16 h function 11 h detects the presence of a key in the keyboard typeahead buffer. The following loop uses a conditional jump (JNZ), explained in Chapter 6. L 1: mov int jnz jmp ah, 11 h 16 h key. Waiting L 1 ; key waiting? ; yes: process it ; no: continue loop key. Waiting: mov scan. Code, 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: F 1 F 2 F 3 F 4 function key key 3 Bh 3 Ch 3 Dh 3 Eh Home End Pg. Up Pg. Dn 47 h 4 Fh 49 h 51 h 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 10 h Functions (1 of 2) Kip Irvine: Assembly Language for Intel-Based Computers
Table 9. Listing of INT 10 h Functions (2 of 2) Kip Irvine: Assembly Language for Intel-Based Computers
INT 10 h (06 h) 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 int ah, 6 al, 5 ch, 0 cl, 0 dh, 24 dl, 79 bh, 7 10 h ; ; ; ; 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 int ah, 6 al, 0 ch, 0 cl, 0 dh, 24 dl, 79 bh, 7 10 h ; ; ; ; 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 10 h (07 h) 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 int ah, 7 al, 1 ch, 0 cl, 0 dh, 24 dl, 79 bh, 0 F 1 h 10 h ; ; ; ; scroll window down scroll one 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 10 h (2 h) Set Cursor Position, and INT 10 h (08 h) Read Character and Attribute locate: mov mov int getchar: mov int mov ah, 2 bh, 0 dx, 0501 h 10 h ; set cursor position ; on video page 0 ; at row 5, column 1 ah, 8 bh, 0 10 h 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. Advance. Cursor proc pusha mov ah, 3 ; get cursor position mov bh, 0 int 10 h inc dl ; increment column mov ah, 2 ; set cursor position int 10 h popa ret Advance. Cursor endp Kip Irvine: Assembly Language for Intel-Based Computers
INT 10 h (09 h) Write Character and Attribute This function does not advance the cursor, so you have to do that separately mov mov mov int ah, 9 al, 0 Ah bh, 0 bl, 2 cx, 1 10 h ; ; ; write character and attribute ASCII character 0 Ah 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 L 1: 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 10 h call Advance. Cursor inc color ; next color inc si ; next character position pop cx ; restore loop counter Loop L 1 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 Code. View, 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 call L 1: mov int Main endp Sum proc or jz add dec call L 2: ret Sum endp cx, 5 ax, 0 Sum ax, 4 C 00 h 21 h ; counter ; holds the sum ; find sum of 5+4+3+2+1 cx, cx L 2 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 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 000 A main proc mov ax, 8 ; calculate 8! push ax call Factorial ; return value in AX mov ax, 4 C 00 h int 21 h main endp Kip Irvine: Assembly Language for Intel-Based Computers
Example 9. The Factorial Procedure (2 of 2) 000 C 000 D 000 F 0012 0015 0017 001 A 001 D 001 E 001 F 0022 0025 0027 0028 Factorial proc push bp mov bp, sp mov ax, [bp+4] cmp ax, 1 ja L 1 mov ax, 1 jmp L 2 L 1: dec ax push ax call Factorial mov bx, [bp+4] mul bx L 2: 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 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
The End Kip Irvine: Assembly Language for Intel-Based Computers


