Advertisement
Guest User

atoi

a guest
Nov 19th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SAS 0.93 KB | None | 0 0
  1. %include "io.inc"
  2.  
  3. section .data
  4.     mystring db "-543", 0
  5.  
  6. section .text
  7. global CMAIN
  8. mul_10:
  9.     push ebp
  10.     mov ebp, esp
  11.     mov ecx, [ebp + 8]
  12.     shl ecx, 1
  13.     mov ebx, ecx
  14.     shl ecx, 2
  15.     add ebx, ecx
  16.     leave
  17.     ret
  18.  
  19.  
  20. atoi:
  21.     push ebp
  22.     mov ebp, esp
  23.     cld
  24.     mov esi, [ebp + 8]
  25.     xor ebx, ebx
  26.     xor eax, eax
  27.     xor edx, edx
  28.    
  29.     lodsb
  30.     cmp al, '-'
  31.     jne not_negative
  32.     mov edx, 1
  33. add_digit:
  34.     lodsb
  35.     test al, al
  36.     je end
  37. not_negative:
  38.     push ebx
  39.     call mul_10
  40.     add esp, 4
  41.     add ebx, eax
  42.     sub ebx, '0'
  43.     ;PRINT_DEC 1, al
  44.     jmp add_digit
  45.    
  46. end:
  47.     cmp edx, 1
  48.     PRINT_DEC 4, edx
  49.     jne not_negative_end
  50.     neg ebx
  51. not_negative_end:
  52.     NEWLINE
  53.     PRINT_DEC 4, ebx
  54.     leave
  55.     ret
  56.  
  57. CMAIN:
  58.     mov ebp, esp; for correct debugging
  59.    
  60.     push mystring
  61.     call atoi
  62.     pop eax
  63.    
  64.     mov ebp, esp
  65.     xor eax, eax
  66.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement