View Single Post
Old May 18th, 2006, 12:47 PM   #12
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>but why did u put 2 in the di regester
Here's how the division algorithm works:
123 / 10
AX = 12
DX = 3

12 / 10
AX = 1
DX = 2

1 / 10
AX = 0
DX = 1
Notice how DX, the value that is used to build the string, gives the digits in reverse order. By starting di at 2 and decrementing it, the algorithm builds the string in reverse order, thus negating the reverse order of the digits:
      --0---1---2---3--
save: |   |   |   |'$'|
      -----------------

DX = '3'
DI = 2

      --0---1---2---3--
save: |   |   |'3'|'$'|
      -----------------

DX = '2'
DI = 1

      --0---1---2---3--
save: |   |'2'|'3'|'$'|
      -----------------

DX = '1'
DI = 0

      --0---1---2---3--
save: |'1'|'2'|'3'|'$'|
      -----------------
You can see what would happen if you just start at the beginning and go to the end by changing the corresponding portion of the code to this:
	mov	di,0

    convert:
	mov	dx,0
	div	bx
	add	dx,'0'
	mov	[save+di],dl
	inc	di
	test	ax,ax
	jnz	convert
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote