- Joined
- Oct 19, 2023
i finally did it
behold: the worst inline assembly you've ever seen
ok i will stop shitting up the thread now
i had fun and learned a couple of things, even though i am sure it's extremely terrible somehow
behold: the worst inline assembly you've ever seen
C:
// c library is for soydevs who can't program
//#include <gaybloat.h>
// compilation command (first line has the most important switches):
// gcc -o <file> -nostdlib -Os -s -static -fno-stack-protector
// -fno-unwind-tables -fno-asynchronous-unwind-tables <name>.c
// (your version of gcc might do weird things idk figure it out)
// write a single character
int write_char(char c, int fd){
volatile long out = 0;
__asm__("xor %%rax, %%rax;"
"inc %%rax;"// (write)
"xor %%rdx, %%rdx;"
"inc %%rdx;" // (1 char)
"mov %[fd], %%rdi;" // file descriptor
"mov %[c], %%rsi;" // the address of our char
"syscall;"
"mov %%rax, %[o];"
: [o] "=m" (out)
: [c] "r" (&c), [fd] "r" ((long)fd)
: "rax", "rcx", "r11", "rdi", "rdx", "rsi");
return out;
}
// never write a program that doesn't handle errors
__attribute((noreturn)) void fuck(){
// print a very helpful error message
write_char('o', 2);
write_char('o', 2);
write_char('f', 2);
write_char('\n', 2);
// exit 1
__asm__("mov $60, %rax;"
"xor %rdi, %rdi;"
"inc %rdi;"
"syscall;");
__builtin_unreachable();
}
// helper that writes to stdout
void write_out(char c){
if(write_char(c, 1) < 0)
fuck();
}
// read one character
// (we also upcase and "split" it while we're at it)
int read_char_substituted(char* c){
long out = 0;
__asm__("xor %%rax, %%rax;" // (read)
"mov $1, %%rdx;" // (1 char)
"xor %%rdi, %%rdi;" // (stdin)
"mov %[c], %%rsi;"
"syscall;"
"mov %%rax, %[o];"
: [o] "=m" (out)
: [c] "r" (c)
: "rax", "rcx", "r11", "rdi", "rdx", "rsi", "memory");
// convert to uppercase, "split" by making commas into newlines
*c = (*c >= 'a' && *c <= 'z') ? (*c-('a'-'A')) : *c;
*c = (*c == ',') ? '\n' : *c;
return out;
}
// main() is bloat
void _start(){
// loop
char x = 0;
while(read_char_substituted(&x)){
write_out(x);
}
// exit
__asm__("mov $60, %rax;"
"xor %rdi, %rdi;"
"syscall;");
__builtin_unreachable();
}
i had fun and learned a couple of things, even though i am sure it's extremely terrible somehow


