-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintutils.sx
138 lines (118 loc) · 2.79 KB
/
printutils.sx
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
.data
pointASCII: .quad 0,0,0,0,0,0,0,0
scalarASCII: .quad 0,0,0,0,0,0
u64ASCII: .quad 0,0
nl: .ascii "\n"
.text
.include "aliases"
.include "commonmacros"
/**
int print(char* buffer, int buffer_length);
prints a string to stdout
Input: x0 address of char buffer
x1: length of string
Return value:
x0 number of bytes printed
*/
BEGIN_C_FUNCTION print
mov x2, x1 //len
mov x1, x0 //buf
mov x0, #1 //fd := STDOUT_FILENO
mov w8, #64 //write is syscall #64
svc #0 //invoke syscall
END_C_FUNCTION print
/**
int printtofile(int fd, char* buffer, int buffer_length);
prints a string into a file
Input:
x0: file descriptor of an open file
x1: address of char buffer
x2: length of string
Return value:
x0 number of bytes printed
*/
BEGIN_C_FUNCTION printtofile
mov w8, #64 //write is syscall #64
svc #0 //invoke syscall
END_C_FUNCTION printtofile
/**
Function printU64
prints a long long integer value to stdout
Input:
x0 the integer
*/
BEGIN_C_FUNCTION printU64
INIT_MSKF1
mov sb2, xzr
mov sb3, xzr
CONVERT_U64_TO_ASCII io0 sb3 sb2
adr io1, u64ASCII
stp sb2, sb3, [io1]
mov io2, #17 //length: 16 chars +1 for new line
mov io0, #1 //stdout
mov w8, #64
svc #0
END_C_FUNCTION printU64
BEGIN_C_FUNCTION printScalar
adr io1, scalarASCII
mov io3, io1
CONVERT_SCALAR_TO_ASCII io0 io3
mov io2, #65 //length: 4x16 chars +1 for new line
mov io0, #1 //stdout
mov w8, #64
svc #0
END_C_FUNCTION printScalar
BEGIN_C_FUNCTION printPoint
adr io1, pointASCII
mov io3, io1
CONVERT_SCALAR_TO_ASCII io0 io3
add io0, io0, #32
CONVERT_SCALAR_TO_ASCII io0 io3
mov io2, #129 //length: 8x16 chars +1 for new line
mov io0, #1 //stdout
mov w8, #64
svc #0
END_C_FUNCTION printPoint
/**
The function read
reads a string into a buffer
Input:
x0 address of buffer
x1 length of buffer
Return value:
x0 number of bytes read
*/
BEGIN_C_FUNCTION read
mov x2, x1 //len
mov x1, x0 //buf
mov x0, #0 //fd := STDIN_FILENO
mov x8, #63 //read is syscall #63
svc #0
END_C_FUNCTION read
/**
int close(int fd);
Closes the file descriptor in x0
*/
BEGIN_C_FUNCTION close
mov w8, #57 //close is syscall 57 (0x39)
svc #0 //invoke syscall
END_C_FUNCTION close
/**
int openfile(int dirfd, const char *pathname, int flags, mode_t mode);
*/
BEGIN_C_FUNCTION openfile
mov w8, #56
svc #0
END_C_FUNCTION openfile
/**
int openfileCWD(const char *pathname, int flags, mode_t mode);
opens a file in the current working directory
*/
BEGIN_C_FUNCTION openfileCWD
mov x3, x2
mov x2, x1
mov x1, x0
mov x0, -100 //AT_CWD = -100
mov w8, #56
svc #0
END_C_FUNCTION openfileCWD