[英]How to Add 4 digit numbers and display the sum in Assembly using one variable?
I am using EMU8086. 我正在使用EMU8086。 How do I add 4 digit numbers and display the sum in Assembly using one variable? 如何添加4位数字并使用一个变量在Assembly中显示总和?
DATA SEGMENT
MSG1 DB "ENTER NUMBER WITH FOUR DIGITS : $"
MSG2 DB 10,13,"RESULT : $"
D1 DB ?
D2 DB ?
D3 DB ?
D4 DB ?
SUM DB ?
RES DB 10 DUP ('$')
DATA ENDS
CODE SEGMENT
ASSUME DS:DATA CS:CODE
START:
MOV AX,DATA
MOV DS,AX
LEA DX,MSG1
MOV AH,9
INT 21H
MOV AH,1
INT 21H
SUB AL,30H
MOV AH,0
MUL AL
MOV D1,AL
MOV AH,1
INT 21H
SUB AL,30H
MOV AH,0
MUL AL
MOV D2,AL
MOV AH,1
INT 21H
SUB AL,30H
MOV AH,0
MUL AL
MOV D3,AL
MOV AH,1
INT 21H
SUB AL,30H
MOV AH,0
MUL AL
MOV D4,AL
ADD AL,D3
ADD AL,D2
ADD AL,D1
MOV SUM,AL
LEA SI,RES
CALL HEX2DEC
LEA DX,MSG2
MOV AH,9
INT 21H
LEA DX,RES
MOV AH,9
INT 21H
MOV AH,4CH
INT 21H
CODE ENDS
HEX2DEC PROC NEAR
MOV CX,0
MOV BX,10
LOOP1: MOV DX,0
DIV BX
ADD DL,30H
PUSH DX
INC CX
CMP AX,9
JG LOOP1
ADD AL,30H
MOV [SI],AL
LOOP2: POP AX
INC SI
MOV [SI],AL
LOOP LOOP2
RET
HEX2DEC ENDP
END START
The question you ask does not entirely correspond to the code that you've written. 您提出的问题并不完全与您编写的代码相对应。 If you were to add a couple of 4-digit numbers then you would of course have to input more than one such number! 如果要添加几个4位数字,那么您当然必须输入多个这样的数字! See the EXTRA section. 请参阅“额外”部分。
Therefore I conclude that you maybe just ill-phrased the question and I'll comment on the code you've shown: 因此,我得出的结论是,您可能只是措辞不当,我将对显示的代码发表评论:
Your program adds the 4 digits of a single 4-digit number. 您的程序将一个4位数字的4位数字相加。 This means that the maximum result can ever be 9+9+9+9 which gives 36. This fact is important for the rest of the program since it effects the choice of instructions. 这意味着最大结果可以是9 + 9 + 9 + 9,即36。这个事实对于程序的其余部分很重要,因为它会影响指令的选择。
When you actually input the individual digits you immediately destroy them by squaring them using the mul al
instruction! 当你真正投入的单个数字您立即使用它们平方摧毁他们mul al
指导!
MOV AH,1
INT 21H
SUB AL,30H ; From "0".."9" to 0..9
MOV AH,0 <-- Don't do this
MUL AL <-- Don't do this
MOV D1,AL
There's no real point in moving the sum to the sum variable since it won't be used as such afterwards. 将sum移到sum变量没有实际意义,因为以后将不再使用它。 But you do need to extend the AL register to the size of AX since the conversion routine expects AX as an input: 但是您确实需要将AL寄存器扩展为AX的大小,因为转换例程希望AX作为输入:
ADD AL,D3
ADD AL,D2
ADD AL,D1
MOV SUM,AL <-- Maybe not needed?
CBW <-- This clears AH (the high byte of AX) because AL is a very small number (less than 128)
LEA SI,RES
CALL HEX2DEC
I see you've chosen to have the conversion routine HEX2DEC to always display 2 decimal digits. 我看到您选择让转换例程HEX2DEC始终显示2个十进制数字。 This is fine. 这可以。
EXTRA 额外
To set you on the way for adding a couple of 4-digit numbers, I'll add the code to retrieve the first of these numbers. 为了使您能够添加几个4位数字,我将添加代码以检索这些数字中的第一个。 Make sure to define word sized variables in the DATA segment. 确保在DATA段中定义单词大小的变量。
Calculation is based on this formula: Number= D1*1000+D2*100+D3*10+D4
计算基于以下公式: Number= D1*1000+D2*100+D3*10+D4
Summing both numbers and displaying them should be a breeze! 将两个数字相加并显示应该很容易!
NUM1 DW ?
NUM2 DW ?
SUM DW ?
...
MOV AH,1
INT 21H
SUB AL,30H
CBW
MOV DX,1000
MUL DX
MOV NUM1,AX
MOV AH,1
INT 21H
SUB AL,30H
CBW
MOV DX,100
MUL DX
ADD NUM1,AX
MOV AH,1
INT 21H
SUB AL,30H
MOV AH,10
MUL AH
ADD NUM1,AX
MOV AH,1
INT 21H
SUB AL,30H
CBW
ADD NUM1,AX
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.