Page 1 of 1

5 Replies - 3834 Views - Last Post: 16 February 2010 - 06:39 AM

#1 Guest_Rei*

Reputation:

Posted 16 February 2010 - 01:30 AM

Help me! plz im a newbie on assembly., can anyone help me on these code.. input two numbers -- output the two numbers -- and my problem... add the two inputted numbers.. pl zhelp me

org 100h

jmp start
inp1 db 'Enter first number: ',24h
inp2 db 0dh,0ah,'Enter second number: ',24h
out1 db 0dh,0ah,0dh,0ah,'The first inputted number: ',24h
out2 db 0dh,0ah,'The second inputted number: ',24h
sum db 0dh,0ah,0dh,0ah,'The sum is: ',24h

start:

mov ah,9
mov dx,inp1
int 21h

mov ah,1
int 21h
mov dl,al
mov [1],dl ;input first number

mov ah,9
mov dx,inp2
int 21h

mov ah,1
int 21h
mov [2],al ;input second number

mov ah,9
mov dx,out1
int 21h

mov dx,[1] ;output first number
mov ah,2
int 21h

mov ah,9
mov dx,out2
int 21h

mov dx,[2] ;output second number
mov ah,2
int 21h

mov ah,9
mov dx,sum
int 21h

mov dl,[2] ;here's my problem... adding the two number
int 21h

mov ah,0
int 16h
int 20h

Is This A Good Question/Topic? 0

#2 Martyn.Rae

• The programming dinosaur

Reputation: 538
• Posts: 1,406
• Joined: 22-August 09

Posted 16 February 2010 - 02:01 AM

The problem you are going to have here, (assuming the two numbers are the values input and output in the code preceding the place where you want to do the addition) is that the digits input are in ASCII. So the digit '0' has a hexadecimal value of 30H (decimal 48), '1' has a hexadecimal value of 31H (decimal 49) etc. You need to subtract a value of 48 from each digit input, add the two binary digits together and, assuming you want to output the result, add 48 to the binary value before it's output.

Hope that helps.

#3 Guest_Rei*

Reputation:

Posted 16 February 2010 - 02:37 AM

Thanks Sir Martyn for posting a reply. You helped me a lot. I owe you one. It did really run. Hurray! The code goes like these:

mov dl,[1]
mov bl,[2]
sub dl,30h ;here's my problem... adding the two number
sub bl,30h
mov ah,2
int 21h

Again, thank you very much. This is like a stepping stone for me to understand this programming better.

#4 Ferencn

• D.I.C Regular

Reputation: 70
• Posts: 322
• Joined: 01-February 10

Posted 16 February 2010 - 03:21 AM

I see another possible snag: when you add 9 and 9, the result will be 18 (12h), adding 30h will give 42h, and that is not a digit, but a 'B'...

#5 Guest_Rei*

Reputation:

Posted 16 February 2010 - 04:52 AM

Hmm... I have some question, how can I input two digit number as the first number? That means I'm going to add or subtract two digit number?

#6 Guest_Rei*

Reputation:

Posted 16 February 2010 - 06:39 AM

Ferencn, on 16 February 2010 - 02:21 AM, said:

I see another possible snag: when you add 9 and 9, the result will be 18 (12h), adding 30h will give 42h, and that is not a digit, but a 'B'...

Thanks Ferencn, that's the one I missed. How can I output 2 digit number? and also how can I input 2 digit number?