(Help) If length of output string less than 8, then add "0" infront

Started by ragdogz, January 21, 2009, 05:31:55 AM

Previous topic - Next topic

ragdogz

Hi, i'm a very newbie in assembly. i want to learn it, but i have some difficulties to understand it..
right now i only use Visual Basic to make a program..

i have a question about string manipulation in assembly. in the sample code i write here, it's a hashcode generator in assembly. it's working properly. but the problem is, i need the result string in 8 digits precisely. if the length of result string less than 8, then i want to insert "0" in front of it..
for example, if the result string is 4cf7084h, then i need it to be 04cf7084h, or if the result is cf7084h, then i need it to be 00cf7084h and so on til the length is 8..

please take a look at the code below. the output hashcode is 19290h, and i want it to be 00019290h..

.386
.model flat,stdcall
option casemap:none

include c:\masm32\include\windows.inc
include c:\masm32\include\user32.inc
include c:\masm32\include\kernel32.inc
includelib c:\masm32\lib\user32.lib
includelib c:\masm32\lib\kernel32.lib

GetHashCode PROTO:DWORD

.data
hCode db "hai",0

.data?
printBuffer db 100 dup(?)
storeBuffer db 100 dup(?)

.code
start:

invoke GetHashCode, addr hCode
invoke ExitProcess,0

GetHashCode proc hCodeFrmS:DWORD
LOCAL temp:DWORD
jmp @F
printtemplate db "hashcode for the above string is %xh ",0
@@:

invoke RtlZeroMemory, addr printBuffer, 100
mov temp,0
mov esi, hCodeFrmS
mov edi, offset storeBuffer

continue:
mov ecx, 1
rep movsb
mov al,[esi-1]
mov eax,temp
mov edx,31
mul edx
mov temp,eax
xor eax,eax
mov al,[esi-1]
add eax,temp
mov temp,eax
push temp
push offset printtemplate
push offset printBuffer
call wsprintfA
add esp,12
mov al,[esi]
cmp al,0
jne continue
invoke MessageBox,0, addr printBuffer,addr storeBuffer,0
ret
GetHashCode endp
end start


and for building..

@echo off

ml /c /coff /Cp hash.asm
link /SUBSYSTEM:WINDOWS /LIBPATH:\masm32\lib hash.obj


thx very much..

MichaelW

You could do the conversion to hex with the MASM32 dw2hex procedure, which outputs an 8 character string with leading zeros, were necessary.
eschew obfuscation

drizz

The truth cannot be learned ... it can only be recognized.

ragdogz

@MichaelW
thx for directing me to dw2hex, which honestly is really new to me, and i've been spent hours to understand it and no result. perhaps i can learn it better next time..
i appreciate it..

@drizz
ur simple answer really really solve my problem..
i really appreciate it ur help..
thx a lot..

i think this topic is solved..