在microsoft MASM汇编程序中输出一个数字

时间:2013-10-29 15:30:11

标签: windows assembly dos masm

我知道如何输出字符串。如何输出数字? 我正在使用MS DOS 使用Windows汇编程序6.11进行编译

这是我尝试过的。但它打印垃圾

我根据以下评论编辑了我的代码。

[EDITED]

DATA SEGMENT
NUM1 DW 0001H
NUM2 DW 0002H
SUM DW 2 DUP(0)
RESULT DW ?
 DATA ENDS

 CODE SEGMENT
 ASSUME CS:CODE,DS:DATA
 START: MOV AX,DATA
MOV DS,AX

MOV CX,00H
MOV AX,NUM1
ADD AX,NUM2
JNC DISPLAY

INC CX
MOV SUM+2,CX

 DISPLAY:
MOV SI,RESULT
ADD SI,9
MOV AX,0
MOV [SI],AX
MOV BX,10

 LOOP1:
XOR DX,DX
DIV BX
ADD DL,'0'
DEC SI
MOV [SI],DL
TEST AX,AX
JNZ LOOP1
MOV AX,SI
LEA SI,RESULT

MOV AH,09H
INT 21H
MOV AH,4CH
INT 21H

 CODE ENDS
END START

1 个答案:

答案 0 :(得分:0)

DATA SEGMENT
NUM1 WORD 000AH
NUM2 WORD 000BH
SUM DW 2 DUP(0)
RESULT DB '        $'
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:  MOV AX,DATA
MOV DS,AX

;-----begin addition-----------------
MOV CX,0000H
MOV AX,NUM1
ADD AX,NUM2
MOV SUM,AX
JNC DISPLAY

INC CX
MOV SUM+2,CX
;------Display on screen------------
DISPLAY:
LEA DI,RESULT
ADD DI,0007H
MOV AX,SUM
MOV BX,000AH    ;BX=10 used as a constatnt in the following loop
LOOP1:
XOR DX,DX
DIV BX      ;quotient in AX remainder in DX
ADD DX,0030H

DEC DI
MOV [DI],DL
TEST AX,AX  ;check if ax is 0
JNZ LOOP1

MOV AH,09H
MOV DX,OFFSET RESULT
INT 21H
;-----end the program--------------
MOV AH,4CH
INT 21H
CODE ENDS
END START