-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibio.asm
142 lines (108 loc) · 1.74 KB
/
libio.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
; -----------------------------------------------------------------------------
; name:
; libio.asm
; author:
; zxsvrx
; copyright:
; Copyright (c) 2024 zxsvrx
; license:
; MIT License (see LICENSE in /master)
; platform:
; macOS
; architecture:
; x86_64
; syntax:
; nasm
; compiler:
; nasm 2.16.01
; compile with:
; nasm -f macho64 -o libio.o libio.asm
;
; A simple input output (io) library that provides subroutines to other
; assembly programs
;
; Functions:
; io_print(adr: 8)
; io_println(adr: 8)
; io_getc()
; io_getline(adr: 8, len: 4)
; -----------------------------------------------------------------------------
section .text
global io_print
global io_println
global io_getc
global io_getline
global io_strlen
io_strlen:
push rbp
mov rbp, rsp
mov rax, 0
io_strlen.loop:
cmp BYTE [rdi], 0
je io_strlen.exit
inc rdi
inc rax
jmp io_strlen.loop
io_strlen.exit:
pop rbp
ret
io_print:
push rbp
mov rbp, rsp
mov rcx, rdi
call io_strlen
mov rdx, rax
mov rsi, rcx
mov rdi, 1
mov rax, 0x2000004
syscall
pop rbp
ret
io_println:
push rbp
mov rbp, rsp
mov rcx, rdi
call io_strlen
mov rdx, rax
mov rsi, rcx
mov rdi, 1
mov rax, 0x2000004
syscall
mov [rbp - 1], BYTE 10
mov rdx, 1
mov rsi, rbp
dec rsi
mov rdi, 1
mov rax, 0x2000004
syscall
pop rbp
ret
io_getc:
push rbp
mov rbp, rsp
mov rax, 0x2000003
mov rdi, 0
lea rsi, [rel input_buffer]
mov rdx, 256
syscall
xor rax, rax
mov al, BYTE [rel input_buffer]
pop rbp
ret
io_getline:
push rbp
mov rbp, rsp
mov QWORD [rbp - 8], QWORD rdi
mov rdx, rsi
mov rsi, rdi
mov rdi, 0
mov rax, 0x2000003
syscall
mov rdi, QWORD [rbp - 8]
add rdi, rax
dec rdi
mov [rdi], BYTE 0
pop rbp
ret
section .bss
input_buffer: resb 256