You're going to write a primitive disassembler - a program that takes as input a hex number representing a Cebollita machine instruction and prints out what the instruction is using the more easily read notation of the assembler.Here's an example:
Enter an instruction in hex: 01284820 ADD $9, $9, $8 Enter an instruction in hex: 0000000c SYSCALL $0, $0, $0 Enter an instruction in hex: c SYSCALL $0, $0, $0 Enter an instruction in hex: SLL $0, $0, $0 Enter an instruction in hex: 23bdfff8 Not a recognized instruction type Enter an instruction in hex: z Syscall: Exit(The Cebollita graphical debugger window includes a disassembly of instructions in the text pane at the top.)A few more particulars, some of which are illustrated in the example above:
- You have to disassemble only the Cebollita instruction set (i.e., not the much larger MIPS instruction set). See the Cebollita Instruction Encoding and Operation Specification page for details on the Cebollita instruction set.
- Even better, you have to disassemble only those instructions that have opcode
0x00
.
- Finally, you have to disassemble only if the input is a valid hex number (contains characters 0-9 and a-f only). (Note that I've simplified things a bit by insisting on lower case hex characters.) If the opcode isn't 0x00 just say you don't recognize the instruction.
- Don't worry about counting the number of hex digits provided by the user. Just do something that works if 8 digits are supplied. (The example above shows how my solution behaves in some natural circumstances.)
- If the input contains a character that can't be part of a hex number, terminate.
- Don't worry about getting the prettiest possible output. Note that the example above prints register arguments whether or not the instruction actually has register arguments. (So does Cebollita itself when it disassembles, at least in some cases.) No biggie.
You'll write two pieces of code, resulting in two files:
main.s
This is the mainline, written in assembler. It sits in a loop printing the prompt, reading a string from the user, and then invoking the second piece of code,
disasm()
, passing it the string it just read. Ifdisasm()
returns a non-zero value, the mainline just loops. Otherwise, it terminates.
disasm.c
This file contains a
C--
function that takes a string (achar*
) as an argument. It convers the string to anint
, then rips theint
apart to isolate the opcode, rs, rt, etc., fields. It then prints the values of those fields in a way that typically looks a lot like an assembly language insruction. It normally returns the value 1, but returns 0 if the string contained a digit that cannot be in a hex number.
Almost everything you have to do in this assignment is new. I suggest you develop your solution in this way:
- Start by writing a standalone
C--
program that disassembles. Get that working first, before worrying about writing a mainline or anything in assembler.The things to be wary of here are the
C--
pointer data types (e.g.,char*
), which are probably new to you, and the fact thatC--
is such a bare bones language that many things that are natural to say and that you can say inJava
orC
will not be recognized inC--
(e.g.,for
loops).
- Once that is working, write the mainline in assembler, and work on getting the procedure call linkage correct.
iolib
sYou'll wnat to useprologue-os.o
andiolib-os.o
In the directory containing disasm.c and main.s, use the command 'turnin -ccse378 main.s disasm.c' to submit your assignment.