chapter 5: procedures and interrupts
Post on 13-Jan-2016
38 Views
Preview:
DESCRIPTION
TRANSCRIPT
Kip Irvine: Assembly Language for Intel-Based Computers
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 21h) Function Calls• BIOS Keyboard Input (INT 16h)• BIOS Video Control (INT 10h)• Recursion
Kip Irvine: Assembly Language for Intel-Based Computers
0006 0006
00A5
SP
SP
(low memory) (low memory)
(high memory) (high memory)
BEFORE AFTER
push 0006h
push 00A5h
PUSH Instruction
Kip Irvine: Assembly Language for Intel-Based Computers
0006
00A5
0001
0002 SP
(low memory)
(high memory)
New Contents ofthe stack after
pushing 0001 and0002:
After pushing 0001 and 0002
Kip Irvine: Assembly Language for Intel-Based Computers
BEFORE AFTER
0006
00A5
0001
0002 SP
(low memory)
(high memory)
0006
00A5
0001 SP
(low memory)
(high memory)
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
main proc mov ax,@data mov ds,ax call MySub mov ax,4c00h ; returns to here int 21hmain endp
MySub proc . ; control transfers here . retMySub endp
Example: Calling a Procedure
Kip Irvine: Assembly Language for Intel-Based Computers
main proc000A call sub1 000C mov ax,... . main endp
sub1 proc . call sub2 0050 ret sub1 endp
sub2 proc . call sub3 0060 ret sub2 endp
sub3 proc . . ret sub3 endp
Nested Procedure Calls (1)
Kip Irvine: Assembly Language for Intel-Based Computers
Nested Procedure Calls (2)
000C
0050
0060 SP
(low memory)
(high memory)
Ret addr of first procedure call
Ret addr of second procedure call
Ret addr of third procedure call
Kip Irvine: Assembly Language for Intel-Based Computers
main proc . call subroutine1 .subroutine1 proc .main endp . . retsubroutine1 endp
Avoid Overlapping Procedures!
Kip Irvine: Assembly Language for Intel-Based Computers
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
.datachar db ?sum dw ? array dw 100h,200h,300h,400h,500harray_size = ($array)/(TYPE array)
; more...
Procedure Calls (1)
Kip Irvine: Assembly Language for Intel-Based Computers
.codemain proc mov ax,@data ; 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 21hmain endp
Procedure Calls (2)
Kip Irvine: Assembly Language for Intel-Based Computers
; input character from keyboard
inputChar proc mov ah,1 ; DOS function #1: char input int 21h ; call DOS to do the work retinputChar endp
; more...
Procedure Calls (3)
Kip Irvine: Assembly Language for Intel-Based Computers
; 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,0CS1: add ax,[bx] add bx,2 ; point to next integer loop CS1 ; repeat for array size pop cx ; restore BX, CX pop bx ret ; sum stored in AXcalcSum endp
Procedure Calls (4)
Kip Irvine: Assembly Language for Intel-Based Computers
0000
0009
0000
00090009 pushedon the stack
0009 poppedinto IP
STACK STACK
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
0000
2FC0
0009
0000
2FC0
0009
CS and IPare pushedon thestack.
The returnsegment andoffset values arepopped into CSand IP.
STACK STACK
Calling a FAR Procedure
main proc2FC0:0006: call far ptr sub12FC0:0009: inc ax . . main endp
sub1 proc3AB6:0080: mov ax,1 . ret sub1 endp sub1 endp
Kip Irvine: Assembly Language for Intel-Based Computers
Preserving Local Registers (1)
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 retWriteint endp
It is common practice to save and restore any registers that a procedure plans to modify.
Kip Irvine: Assembly Language for Intel-Based Computers
Preserving Local Registers (2)
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
What would happen to the following program if Writeint did not preserve CX,BX, and SI?
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
mov...int 10hadd...
F000:F0653069 F000:AB62
return to callingprogram
F000:F065 F066 F067 F068 . .
sti cld push es . . . IRET
1 2 3
Calling program
(entry for INT 10)
Interrupt Vector Table
Interrupt Handler
4
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
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
Simple Console I/O
mov ah,1 ; single character input int 21h mov ah,2 ; single character output mov dl,'A' int 21h mov ah,9 ; string output mov dx,offset message int 21h
Kip Irvine: Assembly Language for Intel-Based Computers
• 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
INT 21h: Standard Input
Kip Irvine: Assembly Language for Intel-Based Computers
DOS Function Number 1 6 7 8
Waits for keystroke? Y N Y Y
Echoes character? Y N N N
Ctrl-Break recognized? Y N N Y
Filters control characters? Y N N N
Comparison of Standard Input
Kip Irvine: Assembly Language for Intel-Based Computers
0 1 2 3 4 . . . . . . . . n + 1
n m
numbers ofcharactersactually input
maximum numbersof charactersallowed
Offset:
input bufferarea
4D
4B
79
69 70
20 6E
20
61 6D 65 20 69 73 20
49 7672 69 6E 65 0D
My name is
Kip Irvine
20 15
max_keys chars_input
Enter key
buffer
Keyboard Parameter Record (Function 0Ah)
Kip Irvine: Assembly Language for Intel-Based Computers
buffer db 127 dup(0)..mov ah,3Fh ; read from file/devicemov bx,0 ; device = keyboardmov cx,127 ; request 127 bytes maximummov dx,offset bufferint 21h ; AX = number chars typed + 2
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.
3Fh: Read from File or Device
Kip Irvine: Assembly Language for Intel-Based Computers
buffer db 127 dup(0)count dw ?.mov ah,40h ; read from file/devicemov bx,1 ; device = consolemov cx,count ; number of chars to writemov dx,offset buffer int 21h
40h: Write to File or Device
Kip Irvine: Assembly Language for Intel-Based Computers
mov ah,2Ahint 21hmov year,cxmov month,dhmov day,dlmov dayOfWeek,al
mov ah,2Bhmov cx,yearmov dh,monthmov dl,dayint 21hcmp al,0jne badDate
Requires administrator privileges under Windows NT.
2Ah: Get Date, 2Bh: Set Date
Kip Irvine: Assembly Language for Intel-Based Computers
mov ah,2Chint 21hmov hours,chmov minutes,clmov seconds,dh
mov ah,2Dhmov ch,hoursmov cl,minutesmov dh,secondsint 21hcmp al,0jne badTime
Requires administrator privileges under Windows NT.
2Ch: Get Time, 2Dh: Set Time
Kip Irvine: Assembly Language for Intel-Based Computers
AH Description
03h
Set Typematic Repeat Rate. Call with AH = 3, AL = 5, BH = repeat delay, BL= repeat rate. The delay values in BH are: ( 0 = 250 ms; 1 = 500 ms; 2 = 750ms; 3 = 1000ms). The repeat rate in BL varies from 0 (fastest) to 1Fh(slowest).
05h
Push Key into Buffer. Pushes a keyboard character and corresponding scancode into the keyboard typeahead buffer. Call with AH = 5, CH = scan code,and CL = character code. If the typeahead buffer is already full, the Carryflag will be set, and AL = 1.
10hWait for Key. If a keystroke is waiting, its scan code is returned in AH and itscharacter code is returned in AL. If no key is waiting, the routine waits in aloop for a key to be pressed.
11h
Check Keyboard Buffer. Examines the keyboard typeahead buffer to see if akey is waiting; if one is, this function returns the scan code in AH and thecharacter code in AL, and clears the Zero flag. Otherwise, ZF = 1. Thekeystroke is not removed from the buffer.
12h Get Keyboard Flags. Shows the keyboard status byte, which is bit mapped.See Figure 5.
INT 16h BIOS Keyboard Input
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
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
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.
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
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
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
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
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
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
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)
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)
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.
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.
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):
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
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.
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
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
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.
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+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
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)
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.
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
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)
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.
Kip Irvine: Assembly Language for Intel-Based Computers
The End
top related