-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.c
244 lines (199 loc) · 5.35 KB
/
driver.c
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include "def.h"
#include "print.h"
#include "sbrk.h"
#include "string.h"
//Structs
typedef struct node {
int value;
float number;
struct node *next;
} node_t;
// Tests
void node_int_test(void);
void node_float_test(void);
void dynamic_int_array(void);
void dynamic_float_array(void);
void string(void);
_Noreturn void exit(int code)
{
/* Infinite for-loop since this function can't return */
for (;;) {
asm("mov %0, %%rax\n\t"
"mov %1, %%rdi\n\t"
"syscall\n\t"
:
: "r" ((uint64) SYS_exit),
"r" ((uint64) code)
: "%rax", "%rdi");
}
}
int main(void) {
node_int_test();
dynamic_int_array();
string();
// node_float_test();
// dynamic_float_array();
return 0;
}
// https://www.reddit.com/r/C_Programming/comments/se3kgi/hello_world_without_libc/
__asm (
".global _start\n"
"_start:\n"
" movl (%rsp), %edi\n"
" lea 8(%rsp), %rsi\n"
" call main\n"
" movl %eax, %edi\n"
" movl $60, %eax\n"
" syscall\n"
);
void node_int_test(void) {
// Create an empty linked list
node_t *head = NULL;
// Allocate the first node
node_t *new_node = (node_t *) sbrk(sizeof(node_t));
if (new_node == NULL) {
printf("Error: out of memory\n");
return;
}
new_node->value = 50;
new_node->next = NULL;
head = new_node;
// Allocate a second node
new_node = sbrk(sizeof(node_t));
if (new_node == NULL) {
printf("Error: out of memory\n");
return;
}
new_node->value = 99;
new_node->next = NULL;
head->next = new_node;
// Print the contents of the linked list
node_t *current_node = head;
while (current_node != NULL) {
printf("%d ", current_node->value);
current_node = current_node->next;
}
printf("\n");
}
void node_float_test(void) {
// Create an empty linked list
node_t *head = NULL;
// Allocate the first node
node_t *new_node = (node_t *) sbrk(sizeof(node_t));
if (new_node == NULL) {
printf("Error: out of memory\n");
return;
}
new_node->number = 1.23;
new_node->next = NULL;
head = new_node;
// Allocate a second node
new_node = sbrk(sizeof(node_t));
if (new_node == NULL) {
printf("Error: out of memory\n");
return;
}
new_node->number = 5.67;
new_node->next = NULL;
head->next = new_node;
// Print the contents of the linked list
node_t *current_node = head;
while (current_node != NULL) {
printf("%2f ", current_node->number);
current_node = current_node->next;
}
printf("\n");
}
void dynamic_int_array(void) {
// Allocate an array of 10 integers
int *array = sbrk(10 * sizeof(int));
if (array == NULL) {
printf("Error: out of memory\n");
return;
}
// Initialize the array
for (int i = 0; i < 10; i++) {
array[i] = i;
}
// Print the contents of the array
for (int i = 0; i < 10; i++) {
printf("%d ", array[i]);
}
printf("\n");
// Resize the array to hold 20 integers
int *new_array = sbrk(20 * sizeof(int));
if (new_array == NULL) {
printf("Error: out of memory\n");
return;
}
// Copy the contents of the old array to the new array
for (int i = 0; i < 10; i++) {
new_array[i] = array[i];
}
// Initialize the remaining elements of the new array
for (int i = 10; i < 20; i++) {
new_array[i] = i;
}
// Print the contents of the new array
for (int i = 0; i < 20; i++) {
printf("%d ", new_array[i]);
}
printf("\n");
}
void dynamic_float_array(void) {
// Allocate an array of 10 integers
float *array = sbrk(10 * sizeof(float));
if (array == NULL) {
printf("Error: out of memory\n");
return;
}
// Initialize the array
for (int i = 0; i < 10; i++) {
array[i] = (float) i + ((float) i / 10);
}
// Print the contents of the array
for (int i = 0; i < 10; i++) {
printf("%3f ", array[i]);
}
printf("\n");
// Resize the array to hold 20 integers
float *new_array = sbrk(20 * sizeof(float));
if (new_array == NULL) {
printf("Error: out of memory\n");
return;
}
// Copy the contents of the old array to the new array
for (int i = 0; i < 10; i++) {
new_array[i] = array[i];
}
// Initialize the remaining elements of the new array
for (int i = 10; i < 20; i++) {
new_array[i] = (float) (i + 0.1);
}
// Print the contents of the new array
for (int i = 0; i < 20; i++) {
printf("%3f ", new_array[i]);
}
printf("\n");
}
void string(void) {
// Allocate memory for two strings
char *str1 = (char *) sbrk(1024);
char *str2 = (char *) sbrk(1024);
// Copy some text into the first string
strncpy(str1, "Hello, world!", 1024);
// Copy the first string into the second string
strncpy(str2, str1, 1024);
// Print the two strings
printf("str1:\t%s\n", str1);
printf("str2:\t%s\n", str2);
printf("char:\t%c\n", 'a');
printf("int:\t%d\n", 543);
printf("float:\t%f\n", 1.99);
// TODO !%...
printf("int!%2:\t%3d\n", 543000);
printf("float!%2:%2f\n", 1.99);
// Free the memory allocated for the strings
brk(str2);
brk(str1);
}