adding 2 numbers 
Author Message
 adding 2 numbers

This code is a lot better because it shows how it works. :)

[section .text]
[org 100h]

        mov     ah, 9
        mov     dx, x
        int     21h             ; print x

        mov     dx, y
        int     21h             ; print y

        mov     si, x+3         ; last digit of x
        mov     di, y+3         ; last digit of y
        mov     bx, z+3         ; last digit of z

        mov     cx, 4           ; 4 digits total
        clc
cycle:
        mov     al, [si]
        adc     al, [di]
        aaa                     ; did you know this one?
        mov     [bx], al
        dec     si
        dec     di
        dec     bx
        loop    cycle

        add     [z], dword 30303030h ; correct the result

        mov     ah, 9
        mov     dx, z
        int     21h             ; print result (z=x+y)

        mov     ax, 4c00h
        int     21h

x       db "0012+$"           ; x
y       db "0018=$"           ; y
z       db "0000",13,10,"$" ; result variable



Thu, 15 Aug 2002 03:00:00 GMT  
 adding 2 numbers


comp.lang.asm.x86:

Quote:
> I'm having trouble adding two double digit numbers.  12 + 12 = 24 which is
> fine, but 12 + 18 = 2:

> This is driving me nuts!! If there is a carry in the one's position the
> answer returns a ':' instead of a 0.  Can anyone help?

> Very Desperate, :(
> Erich

I'm afraid your request is not very clear.  Where are the numbers you
are trying to add?  In registers, memory, text strings?  If you can
post a sample of the code which is giving you a problem we can offer
advice.

I can see how you could get a ':', but I don't have any idea how you
are getting 2 out of 12 + 18.

Jack Klein
--
Home: http://jackklein.home.att.net



Thu, 15 Aug 2002 03:00:00 GMT  
 adding 2 numbers

Erich schrieb:

Quote:

> I'm having trouble adding two double digit numbers.  12 + 12 = 24 which is
> fine, but 12 + 18 = 2:

> This is driving me nuts!! If there is a carry in the one's position the
> answer returns a ':' instead of a 0.  Can anyone help?

That's because the next char after the digits 0..9 is the colon. You you
calc 2+8=10, plus 48 is 58, which is the the ascii-code of the colon.
You should compare the result of the digit-addition with 10 (or 58,
depends on the way to take) and correct it "manually".

MfG Alexander



Thu, 15 Aug 2002 03:00:00 GMT  
 adding 2 numbers

mov ax, 12
add ax, 12
ax=24 now

mov ax, 12
add ax, 18
ax=30 now

all the values above are decimal (don't confuse with hex, BCD and
symbloc representation).

if you're working with symbolical representation, code should be like
this (NASM source):
--------------------------------8<--------------------------------
[section .text]
[org 100h]

        mov     si, x+3         ; last digit of x
        mov     di, y+3         ; last digit of y
        mov     bx, z+3         ; last digit of z

        mov     cx, 4           ; 4 digits total
        clc
cycle:
        mov     al, [si]
        adc     al, [di]
        aaa                     ; did you know this one?
        mov     [bx], al
        dec     si
        dec     di
        dec     bx
        loop    cycle

        add     [z], dword 30303030h ; correct the result

        mov     ax, 4c00h
        int     21h

x       db "0012"     ; x
y       db "0018"     ; y
z       db "0000"     ; result variable (z=x+y)
--------------------------------8<--------------------------------

IMHO you need a GOOD asm language book.

Good Luck
Alexei A. Frounze

Quote:

> I'm having trouble adding two double digit numbers.  12 + 12 = 24 which is
> fine, but 12 + 18 = 2:

> This is driving me nuts!! If there is a carry in the one's position the
> answer returns a ':' instead of a 0.  Can anyone help?

> Very Desperate, :(
> Erich



Thu, 15 Aug 2002 03:00:00 GMT  
 adding 2 numbers
You seem to be adding 2 values displayed in hex and show the digits by
adding "0" = 48 decimal = 30h to the value of each digit.

12h + 12h = 18 decimal + 18 decimal = 36 decimal = 24h

12h + 18h = 18 decimal + 24 decimal = 42 decimal = 2Ah

You get 2: instead, you have to add 7 more when over value 09 or "9"
depending on how you do it (, because after 9 comes A in hex).

Suppose you have the value of the digit in AL, then:
        ADD     AL,"0"
        CMP     AL,"9"
        JBE     DIGIT_OK
        ADD     AL,07
DIGIT_OK:

or
        CMP     AL,09
        JBE     LOW_VAL
        ADD     AL,07
LOW_VAL:ADD     AL,"0"

--

Eric P. van Westendorp  Tel: +31(0252)210579
Reigerslaan 22  2215NN Voorhout  Netherlands

Quote:

> I'm having trouble adding two double digit numbers.  12 + 12 = 24 which is
> fine, but 12 + 18 = 2:

> This is driving me nuts!! If there is a carry in the one's position the
> answer returns a ':' instead of a 0.  Can anyone help?

> Very Desperate, :(
> Erich



Thu, 15 Aug 2002 03:00:00 GMT  
 adding 2 numbers

Quote:

> I'm having trouble adding two double digit numbers.  12 + 12 = 24 which is
> fine, but 12 + 18 = 2:

> This is driving me nuts!! If there is a carry in the one's position the
> answer returns a ':' instead of a 0.  Can anyone help?

> Very Desperate, :(

If you're that desperate, you'd better just grab the first instruction
you can find - "AAA" :)

Add the one's position - using al as the destination, AAA, then ADC the
ten's position.

"2 + 8 = 10" *looks* like it should cause a carry, but 10 fits quite
happily in a byte, so it doesn't. AAA sets the carry, subtracts 10 from
al, and sets ah to 1 (so that's not where you should store the "ten's
position"!).

That's rather a wild guess... if I'm way off, maybe you should post some
of the code that's causing the problem.

Best,
Frank



Thu, 15 Aug 2002 03:00:00 GMT  
 adding 2 numbers
Then how do I convert back into the decimal form, so I don't get the ':'
printed on the screen? What I want my program to do is read 2 double digit
numbers from a text file then print out the sum of the screen.  The program
reads from the file into a buffer variable fine, but it won't properly
calculate.  Whenever I use a ADD or MUL, does the assembler always perform
the calculations in ASCII mode? Or can I force it to do it in decimal mode?

Quote:

>Erich schrieb:

>> I'm having trouble adding two double digit numbers.  12 + 12 = 24 which
is
>> fine, but 12 + 18 = 2:

>> This is driving me nuts!! If there is a carry in the one's position the
>> answer returns a ':' instead of a 0.  Can anyone help?

>That's because the next char after the digits 0..9 is the colon. You you
>calc 2+8=10, plus 48 is 58, which is the the ascii-code of the colon.
>You should compare the result of the digit-addition with 10 (or 58,
>depends on the way to take) and correct it "manually".

>MfG Alexander



Thu, 15 Aug 2002 03:00:00 GMT  
 adding 2 numbers

Quote:

> Whenever I use a ADD or MUL, does the assembler always perform
>the calculations in ASCII mode? Or can I force it to do it in decimal mode?

The normal mode of x86 processors is BINARY. That is a register can hold
up to 65535, or in 32-bit mode up to about 4 billion. One can do also
addition and subtraction in BCD and ASCII modes but that is not a good
idea.

Assembler does only one task: it translates the assembler code into
machine code.

Normally you would read the numbers and convert them into binary. Lets
say you have a two digit ASCII number in CX, you can do:

sub cx,3030h
mov al,10
mul ch
add al,cl

And you have the number in AL.

To convert back (AL=>AX) use

xor ah,ah
mov bl,10
div bl      ; result in al, remainder  in ah.
xchg al,ah
add ax,3030h

Now if ah is higher than '9' then the number is over 99.

To convert larger values the idea in reading is:

initialize result to zero
while input character is number
  multiply result by 10
  add the input character
End;

and To convert to ASCII

Repeat
  divide by 10, pushing remainder in stack
Until result=0
Pop the remainders from stack outputting them

(one can either count the number of remainders pushed to stack, or at
first push a marker, like value 10).

Osmo



Thu, 15 Aug 2002 03:00:00 GMT  
 adding 2 numbers

forth this really invaluable piece of information:

Quote:
>I'm having trouble adding two double digit numbers.  12 + 12 = 24 which is
>fine, but 12 + 18 = 2:

>This is driving me nuts!! If there is a carry in the one's position the
>answer returns a ':' instead of a 0.  Can anyone help?

When you get the carry you are adding 10 to the '0' charactor, which
is a ':' charactor. You need to only add 0 to it, and add the 1 to the
next charactor. HTH


Wed, 21 Aug 2002 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. adding random numbers to input lines

2. Adding x number of months to a date

3. Add 2 numbers in Assembly

4. help adding two numbers

5. Newbie:How to add big numbers?

6. adding hex numbers??????

7. Adding Binary numbers

8. Adding a number to an existing file without affecting existing data

9. WANTED: Example of adding line numbers to a text widget

10. Problem adding large numbers

11. Should I add complex numbers to the Python core?

12. VPREXX Help? Adding number to string number

 

 
Powered by phpBB® Forum Software