we were asked to make a program that adds two digit numbers. I was ableto scan and add the input numbers but I have trouble doing the 'carry' part. i can't divide the sum of the ones place integers by ten.

what's wrong with this code? :

mov al,bl;move bl to al to make it the dividend, the sum is stored in bl
sub al,30h;subtract 30h to make it number
mov bl,10;move 10 to bl. 
	
div bl;divide to get the remainder/

thanls in advance

we were asked to make a program that adds two digit numbers. I was ableto scan and add the input numbers but I have trouble doing the 'carry' part. i can't divide the sum of the ones place integers by ten.

what's wrong with this code? :

mov al,bl;move bl to al to make it the dividend, the sum is stored in bl
sub al,30h;subtract 30h to make it number
mov bl,10;move 10 to bl. 
	
div bl;divide to get the remainder/

thanls in advance

Hi, you need to make use of AX and BX for your DIV needs 16 bit regs.

mov ax, 100
mov bx, 10
div bx
; result in ax:dx, quotient in ax, remainder in dx

; You may also look at my assembly code, lines 20-22 . There i make division too.

-- tesu

we were asked to make a program that adds two digit numbers. I was ableto scan and add the input numbers but I have trouble doing the 'carry' part. i can't divide the sum of the ones place integers by ten.

what's wrong with this code? :

mov al,bl;move bl to al to make it the dividend, the sum is stored in bl
sub al,30h;subtract 30h to make it number
mov bl,10;move 10 to bl. 
	
div bl;divide to get the remainder/

thanls in advance

Add two digit #'s?
30h....39h + 30h....39h 0...9 + 0...9 = 0...18
There is no carry!
If you have a two ASCII digits
'1','9' so 31h, 39h
They are already grouped as 1's, 10's
Tens = '1'-'0' = 1
Ones = '9'-'0' = 9

If you truly want the divide!
# = (Tens * 10) + Ones
or
# = (Tens << 3) + Tens + Tens + Ones

You can use the div (unsigned) or idiv (signed) instructions to break the Dividend into its Quotient and Remainder.

Actually an 8-bit divisor divides against the 16-bit dividend in
AX, AH=remainder AL=quotient, so he must clear AH before the division:

xor ah, ah
mov al, bl
mov bl, 10
div bl

And a 16-bit divisor divides against the dividend in DX:AX
, remainder is stored in DX, quotient in AX

Oh, yeah in reply to your question about the 'carry',
if you are adding two unpacked BCDs in the range 0-9,
and wan't to know when there is a decimal carry:

mov al, 39h
mov bl, 31h
sub al, 30h
sub bl, 30h
; now al=9 bl=1
add al, bl
aaa ; ASCII adjust after addition
cmp ah, 0 ; if AH non-zero there was carry 
jnz carry
...
carry:
add [nextbcd-si], ah ; add 1 to lower BCD in chain

When adding up two multi-digit values represented as unpacked BCDs
the unpacked BCDs of each value are added one at a time, direction depends
upon whether values were reversed, e.g. 01020304 -> 04030201,
AAA is executed after each addition, AH will equal one on carry.

thank you very much! :D

Be a part of the DaniWeb community

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