ui32toa 模拟

; Prototype
;
eax ui32toa(unsigned int num, char *str, int base)
;
Param
;
num: The unsigned 32-bit integer to be tranlated.
;
str: The address of a buffer where to save the ASCII result.
;
base: The basement of division.
;
Function own the stack.
;
Paramter push from left to right.
ui32toa PROC NEAR32
push ebp
mov ebp, esp
pushf
push ebx ; Base of divisor
push edx
push esi
push edi

mov ebx, [ebp + 8]
mov esi, [ebp + 12]
mov edi, esi
mov eax, [ebp + 16]
cld

; Add a null terminate character in the front of buffer.
push eax
mov eax, 0
stosb
pop eax

; Integer to ASCII.
while_ui32toa:
mov edx, 0
div ebx
cmp dl, 10
jb below_10_ui32toa
sub dl, 10 ; dl > 10 => dl -= 10
add dl, 31h ; Add 11h additional to hexidecimal character and so on.
below_10_ui32toa:
add dl, 30h ; To ASCII.

xchg eax, edx
stosb
xchg eax, edx
cmp eax, 0
jne while_ui32toa

; Reverse the ASCII string
dec edi
while_reverse_ui32toa:
mov al, [esi]
xchg al, [edi]
mov [esi], al
inc esi
dec edi
cmp esi, edi
jb while_reverse_ui32toa

exit_ui32toa:
pop edi
pop esi
pop edx
pop ebx
popf
pop ebp
ret 4
ui32toa ENDP


; Prototype
;
eax i32toa(int num, char *str, int base)
;
Param
;
num: The unsigned 32-bit integer to be tranlated.
;
str: The address of a buffer where to save the ASCII result.
;
base: The basement of division.
;
Function own the stack.
;
Paramter push from left to right.
i32toa PROC NEAR32
push ebp
mov ebp, esp
pushf
push ebx ; Base of divisor
push edx
push esi
push edi

mov ebx, [ebp + 8]
mov esi, [ebp + 12]
mov edi, esi
mov eax, [ebp + 16]
cld

; Add a null terminate character in the front of buffer.
push eax
mov eax, 0
stosb
pop eax

; Integer to ASCII.
while_i32toa:
cdq
idiv ebx
cmp dl, 0
jge greater_0_i32toa
neg dl ; dl < 0 => dl = -dl
greater_0_i32toa:
cmp dl, 10
jl less_10_i32toa
sub dl, 10 ; dl > 10 => dl -= 10
add dl, 31h ; Add 11h additional to hexidecimal character and so on.
less_10_i32toa:
add dl, 30h ; To ASCII.

xchg eax, edx
stosb
xchg eax, edx
cmp eax, 0
jne while_i32toa

; Judge the sign
mov eax, [ebp + 16]
cmp eax, 0
jge positive_i32toa
mov al, '-'
stosb
positive_i32toa:

; Reverse the ASCII string
dec edi
while_reverse_i32toa:
mov al, [esi]
xchg al, [edi]
mov [esi], al
inc esi
dec edi
cmp esi, edi
jb while_reverse_i32toa

exit_i32toa:
pop edi
pop esi
pop edx
pop ebx
popf
pop ebp
ret 4
i32toa ENDP

  
posted @ 2011-08-30 11:37  walfud  阅读(388)  评论(0编辑  收藏  举报