microprocessor-10

advertisement
Instruction System Procedure & System Routine
计算机学院 李征
Tel:13882153765
Email:lizheng@cs.scu.edu.cn
OICQ: 1340915
Procedure & System Routine
Procedure: A software module which is with
independent function and can be used
repeatedly.
The usage of procedure must include calling
and returning operation.
System Routine: A procedure which perform
some system functions and can be triggered
by an interrupt request.
Procedure Calling
Direct Calling:
CALL PROC_NAME
Near Direct Calling:
If PROC_NAME is in the same segment with
‘CALL’.
SP <= (SP)-2
(SP) <= (IP)
IP <= Offset of PROC_NAME
Procedure Calling
Far Direct Calling:
If PROC_NAME is not in the same segment with
‘CALL’.
SP <= (SP)-2
(SP) <= (CS)
SP <= (SP)-2
(SP) <= (IP)
CS <= Segment of PROC_NAME
IP <= Offset of PROC_NAME
Procedure Calling
Indirect Calling
Near Indirect Calling:
CALL register or memory cell
Example:
CALL BX
CALL [BX]
Procedure Calling
Far Indirect Calling:
CALL DWORD PTR memory cell
Example:
CALL DWORD PTR [BX+100H]
Procedure Returning
Near Returning:
RETN
IP <= ((SP))
SP <= (SP)+2
Procedure Returning
Far Returning:
RETF
IP <= ((SP))
SP <= (SP)+2
CS <= ((SP))
SP <= (SP)+2
Procedure Returning
PROC_NAME PROC (FAR or NEAR)
……
RET
; If this is a near procedure, ‘RET’ will be
; assembled to ‘RETN’. Or else, it will be
; assembled to ‘RETF’.
PROC_NAME ENDP
Procedure Returning
Returning with parameter clearing:
RET
N;
N must be an even integer.
IP <= ((SP))
SP <= (SP)+2
CS <= ((SP)) ; if far
SP <= (SP)+2 ; if far
SP <= (SP)+N
This instruction clears N / 2 parameters which is in stack
and transferred from calling program.
Stack Operation in Near Calling
and Returning
Example:
Assume A is calling program, B, C, and D
are near procedures.
When executing, A calls B, B calls C, and
C calls D.
Stack Operation in Near Calling
and Returning
C的返回点
B的返回点
SP
A的返回点
Notes for Procedure Design
Notes for Procedure Design:
1) ‘CALL’ and ‘RET’ instructions must match.
2) CPU execution scene must be preserved in
procedure.
3) Parameter transfer agreement must be kept
by both calling program and procedure.
Parameter transfer in Procedure
Three parameter transfer methods:
1) Transfer with Register
2) Transfer with Stack
3) Transfer with Parameter Region in Data
Segment
Parameter transfer in Procedure
Example of parameter transfer with stack:
Procedure Function: Transfer a given 8-bit
or 16-bit binary to an ASCII string
composed of ‘1’ and ‘0’. Preserve the
string at given position in memory.
Parameter transfer in Procedure
Input Parameter:
1) Binary given by calling program
2) Bit number of binary given by calling program
3) Memory address given by calling program for
string storage
Output Parameter:
None
Parameter transfer in Procedure
C function:
void Transfer_Bin(BYTE* lp_Data, int
Bit_Num, char* lp_Res)
Parameter transfer in Procedure
DATA SEGMENT
BIN8
DB 57H
BIN16
DB 47bcH
ASCBUF DB 20H DUP(20H)
DATA ENDS
STACK1
STACK1
SEGMENT STACK
DW
40H DUP(0)
ENDS
Parameter transfer in Procedure
CODE
SEGMENT
ASSUME CS:CODE,DS:DATA,SS:STACK1
MAIN:
MOV
AX, DATA
MOV
DS, AX
;Transfer Given 8-bit Binary
MOV
AX, 0
MOV
AL, BIN8
PUSH AX
; Transfer Bit Number
MOV
AX, 8
PUSH AX
Parameter transfer in Procedure
; Transfer given string address
LEA
AX, ASCBUF
PUSH
AX
CALL
BITASC ; call procedure
; The second calling
MOV
AX, BIN16
PUSH
AX
MOV
AX, 16
PUSH
AX
Parameter transfer in Procedure
LEA
PUSH
CALL
MOV
INT
AX, ASCBUF+10H
AX
BITASC
AH, 4CH
21H
Parameter transfer in Procedure
;Procedure
BITASC
PROC
; Use BP to extract parameter,
; not SP
MOV BP, SP
; Preserve CPU execution scene
PUSH AX
PUSH CX
PUSH DX
PUSH DI
Parameter transfer in Procedure
MOV
MOV
MOV
CMP
JNE
MOV
DI, [BP+2]
CX, [BP+4]
DX, [BP+6]
CX, 8
TRANS
DH, DL
Parameter transfer in Procedure
TRANS:
ROL DX, 1 ; Transform each bit
MOV AL, DL
AND AL,1
ADD AL, 30H
MOV [DI], AL
INC DI
LOOP TRANS
; Restore CPU execution scene
POP DI
POP DX
Parameter transfer in Procedure
BITASC
CODE
POP
CX
POP
AX
; Return and clear parameters
RET
6
ENDP
ENDS
END
MAIN
Parameter transfer in Procedure
If there are output parameters, they can be
put to stack by procedure.
Generally, output parameters replace input
parameters in stack.
After returning, calling program extract
output parameters from stack top.
System Routine
Most system routines are used for I/O
interaction between CPU and interface or
some system operations.
System routines can be triggered by
interrupt request, and can be called by
program.
System Routine
If system routine is called in program, ‘INT’
instruction is used.
INT
N
N is the routine number, not the procedure
name (beginning address).
System Routine
Execution of ‘INT’:
1) SP <= (SP)-2
2) (SP) <= (FR)
3) SP <= (SP)-2
4) (SP) <= (CS)
5) SP <= (SP)-2
6) (SP) <= (IP)
7) IF <=0, TF <=0
8) Transform N to beginning address of routine
9) CS <= Segment of beginning address
10) IP <= Offset of beginning address
System Routine
Routine1 PROC
……
IRET
Routine1 ENDP
‘IRET’ used in system routine, rather than
‘RET’.
System Routine
Execution of ‘IRET’:
1) IP <= ((SP))
2) SP <= (SP)+2
3) CS <= ((SP))
4) SP <= (SP)+2
5) FR <= ((SP))
6) SP <= (SP)+2
System Routine
Application designers can perform I/O
operation of I/O device by calling system
routines.
System routines are designed by system
designers.
Then, most application designers can
focus on the application design.
System Routine
Application
DOS Routine
BIOS Routine
System Routine
INT 21H:DOS Routines
INT 16H:BIOS Routines for keyboard
INT 10H:BIOS Routines for display
INT 13H:BIOS Routines for disk
Parameter Transfer in System
Routine
Most system Routines use registers for
parameter transferring.
One must know parameter transfer agreement of
a routine before one can use it.
Original data of other registers before calling is
protected as CPU execution scene by routines.
System Routine
Example 1:
; transfer sub-function number
MOV AH,01H
INT
21H
This routine waits a keyboard input, and return
the ASCII code of input key to register AL.
System Routine
Example 2:
; transfer ASCII code
MOV DL, ‘A’
; transfer sub-function number
MOV AH, 02H
INT
21H
This routine outputs the given character at
current cursor position in screen, and then
adjust cursor position.
System Routine
Example 3:
; transfer sub-function number
MOV AH, 4CH
INT
21H
This routine return CPU controlling to DOS
(operating system).
Download