Hello,
I need to convert a variable of type DWORD in a String. I know there are functions in the masm32.lib that do this, as I have searched the board and read about it. However, I want to know how it works and what it does (since I want to learn) - can't simply open the lib...
Does anyone know a proc that does this, that I can look at? I coulddn't find any on the board and I'm not sure how to go about it.
Thanks,
~Adam
Posted on 2002-09-18 09:01:33 by adamjjackson
It is a FAQ : http://www.asmcommunity.net/board/index.php?topic=1123

IIRC the complete source code to the MASM32 library is available with it (I even think the lib is self-generated during the MASM32 setup from the source).
Posted on 2002-09-18 09:07:25 by JCP
Hi,
Thanks, I've found it, should have just looked harder! Just got to understand it now. I'll post it here too:



; -----------------------------------------
; This procedure was written by Tim Roberts
; -----------------------------------------

.386
.model flat, stdcall ; 32 bit memory model
option casemap :none ; case sensitive

.code

; ##################################################

atodw proc uses edi esi String:PTR BYTE

;----------------------------------------
; Convert decimal string into dword value
; return value in eax
;----------------------------------------

xor eax, eax
mov esi, [String]
xor ecx, ecx
xor edx, edx
mov al, [esi]
inc esi
.if al == '-'
mov edx, -1
mov al, [esi]
inc esi
.endif
.while al != 0
sub al, '0' ; convert to bcd
lea ecx, [ecx+ecx*4] ; ecx = ecx * 5
lea ecx, [eax+ecx*2] ; ecx = eax + old ecx * 10
mov al, [esi]
inc esi
.endw

lea eax, [ecx+edx] ; move to eax and negate
xor eax, edx

ret

atodw endp

; #########################################################################

end
Posted on 2002-09-18 09:16:11 by adamjjackson
Sorry, I mean:


; #########################################################################

; -----------------------------------------
; This procedure was written by Tim Roberts
; -----------------------------------------

.386
.model flat, stdcall ; 32 bit memory model
option casemap :none ; case sensitive

.code

; #########################################################################

dwtoa proc public uses esi edi dwValue:DWORD, lpBuffer:PTR BYTE

; -------------------------------------------------------------
; convert DWORD to ascii string
; dwValue is value to be converted
; lpBuffer is the address of the receiving buffer
; EXAMPLE:
; invoke dwtoa,edx,ADDR buffer
;
; Uses: eax, ecx, edx.
; -------------------------------------------------------------

mov eax, dwValue
mov edi, [lpBuffer]

.if (eax != 0)

; Is the value negative?

.if (sdword ptr eax < 0)
mov byte ptr [edi], '-' ; store a minus sign
inc edi
neg eax ; and invert the value
.endif

mov esi, edi ; save pointer to first digit

mov ecx, 10
.while (eax > 0) ; while there is more to convert...
xor edx, edx
div ecx ; put next digit in edx
add dl, '0' ; convert to ASCII
mov [edi], dl ; store it
inc edi
.endw

mov byte ptr [edi], 0 ; terminate the string

; We now have all the digits, but in reverse order.

.while (esi < edi)
dec edi
mov al, [esi]
mov ah, [edi]
mov [edi], al
mov [esi], ah
inc esi
.endw

.else
mov word ptr [edi], 0030H ; string "0", 0 in little endian
.endif

ret

dwtoa endp

; #########################################################################

end
Posted on 2002-09-18 09:24:50 by adamjjackson