-
Notifications
You must be signed in to change notification settings - Fork 0
/
printf.asm
78 lines (73 loc) · 1.62 KB
/
printf.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
format ELF64
include "print_decimal.asm"
include "print_string.asm"
include "print_char.asm"
public printf
section '.printf' executable
; | input:
; rax = format
; stack = values
; | output:
; rax = count
printf:
push rbx
push rcx
; call/ret = 8byte
; rax+rbx+rcx = 24byte
mov rbx, 32
; count of format elements
xor rcx, rcx
.next_iter:
cmp [rax], byte 0
je .close
cmp [rax], byte '%'
je .special_char
jmp .default_char
.special_char:
inc rax
cmp [rax], byte 's'
je .print_string
cmp [rax], byte 'd'
je .print_decimal
cmp [rax], byte 'c'
je .print_char
cmp [rax], byte '%'
je .default_char
jmp .is_error
.print_string:
push rax
mov rax, [rsp+rbx]
call print_string
pop rax
jmp .shift_stack
.print_decimal:
push rax
mov rax, [rsp+rbx]
call print_decimal
pop rax
jmp .shift_stack
.print_char:
push rax
mov rax, [rsp+rbx]
call print_char
pop rax
jmp .shift_stack
.default_char:
push rax
mov rax, [rax]
call print_char
pop rax
jmp .next_step
.shift_stack:
inc rcx
add rbx, 8
.next_step:
inc rax
jmp .next_iter
.is_error:
mov rcx, -1
.close:
mov rax, rcx
pop rcx
pop rbx
ret