Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are required to write a C program - whose input is a MIPS Assembly Language (MAL) program and - whose output is a list

You are required to write a C program

- whose input is a MIPS Assembly Language (MAL) program and

- whose output is a list of labels of the MAL program used as variables, a list of labels of the MAL program used for the flow control, or both.

Details regarding MAL programs and lists of labels 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 -v, -f or -b.

- If the flag is -v, your program must produce only a list of labels of the MAL source program used as variables in the specified output file. (Variable labels are identifiers defined in the .DATA segment of a MAL program.)

- If the flag is -f, your program must produce only a list of labels of the MAL program used for the flow control in the specified output file. (Flow control labels are identifiers defined in the .TEXT segment of a MAL program.)

- If the flag is -b, your program must produce (i) a list of labels of the MAL source program used as variables, and (ii) a list of labels of the MAL program used for the flow control in the specified output file.

Details Regarding MAL Programs:

1.) Each line of a MAL program may be

(a) a blank line (containing only white-space characters),

(b )a comment line (a line that starts with the character #) or

(c) a MAL statement which has the following fields separated by one or more spaces or tabs:

i) an optional label field terminated by ' :'

ii) a mandatory opcode field,

iii) an optional operand field consisting of zero or more operands separated by commas, and

iv) an optional comment field starting with '#'

2.) Every line of a MAL program is terminated by the newline ( ) character and has at most 80 characters (including the newline character).

3.) Any line that has # as the first character is a comment line.

4.) The labels start from the first position, thus, a line that starts with a space or tab does not have a label.) The maximum number of labels that can appear in any MAL program is 100.

5.) The first character of any identifier is a lower- or upper-case letter or the underscore character 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.

6.) Opcodes are not identifiers. Also, operands starting with the symbol $ are not identifiers; they represent registers of the MIPS machine.

7.) 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.)

8.) An identifier in a MAL program:

(a) is defined in the source line where the identifier appears as a label and

(b) is used in lines where it appears in the operand field.

Examples:

iloop_beg: la $7,arr #Get address of arr into register 7.

In the above, 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 statement defines the identifier iloop_beg and uses the identifier arr.

loop: sw $15,avg #Store reg. 15

In the above instruction, the label is loop, the opcode is sw and the operands are $15 and avg. The instruction defines the identifier loop and uses the identifier avg.

An example of a MAL program and lists of labels are shown below. Be sure to study the example given below before writing your program.

Suppose the file example.mal contains the following MAL program.

#A sample MAL program.

.data #Data segment begins here.

avg: .word #Will store the average.

i1: .word 20 #First integer.

i2: .word 13 #Second integer.

i3: .word 82 #Third integer.

prompt: .asciiz "Value is: "

nl: .byte

.text #Text segment begins here.

__start: lw $15,i1 #$15 contains 20.

lw $16,i2 #$16 contains 13.

i10: add $15,$15,$16 #Operand field has no ids.

lw $16,i3 #$16 contains 82.

add $15,$15,$16 #$15 contains the sum (115).

li $16,3 #$16 contains 3.

div $15,$15,$16 #$15 contains the average (38).

i20: sw $15,avg #Store the average.

puts prompt

put avg

putc nl

sw $15,avg

la $16,i1

sw $15,0($16)

add i3,i3,1

done #Similar to halt.

After the execution of the program using the command line

prog4 -b example.mal output.lst

the contents of the output file output.lst should be as shown below. In studying the output file, you should keep the following in mind:

a.) Name of the identifier in the list is surrounded with -.

b.) The identifiers in the list are in order of their definition in the program.

c.) For each identifier, the source lines where it is used are in order of their appearance in the program.

d.) If an identifier is not used, there is neither source line, no empty line after the identifier.

e.) When an identifier is used several times in a source line (e.g. the identifier i3 appears twice in one line), the source line appears only once in the list.

Contents of the file output.lst:

Variable ID avg-

i20: sw $15,avg #Store the average.

put avg

sw $15,avg

Variable ID -i1-

__start: lw $15,i1 #$15 contains 20.

la $16,i1

Variable ID -i2-

lw $16,i2 #$16 contains 13.

Variable ID -i3-

lw $16,i3 #$16 contains 82.

add i3,i3,1

Variable ID prompt-

puts prompt

Variable ID nl-

putc nl

Flow Control ID -__start-

Flow Control ID -i10-

Flow Control ID -i20-

Programming Suggestions:

a.) Data structure to be used: array (of size 100) of struct, where each struct has the following data members:

1) a char array of size 11 (to store an identifier),

2) a pointer to a linked list; each node of the list stores a source line where the identifier is used.

b.) Dont assume any limit on the number of lines in the input file. Read the input line by line (using fgets). For each line of the input file which is not a comment line or a blank line, use strtok to parse the line (i.e., extract the various fields of the instruction).

c.) All input files will satisfy the following conditions.

1) The MAL program in the input file wont contain any errors.

2) Every identifier will be defined somewhere in the MAL program. (However, it is possible that an identifier is not used anywhere.)

3)There will be no multiply defined identifiers.

d.) 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.

e.) The input and output file names must NOT be hard-coded into your program. These files names are available to your program as Unix command line arguments.

f.) Program must contain several functions in addition to main.

g.)The program has two major components:

1) Processing each MAL statement (e.g. checking if the statement is a comment, a blank line or a MAL statement, identifying the label and/or the identifiers appearing in a MAL statement).

2) Maintaining the data structure that stores the lists information.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

List and define the three classifications of quality costs.

Answered: 1 week ago