day 4
This commit is contained in:
67
day1.c
67
day1.c
@@ -1,4 +1,65 @@
|
||||
#include "lib.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdcountof.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#define SYS_write 1
|
||||
#define SYS_exit_group 231
|
||||
|
||||
#define STDOUT_FILENO 1
|
||||
|
||||
static
|
||||
void write(uint64_t fileno, void *buffer, size_t len) {
|
||||
uint64_t a,d;
|
||||
asm volatile("syscall"
|
||||
:"=a"(a),"=d"(d):"a"(SYS_write),"D"(fileno),"S"(buffer),"d"(len):"memory");
|
||||
}
|
||||
|
||||
[[noreturn]] static
|
||||
void exit_group() {
|
||||
asm volatile(
|
||||
"syscall\n"
|
||||
"ud2"
|
||||
::"a"(SYS_exit_group),"D"(0));
|
||||
for (;;);
|
||||
}
|
||||
|
||||
#define xstr(s) str(s)
|
||||
#define str(s) #s
|
||||
|
||||
static
|
||||
void print_many_char(size_t len, char chars[len]) {
|
||||
write(STDOUT_FILENO, chars, len);
|
||||
}
|
||||
|
||||
#define print(string) print_many_char(sizeof(string)-1, string)
|
||||
|
||||
static
|
||||
void printd(unsigned long v) {
|
||||
if (!v) {
|
||||
print("0");
|
||||
return;
|
||||
}
|
||||
char buf[32];
|
||||
char *p = buf+32;
|
||||
unsigned count = 0;
|
||||
while (v) {
|
||||
p--;
|
||||
*p = '0' + (v % 10);
|
||||
v /= 10;
|
||||
count++;
|
||||
}
|
||||
write(STDOUT_FILENO, p, count);
|
||||
}
|
||||
|
||||
static
|
||||
unsigned grabnum(const char *s, const char **end) {
|
||||
unsigned r = 0;
|
||||
for (; *s >= '0' && *s <= '9'; s++)
|
||||
r = r * 10 + (*s - '0');
|
||||
*end = s;
|
||||
return r;
|
||||
}
|
||||
|
||||
#define DBG 1
|
||||
|
||||
@@ -74,7 +135,7 @@ unsigned do_part2(size_t src_len, const char src[src_len]) {
|
||||
return count;
|
||||
}
|
||||
|
||||
void _start() {
|
||||
void run() {
|
||||
#if RUN_TEST1
|
||||
print("PART 1 TEST: ");
|
||||
if (unsigned long v = do_part1(countof(test), test); v != TEST1_EXPECT) {
|
||||
@@ -109,5 +170,5 @@ void _start() {
|
||||
print("\n");
|
||||
#endif
|
||||
|
||||
syscall(SYS_exit_group, 0);
|
||||
exit_group();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user