CS 291 Assignment #6

Due Tuesday, March 31st (by 11:59pm)
Not accepted after April 5th

Introduction:

On this assignment you'll get a chance to practice cyclic programming, then prove some simple properties about functional programs.

The Assignment

  1. As mentioned in class, when single-pass assemblers encounter jump instructions, they don't yet know the memory address of the jump's target. One can leave "placeholders" for the jump instructions and come back to fill them in with actual addresses once the rest of the machine code has been generated, but cyclic programming makes it possible to express a single-pass solution. For this problem, assume a simple "assembly language" where the instructions are arbitrary strings, with the exception of the "J END" instruction which jumps to the end of the current procedure. (That is, just past the end of the list of instructions.)

    Write the function patch_jumps that takes a list of these assembly instructions (strings), and replaces all instances of "J END" with a jump to the address of the instruction that would follow the given code sequence, assuming that the address of the first instruction in the list is 0 and that every instruction has a "size" of one. For full credit, your program should make just one pass over the list. Sample inputs:

    Main> patch_jumps []
    []
    Main> patch_jumps ["J END"]
    ["J 1"]
    Main> patch_jumps ["J END", "J END"]
    ["J 2","J 2"]
    Main> patch_jumps ["LOAD", "CMP", "J END", "STORE", "LOAD", "J END", "STORE"]
    ["LOAD","CMP","J 7","STORE","LOAD","J 7","STORE"]
    
    Hint: Start by writing a two-pass version to get a feel for the problem. Next, write an explicitly recursive function that does two things during a pass over an input list, and think about how to invoke it to solve the problem in one pass.

  2. Problem 8.3 from the book. (Hint: There are two lists involved, but you really only need to worry about one of them.)

  3. Problem 8.4 from the book. (Hint: Read the previous hint.)

Submitting:

Please submit all of your work in a single file — the solutions to the last two problems should be included as block comments in the Haskell file containing the patch_jumps solution. Submit your file as an attachment to me at brichards@ups.edu. Please do not just copy and paste your solutions into the body of the E-mail! (If your E-mail client insists on including the content of text files into the body of the message, please zip the files up and attach the archive.) No need for sample output.


Brad Richards, 2009