osimer pothe
osimer pothe

Reputation: 2897

sum of digits in assembly program

I want to sum of digits of a number . That is why , I have written the following programs :

.MODEL SMALL
.STACK 100H
.DATA
MSG DB 0AH,0DH,'THE SUM OF '
C1  DB ?,'AND'
C2  DB ?,'IS'
SUM DB ?,'$'
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS,AX

MOV AH,2
MOV DL,'?'
INT 21H

MOV AH,1
INT 21H
MOV C1,AL
INT 21H
MOV C2,AL

ADD AL,C1
SUB AL,30H
MOV SUM,AL

LEA DX,MSG
MOV AH,9
INT 21H

MOV AH,4CH
INT 21H

MAIN ENDP
END MAIN

I can give the number such as 27 . But in the screen , ony the following string is shown .

"THe sum of "

I want to show the following string :

"The sum of 2 and 7 is 9"

How can I do this ? Any advice is of great help .

Upvotes: 0

Views: 3383

Answers (1)

Fifoernik
Fifoernik

Reputation: 9899

Your program should work as expected provided you stick with these small numbers.
I would however recommend the following changes:

  • Put spaces before and after the words and and is for better layout.
  • Don't use the ? question mark. Your assembler might have chosen to store such bytes in the uninitialized data section, disrupting your ordering.

.

C1  DB 'x AND '
C2  DB 'x IS '
SUM DB 'x$'

Upvotes: 1

Related Questions