|
|
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; Puts the stringified version of eax on the stack
- ; IN: eax = number to write
- ; IN: edi = base of where to write string
- ; OUT: ecx = number of characters written to buffer
- IntToString: ; (eax = number, edi = buffer base) -> ecx = number of characters
- push eax
- push edi
- ;push ecx
- push edx
- push esi
- push ebp
-
- mov ecx, edi ; store negative original buffer base
- neg ecx ; (used for counting amount of characters)
-
- test eax, eax
- jns .not_negative
-
- mov BYTE [edi], '-'
- neg eax
- add edi, 1
-
- .not_negative:
- mov esi, edi ; store pointer after potential negative sign character
- mov ebp, 0xcccccccd ; ebp = 32 bit multiplicative reciprocal of 10
-
- .divide_loop:
-
- mov ebx, eax ; save number in ebx
-
- mul ebp
- shr edx, 3 ; edx = quotient
-
- lea eax, [edx + edx*4] ; eax = quotient * 10 = (number - remainder)
- add eax, eax ; |
-
- sub ebx, eax ; ebx = number - (number - remainder) = remainder
-
- add ebx, '0' ; remainder is our digit, turn it ASCII
- mov BYTE [edi], bl ; store ASCII digit to buffer
- lea edi, [edi+1]
-
- mov eax, edx ; We need quotient in eax for 'mul'
-
- test eax, eax
- jne .divide_loop
-
- add ecx, edi ; Update amount of characters
-
- mov byte [edi], 0 ; Null terminator
-
- lea edi, [edi-1] ; We incremented the buffer pointer once too much
-
- .swap_loop:
- cmp esi, edi
- jae .done_swapping
- movzx eax, BYTE [edi]
- movzx edx, BYTE [esi]
- mov BYTE [edi], dl
- mov BYTE [esi], al
- sub edi, 1
- add esi, 1
- jmp .swap_loop
-
- .done_swapping:
-
- pop ebp
- pop esi
- pop edx
- ;pop ecx
- pop edi
- pop eax
- ret
|