Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CSCI 2500 — Computer Organization

Homework 5 (document version 1.2)

Generating MIPS Code

● This homework is due in Submitty by 11:59PM EST on Friday, April 15, 2022 (v1.2)

● You can use at most three late days on this assignment

● This homework is to be done individually, so do not share your code with anyone else

● You must use  C for this assignment,  and all submitted code  must successfully compile via gcc with no warning messages when the -Wall (i.e., warn all) compiler option is used; we will also use -Werror, which will treat all warnings as critical errors

● All submitted code  must successfully compile and run on  Submitty,  which uses Ubuntu v20.04.3 LTS and gcc version 9.3.0 (Ubuntu  9.3.0-17ubuntu1~20.04)

 

Readings

In our textbook, please read Chapters 3 and 4 as follows:

● Section 3.6 Parallelism and computer arithmetic: Subword parallelism

● Section 4.1 Introduction

● Section 4.2 Logic design conventions

● Section 4.3 Building a datapath (focus on this section)

● Section 4.4 A simple implementation scheme (focus on this section)

● Section 4.5 A multicycle implementation

● Section 4.6 An overview of pipelining

● Section 4.7 Pipelined datapath and control

 

Practice problems

Work through the practice problems below, but do not submit solutions to these problems.  Feel free to post questions, comments, and answers in our Discussion Forum.

● Textbook problem 4.3

● Textbook problem 4.5

● Textbook problem 4.6

● Textbook problem 4.11

● Textbook problem 4.18

● Textbook problem 4.19

● Textbook problem 4.20


Graded programming assignment

 

The core of this homework is to write C code to parse an input file containing C source code and generate MIPS code. More specifically, you will implement a rudimentary compiler that translates

one or more assignment statements into valid MIPS code.

Place all of your code in a hw5.c file.

For parsing, assume that each C variable name is one lowercase letter (e.g., a, b, c, etc.)  and of type int. Integer constants may be positive, negative, or zero. You will need to support addition (+) and subtraction (-).

You will also need to support multiple lines of input, i.e., multiple assignment instructions. To avoid overcomplicating your implementation, you can assume that each assignment statement will consist of a mix of addition and subtraction operations or a simple assignment, e.g., x = 42.  Therefore, you can always just parse each line from left to right.

The MIPS code you generate must make proper use of registers $s0,...,$s7 to correspond to C variables as you discover them.  Further, registers $t0,...,$t9 correspond to any temporary variables that you need.

You can assume that no more than eight variables will be defined. For temporary registers, which store intermediate results, start with $t0, then cycle through to $t9, cycling from $t9 back to $t0 as necessary.

 

Required input and output

Your code must read the input file specified by the first command-line argument, i.e., argv[1]. Translate each line of input into corresponding MIPS instructions accordingly.  Also include each original line being translated as a comment. Do not use pseudo-instructions.

Below are a few example runs of your program that you can use to better understand how your program should work, how you can test your code, and what output formatting to use for Submitty.

In the first example (i.e., with input example1.src), register $s0 corresponds to variable g, register $s1 corresponds to h, and register $s2 corresponds to f.

 

bash$  cat  example1.src

g  =  100;

h  =  200;

f  =  g  +  h  +  42;           <==  (v1.1)  fixed  a  typo

bash$  ./a.out  example1.src

#  g  =  100;

ori  $s0,$0,100

#  h  =  200;

ori  $s1,$0,200

#  f  =  g  +  h  +  42;

add  $t0,$s0,$s1

addi  $s2,$t0,42

 

bash$  cat  example2.src

q  =  12;

j  =  q  -  2;

bash$  ./a.out  example2.src

#  q  =  12;

ori  $s0,$0,12

#  j  =  q  -  2;

addi  $s1,$s0,-2

 

(v1.1) Additional examples are shown below.

 

bash$  cat  example3.src

q  =  12;

j  =  16  +  q  -  r;

bash$  ./a.out  example3.src

#  q  =  12;

ori  $s0,$0,12

#  j  =  16  +  q  -  r;

ori  $t0,$0,16

add  $t1,$t0,$s0

sub  $s1,$t1,$s2

 

bash$  cat  example4.src

x  =  y  +  1  -  y  -  1;

bash$  ./a.out  example4.src

#  x  =  y  +  1  -  y  -  1;

addi  $t0,$s1,1

sub  $t1,$t0,$s1

addi  $s0,$t1,-1

 

Assumptions

 

Given the complexity of this assignment, you can make the following assumptions:

 

● Assume all input files are valid

● Assume the length of argv[1] is at most 32 characters

● Assume that each line will be formatted as shown above, i.e., exactly one space character delimiting each “token” and lines always ending with a semicolon  ; character

● Assume that expressions will never have two constants adjacent to one another, e.g., x = 42 - 13 is not possible

●  (v1.1) Assume that all constants are valid signed 16-bit values, which therefore will be valid in I-format instructions


Submission instructions

Before you submit your code, be sure that you have clearly commented your code—this should not be an afterthought!  Further, your code should have a clear and logical organization.  In general, each function should easily fit within a single window and have a clear (and clearly documented) purpose; if not, separate into multiple functions.  Variable names and function names should be intuitive and meaningful. Be consistent in your indentation.

To submit your assignment (and also perform final testing of your code), please use Submitty.

Note that this assignment will be available on Submitty a minimum of three days before the due date. There will be hidden test cases.

Also note that output to standard output  (stdout) is buffered.   To disable buffered output on Submitty, use setvbuf() as follows:

 

setvbuf(  stdout,  NULL,  _IONBF,  0  );

 

You would not generally do this in practice, as this can substantially slow down your program, but to ensure you see output even on a segmentation fault on Submitty, this is a good technique to use.