0

I know how to output a string. How do I output a number? I am using MS DOS Compiling with windows assembler 6.11

Here is what I have tried. but it prints garbage

I have edited my code according to comments as below.

[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
user2756339
  • 685
  • 1
  • 10
  • 17
  • 1
    You'll have to convert the number to a string first, and then print that string. – Michael Oct 29 '13 at 15:38
  • @Michael Did a little search. A method is including a c function from library. Seems complex. Is there a direct DOS call like MOV AH,09H for strings? – user2756339 Oct 29 '13 at 16:04
  • 1
    It's not necessary to use any C functions. See e.g. http://stackoverflow.com/questions/19309749/nasm-assembly-convert-input-to-integer/19312503#19312503 – Michael Oct 29 '13 at 16:17
  • @Michael Helpful answer. However my problem is still not solved though I tried my best to extract what code you have used. I added the edited code above . PS I am a beginner – user2756339 Oct 29 '13 at 17:23
  • 1
    The code I linked to was written in NASM syntax. `MOV SI,RESULT` should be `LEA SI,RESULT` or `MOV SI,OFFSET RESULT` in MASM syntax. Also, the string terminator for DOS interrupts is usually `'$'` rather than zero. – Michael Oct 29 '13 at 18:15
  • Sorry,Your code is correct and working and can easily display constants. The problem is with my assembler i think, because any operation with SI gives garbage. LEA SI,RESULT loaded SI with ascii 00H. Thus I cannot proceed further from there. – user2756339 Oct 31 '13 at 09:47

1 Answers1

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
user2756339
  • 685
  • 1
  • 10
  • 17