Skip to content

Instantly share code, notes, and snippets.

@lupyuen
Last active February 2, 2024 13:31
Show Gist options
  • Save lupyuen/cc74229e39b3cea0d6974a85fb06f65a to your computer and use it in GitHub Desktop.
Save lupyuen/cc74229e39b3cea0d6974a85fb06f65a to your computer and use it in GitHub Desktop.
Apache NuttX App for Ox64 BL808 SBC, compiled with TCC Compiler. See https://github.com/lupyuen/tcc-riscv32-wasm
int main(int argc, char *argv[]) {
// Make NuttX System Call to write(fd, buf, buflen)
const unsigned int nbr = 61; // SYS_write
const void *parm1 = 1; // File Descriptor (stdout)
const void *parm2 = "Hello, World!!\n"; // Buffer
const void *parm3 = 15; // Buffer Length
// Load the Parameters into Registers A0 to A3
// TODO: This doesn't work, so we load again below
register long r0 asm("a0") = (long)(nbr);
register long r1 asm("a1") = (long)(parm1);
register long r2 asm("a2") = (long)(parm2);
register long r3 asm("a3") = (long)(parm3);
// Execute ECALL for System Call to NuttX Kernel
// Load the Parameters into Registers A0 to A3
asm volatile (
// Load 61 to Register A0 (SYS_write)
"addi a0, zero, 61 \n"
// Load 1 to Register A1 (File Descriptor)
"addi a1, zero, 1 \n"
// Load 0x80101000 to Register A2 (Buffer)
"lui a2, 0x80 \n"
"addiw a2, a2, 257 \n"
"slli a2, a2, 0xc \n"
// Load 15 to Register A3 (Buffer Length)
"addi a3, zero, 15 \n"
// ECALL for System Call to NuttX Kernel
"ecall \n"
// NuttX needs NOP after ECALL
".word 0x0001 \n"
// Input+Output Registers: None
// Input-Only Registers: A0 to A3
// Clobbers the Memory
:
: "r"(r0), "r"(r1), "r"(r2), "r"(r3)
: "memory"
);
// Loop Forever
for(;;) {}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment