"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:
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.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"]
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.