Advertisement
Guest User

Updated itoa

a guest
Jul 2nd, 2022
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. section .text
  2.   global _start
  3.  
  4. %include "includes/print.asm"      ; syscall to print to screen function
  5. %include "includes/leave.asm"      ; syscall to exit program
  6.  
  7. itoa:
  8.   push ebp
  9.   mov ebp, esp
  10.   mov ebx, 0                       ; i = 0
  11. itoa_start:
  12.   mov esi, [ebp+8]
  13.   mov edi, [ebp+12]
  14.   xor edx, edx                     ; clear edx
  15.   mov eax, esi
  16.   mov ecx, 10                      ; move 10 into ecx (divisor)
  17.   div ecx                          ; div ecx (ex. 1234 / 10 = 1234.5)
  18.   mov [ebp+8], eax
  19.   add edx, '0'                     ; add '0' (48) to remainder to convert into ASCII (5 -> '5')
  20.   mov [edi + ebx], dl
  21.   cmp eax, 0                       ; compare remainder (edx ex. .5) to 0
  22.   je itoa_end                      ; if remainder == 0 jump to end label
  23.   add ebx, 1                       ; increase ebx value by 1
  24.   jmp itoa_start
  25. itoa_end:
  26.   add ebx, 1                       ; increase ebx value by 1 for terminating string
  27.   mov dword [edi + ebx], 0
  28.   mov eax, edi
  29.   mov esp, ebp
  30.   pop ebp
  31.   ret
  32.  
  33. _start:
  34.   push res
  35.   push num
  36.   call itoa
  37.   pop ebx
  38.  
  39.   mov ecx, eax
  40.   mov edx, 5
  41.   call print                       ; print res string
  42.  
  43.   call leave
  44.  
  45.  
  46. section .data
  47.  
  48.   num dd 12345                     ; Defining word num with value 12345
  49.  
  50. section .bss
  51.  
  52.   res resd 6                       ; Reserving double word of 6 for res variable
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement