汇编语言题目 从键盘上输入0~9之间的任意一个数字,利用查表的方法计算其平方值,并将计算的结果在

2024-12-11 19:39:59
推荐回答(2个)
回答1:

自己写的:

ASSUME CS:CODE, DS:DATA

DATA SEGMENT

OUT_TABLE DW PF0, PF1, PF2, PF3, PF4, PF5, PF6, PF7, PF8, PF9

;PIFANG_TABLE
PF0 DB 13, 10, '0, 0H $'
PF1 DB 13, 10, '1, 1H $'
PF2 DB 13, 10, '4, 4H $'
PF3 DB 13, 10, '9, 9H $'
PF4 DB 13, 10, '16, 10H $'
PF5 DB 13, 10, '25, 19H $'
PF6 DB 13, 10, '36, 24H $'
PF7 DB 13, 10, '49, 31H $'
PF8 DB 13, 10, '64, 40H $'
PF9 DB 13, 10, '81, 51H $'

DATA ENDS

CODE SEGMENT

START:
MOV AX, DATA
MOV DS, AX

MOV AH, 1
INT 21H ;输入一字符
CMP AL, '0'
JB EXIT
CMP AL, '9'
JA EXIT

AND AL, 0FH ;AL=0~9
MOV AH, 0
MOV BX, AX ;BX=0~9
ADD BX, BX
MOV AX, [BX] ;查表得出结果
LEA DX, OUT_TABLE
ADD DX, AX
MOV AH, 9 ;显示
INT 21H
EXIT:
MOV AX, 4C00H
INT 21H
CODE ENDS
END START

回答2:

DATA SEGMENT
BUFFER DB '0 , 0H $','1 , 1H $','4 , 4H $','9 , 9H $','16,10H $'
DB '25,19H $','36,24H $','49,31H $','64,40H $','81,51H $'
MUB DB 0
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA
MOV DS, AX
MOV AH, 1
INT 21H ;输入一字符
MOV MUB,AL ;暂存输入的字符
MOV DL,13 ;换行
MOV AL,02H
INT 21H
MOV AL,MUB ;取字符
CMP AL, '0' ;确定字符范围0~9
JB EXIT
CMP AL, '9'
JA EXIT
AND AL, 0FH ;AL=0~9
MOV BL,08
MUL BL
MOV DX, OFFSET BUFFER ;打印菜单
ADD DX,AX
MOV AH, 9
INT 21H
EXIT:
MOV AH,4CH ;退出程序
INT 21H
CODE ENDS
END START