martedì 22 marzo 2011

LeadingZero - aggiungere degli zeri in testa ad una stringa

Questa funzione aggiunge degli zeri "0" in testa ad un stringa fino al raggiungimento di una lunghezza prefissata:


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Push "12345"
; Push 8
; Call LeadingZero
; Pop $0
; DetailPrint "result: $0" ; 00012345
Function LeadingZero
Exch $R2 ; zero
Exch
Exch $R1 ; number
Push $R0 ; result
Exch 2
Push $R3
StrCpy $R0 $R1

Loop:
StrLen $R3 $R0
${If} $R3 >= $R2
GoTo Exit
${ElseIf} $R3 > 32 ; exit if is more of 32 len
GoTo Exit
${EndIf}
StrCpy $R0 "0$R0"
GoTo Loop

Exit:
Pop $R3
Pop $R2
Pop $R1
Exch $R0
FunctionEnd

!macro LeadingZero Res Num Zero
Push "${Num}"
Push "${Zero}"
Call LeadingZero
Pop "${Res}"
!macroend

!define LeadingZero "!insertmacro LeadingZero"