day 4
This commit is contained in:
43
lib.h
43
lib.h
@@ -1,11 +1,30 @@
|
||||
#pragma once
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
#include <stdcountof.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.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(uint64_t code) {
|
||||
asm volatile(
|
||||
"syscall\n"
|
||||
"ud2"
|
||||
::"a"(SYS_exit_group),"D"(code));
|
||||
for (;;);
|
||||
}
|
||||
|
||||
#define xstr(s) str(s)
|
||||
#define str(s) #s
|
||||
|
||||
@@ -13,7 +32,7 @@ typedef uint64_t bcdint;
|
||||
|
||||
static
|
||||
void printns(size_t len, unsigned char chars[len]) {
|
||||
syscall(SYS_write, STDOUT_FILENO, chars, len);
|
||||
write(STDOUT_FILENO, chars, len);
|
||||
}
|
||||
|
||||
static
|
||||
@@ -29,12 +48,12 @@ void printptr(uintptr_t v) {
|
||||
}
|
||||
v >>= 4;
|
||||
}
|
||||
syscall(SYS_write, STDOUT_FILENO, p, countof(buf));
|
||||
write(STDOUT_FILENO, p, countof(buf));
|
||||
}
|
||||
|
||||
static
|
||||
void print_many_char(size_t len, char chars[len]) {
|
||||
syscall(SYS_write, STDOUT_FILENO, chars, len);
|
||||
write(STDOUT_FILENO, chars, len);
|
||||
}
|
||||
|
||||
#define print(string) print_many_char(sizeof(string)-1, string)
|
||||
@@ -54,7 +73,7 @@ void printd(unsigned long v) {
|
||||
v /= 10;
|
||||
count++;
|
||||
}
|
||||
syscall(SYS_write, STDOUT_FILENO, p, count);
|
||||
write(STDOUT_FILENO, p, count);
|
||||
}
|
||||
|
||||
static
|
||||
@@ -76,7 +95,7 @@ void printh(unsigned long v) {
|
||||
v >>= 4;
|
||||
count++;
|
||||
}
|
||||
syscall(SYS_write, STDOUT_FILENO, p, count);
|
||||
write(STDOUT_FILENO, p, count);
|
||||
}
|
||||
|
||||
static
|
||||
@@ -95,7 +114,7 @@ void print_hex_char(unsigned char v) {
|
||||
buf[1] = 'A' - 0xA + (v & 0x0F);
|
||||
}
|
||||
|
||||
syscall(SYS_write, STDOUT_FILENO, buf, sizeof(buf));
|
||||
write(STDOUT_FILENO, buf, sizeof(buf));
|
||||
}
|
||||
|
||||
static
|
||||
@@ -125,11 +144,11 @@ bcdint grabbcd(unsigned char *s, unsigned char **end) {
|
||||
}
|
||||
|
||||
static
|
||||
unsigned grabnum(const char *s, const char **end) {
|
||||
unsigned r = 0;
|
||||
unsigned long grabnum(unsigned char *s, unsigned char **end) {
|
||||
unsigned long r = 0;
|
||||
for (; *s >= '0' && *s <= '9'; s++)
|
||||
r = r * 10 + (*s - '0');
|
||||
*end = s;
|
||||
if (end) *end = s;
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user