/* testemit.h * * a small example program to demonstrate the use of the functions/class * defined in emit.h/cc * * to run testemit: * * testemit > testemit.s * * You can then examine the mips code generated in testemit.s * OR, you can execute the generated code in SPIM with the following command * (once you have put spim in your path). * * spim testemit.s * */ #include "emit.h" // create a label generator NameGenerator lgen("label"); // the following saves typing of quotes and dollar-signs. // convenient for heavily used names const string t0 = "$t0"; const string sp = "$sp"; const string ra = "$ra"; int main() { string loop = lgen.gen(); // generate two unique labels string end = lgen.gen(); cout << ".text" << endl; // mips instructions go in the text segment // main program entry point emitLabelDef(cout, "main"); // push return address on stack emit(cout, "addiu", sp, sp, imm(-4)); emit(cout, "sw", ra, ind(4, sp)); emit(cout, "move", t0, "$zero"); // $zero reg is hardwired with 0 value emitLabelDef(cout, loop); emit(cout, "move", "$a0", t0); emit(cout, "jal", "_int_println"); emit(cout, "addiu", t0, t0, imm(1)); emit(cout, "blt", t0, imm(10), loop); // this label is unused by the code, but allows us to see an // example of a second unique label emitLabelDef(cout, end); // pop return address emit(cout, "lw", ra, ind(4,sp)); emit(cout, "addiu", sp, sp, imm(4)); // return from main emit(cout, "jr", ra); return 0; }