ady
ady

Reputation: 173

Why did mov dx,0 fix this division routine?

A simple loop to find the largest divisor, which would be the integer of the root, in this case dropping out at 5 in a 345 triangle

mov ax,3                        
mul ax                          
push ax                         
mov ax,4                        
mul ax                          
pop bx                          
add ax,bx                       
push ax    ;save 25 decimal for repeated loops                        
mov bx,1                        
mov bp,sp                       

agin                            
mov dx,0      ;fixed problem                  
mov ax,[bp]   ;25 decimal                  
inc bx        ;starts at 2                  
div bx                          
cmp bx,ax                       
jb agin                         

out

However, weird things happened when the program went from 2 to 3, I got hex555D instead of 8 (8*3)

Putting mov dx,0 in fixed it and returned things to normal

I have no idea why, no flags changed

Anyone know why? is this a signed/unsigned issue?

Upvotes: 1

Views: 1231

Answers (1)

Kyurem
Kyurem

Reputation: 1869

The DIV instruction divides the number DX:AX by the operand.

DX is the upper word. AX is the lower word. Therefore if you want to divide AX by BX, you need to zero DX.

Upvotes: 5

Related Questions