#include #include unsigned int code[] = { // end: 0xA0000000, // syscall exit 0xA0000000, 0xA0000000, // start: //(3) 0xA0000002, // ask user for a number 0xE0000000, // push 0 0x90000000, // store result into address 0 0xE000002C, // push ra 0xE0000000, // push 0 0x80000000, // load arg 0xE0000034, // push fib_loop_addr 0x70000000, // jump //(11) // back from loop. result is top of the stack 0xA0000001, // print the integer 0xA0000000, // quit // fib_loop: (13) 0xE0000004, // push 4 0x90000000, // store (argument) 0xE0000000, // push 0 0x90000000, // store (ra) 0xE0000004, // push 4 0x80000000, // load (argument) 0xE0000002, // push 2 0x20000000, // sub (see if less than 2) 0x50000068, // bgtz continue (if arg > 0) // base_case: (22) 0xE0000001, // push 1 (return value) 0xE0000000, // push 0 (address of ra) 0x80000000, // load (ra) 0x70000000, // jump (go back) // continue: (26) // 1st call: 0xE0000000, // push 0 0x80000000, // load (my ra) 0xE0000004, // push 4 0x80000000, // load (my arg) 0xE0000094, // push addr of back frm 1st call 0xE0000004, // push 4 0x80000000, // load (my arg) 0xE0000001, // push 1 0x20000000, // sub (so next arg is my arg - 1) 0xE0000034, // push fib_loop addr 0x70000000, // jump // back from 1st call: (37) 0xE0000008, // push 8 0x90000000, // store (result in 8) 0xE0000004, // push 4 0x90000000, // store (my arg) 0xE0000008, // push 8 0x80000000, // load (1st result) 0xE00000C8, // push addr of back frm 2nd call 0xE0000004, // push 4 0x80000000, // load (my arg) 0xE0000002, // push 2 0x20000000, // sub (next arg is my arg - 2) 0xE0000034, // push fib_loop addr 0x70000000, // jump // back frm 2nd call (50): 0x10000000, // add (add the results) 0xE0000008, // push 8 0x90000000, // store (result) 0xE0000000, // push 0 0x90000000, // store (ra) 0xE0000008, // push 8 0x80000000, // load (result) 0xE0000000, // push 0 0x80000000, // load (ra) 0x70000000 // jump // total = 60 (0->59) }; unsigned int data[] = { 0x00000000, 0x12345678, 0x87654321 }; typedef struct _Header378Binary { unsigned char MagicCode[4]; unsigned int DataSize; unsigned int CodeSize; unsigned int InitialPC; } Header378Binary; int main() { FILE *fp = fopen("fib.binary", "wb"); Header378Binary Header; Header.MagicCode[0] = '3'; Header.MagicCode[1] = '7'; Header.MagicCode[2] = '8'; Header.MagicCode[3] = ' '; Header.DataSize = sizeof(data); Header.CodeSize = sizeof(code); Header.InitialPC = 0x0000000C; printf("code=%d, data=%d\n", sizeof(code), sizeof(data)); fwrite(&Header, 1, sizeof(Header), fp); fwrite(data, 1, sizeof(data), fp); fwrite(code, 1, sizeof(code), fp); fclose(fp); }