1

I wanted to write a program that adds two Numbers and show the result in Decimal form, initially it was looking a piece of cake but as it turned out, it wasn't !!! because there is just 0-9 characters in decimal and when we want to add any number greater than those we've to perform some mathematics,

Here is what i've done, I wanted to add two numbers 35 and 39,

35 + 39 = 74

MOV BL,35H
MOV AL,39H
ADD AL,BL

DAA ;Decimal after Addition => the result of it would be 0074H


PUSH AX     ;PRESERVE 0074H

; Separating the two numbers

AND AL,00001111B ; AL => 0000 0100 
ADD AL,30H       ; ; AL => 0004H + 30H = 4 of Decimal

POP AX ;AX = 74H => 01110100

ROR AL,1
ROR AL,1
ROR AL,1
ROR AL,1

AND AL,00001111B ;AL => 0000 0111
ADD Al,30H       ;A: => 0007H + 30H = 7 of Decimal
MOV DL,AL 



MOV AH,4CH ;Return Control to the DOS
INT 21H

I recovered both the numbers but now how to show the result as '74' ???

Also , this method is so time consuming, is there any better and more efficient way to do this ?

4

2 回答 2

2
MOV BL,35H
MOV AL,39H
ADD AL,BL

DAA          # al = 74h = 0111.0100

XOR AH,AH    # ah = 0 (just in case it wasn't)
             # ax = 0000.0000.0111.0100

ROR AX,4     # ax = 0100.0000.0000.0111 = 4007h
SHR AH,4     # ax = 0000.0100.0000.0111 = 0407h
ADD AX,3030h # ax = 0011.0100.0011.0111 = 3437h = ASCII "74" (reversed due to little endian)

现在您只需将 AX 复制到缓冲区并打印它。

于 2012-09-21T11:51:08.180 回答
2

您可以将结果打印为单个字符('7' 表示 7,'4' 表示 4)或将这些字符组合成一个字符串,在其上附加 '$' 字符并打印该字符串(终端 '$' 不会'不打印)。

DOS 具有打印单个字符(ah=2,dl=ASCII 字符代码)和以 $ 结尾的字符串(ah=9,dsdx=$ 结尾字符串的地址)的功能。

于 2012-09-20T23:53:59.240 回答