宏指令设计实验
1、下面是按要求实现的源程序,现要求将源程序中的重复部分指令改用宏指令实现。
从键盘输入一字符,判断该字符是小写字母、大写字母、数字还是其它字符。若输入为小写字母,则显示“You Input a Lowercase Letter!”; 若输入为大写字母,则显示“You Input a Uppercase Letter!”; 若输入为数字,则显示“You Input a Digit!”; 若输入为其它字符,则显示“You Input Other Letter!”。
-
data SEGMENT
-
INfor1 DB 0ah,0dh,'please press any key to input a letter:$'
-
INfor2 DB 0ah,0dh,'you input a lowetcase letter!$'
-
INfor3 DB 0ah,0dh,'you input a uppercase letter!$'
-
INfor4 DB 0ah,0dh,'you input a digit!$'
-
INfor5 DB 0ah,0dh,'you input a other letter!$'
-
data ENDS
-
code SEGMENT
-
ASSUME CS:code,DS:data
-
start: MOV AX,data
-
MOV DS,AX
-
MOV DX,OFFSET INfor1
-
MOV AH,09
-
INT 21h
-
MOV AH,01h
-
INT 21h
-
CMP AL,'0'
-
JB other
-
CMP AL,'9'
-
JBE DIgit
-
CMP AL,'A'
-
JB other
-
CMP AL,'Z'
-
JBE upper
-
CMP AL,'a'
-
JB other
-
CMP AL,'z'
-
JBE LOWer
-
JMP pend
-
LOWer: MOV DX,OFFSET INfor2
-
MOV AH,09h
-
INT 21h
-
JMP pend
-
upper: MOV DX,OFFSET INfor3
-
MOV AH,09h
-
INT 21h
-
JMP pend
-
DIgit: MOV DX,OFFSET INfor4
-
MOV AH,09h
-
INT 21h
-
JMP pend
-
other: MOV DX,OFFSET INfor5
-
MOV AH,09h
-
INT 21h
-
-
pend: ;MOV AH,01
-
;INT 21h
-
-
MOV AH,4cH
-
INT 21h
-
code ENDS
-
END START
改用宏指令实现:
-
INPUT MACRO
-
MOV AH,01H
-
INT 21H
-
ENDM
-
OUTPUT MACRO XX
-
MOV DX,OFFSET INFOR&XX
-
MOV AH,09H
-
INT 21H
-
ENDM
-
DATAS SEGMENT
-
INFOR1 DB 0AH,0DH,'please press any key to input a letter:$'
-
INFOR2 DB 0AH,0DH,'you input a lowercase letter!$'
-
INFOR3 DB 0AH,0DH,'you input a uppercase letter!$'
-
INFOR4 DB 0AH,0DH,'you input a digit!$'
-
INFOR5 DB 0AH,0DH,'you input a other letter!$'
-
DATAS ENDS
-
-
CODES SEGMENT
-
ASSUME CS:CODES,DS:DATAS
-
START:
-
MOV AX,DATAS
-
MOV DS,AX
-
-
OUTPUT 1
-
-
INPUT
-
-
CMP AL,'0'
-
JB OTHER
-
-
CMP AL,'9'
-
JBE DIGIT
-
-
CMP AL,'A'
-
JB OTHER
-
-
CMP AL,'Z'
-
JBE UPPER
-
-
CMP AL,'a'
-
JB OTHER
-
-
CMP AL,'z'
-
JBE LOWER
-
-
JMP OTHER
-
-
LOWER:
-
OUTPUT 2
-
JMP RESULT
-
-
UPPER:
-
OUTPUT 3
-
JMP RESULT
-
-
DIGIT:
-
OUTPUT 4
-
JMP RESULT
-
-
OTHER:
-
OUTPUT 5
-
-
RESULT:
-
MOV AH,4CH
-
INT 21H
-
CODES ENDS
-
END START
2、编程实现:找出下面数据段所定义的20个数中的最大数,并将找到的最大数以十进制数的形式在屏幕上显示出来,能用宏指令的用宏指令。
-
DATAS SEGMENT
-
xx Db 73,59,61,45,81,107,37,25,14,64
-
db 3,17,9,23,55,97,115,78,121,67
-
db 215,137,99,241,36,58,87,100,74,62
-
yy db ?
-
BUF DB "THE MAX IS:$"
-
DATAS ENDS
感谢fansy同学的帮忙 TAT 终于写出来了:
-
;-------------------------------------------------------------------
-
; Title:
-
; 找出下面数据段所定义的20个数中的最大数,
-
; 并将找到的最大数以十进制数的形式在屏幕上显示出来。
-
;
-
; Code by linyq@2008-12-4
-
;-------------------------------------------------------------------
-
OUTPUT MACRO WW ; 输出指定的字符
-
MOV DL,WW
-
ADD DL,30H
-
MOV AH,02H
-
INT 21H
-
ENDM
-
;-------------------------------------------------------------------
-
DECDIV MACRO RESULT,XX ; 输出除法指令后的商
-
MOV AX,RESULT
-
MOV DX,0
-
DIV XX
-
MOV RESULT,DX
-
OUTPUT AL
-
ENDM
-
;-------------------------------------------------------------------
-
DATAS SEGMENT
-
XX DB 73,59,61,45,81,107,37,25,14,64
-
DB 3,17,9,23,55,97,115,78,121,67
-
DB 215,137,99,241,36,58,87,100,74,62
-
BUF DB 'The max is:$'
-
CNT EQU $-XX ; CNT为XX数组长度
-
DATAS ENDS
-
;-------------------------------------------------------------------
-
CODES SEGMENT
-
;-------------------------------------------------------------------
-
MAIN PROC FAR
-
ASSUME CS:CODES,DS:DATAS
-
START:
-
MOV AX,DATAS
-
MOV DS,AX
-
-
MOV DX,OFFSET BUF ; 输出提示信息
-
MOV AH,09H
-
INT 21H
-
-
CALL SEARCH ; 查找最大值并存储到BX
-
-
MOV BL,BH
-
MOV BH,00H
-
CALL BINIDEC ; 输出最大值
-
-
MOV AH,4CH
-
INT 21H
-
MAIN ENDP
-
;-------------------------------------------------------------------
-
SEARCH PROC NEAR ; 查找最大值
-
-
MOV SI,OFFSET XX ; SI指向XX数组首地址
-
MOV BX,[SI] ; BX初始化为SI内容
-
-
MOV CX,CNT-1 ; 循环变量CX
-
COMPARE:
-
INC SI ; SI向后移一位
-
CMP BX,[SI] ; 比较BX与SI当前内容
-
JA LITTLE ; 若小于则跳到LITTLE
-
-
MOV BX,[SI] ; 不小于则将BX更新为SI当前内容
-
LITTLE:
-
DEC CX ; CX--
-
JNZ COMPARE ; 继续循环
-
-
RET
-
SEARCH ENDP
-
;-------------------------------------------------------------------
-
BINIDEC PROC NEAR ; 输出四位十进制数
-
MOV CX,1000D
-
DECDIV BX,CX
-
MOV CX,100D
-
DECDIV BX,CX
-
MOV CX,10D
-
DECDIV BX,CX
-
MOV CX,1D
-
DECDIV BX,CX
-
RET
-
BINIDEC ENDP
-
;-------------------------------------------------------------------
-
CODES ENDS
-
END START