宏指令设计实验
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
循环结构、选择结构练习
试编写一程序,要求能从键盘接收一个个位数N,然后响铃N次(响铃的ASCII码为07H)。
-
DATAS SEGMENT
-
INFOR DB 'please input the number(0~9):$'
-
RING DB 0AH,0DH,'RING~$'
-
DATAS ENDS
-
-
CODES SEGMENT
-
MAIN PROC FAR
-
ASSUME CS:CODES,DS:DATAS
-
START:
-
PUSH DS
-
SUB AX,AX
-
PUSH AX
-
MOV AX,DATAS
-
MOV DS,AX
-
-
MOV DX,OFFSET INFOR
-
MOV AH,09H
-
INT 21H
-
-
MOV AH,01H
-
INT 21H
-
SUB AL,30H
-
MOV BL,AL
-
-
CMP BL,0
-
JLE EXIT
-
-
CMP BL,9
-
JG EXIT
-
-
RINGUP:
-
CALL WAITTIME
-
-
MOV AH,02H
-
MOV DL,07H
-
INT 21H
-
-
MOV DX,OFFSET RING
-
MOV AH,09H
-
INT 21H
-
-
DEC BL
-
JNZ RINGUP
-
-
EXIT:
-
MOV AH,4CH
-
INT 21H
-
MAIN ENDP
-
-
WAITTIME PROC
-
PUSH CX
-
MOV CX,2000H
-
-
WAITFOR:
-
CALL WAITTIME2
-
LOOP WAITFOR
-
POP CX
-
RET
-
-
WAITTIME ENDP
-
-
WAITTIME2 PROC
-
PUSH CX
-
MOV CX,3600H
-
-
WAITFOR2:
-
LOOP WAITFOR2
-
POP CX
-
RET
-
-
WAITTIME2 ENDP
-
-
CODES ENDS
-
END START
选择结构练习
1、从键盘输入一字符,判断该字符是小写字母、大写字母、数字还是其它字符。若输入为小写字母,则显示“You Input a Lowercase Letter!”;若输入为大写字母,则显示“You Input a Uppercase Letter!”;若输入为数字,则显示“You Input a Digit!”;若输入为其它字符,则显示“You Input Other Letter!”。
-
DATAS SEGMENT
-
INFOR DB 'Are you really want to exit?$'
-
INFOR_Y DB 0AH,0DH,'Thank you for your using!$'
-
INFOR_N DB 0AH,0DH,'Lets continue!$'
-
INFOR_O DB 0AH,0DH,'You press an error key!$'
-
DATAS ENDS
-
-
CODES SEGMENT
-
ASSUME CS:CODES,DS:DATAS
-
START:
-
MOV AX,DATAS
-
MOV DS,AX
-
-
MOV DX,OFFSET INFOR
-
MOV AH,09H
-
INT 21H
-
-
MOV AH,01H
-
INT 21H
-
-
AND AL,11011111B
-
-
CMP AL,01011001B
-
JE INPUT_Y
-
-
CMP AL,01001110B
-
JE INPUT_N
-
-
JMP INPUT_O
-
-
INPUT_Y:
-
MOV DX,OFFSET INFOR_Y
-
JMP RESULT
-
-
INPUT_N:
-
MOV DX,OFFSET INFOR_N
-
JMP RESULT
-
-
INPUT_O:
-
MOV DX,OFFSET INFOR_O
-
-
RESULT:
-
MOV AH,09H
-
INT 21H
-
-
MOV AH,4CH
-
INT 21H
-
CODES ENDS
-
END START
第一个asm……好囧
这真是一门可怕的学科 TAT
编写程序实现:从键盘输入0~9中任意一个自然数,将其平方值在屏幕上显示出来(要求使用查表法)。