Overview

advertisement
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
PUSH Instruction
BEFORE
AFTER
(high memory)
(high memory)
0006
push 0006h
push 00A5h
SP
0006
00A5
(low memory)
(low memory)
Kip Irvine: Assembly Language for Intel-Based Computers
SP
After pushing 0001 and 0002
(high memory)
New Contents of
the stack after
pushing 0001 and
0002:
0006
00A5
0001
0002
SP
(low memory)
Kip Irvine: Assembly Language for Intel-Based Computers
Before and After Popping from the Stack
pop
AX
; now, AX=0002
BEFORE
AFTER
(high memory)
(high memory)
0006
0006
00A5
00A5
0001
0001
0002
(low memory)
SP
(low memory)
Kip Irvine: Assembly Language for Intel-Based Computers
SP
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 MySub
mov ax,4c00h
int 21h
main endp
MySub proc
.
.
ret
MySub endp
; returns to here
; control transfers here
Kip Irvine: Assembly Language for Intel-Based Computers
Nested Procedure Calls (1)
main proc
000A
call sub1
000C
mov ax,...
.
main endp
0050
sub1 proc
.
call sub2
ret
sub1 endp
0060
sub2 proc
.
call sub3
ret
sub2 endp
sub3 proc
.
.
ret
sub3 endp
Kip Irvine: Assembly Language for Intel-Based Computers
Nested Procedure Calls (2)
(high memory)
Ret addr of first procedure call
000C
Ret addr of second procedure call
0050
Ret addr of third procedure call
0060
(low memory)
Kip Irvine: Assembly Language for Intel-Based Computers
SP
Avoid Overlapping Procedures!
main proc
.
call subroutine1
.
subroutine1 proc
.
main endp
.
.
ret
subroutine1 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 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
Procedure Calls (2)
.code
main proc
mov
ax,@data
mov
ds,ax
call
mov
inputChar
char,AL
; set up the DS register
; input char into AL
; store in a variable
; Prepare to call the calcSum procedure.
mov
mov
call
mov
bx,offset array
cx,array_size
calcSum
sum,ax
mov
ax,4C00h
int
21h
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
inputChar
mov
int
ret
inputChar
proc
ah,1
21h
; 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.
calcSum proc
push bx
push cx
mov
ax,0
CS1: add
ax,[bx]
add
bx,2
loop CS1
pop
cx
pop
bx
ret
calcSum 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 sub1
0009:
inc ax
.
main endp
sub1 proc
0080: mov ax,1
.
ret
sub1 endp
STACK
STACK
0000
0000
0009
0009 pushed
on the stack
0009
0009 popped
into IP
Kip Irvine: Assembly Language for Intel-Based Computers
Calling a FAR Procedure
main proc
2FC0:0006: call far ptr sub1
2FC0:0009: inc ax
.
.
main endp
STACK
0000
2FC0
0009
sub1 proc
3AB6:0080:
mov ax,1
.
ret
sub1 endp
sub1 endp
STACK
CS and IP
are pushed
on the
stack.
0000
2FC0
0009
The return
segment and
offset values are
popped into CS
and IP.
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
push
push
.
.
pop
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 aList
L1: mov ax,[si]
call Writeint
add si,2
Loop L1
...
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
Interrupt Handler
Calling program
mov...
int 10h
add...
1
3069
F000:F065
F000:F065
F066
3 F067
F068
.
.
2
sti
cld
push es
.
.
.
IRET
F000:AB62
(entry for INT 10)
Interrupt Vector Table
return to calling
program
Kip Irvine: Assembly Language for Intel-Based Computers
4
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
int
ah,1
21h
; single character input
mov
mov
int
ah,2
dl,'A'
21h
; single character output
mov
mov
int
ah,9
dx,offset message
21h
; string output
Kip Irvine: Assembly Language for Intel-Based Computers
INT 21h: Standard Input
•
•
•
•
•
•
•
•
01h
06h
07h
08h
0Ah
0Bh
0Ch
3Fh
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
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
Kip Irvine: Assembly Language for Intel-Based Computers
Related documents
Download