Purpose:
1. Master the use of comparison instructions, transfer instructions, and loop instructions.
2. Further master the usage of debugging tools.
Experiment content:
Calculate 1+2+.....+n=?, where n is entered through the keyboard, and the cumulative sum is less than 2^16. The expected result is as follows:
Program flow chart:
Program code (with explanation):
Program points:
1. Receive the decimal number input by the keyboard and convert it into a binary number (stored in the AX register) instruction sequence
2. How to display the entered number as the target image in the print result
3. Convert the binary number stored in AX to decimal number and display the instruction sequence
;数据段
DATA SEGMENT
INF1 DB "Please input a number(1-361):$" ;$处截止
INF2 DB "1+2+...+$"
INF3 DB "=$" ;为了实现输出结果设置两个字符串进行连接评凑结果
IBUF DB 7,0,6 DUP(?)
;存放输入的值
;注意IBUF+1为输入数的位数,IBUF+2以后的及存放的值,比如:360 按33H,36H,30H存放
OBUF DB 6 DUP(?) ;输出结果
DATA ENDS
;代码段
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
MOV AX,DATA
MOV DS,AX
MOV DX,OFFSET INF1 ;输出INF1字符串,值存入IBUF1中,利用9号功能键
MOV AH,09H
INT 21H
MOV DX,OFFSET IBUF ;在此键入十进制数
MOV AH,0AH
INT 21H
MOV CL,IBUF+1 ;十进制数的位数送给计数器进行循环
MOV CH,0
MOV SI,OFFSET IBUF+2 ;取偏移地址,找到存第一个数值的位置,给SI,若为360,这时[SI]为3
MOV AX,0 ;开始将十进制转为二进制
AGAIN:
MOV DX,10 ;((0*10+a4)*10+...)*10+a0
MUL DX
AND BYTE PTR [SI],0FH ;将相应存储单元中的字符ASCII转换为数字
ADD AL,[SI] ;2条指令的功能为(AX)<=(AX)+数字
ADC AH,0
INC SI ;[SI]指向下一个
LOOP AGAIN ;循环执行,完成(AX)<=(AX)*10+[SI]
MOV CX,AX
MOV AX,0
MOV BX,1
LOOP2:
ADD AX,BX ;这里分析流程图吧
INC BX
LOOP LOOP2
MOV BX,OFFSET OBUF+5 ;开始二进制转为10进制
MOV BYTE PTR [BX],'$'
MOV CX,10 ;作(DX):(AX)/10运算
LOOP1:
MOV DX,0 ;被除数高16位清零
DIV CX
ADD DL,30H ;将10进制数转换为ASCII码
DEC BX
MOV [BX],DL
OR AX,AX
JNZ LOOP1
;判断商是否为0,不为0继续
MOV DL,0AH ;打印回车换行符,利用2号功能键
MOV AH,02H
INT 21H
MOV DX,OFFSET INF2 ;打印INF2字符串
MOV AH,09H
INT 21H
MOV CL,IBUF+1 ;实现将输入的值打印出来,符合格式要求
MOV SI,OFFSET IBUF+2
LOOP3:
MOV DL,[SI] ;逐个字符打印输入的值n
ADC DL,30H
MOV AH,02H
INT 21H
INC SI
DEC CL
JNZ LOOP3
MOV DX,OFFSET INF3 ;打印INF3字符串,凑格式
MOV AH,09H
INT 21H
MOV DX,BX ;显示求和结果转换得到的十进制数
MOV AH,09H
INT 21H
MOV AH,4CH
INT 21H
CODE ENDS
END START
Realize the result:
It is found that there is no problem in entering two or three digits