first C hello world!

This commit is contained in:
pommicket 2022-02-13 15:07:26 -05:00
parent 6acd24e315
commit 2fef698195
7 changed files with 68 additions and 47 deletions

View file

@ -1,49 +1,50 @@
typedef unsigned long va_list;
#define va_start(list, arg) ((list) = (unsigned long)&arg)
#define va_arg(list, type) (*((type *)(list += ((sizeof(type) + 7) & 0xfffffffffffffff8))))
#define va_end(list)
static unsigned char __syscall_data[] = {
// mov rax, [rsp+24]
0x48, 0x8b, 0x84, 0x24, 24, 0, 0, 0,
// mov rdi, rax
0x48, 0x89, 0xc7,
// mov rax, [rsp+32]
0x48, 0x8b, 0x84, 0x24, 32, 0, 0, 0,
// mov rsi, rax
0x48, 0x89, 0xc6,
// mov rax, [rsp+40]
0x48, 0x8b, 0x84, 0x24, 40, 0, 0, 0,
// mov rdx, rax
0x48, 0x89, 0xc2,
// mov rax, [rsp+48]
0x48, 0x8b, 0x84, 0x24, 48, 0, 0, 0,
// mov r10, rax
0x49, 0x89, 0xc2,
// mov rax, [rsp+56]
0x48, 0x8b, 0x84, 0x24, 56, 0, 0, 0,
// mov r8, rax
0x49, 0x89, 0xc0,
// mov rax, [rsp+64]
0x48, 0x8b, 0x84, 0x24, 64, 0, 0, 0,
// mov r9, rax
0x49, 0x89, 0xc1,
// mov rax, [rsp+16]
0x48, 0x8b, 0x84, 0x24, 16, 0, 0, 0,
// syscall
0x0f, 0x05,
// mov [rsp+8], rax
0x48, 0x89, 0x84, 0x24, 8, 0, 0, 0,
// ret
0xc3
};
int sum(int n, ...) {
va_list args;
int i;
int total = 0;
va_start(args, n);
for (i = 0; i < n; ++i) {
total += va_arg(args, int);
}
return total;
#define __syscall(no, arg1, arg2, arg3, arg4, arg5, arg6)\
(((unsigned long (*)(unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long))__syscall_data)\
(no, arg1, arg2, arg3, arg4, arg5, arg6))
typedef unsigned long size_t;
long write(int fd, void *buf, size_t count) {
__syscall(1, fd, buf, count, 0, 0, 0);
}
long factorial(long x) {
if (x == 0) {
return 1;
} else {
return x * factorial(x-1);
}
}
long fibonacci(long x) {
return x > 0 ?
x > 1 ?
fibonacci(x-1) + fibonacci(x-2)
: 1
: 0;
}
long gcd(long a, long b) {
while (a != 0) {
long temp = a;
a = b % a;
b = temp;
}
return b;
}
int f() {
lb: goto lb;
}
int main(int argc, char **argv) {
return sum(3, -100, 200, -300);
int main(int argc, char **argv) {
write(1, "Hello, world!\n", 14);
return 0;
}