The Runlong Hall of Fame (> 10,000 steps)
Most of you submitted correct solutions for the runlong problem, that is, programs that run for at least 25 steps without encountering a "bneg" instruction and stop by executing the "halt" command. A few others either executed a "bneg" instruction, had the IC overflow past 0x1f, or went into an infinite loop. Some tried to provide more than 24 instructions, but recall that the entire program needs to fit into the 24 bytes of ROM. The programs that worked correctly and ran for at least 25 steps are:
2875 steps #1 tianxingd.hex 31 steps zeinhorn.hex 2429 steps schar2.hex 31 steps tianshengs.hex 1731 steps #2 gpan.hex 31 steps pfricke.hex 961 steps #3 elneary.hex 31 steps eemerson.hex 326 steps schar1.hex 31 steps ascannell.hex 189 steps #4 roehmler.hex 31 steps aronoh.hex 149 steps #5 mingfangz.hex 31 steps angulumbi.hex 91 steps mczekanski.hex 31 steps afarley.hex 90 steps gbeaulieu.hex 30 steps tmonaco.hex 75 steps cbintz.hex 30 steps mvangemeren.hex 51 steps hmound.hex 30 steps mpolito.hex 49 steps cbirdsey.hex 30 steps bchoe.hex 47 steps hevans.hex 29 steps csutton.hex 45 steps abaskin.hex 27 steps nalbers.hex 44 steps cblondin.hex 26 steps cdiprinzio.hex 39 steps kevinc.hex 25 steps zlevitt.hex 32 steps sjordan.hex 25 steps nroleary.hex 32 steps mbock.hex 25 steps kristines.hex 32 steps lfriedrich.hex 25 steps jravishankar.hex 32 steps jenglish.hex 25 steps fsullivan.hex 32 steps enakagawa.hex 25 steps batwood.hex 25 steps aturcios.hex
Congratulations to all, and especially to Roger, Guanghan, Emma, Trey, and Melody, the top five finishers of the first round of the contest, all of whom achieved over 100 steps. Guanghan and Emma beat my own entry schar1.hex, and Roger even beat my entry schar2.hex! :)
There are many ways to solve this problem, but all involve writing "self-modifying code". That is, your program has to store some instructions in RAM and then execute them. In the simplest case the program stores the instruction "halt" in the last byte of RAM (0x1f) as follows:
0: 0x82 b 2 /* jump around the next instruction */ 1: 0xff /* the opcode for halt - not executed */ 2: 0x01 ld A, 1 /* load the opcode for halt... */ 3: 0x7f st A, 0x1f /* ...and store it at the end of RAM */
This program runs for 31 steps (assuming the RAM is initially all 0, which you should not generally assume). Most steps execute opcode 0x00, i.e., the "harmless" opcode "ld A, 0". To get 32 steps you have to disguise the data (i.e., the halt opcode) using instructions, for example as the difference of two harmless opcodes. To run for more than 32 steps, you need to implement some sort of self-modifying loop. There are many possibilities, including the following two:
Now that you see what's possible, and also have an idea of how it could be done, can you beat the current front-runner, and perhaps make it into the "runlong hall of fame" (> 10,000 steps)?
I'll accept new entries anytime, for the rest of the semester. Simply email me the text file with the opcodes in hex.