sippycup
sippycup

Reputation: 145

Dividing in assembly trouble

I'm working on a simple program that adds two numbers together and gives the average for them. My trouble is with the division. I set bl =2 and use it to divide into the ax register which has the correct integer however, I can not seem to get a correct answer. for example when I divide 8 by 2 I get a 1c in the al register.

mov dl, bl    
add dx, 30h; two user entered numbers add together and converted
mov ah, 2h
int 21h

;mov dx, 0h  
mov ax, dx ; 
mov bl, 2
div bl ; al, ah for results 

Upvotes: 0

Views: 57

Answers (1)

Michael
Michael

Reputation: 58427

You added 30h to dx, so you're dividing 38h, not 8. And 38h / 2 == 1Ch.

As a side note, in the special case where you're dividing by a power of 2 (such as 2, 4, 8, 16, etc), you can do that by shifting log2(divisor) bits to the right instead. In this case that would be shr ax, 1 (or sar ax, 1 if you want to treat ax as a signed number).

Upvotes: 7

Related Questions