All pastes #294104 Raw Edit

kastein

public text v1 · immutable
#294104 ·published 2006-12-28 04:43 UTC
rendered paste body
; value in eax on entry
; pointer to buffer in ebx
; preserves all registers and flags

itoa:
	; start by storing all registers
	push eax
	push ebx
	push ecx
	pushfd
	; set up loop counter
	mov ecx, 8
startloop:
	; get next 4 bits into LSBs of EAX
	rol eax, 4
	; store progress
	push eax
	; mask into a single nibble
	and eax, 0Fh
	; shift into ASCII digits range
	add eax, 30h
	; if it's above 39h we need to add another 7 to make it alpha range
	cmp eax, 39h
	jna digits_range
	add eax, 7
digits_range:
	; store character
	mov [ebx], al
	; next buffer location
	inc ebx
	; restore progress
	pop eax
	; decrement loop counter
	dec ecx
	; if we still have work to do, go back and do it
	jnz startloop
endloop:
	; restore registers
	popfd
	pop ecx
	pop ebx
	pop eax
	retn