Question: The following must be written in C Write a program: - whose input is a MIPS Assembly Language (MAL) program and - whose output is

The following must be written in C

Write a 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.

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

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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!