Problem 4—OOPS
You've been sitting in front of the terminal for three hours savagely
coding the Assembler assignment that's due tomorrow. Your best friend, who finished the program last week and has been
rubbing it in for just as long, saunters up to your terminal and offers,
"Coke break, slow poke?" You
take the Coke he hands you and you lean your head back, savoring the refreshing
feeling of the drink as it soothes your parched throat; too much coding always
made your throat raw. “Hey, can I look
at how far you've gotten?” your friend asks.
“Sure,” you reply, taking another long drink. As you're enjoying your
drink and thinking of what “The Real Thing” really is, you hear a whispered
“oops” from the direction of your terminal.
“What?!” you demand. Your friend
replies, backing away, “I think I just deleted your source code.”
“Arrrgggghhh!” you spurt as your friend runs from the computer lab,
mumbling prayers for divine protection.
You double-check and—sure enough—all you have left of your precious Assembler
assignment is the executable code from your last compile. And you were so close to finishing! Do you take a zero? No, that last test was a killer. Start over? NOT! Instead, your hands fumble for your Assembler textbook. Ah, here we go: a description of the
encoding scheme used by the computer's assembler. It doesn't look too bad and you remember most of the hand
disassembling you did in class! It's decided then, you'll just have to write a
quick disassembler to convert the executable code back into your source code so
that you can finish your assignment before Survivor comes on. You open your textbook and begin reading:
Instruction Set
HEX Code Instruction
0 ADD arg,arg
1 SUB arg,arg
2 MUL arg,arg
3 DIV arg,arg
4 MOV arg,arg
5 BREQ arg
6 BRLE arg
7 BRLS arg
8 BRGE arg
9 BRGR arg
A BRNE arg
B BR arg
C AND arg,arg,arg
D OR arg,arg,arg
E XOR arg,arg,arg
F NOT arg
ADD arg,arg means that the ADD instruction
takes two arguments.
Opcodes are 4 bytes in length. An operand is made up of two fields: a mode
and a value. The mode is two bits and the value is fourteen bits for a total of
sixteen bits. The possible values for mode are: 00: register; 01: absolute; 10:
PC-relative; 11: constant
The value field for a register operand specifies the number of the
register. For example, if the value field contained a seven then that would
specify register number 7, written "R7". The value field for an absolute operand
specifies the absolute address that the operand is to be stored at. For example,
if the value field contained the number 110, that would denote address location
110, which is written "$110". The value
field for a PC-Relative operand specifies the offset of the address relative to
the program counter. (On this computer, all PC-relative offsets are
non-negative). For example, if the value field contained the number 45, that
would specify the address location (Program Counter) + 45, which is written
"PC+45". The value field for a
constant operand specifies a constant.
For example, if the value field contained the number 1276, then that
would specify the actual number 1276 and is written as "1276".
INPUT SPECIFICATION. The input file
will contain a series of hexadecimal digits (0-F) corresponding to a compiled
program. There will be exactly 30
digits on each line, except for the last line which might contain fewer. Each line is terminated by <EOLN>. An extra <EOLN> (a blank line)
will signal the end of the input. There
will be no additional spaces or <EOLN>'s in the file, but it is
certainly possible that an <EOLN> will occur between the bytes of
an individual command. All commands in
the input file will be valid.
OUTPUT SPECIFICATION. The output files should contain the assembler commands corresponding to the hex code in the input file. There should be one space between the command and its arguments and one comma between successive arguments to a command. There should be one <EOLN> character following the final argument of a command. No extra spaces or <EOLN>s beyond these should appear in the output file.
SAMPLE INPUT.
4C00D00004C0020001000000001400<EOLN>
005FFFB801E<EOLN>
<EOLN>
<EOF>
SAMPLE
OUTPUT.
MOV·13,R0<EOLN>
MOV·2,R1<EOLN>
ADD·R0,R1<EOLN>
MOV·R0,$8191<EOLN>
BR·PC+30<EOLN>
<EOF>