Hey, its me the newbie again. How would you convert any type of number (dd,dw,db,etc..) into a String? In all of the other languages this is easy. I've been trying to figure this out, but I've had no luck but I am a newbie....
This Topic has discussed millions of times on this board.
Search for "dwtoa" (dword to ascii) brings up over 50 hits.
Search for "dwtoa" (dword to ascii) brings up over 50 hits.
I found lots of results, but all the results besides this one was for MASM, and referred to MASM32.Lib. I use Fasm, is the algorithm the same?
Found something about wsprintf, but I can't seem to import in FASM. Is it because it uses the _cdecl calling convention?
If you use Fasm in MS COFF format and link to make the exe then you can use the functions from masm32.lib as you wish, simply include something like the following in your Fasm source:
extrn _dwtoa@8:dword
dwtoa equ _dwtoa@8
extrn _dwtoa@8:dword
dwtoa equ _dwtoa@8
If you want a FASM translation of dwtoa from the MASM library
[size=9]SECTION ".data" DATA READABLE WRITEABLE
buffer RB 16
... blah! blah! blah!
proc dwtoa, dwValue, lpBuffer
enter
push ebx
push esi
push edi
mov eax, [dwValue]
mov edi, [lpBuffer]
test eax, eax
jnz __sign
mov WORD [edi], 30h
leave
retn 8
__sign:
jns __pos
mov BYTE [edi], '-'
neg eax
inc edi
__pos:
mov ecx, 429496730
mov esi, edi
__while_1:
test eax, eax
jle __e_while_1
mov ebx, eax
mul ecx
mov eax, edx
lea edx, [edx*4+edx]
add edx, edx
sub ebx, edx
add bl, '0'
mov [edi], bl
inc edi
jmp __while_1
__e_while_1:
mov BYTE [edi], 0
__while_2:
cmp esi, edi
jae __e_while_2
dec edi
mov al, BYTE [esi]
mov ah, BYTE [edi]
mov BYTE [edi], al
mov BYTE [esi], ah
inc esi
jmp __while_2
__e_while_2:
pop edi
pop esi
pop ebx
leave
retn 8[/size]
usage: stdcall dwtoa, -9999999, bufferthat doesn't work...it only works if you put like stdcall dwtoa,999,buffer but it doesn't work if its stdcall dwtoa,var1,buffer even though var1 is a dw type. it says 4198554 just like when i tried to do it with wsprintf.........var1 is a dw by the way
CyberGut, first dwtoa,var1,buffer wouldn't work, you'd have to do dwtoa,,buffer. Perhaps MASm would be more your style, its suited better to noobs ;) .
Secondly the routine is designed to work with dwords, not words. If you want a generic routine which works with any input then either write yourself some good macros or use a HLL, don't however complain to someone who is only trying to be helpful, noone else will help you then :mad: .
Secondly the routine is designed to work with dwords, not words. If you want a generic routine which works with any input then either write yourself some good macros or use a HLL, don't however complain to someone who is only trying to be helpful, noone else will help you then :mad: .
I wasn't complaining....I was just saying what happened...its all depends on how you interpret what i wrote....var1 is a dword, and when I do "dwtoa,,buffer " it the ASM compiler doesn't bring up any errors, but when I run the EXE file I get an internal error....thanks anyway for trying to help me.
Well then I think I spot your mistake, dw doesn't mean dword, it means data word i.e two bytes. You should be using dd, data dword :) .
[size=9]FORMAT PE GUI 4.0
ENTRY START
INCLUDE "\fasm\include\kernel.inc"
INCLUDE "\fasm\include\user.inc"
INCLUDE "\fasm\include\macro\stdcall.inc"
INCLUDE "\fasm\include\macro\import.inc"
SECTION ".data" DATA READABLE WRITEABLE
var1 DD 9
var2 DW 12005
var3 DB -128
buffer RB 16
SECTION ".code" CODE READABLE EXECUTABLE
proc dwtoa, dwValue, lpBuffer
enter
push ebx
push esi
push edi
mov eax, [dwValue]
mov edi, [lpBuffer]
test eax, eax
jnz __sign
mov WORD [edi], 30h
leave
retn 8
__sign:
jns __pos
mov BYTE [edi], '-'
neg eax
inc edi
__pos:
mov ecx, 429496730
mov esi, edi
__while_1:
test eax, eax
jle __e_while_1
mov ebx, eax
mul ecx
mov eax, edx
lea edx, [edx*4+edx]
add edx, edx
sub ebx, edx
add bl, '0'
mov [edi], bl
inc edi
jmp __while_1
__e_while_1:
mov BYTE [edi], 0
__while_2:
cmp esi, edi
jae __e_while_2
dec edi
mov al, BYTE [esi]
mov ah, BYTE [edi]
mov BYTE [edi], al
mov BYTE [esi], ah
inc esi
jmp __while_2
__e_while_2:
pop edi
pop esi
pop ebx
leave
retn 8
START:
stdcall dwtoa, [var1], buffer
invoke MessageBox, NULL, buffer, NULL, MB_OK
stdcall dwtoa, -99998, buffer
invoke MessageBox, NULL, buffer, NULL, MB_OK
stdcall dwtoa, 99998, buffer
invoke MessageBox, NULL, buffer, NULL, MB_OK
[color=#3366FF]xor eax, eax
movsx eax, [var2][/color]
stdcall dwtoa, eax, buffer
invoke MessageBox, NULL, buffer, NULL, MB_OK
[color=#3366FF]xor eax, eax
movsx eax, [var3][/color]
stdcall dwtoa, eax, buffer
invoke MessageBox, NULL, buffer, NULL, MB_OK
invoke ExitProcess, 0
SECTION ".idata" IMPORT DATA READABLE WRITEABLE
library kernel, "kernel32.dll", \
user, "user32.dll"
kernel:
import ExitProcess, "ExitProcess"
user:
import MessageBox, "MessageBoxA"[/size]