``````org  100h	; set location counter to 100h

.model small
.stack 200
.data
crlf    db  0dh,0ah,'\$'
prompt1 db  'enter the first positive integer: ','\$'
prompt2 db  'enter the second positive integer: ','\$'
prompt3 db  'The sum of two numbers is: ','\$'
num1    db  ?
num2    db  ?
res1     db  ?
res2     db  ?
.code
.startup
lea dx, prompt1
mov ah, 09h
int 21h

mov ah, 01h
int 21h

sub al, 30h
mov cl, al

mov ah, 01h
int 21h

sub al, 30h
mov num1, al

lea dx, crlf
mov ah,09h
int 21h

lea dx, prompt2
mov ah, 09h
int 21h

mov ah, 01h
int 21h

sub al, 30h

mov res1, al

mov ah, 01h
int 21h

sub al, 30h

mov res2,al

lea dx, crlf
mov ah,09h
int 21h

lea dx,prompt3
mov ah,09h
int 21h

mov dl,res1
mov ah, 02h
int 21h

mov dl,res2
mov ah, 02h
int 21h

.exit
end``````

The code above is working only if the number you're adding don't have any carry on it. Like if I add 16+13 the answer would be correct but if i add 19+13, it would display just 2 and a ascii character..How would I solve this?

## All 12 Replies

Your code is unconventional to say the least, but creative non the less. You'll also find you get unusual results if you add 54 + 93 = 7< and if you add 87 + 44 = <;

7 + 4 = 11, then add 48 to this and you get 59 which equates to ";".

So if you would like some help rebuilding this let's start with cleaning it up considerably by putting you entry parts into a subroutine and returning a binary number in AX.

Otherwise there is no viable solution to fixing what you've got

If you prefer we could use BCD (Binary Coded Decimals), that would be easier yet.

Your code is unconventional to say the least, but creative non the less. You'll also find you get unusual results if you add 54 + 93 = 7< and if you add 87 + 44 = <;

7 + 4 = 11, then add 48 to this and you get 59 which equates to ";".

So if you would like some help rebuilding this let's start with cleaning it up considerably by putting you entry parts into a subroutine and returning a binary number in AX.

Otherwise there is no viable solution to fixing what you've got

Obviously, im completely an amateur on assembly.:)
So my program is completely wrong isn't it? We were given a task to have a program that calculates a sum by using simple commands and we're not allowed to use transfer commands like JMP. Is it possible that i could create a program without using subroutines and transfer commands?

Can you give me a idea on how can i rebuild this program simpliest as possible?

Ok, I'm going to assume that it's ok to use BCD, so here is a simple snippet to get a two digit entry and leave the result in DL. Place your prompting code before here

``````mov    ah, 1
int    21h
and    al, 15
mov    cl, 4
shl    ax, cl
mov    dl, al
mov    ah, 1
int    21h
and    al, 15

Now move DL to somewhere else and do this again for the second value. Then we'll deal with addition and displaying the result.

You code is not WRONG. The only problem you're having is that it's not achieving the desired result. It could be fixed easily if you were allowed to use JMP or some sort of branching mechanism.

Ok, I'm going to assume that it's ok to use BCD, so here is a simple snippet to get a two digit entry and leave the result in DL. Place your prompting code before here

``````mov    ah, 1
int    21h
and    al, 15
mov    cl, 4
shl    ax, cl
mov    dl, al
mov    ah, 1
int    21h
and    al, 15

Now move DL to somewhere else and do this again for the second value. Then we'll deal with addition and displaying the result.

You code is not WRONG. The only problem you're having is that it's not achieving the desired result. It could be fixed easily if you were allowed to use JMP or some sort of branching mechanism.

Its working, now how can i add and display the desired results?

Let me see your code in its entirety so I can give specific details. In general your going to add your two numbers and then use DAA. Afterward you'll take that packed binary decimal and add 30H to it again to get ASCII equivalent

``````org  100h

.model small
.stack 200
.data
crlf    db  0dh,0ah,'\$'
prompt1 db  'enter the first positive integer: ','\$'
prompt2 db  'enter the second positive integer: ','\$'
prompt3 db  'The sum of two numbers is: ','\$'
num1    db  ?
num2    db  ?
res1     db  ?
res2     db  ?
.code
.startup
lea dx, prompt1
mov ah, 09h
int 21h

mov    ah, 1
int    21h
and    al, 15
mov    cl, 4
shl    ax, cl
mov    dl, al
mov    ah, 1
int    21h
and    al, 15

mov num1, dl

lea dx, crlf
mov ah, 09h
int 21h

lea dx, prompt2
mov ah, 09h
int 21h

mov    ah, 1
int    21h
and    al, 15
mov    cl, 4
shl    ax, cl
mov    dl, al
mov    ah, 1
int    21h
and    al, 15

daa

lea dx, crlf
mov ah, 09h
int 21h

lea dx, prompt3
mov ah, 09h
int 21h

mov ah, 02h
int 21h

.exit
end``````

Is this correct? Is this how it should be done?

Ok, here's my version. Once again to characterize something as being right or wrong in programming is somewhat subjective. Correctness in my mind is if the application is doing what it's supposed to do. That being said, when I code I try to combine efficiency of memory and speed. In this case, even my example below does not meet that criteria, but non the less it gets the job done.

``````org  100h

lea     dx, [StrA]
mov     ah, 09h
int     21h

mov    ah, 1
int    21h
and    al, 15
mov    cl, 4
shl    ax, cl
mov    dl, al
mov    ah, 1
int    21h
and    al, 15

push    dx

lea     dx, [StrB]
mov     ah, 09h
int     21h

mov    ah, 1
int    21h
and    al, 15
mov    cl, 4
shl    ax, cl
mov    dl, al
mov    ah, 1
int    21h
and    al, 15
pop    ax
daa

push    ax
and    ax, 15
or    al, 30H
mov    [OutPut + 1], al
pop    ax
shr    ax, cl
and    ax, 15
or    al, 30H
mov    [OutPut], al

lea    dx, [Strc]
mov    ah, 9
int    21H
ret

StrA db  10, 10, 10, 13, 'First  Integer:  \$'
StrB db  13, 10, 'Second Integer:  \$'
Strc db  10, 10, 10, 13, 9, 'Sum is: '
OutPut  db    '00', 10, 10, 10, 13, '\$'``````

There are a few subtle differences, especially as I code using NASM. Apart from that you'll figure out the rest. Notice how I incorporate carriage returns and line feeds right into the string instead of making a separate call, and whenever possible I use registers instead of memory.

If you force you addition to be a value greater than 100, then this application will need to be modified. IE: 72 + 48 = 120. The result you will see will be 20.

That's great..I should be studying your code then..Thank you very much..I would then modify your code until i understand it competely.

At which part of your code, displays the result, im confused on how you displayed the result.

At which part of your code, displays the result, im confused on how you displayed the result.

Line 38 is where I start the display process. The trick you might be missing is after I convert each character I actually put it in the final display string. Use DEBUG and break at line 38 and just trace through it step by step and you'll get what's going on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.