Basic FASM Addition

Adding two numbers

Page 1 of 1

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

#1 Guest_Rei*


Reputation:

Basic FASM Addition

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
add dl,[1]
int 21h

mov ah,0
int 16h
int 20h

Is This A Good Question/Topic? 0

Replies To: Basic FASM Addition

#2 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

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

Re: Basic FASM Addition

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.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Rei*


Reputation:

Re: Basic FASM Addition

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
add dl,bl
add dl,30h
mov ah,2
int 21h

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

#4 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

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

Re: Basic FASM Addition

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'...
Was This Post Helpful? 0
  • +
  • -

#5 Guest_Rei*


Reputation:

Re: Basic FASM Addition

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?
Was This Post Helpful? 0

#6 Guest_Rei*


Reputation:

Re: Basic FASM Addition

Posted 16 February 2010 - 06:39 AM

View PostFerencn, 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?
Was This Post Helpful? 0

Page 1 of 1