# please explain me this multi-digit print program

• June 24th, 2012, 02:42 PM
bringo
please explain me this multi-digit print program

.model small
.stack 100h
.code

.startup

mov AX,235

; in 16 bit DIV
; dividend = AX
; divisor = any register, bx in this case
; quotient = AX
; remainder = DX

mov cx,0
mov bx,10

Next:
mov dx,0
div bx
push dx
inc cx
cmp ax,0
ja Next

show:
pop dx
mov dh,0
mov ah,02
int 21h
loop show

mov ah,4ch ; terminate to DOS successfuly
int 21h
end

I know the logic is "divide the number repeatedly, pushing remainders in stack, and then display one by one poping back from stack" My confusion is this;
ax = 235
after 1st div: remainder = dx = 5, and quotient = ax = 23
after 2nd div:remainder = dx = 3, and quotient = ax = 2
after 3rd div: remainder = dix = 0, and quotient = ax = 0.2

poping should yield:035 but it gives 235 correctly. I wonder what happens with last div that is gives 2 correctly. Please help me.

Best Regards
• June 24th, 2012, 06:06 PM
Eri523
Re: please explain me this multi-digit print program
No black magic at all: An integral division of n by m with n < m always yields a quotient of 0 and a remainder of n. It's simply your manual calculation of the last step that is wrong. (Besides, the AX register never could hold a fractional value like 0.2 anyway... ;))

BTW, your "terminate to DOS successfully" code is broken or at least easily breakable: It relies on Int 21h Function 02h to return 0 in AL or at least leave it unchanged, which is not the case (see http://www.ctyme.com/intr/rb-2554.htm). Therefore your program will exit properly but return a non-zero completion code which (commonly) indicates failure.

Please use code tags when posting code. The labels are somewhat hard to spot in assembly language code lacking proper formatting like that above.
• September 19th, 2012, 12:45 AM
kevin619
Re: please explain me this multi-digit print program
This is not valid assembly code
• September 19th, 2012, 10:20 AM
Eri523
Re: please explain me this multi-digit print program
I didn't actually try to assemble it, but to me it looks perfectly valid (although, admittedly, not nicely formtted). Why do you think it isn't?