Question
You are required to write a C program whose input is a MIPS Assembly Language (MAL) program and whose output is a listing of the
You are required to write a C program whose input is a MIPS Assembly Language (MAL) program and whose output is a listing of the MAL program with line numbers and/or a cross reference table for the MAL program. Details regarding MAL programs and the contents of cross reference tables are given below.
Command Line Details: Suppose the executable version of your program is named prog4.out. The program will be executed by a command line of the following form:
prog4.out flag inputfile outputfile
In the command line above, the arguments inputfile and outputfile specify the names of the input file (which contains a MAL program) and the output file respectively. A valid flag argument is one of -l, -c or -b. If the flag is -l, your program must produce only a listing of the MAL source program in the specified output file. If the flag is -c, your program must produce only the cross reference table for the MAL source program in the specified output file. If the flag is -b, your program must produce both the listing and the cross reference table in the specified output file.
Details Regarding MAL Programs: Each line of a MAL program may be a blank line (containing only white-space characters), a comment line (a line that starts with the character #) or a MAL statement. Each MAL statement has an optional label field (terminated by a colon), a mandatory opcode field, an optional operand field consisting of zero or more operands separated by commas, and an optional comment field (starting with the character #). The different fields are separated by one or more spaces or tabs.
An identifier in a MAL program is defined in the source line where the identifier appears as a label and is used in lines where it appears in the operand field. (Note that opcodes are not identifiers. Also, operands starting with the symbol $ are not identifiers; they represent registers of the MIPS machine.)
Example: Consider the following MAL statement:
iloop_beg: la $7,arr #Get address of arr into register 7.
In the above MAL statement, the label field has the identifier iloop_beg. The opcode field has la. The two operands are $7 (a register not an identifier) and arr (an identifier). The operand field is followed by the comment field that starts with the # character. The above MAL statement defines the identifieriloop_begandusestheidentifierarr.
A cross reference table for a MAL program indicates for each identifier, the line number in the source program where the identifier is defined and the line number(s) in the source program where the identifier is used. An example of a MAL program and its cross reference table are shown on pages 3 and 4 of this handout.
Here are some additional details regarding MAL programs.
Every line of a MAL program is terminated by the newline ( ) character. There are at most 80 characters (including the newline character) on each line.
Any line that has # as the first character is a comment line.
In any non-comment non-blank line, the label, opcode, operand and comment fields are separated by one or more spaces or tabs. If such a line has a label, the label will start from the first position. (Thus, a line that starts with a space or tab does not have a label.)
The first character of any identifier is a lower or upper case letter or the underscore character; this is followed by zero or more upper/lower case letters, digits or underscore characters. The identifiers are case sensitive. (Thus, Val and val represent different identifiers.) The maximum length of an identifier is 10. The maximum number of labels that can appear in any MAL program is 100.
If the operand field of a MAL program starts with the single quote character () or the double quote character ("), then the operand field does not have any identifiers. (The reason is that an operand field starting with the single quote specifies a literal character; an operand field starting with the double quote character specifies a string.)
Be sure to study the example given on pages 3 and 4 of this handout before writing your program.
Errors to be detected: You need to check only for the usual command line errors (wrong number of parameters on the command line, invalid flag, the input or the output file cant be opened). In such a case, your program must output a suitable error message to stderr and stop.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started