Here you go, I am feeling really generous
.
Code:
_Num2HexStr:
PUSH EBP
PUSH EAX
PUSH EBX
PUSH ECX
PUSH ESI
MOV EBP, ESP
; === START OPTIONAL CODE ===
.Hexadecimal_Conversion:
MOV ESI, DWORD [EBP + 24]
MOV EBX, DWORD [EBP + 28]
CMP EBX, 0
JE .End_Hex_Conversion_Zero
MOV ECX, 8
.Remove_Lead_Zeros:
ROL EBX, 4
MOV AL, BL
AND BL, 0x0F
CMP BL, 0
MOV BL, AL
JE .Skip_Zero
JMP SHORT .Start_Conversion_Loop
.Skip_Zero:
DEC ECX
JMP SHORT .Remove_Lead_Zeros
.Start_Conversion_Loop:
ROR EBX, 4
; === END OPTIONAL CODE ===
.Conversion_Loop:
ROL EBX, 4
MOV AL, BL
AND AL, 0x0F
OR AL, 0x30
CMP AL, 0x39
JNA .Dont_Add_7
ADD AL, 0x07
.Dont_Add_7:
MOV BYTE [ESI], AL
INC ESI
DEC ECX
CMP ECX, 0
JE .End_Hex_Conversion
JMP SHORT .Conversion_Loop
.End_Hex_Conversion:
MOV BYTE [ESI], 0
JMP .End_Conversion
.End_Hex_Conversion_Zero:
MOV BYTE [ESI], '0'
MOV BYTE [ESI + 1], 0
.End_Conversion:
POP ESI
POP ECX
POP EBX
POP EAX
POP EBP
RET 8
This code works as follows:
You must push two parameters onto the stack, first push the number (eax), then push the address of a buffer to place the string in, then call the function, the parameters will be removed for you, if you want to make the function smaller you can remove the code marked as optional, all it does is remove any leading zeros from the number, for example if you passed the number 0x0003dbb4 it would put in the buffer 3dbb4.
Here is an example of its use, you could write a function that would display the contents of eax like this:
Code:
buffer db 0,0,0,0,0,0,0,0,0 ; requires 9 bytes at the most
_Display_EAX:
push eax
push buffer
call _Num2HexStr
mov si, buffer
call print
ret
Hope this helps.