hi... ive tried alot of things already...and i know what the problem is, just not sure how to fix it.
ive isolated it to this part of the code thats giving me problems... and i think that its because the register cant hold the number (hence the overflow error)

MOV AX, number		;for division
	MOV CX, 2d		;''
	DIV CX			; AX (number) / CX (2) = n/2 in ax
	INC AX			;so now its n/2 +1
	MOV countmax, AX	;store max var.

number will be from 100-999

since my div skills arent that great...what is the best way to divide something? my book isnt very helpful.
thanks
-g

Dani AI

Generated

Short answer: the divide overflow happens because the DIV instruction uses DX:AX as the 32-bit dividend for a 16‑bit divisor. If DX is not zero (or not correctly sign‑extended for signed division) the dividend is much larger than you expect and the quotient can overflow AX. As pointed out, the quotient must fit the destination register or you get interrupt 0.

Simple, fast fix (best for dividing by 2 and when number is unsigned 100–999): use a logical shift instead of DIV. SHR avoids the DX:AX issue and is much cheaper:

; number already in AX (0..65535)
shr ax,1        ; AX = floor(n/2)
inc ax          ; AX = floor(n/2) + 1
mov [countmax], ax

If you need to use DIV (for a non power‑of‑two divisor or other reasons), explicitly zero DX first for unsigned division, or sign‑extend AX into DX for signed division. Example:

xor dx,dx       ; zero high word for unsigned dividend
mov ax,[number]
mov cx,2
div cx          ; DX:AX / CX -> AX = quotient, DX = remainder
inc ax
mov [countmax], ax

Quick troubleshooting tips: 1) Step the DIV in a debugger and inspect DX before the instruction. 2) Remember 8‑bit DIV yields quotient in AL (max 255); 16‑bit DIV yields quotient in AX (max 65535). 3) For signed division use CWD (to sign‑extend AX into DX) and IDIV. Using SHR/SAR for powers of two is usually the cleanest solution for the case you described.

when we do "div",there some rules we must abide by.
1.8 digits "div",like
mov bh,1
mov ax,11
div bh
send the quotient to al, and send the arithmetical complimentremainder to ah
2. 16 digits "div",like
mov bx,11
mov ax,3223
div bx
send the quotient to ax, and send the arithmetical complimentremainder to dx
so, if the quotient lager than al in 8 digits "div" and larger than ax in 16 digits "div", system will call 0 interrupt, and display "Divide overflow"

right... i understand all of that.. thats the problem that i am having...
but how would you go about doing the division then....

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.