#include #include unsigned int code[] = { // end: // This program should accept any positive numbers forever. When you type // a 0, it should print a 757 and exit with code 0. // If it prints 42 or 43, there's something wrong with the translation. 0xA0000000, // syscall exit // Address: 0x00000000 0xA0000000, // Input 0xA0000002, // If input is greater than zero, branch to 8 0x50000008, // Otherwise, jump (will quit on next input) // push 0xE0000024, // Address: 0x00000010 // jump 0x70000000, // Shouldn't reach here // push 43 0xE000002B, // Print it 0xA0000001, // Exit 0xA0000000, // Address: 0x00000020 // push a 757 0xE00002F5, // Print it 0xA0000001, // push a 0 0xE0000000, // bez... 0x60000000, // Address: 0x00000030 // It should never reach this: // push 42 0xE000002A, // print 0xA0000001, // exit 0xA0000000, }; unsigned int data[] = { }; typedef struct _Header378Binary { unsigned char MagicCode[4]; unsigned int DataSize; unsigned int CodeSize; unsigned int InitialPC; } Header378Binary; int main() { FILE *fp = fopen("branch.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 = 0x00000008; 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); }