-
Notifications
You must be signed in to change notification settings - Fork 12
/
print.asm
64 lines (47 loc) · 879 Bytes
/
print.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
%include "print_int.mac"
%include "syscall.mac"
section .data
DEF_STR_DATA newline, 10
section .bss
print_num_buf resb 8
print_num_buf_end equ $
section .text
global print
global print_num
global newline
global newline_len ; generated by DEF_STR_DATA
; rax: pointer to string
; rdx: string length
print:
mov rcx, STDOUT
call write
ret
; rax: number
print_num:
push rbx
push rsi
mov rbx, 10 ; for idiv
mov rcx, 0 ; counter for digits
.loop:
; divide [rdx:rax] by rbx
; rdx contains remainder - digit
mov rdx, 0
idiv rbx
; add code of 0 to get ASCII char code
add dl, '0'
; save digit in the first free cell in
; buffer from the end
inc rcx
mov rsi, print_num_buf_end
sub rsi, rcx
mov [rsi], dl ; save
cmp rax, 0
jne .loop
mov rax, print_num_buf_end
sub rax, rcx
mov rdx, rcx
call print
pop rsi
pop rbx
ret
; vim:ft=nasm