实验三 循环和子程序设计
1、掌握子程序、宏和中断程序的设计方法;
2、熟悉在PC机上建立、汇编、连接、调试和运行8086/8088汇编语言程序的过程。
1. 编写一个子程序计算z=f(x,y)=x/y+x-y(x,y,z有符号数内存字数)。要求通过堆栈传送输入参数,输出参数通过AX传递。(要求输入输出实现,且有提示)。
.MODEL SMALL
.STACK
.DATA
INPUTX BYTE 'INSERT THE X:', 13, 10, '$'
INPUTY BYTE 'INSERT THE Y:', 13, 10, '$'
OUTPUT BYTE 'RESULT IS:', 13, 10, '$'
.CODE
.STARTUP
LEA DX, INPUTX
MOV AH, 0AH
INT 21H
MOV AH, 1
INT 21H
AND AX, 0FH
PUSH AX
LEA DX, INPUTY
MOV AH, 0AH
INT 21H
MOV AH, 1
INT 21H
AND AX, 0FH
PUSH AX
CALL COUNT
MOV CX, 4
MOV DX, AX
S:
MOV DX, AX
AND AX, 0FH
ADD AL, 48
PUSH CX
MOV CL, 4
SHR DX, CL
POP CX
MOV AH, 9
INT 21H
LOOP S
.EXIT 0
COUNT PROC
PUSH BP
MOV BP, SP
PUSH AX
PUSH BX
PUSH CX
PUSH DX
MOV BX, [BP+4]
MOV AX, [BP+6]
MOV CX, AX
CWD
IDIV BX
ADD AX, CX
SUB AX, BX
POP DX
POP CX
POP BX
POP AX
POP BP
RET 4
COUNT ENDP
END
2. 编写一个宏,求三个数的最大数,原型为:MAX3 x,y,z,max,最大值要求输出。
MAX3 MACRO X, Y, Z, MAX
PUSH AX
MOV AX, X
.IF SWORD PTR AX < Y
MOV AX, Y
.ENDIF
.IF SWORD PTR AX < Z
MOV AX, Z
.ENDIF
MOV MAX, AX
POP AX
ENDM
.MODEL SMALL
.STACK
.DATA
X DW ?
Y DW ?
Z DW ?
MAX DW ?
.CODE
.STARTUP
MAX3 X, Y, Z, MAX
.EXIT 0
END
3. 挂接1CH中断,正计时90秒后退出。要求屏幕显示0-89的秒数。
.MODEL SMALL
.STACK
.DATA
INTNO EQU 1CH
OLDISR WORD ?, ?
TIMER BYTE 100
COUNTER WORD 0
ISDONE BYTE 0
.CODE
.STARTUP
MOV AX, 0
MOV ES, AX
CLI
MOV AX, ES:[4 * INTNO]
MOV OLDISR[0], AX
MOV AX, ES:[4 * INTNO + 2]
MOV OLDISR[2], AX
STI
CLI
MOV ES:[4 * INTNO], OFFSET ISR
MOV ES:[4 * INTNO + 2], SEG ISR
STI
WAITHERE:
CMP ISDONE, 1
JNZ WAITHERE
CLI
MOV AX, OLDISR[0]
MOV ES:[INTNO * 4], AX
MOV AX, OLDISR[2]
MOV ES:[INTNO * 4 + 2], AX
STI
.EXIT 0
ISR PROC FAR
PUSH DX
PUSH AX
STI
INC TIMER
AGAIN:
CMP TIMER, 1000 / 55
JB DONE
MOV TIMER, 0
MOV AH, 2
MOV DL, 13
INT 21H
MOV AX, COUNTER
MOV DL, 10
DIV DL
MOV DH, AH
MOV DL, AL
MOV AH, 2
ADD DL, 30H
INT 21H
MOV DL, DH
ADD DL, 30H
INT 21H
INC COUNTER
CMP COUNTER, 90
JL DONE
MOV ISDONE, 1
DONE:
PUSHF
CALL DWORD PTR OLDISR
CLI
POP AX
POP DX
IRET
ISR ENDP
END