tags:

views:

33

answers:

1

i have double word number in si:bx. How can i write it to array as string?

+2  A: 

The simplest way is to convert the number to hex. Each group of 4 bits becomes a hex digit.

; this is pseudocode
mov di, [addr_of_buffer]  ; use the string instructions
cld                       ; always remember to clear the direction flag

; convert bx
mov cx, 4    ; 4 hexits per 16 bit register
hexLoopTop:
mov al, bl   ; load the low 8 bits of bx
and al, 0x0F ; clear the upper 4 bits of al
cmp al, 10
jl decimal
add al, 65   ; ASCII A character
jmp doneHexit:
decimal:
add al, 48   ; ASCII 0 character
doneHexit:
stosb        ; write al to [di] then increment di (since d flag is clear)
ror bx       ; rotate bits of bx down, happens 4 times so bx is restored
loop hexLoopTop

; repeat for si, note that there is no need to reinit di

Now if you want decimal output, you'll need a rather more complicated algorithm. The obvious way to do that is by repeated division. Note that if you have a 32bit CPU (386 or later) you can use the 32bit registers from 16 bit mode, which makes this easier.

Move si:bx into eax to start. To get the next digit you execute a cdq instruction to extend eax into edx:eax, div by 10, and the remainder in edx is the next digit. The quotient in eax is either 0 in which case you're done, or the input for the next round of the loop. This will give you the most significant digit first, so you'll need to reverse the digits when you're done. Your buffer will need to be at least the base 10 log of 2^32 rounded up which is 10. It is not too hard to adjust this algorithm to work with signed integers.

Callum