Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5: Procedures and Interrupts

Similar presentations


Presentation on theme: "Chapter 5: Procedures and Interrupts"— Presentation transcript:

1 Chapter 5: Procedures and Interrupts
Slides to Accompany Assembly Language for Intel-Based Computers, Third Edition Kip Irvine: Assembly Language for Intel-Based Computers

2 Kip Irvine: Assembly Language for Intel-Based Computers
Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS (INT 21h) Function Calls BIOS Keyboard Input (INT 16h) BIOS Video Control (INT 10h) Recursion Kip Irvine: Assembly Language for Intel-Based Computers

3 Kip Irvine: Assembly Language for Intel-Based Computers
PUSH Instruction push 0006h push 00A5h Kip Irvine: Assembly Language for Intel-Based Computers

4 Kip Irvine: Assembly Language for Intel-Based Computers
After pushing 0001 and 0002 Kip Irvine: Assembly Language for Intel-Based Computers

5 Before and After Popping from the Stack
pop AX ; now, AX=0002 Kip Irvine: Assembly Language for Intel-Based Computers

6 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

7 Example: Calling a Procedure
main proc mov mov ds,ax call MySub mov ax,4c00h ; returns to here int 21h main endp MySub proc ; control transfers here . ret MySub endp Kip Irvine: Assembly Language for Intel-Based Computers

8 Nested Procedure Calls (1)
main proc 000A call sub1 000C mov ax,... . main endp sub1 proc call sub2 ret sub1 endp sub2 proc . call sub3 ret sub2 endp sub3 proc ret sub3 endp Kip Irvine: Assembly Language for Intel-Based Computers

9 Nested Procedure Calls (2)
Kip Irvine: Assembly Language for Intel-Based Computers

10 Avoid Overlapping Procedures!
main proc . call subroutine1 subroutine1 proc main endp ret subroutine1 endp Kip Irvine: Assembly Language for Intel-Based Computers

11 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 100h .data char db ? sum dw ? array dw 100h,200h,300h,400h,500h array_size = ($-array)/(TYPE array) ; more... Kip Irvine: Assembly Language for Intel-Based Computers

12 Kip Irvine: Assembly Language for Intel-Based Computers
Procedure Calls (2) .code main proc mov ; set up the DS register mov ds,ax call inputChar ; input char into AL mov char,AL ; store in a variable ; Prepare to call the calcSum procedure. mov bx,offset array ; BX points to array mov cx,array_size ; CX = array count call calcSum ; calculate sum mov sum,ax ; store in a variable mov ax,4C00h ; return to DOS int 21h main endp Kip Irvine: Assembly Language for Intel-Based Computers

13 Kip Irvine: Assembly Language for Intel-Based Computers
Procedure Calls (3) ; input character from keyboard inputChar proc mov ah,1 ; DOS function #1: char input int 21h ; call DOS to do the work ret inputChar endp ; more... Kip Irvine: Assembly Language for Intel-Based Computers

14 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. calcSum proc push bx ; save BX, CX push cx mov ax,0 CS1: add ax,[bx] add bx, ; point to next integer loop CS ; repeat for array size pop cx ; restore BX, CX pop bx ret ; sum stored in AX calcSum endp Kip Irvine: Assembly Language for Intel-Based Computers

15 Calling a NEAR Procedure
main proc 0006: call sub1 0009: inc ax . main endp sub1 proc 0080: mov ax,1 . ret sub1 endp Kip Irvine: Assembly Language for Intel-Based Computers

16 Calling a FAR Procedure
main proc 2FC0:0006: call far ptr sub1 2FC0:0009: inc ax . main endp sub1 proc 3AB6:0080: mov ax,1 . ret sub1 endp Kip Irvine: Assembly Language for Intel-Based Computers

17 Preserving Local Registers (1)
It is common practice to save and restore any registers that a procedure plans to modify. Writeint proc push cx ; save registers that will change push bx push si . pop si ; restore the same registers pop bx ; (in reverse order) pop cx ret Writeint endp Kip Irvine: Assembly Language for Intel-Based Computers

18 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 aList L1: mov ax,[si] call Writeint add si,2 Loop L1 main endp Kip Irvine: Assembly Language for Intel-Based Computers

19 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

20 Interrupt Vectoring Process
Kip Irvine: Assembly Language for Intel-Based Computers

21 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 10h - video BIOS INT 14h - Serial I/O INT 16h - keyboard BIOS INT 17h - printer services INT 1Ah - Time of day INT 1Ch - User timer INT 21h - DOS services Kip Irvine: Assembly Language for Intel-Based Computers

22 DOS Function Calls (INT 21h)
The INT 21h instruction activates a DOS function call The function number (0-255) is placed in the AH register before invoking INT 21h Some functions require that you assign values to certain registers before invoking INT 21h Some functions return values in registers Kip Irvine: Assembly Language for Intel-Based Computers

23 Kip Irvine: Assembly Language for Intel-Based Computers
Simple Console I/O mov ah, ; single character input int 21h mov ah, ; single character output mov dl,'A' mov ah, ; string output mov dx,offset message Kip Irvine: Assembly Language for Intel-Based Computers

24 Kip Irvine: Assembly Language for Intel-Based Computers
INT 21h: Standard Input • 01h Filtered Input With Echo • 06h Direct Input Without Waiting • 07h Direct Input, No Ctrl-Break • 08h Direct Input with Ctrl-Break • 0Ah Buffered Input • 0Bh Get Input Status • 0Ch Clear Input Buffer, Invoke Input Function • 3Fh Read From File or Device Kip Irvine: Assembly Language for Intel-Based Computers

25 Comparison of Standard Input
Kip Irvine: Assembly Language for Intel-Based Computers

26 Keyboard Parameter Record (Function 0Ah)
Kip Irvine: Assembly Language for Intel-Based Computers

27 3Fh: Read from File or Device
When the user presses Enter at the end of the input, two bytes (0Dh,0Ah) 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,3Fh ; read from file/device mov bx, ; device = keyboard mov cx, ; request 127 bytes maximum mov dx,offset buffer int 21h ; AX = number chars typed + 2 Kip Irvine: Assembly Language for Intel-Based Computers

28 40h: Write to File or Device
buffer db 127 dup(0) count dw ? . mov ah,40h ; read from file/device mov bx, ; device = console mov cx,count ; number of chars to write mov dx,offset buffer int 21h Kip Irvine: Assembly Language for Intel-Based Computers

29 2Ah: Get Date, 2Bh: Set Date
mov ah,2Ah int 21h mov year,cx mov month,dh mov day,dl mov dayOfWeek,al mov ah,2Bh mov cx,year mov dh,month mov dl,day int 21h cmp al,0 jne badDate Requires administrator privileges under Windows NT. Kip Irvine: Assembly Language for Intel-Based Computers

30 2Ch: Get Time, 2Dh: Set Time
mov ah,2Dh mov ch,hours mov cl,minutes mov dh,seconds int 21h cmp al,0 jne badTime mov ah,2Ch int 21h mov hours,ch mov minutes,cl mov seconds,dh Requires administrator privileges under Windows NT. Kip Irvine: Assembly Language for Intel-Based Computers

31 INT 16h BIOS Keyboard Input
Kip Irvine: Assembly Language for Intel-Based Computers

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

33 Keyboard Input Using INT 16h
Use INT 16h to input any key, including function keys, arrow keys, and other extended keys. mov ah,10h ; wait for key int 16h ; AH=scan code, AL=ASCII code Kip Irvine: Assembly Language for Intel-Based Computers

34 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 L ; no: continue loop keyWaiting: mov scanCode,ah mov ASCIICode,al Kip Irvine: Assembly Language for Intel-Based Computers

35 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: Home 47h End 4Fh PgUp 49h PgDn 51h F1 function key 3Bh F2 function key 3Ch F3 function key 3Dh F4 function key 3Eh Kip Irvine: Assembly Language for Intel-Based Computers

36 Common ASCII Control Characters
Kip Irvine: Assembly Language for Intel-Based Computers

37 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

38 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

39 4-bit Foreground Colors
Kip Irvine: Assembly Language for Intel-Based Computers

40 4-bit Background Colors
Kip Irvine: Assembly Language for Intel-Based Computers

41 Table 9. Listing of INT 10h Functions (1 of 2)
Kip Irvine: Assembly Language for Intel-Based Computers

42 Table 9. Listing of INT 10h Functions (2 of 2)
Kip Irvine: Assembly Language for Intel-Based Computers

43 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, ; scroll window up mov al, ; scroll 5 lines mov ch, ; upper left row mov cl, ; upper left column mov dh, ; lower right row mov dl, ; lower right column mov bh, ; attribute for blank lines int 10h ; call BIOS Kip Irvine: Assembly Language for Intel-Based Computers

44 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, ; scroll window up mov al, ; entire window mov ch, ; upper left row mov cl, ; upper left column mov dh, ; lower right row mov dl, ; lower right column mov bh, ; attribute for blank lines int 10h ; call BIOS Kip Irvine: Assembly Language for Intel-Based Computers

45 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 ( ): mov ah, ; scroll window down mov al, ; scroll one row mov ch, ; upper left row mov cl, ; upper left column mov dh, ; lower right row mov dl, ; lower right column mov bh,0F1h ; blank line's attribute int 10h ; call BIOS Kip Irvine: Assembly Language for Intel-Based Computers

46 INT 10h (2h) Set Cursor Position, and
INT 10h (08h) Read Character and Attribute locate: mov ah, ; set cursor position mov bh, ; on video page 0 mov dx,0501h ; at row 5,column 1 int 10h getchar: mov ah, ; read char/attribute mov char,al ; save the character mov attrib,ah ; save the attribute Kip Irvine: Assembly Language for Intel-Based Computers

47 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, ; get cursor position mov bh,0 int 10h inc dl ; increment column mov ah, ; set cursor position popa ret AdvanceCursor endp Kip Irvine: Assembly Language for Intel-Based Computers

48 INT 10h (09h) Write Character and Attribute
This function does not advance the cursor, so you have to do that separately mov ah, ; write character and attribute mov al,0Ah ; ASCII character 0Ah mov bh, ; video page 0 mov bl,2 ; color (attribute) = green mov cx, ; display it one time int 10h Kip Irvine: Assembly Language for Intel-Based Computers

49 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, ; write character and attribute mov al,[si] ; character to display mov bh, ; video page 0 mov bl,color ; get the color mov cx, ; 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

50 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

51 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

52 Recursion Example: Sum of Integers
Main proc mov cx, ; counter mov ax, ; holds the sum call Sum ; find sum of L1: mov ax,4C00h int 21h Main endp Sum proc or cx,cx ; check counter value jz L ; 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

53 Table 11. Stack Frame for the Sum Program
Kip Irvine: Assembly Language for Intel-Based Computers

54 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 mov ax, ; calculate 8! push ax call Factorial ; return value in AX mov ax,4C00h 000A int 21h main endp Kip Irvine: Assembly Language for Intel-Based Computers

55 Example 9. The Factorial Procedure (2 of 2)
000C push bp 000D mov bp,sp 000F mov ax,[bp+4] ; get n cmp ax, ; n <= 1? ja L ; no: continue mov ax, ; yes: return 1 001A jmp L2 001D L1: dec ax 001E push ax ; Factorial(n‑1) 001F call Factorial mov bx,[bp+4] ; get n mul bx ; ax = ax * bx 0027 L2: pop bp ret ; AX = result Factorial endp Kip Irvine: Assembly Language for Intel-Based Computers

56 Figure 8. Stack Frame, Factorial Program
Kip Irvine: Assembly Language for Intel-Based Computers

57 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

58 Kip Irvine: Assembly Language for Intel-Based Computers
The End Kip Irvine: Assembly Language for Intel-Based Computers


Download ppt "Chapter 5: Procedures and Interrupts"

Similar presentations


Ads by Google