I have a small problem with converting my 16 Bit code to 32 bit. The code in question is converting a binary number to ascii, now this works O.K. in my original 16 bit code, but I can't get it to work in 32 bit. The problem seems to be when I divide by 10 i.e.

mov ecx,10
div ecx

where in 32 bit assembler does the remainder finish up? :confused:
Posted on 2001-08-25 08:45:33 by Mel
in win 32 you don't need to use a routine inside your app.
You can use routines ready for this.
Look at wsprintf function on win32 Programmer's Reference.

If you want you can always use your own routine but it must be adapted to 32bit registers.
Posted on 2001-08-25 10:20:19 by Bit7
the remainder is in edx. you should delete the contents of edx to make a division.

NOP-erator
Posted on 2001-08-25 11:43:15 by NOP-erator
Speaking of binary to ASCII, I've been looking for an excuse to post this. :) It does ths usual conversion and also inserts commas to make the result easier to read.



; This function converts a binary DWORD to an ASCII string, inserting commas
; as needed. and places a sign in the last position. A - is used for negative
; numbers, and a space for positive numbers.
;
; Example code:
; ...
; dtoae proto :dword, :dword
; ...
; invoke dtoae, inputdw, addr outstring
; ...
;
; The first parameter is the 32-bit value to be converted. The second
; parameter is the address of a string that will contain the result. A
; maximum of 15 characters will be returned in the edited, signed, null
; terminated string. Never allocate less than 4 bytes for the string,
; and use caution if reserving fewer than 15 bytes.
;
; EAX returns the length of the string, not including the null terminator.
; If you don't care about the sign, and/or if you want to eliminate the
; ending space character sign in positive numbers, use the following code
; after your INVOKE to terminate the string at the last digit:
;
; dec eax
; mov outstring [eax], 0
;
; Example results:
;
; Source value Result string
; ------------ --------------
; -1234567890 1,234,567,890-
; -1000 1,000-
; -999 999-
; 0 0
; 123 123
; 12345 12,345
; 999999 999,999
; 1000000 1,000,000
; 111111111 111,111,111
; 2000000000 2,000,000,000
;
; standard proc startup
;
.586
.model flat, stdcall
.code
dtoae proc idwd:dword, ostr:dword
;
; get and check caller parm - save regs - setup buffer and loop 1
;
push edi ; save edi
mov edi, [ostr] ; get pointer to result string
mov eax, idwd ; get input parm
or eax, eax ; test it
jz zxit ; if 0 quick exit
push esi ; save other caller regs
push edx ; .
push ecx ; .
mov byte ptr [edi], ' ' ; clear result sign
jns @f ; if parm negative...
mov byte ptr [edi], '-' ; minus result sign
neg eax ; make parm positive
@@: inc edi ; pointer +1
mov ecx, 3 ; number of digits before comma
;
; loop 1 - divide parm by 10 - convert remainder to ascii character and
; place in string - place commas in string as needed -
; string is in reverse order when done
;
@@: xor edx, edx ; clear for div
div k10 ; divide parm by 10
or dl, 30h ; make remainder ascii digit
mov byte ptr [edi], dl ; save in string
inc edi ; pointer +1
or eax, eax ; are we done?
jz @f ; yes - exit loop
dec ecx ; digits before comma -1
jnz @b ; if comma needed...
mov byte ptr [edi], ',' ; save , in string
inc edi ; pointer +1
mov ecx, 3 ; set digits before next comma
jmp @b ; continue loop
;
; get string length - terminate string - setup loop 2
;
@@: mov ecx, edi ; point to end of string
sub ecx, [ostr] ; compute string length
push ecx ; save it
sar ecx, 1 ; compute loop count
mov byte ptr [edi], 0 ; null terminate string
dec edi ; end pointer -1
mov esi, [ostr] ; point to start of string
;
; loop 2 - flip the string - includes sign byte
;
@@: mov al, byte ptr [esi] ; get character from start of string
mov dl, byte ptr [edi] ; get char from end
mov byte ptr [esi], dl ; put it in start
mov byte ptr [edi], al ; and put start character in end
dec ecx ; count -1
jz @f ; exit loop if done
dec edi ; end pointer -1
inc esi ; start pointer +1
jmp @b ; continue loop
;
; exit with string length (less null) in eax
;
@@: pop eax ; get string length
pop ecx ; restore caller regs
pop edx ; .
pop esi ; .
pop edi ; .
ret ; return to caller
;
; quick exit if called with 0
;
zxit: mov dword ptr [edi], 00002030h ; quick move - zero space null null
mov eax, 2 ; set string length
pop edi ; restore edi
ret ; return to caller
;
align 4 ; align on dword
k10 dd 10 ; constant for div
;
dtoae endp
end
Posted on 2001-08-25 12:26:09 by S/390
Nice :)

As well Mel, if your looking for more information in DIV in general, i posted a discussion on my web-site on just this issue (from the old message board, and i thought it was worth saving..)

Anywho check it out if you think it could help...

The Ins and Outs of DIV

Hope it helps...
:alright:
NaN
Posted on 2001-08-25 16:12:58 by NaN
Thanks everyone very helpful, seems like I already new the basics so it must be my code that's incorrect.
By the way has anyone used 'dwtoa' proc in the MASM32 library looks like this does the same thing,
Mel
Posted on 2001-08-26 03:08:42 by Mel