Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

73 rindas
1.6 KiB

pirms 5 gadiem
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ; Puts the stringified version of eax on the stack
  3. ; IN: eax = number to write
  4. ; IN: edi = base of where to write string
  5. ; OUT: ecx = number of characters written to buffer
  6. IntToString: ; (eax = number, edi = buffer base) -> ecx = number of characters
  7. push eax
  8. push edi
  9. ;push ecx
  10. push edx
  11. push esi
  12. push ebp
  13. mov ecx, edi ; store negative original buffer base
  14. neg ecx ; (used for counting amount of characters)
  15. test eax, eax
  16. jns .not_negative
  17. mov BYTE [edi], '-'
  18. neg eax
  19. add edi, 1
  20. .not_negative:
  21. mov esi, edi ; store pointer after potential negative sign character
  22. mov ebp, 0xcccccccd ; ebp = 32 bit multiplicative reciprocal of 10
  23. .divide_loop:
  24. mov ebx, eax ; save number in ebx
  25. mul ebp
  26. shr edx, 3 ; edx = quotient
  27. lea eax, [edx + edx*4] ; eax = quotient * 10 = (number - remainder)
  28. add eax, eax ; |
  29. sub ebx, eax ; ebx = number - (number - remainder) = remainder
  30. add ebx, '0' ; remainder is our digit, turn it ASCII
  31. mov BYTE [edi], bl ; store ASCII digit to buffer
  32. lea edi, [edi+1]
  33. mov eax, edx ; We need quotient in eax for 'mul'
  34. test eax, eax
  35. jne .divide_loop
  36. add ecx, edi ; Update amount of characters
  37. mov byte [edi], 0 ; Null terminator
  38. lea edi, [edi-1] ; We incremented the buffer pointer once too much
  39. .swap_loop:
  40. cmp esi, edi
  41. jae .done_swapping
  42. movzx eax, BYTE [edi]
  43. movzx edx, BYTE [esi]
  44. mov BYTE [edi], dl
  45. mov BYTE [esi], al
  46. sub edi, 1
  47. add esi, 1
  48. jmp .swap_loop
  49. .done_swapping:
  50. pop ebp
  51. pop esi
  52. pop edx
  53. ;pop ecx
  54. pop edi
  55. pop eax
  56. ret