-
Notifications
You must be signed in to change notification settings - Fork 1
/
Strings.asm
47 lines (45 loc) · 1.55 KB
/
Strings.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
; -------------------------------------------------------------------------------------------
; Input:
; ESI = pointer to the string to convert
; ECX = number of digits in the string (must be > 0)
;
; Output:
; EAX = integer value
; -------------------------------------------------------------------------------------------
global _string_to_int
section .text
_string_to_int:
xor ebx, ebx ; clear ebx
.next_digit:
movzx eax, byte [esi]
inc esi
sub al, '0' ; convert from ASCII to number
imul ebx, 10
add ebx, eax ; ebx = ebx*10 + eax
loop .next_digit ; while (--ecx)
mov eax, ebx
ret
; -------------------------------------------------------------------------------------------
; Input:
; EAX = integer value to convert
; ESI = pointer to buffer to store the string in (must have room for at least 10 bytes)
;
; Output:
; EAX = pointer to the first character of the generated string
; -------------------------------------------------------------------------------------------
global _int_to_string
section .text
_int_to_string:
add esi,9
mov byte [esi], 0x00
mov ebx, 10
.next_digit:
xor edx, edx ; Clear edx prior to dividing edx:eax by ebx
div ebx ; eax /= 10
add dl, '0' ; Convert the remainder to ASCII
dec esi ; store characters in reverse order
mov [esi], dl
test eax, eax
jnz .next_digit ; Repeat until eax==0
mov eax, esi
ret