-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatoi.S
More file actions
38 lines (35 loc) · 848 Bytes
/
atoi.S
File metadata and controls
38 lines (35 loc) · 848 Bytes
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
.global _atoi
.global _print
/* atoi takes a pointer to a string
* Iterate through the string with
* S as the sum so far, d current digit
* S' = 10*S + d
* This will assume no negatives!
* The pointer is passed on the stack
*/
.text
_atoi:
xor %rdi,%rdi
# rsi : ptr to the string yet to be processed
# rdi : sum
# al : next digit
mov 8(%rsp),%rsi # A pointer to the string
loop:
xor %rcx,%rcx # Zero out the upper bits of rcx
movb (%rsi),%cl # rcx has the ascii rep of next digit
cmp $0,%cl # Check for null byte
je end
cmp $48,%cl # Check for non-digit (too low)
jb end
cmp $58,%cl # Check for non-digit (too high)
jge end
inc %rsi
sub $48,%cl # rcx has the decimal value
mov $10,%rax
mul %rdi
add %rcx,%rax
mov %rax,%rdi
jmp loop
end:
mov %rdi,%rax # Move the result in %rax to return
ret