---------------------------------------- - Simple Code Generation Example 1 - - (based on the Final Project grammar) - ---------------------------------------- Source code: { int i i = 5 print(i) } $ int i makes an entry into the stack temp table, aliasing i with location t0 xx. If we are being nice, then we could initialize i to 0 with the following code: A9 00 8D t0 xx. (LDA 0, STA t0xx) i=5 turns into A9 05 8D T0 XX (remember, t0xx will be replaced by a real value later.) 00: A9 00 8D T0 XX A9 05 8D 08: t0 xx .. .. .. .. .. .. 10: .. .. .. .. .. .. .. .. print(i) adds AC T0 XX A2 01 FF to the opcode stream 00: A9 00 8D t0 xx A9 05 8D 08: t0 xx AC t0 xx A2 01 FF 10: .. .. .. .. .. .. .. .. Then let's put a break (00) at the end. 00: A9 00 8D T0 XX A9 05 8D 08: T0 XX AC T0 XX A2 01 FF 10: 00 .. .. .. .. .. .. .. Now we can see that we can begin the stack area at location 0x11. In the little endian convention, that's 11 00. Backpatch that into the code. 00: A9 00 8D 11 00 A9 05 8D 08: 11 00 AC 11 00 A2 01 FF 10: 00 .. .. .. .. .. .. .. And we're done. Looking at it as a stream: A9 00 8D 11 00 A9 05 8D 11 00 AC 11 00 A2 01 FF 00 Try that in an OS on our class site.