Converting C program to mips. The coding should run in Mars Mips Stimulator

profileNiks333
ProgrammingAssignment1Instructions4.pdf

CMPS 290

Fall 2018

Programming Assignment #1

Due 9/17/18 by 10:00PM

For the first assignment, you will be converting the C code bellow into equivalent MIPS code that will

run in the MARS MIPS simulator.

C Code: Loops and print out the value of i up to a specific value and then prints that value out as

well.

int i = 2;

while( i != 2097152){

printf(“%d\n”,i);

i = i * 2;

{

printf(“%d\n”,i);

So to break it down, starting at 2, we can see that the loop goes through and prints the current value of i

along with a new line character (\n), and then it multiplies itself by 2 and starts over. Once the loop

terminates, it does one final print of the value of i plus a new line.

Your Task:

1. Convert this to working MIPS code for the MARS simulator.

2. Have it have the same output as if you were to compile and run the above code (see the

sample output below).

3. Try to be memory conscious. Even though we have plenty of registers for this program, try

to use as few as possible.

4. For printing – see below for a quick reminder.

5. Be sure to end the program correctly, so that it doesn’t fall off the bottom.

Submission:

Name your file Assignment01YourLastName.asm and submit only that to Moodle. Make sure it

assembles and works correctly. You may leave your submission as a draft. This will allow you to modify

your submission should you submit the wrong file or want to make a change, but still allow me to see

the final submission date and time. Keep in mind I can only see the final submission time so if you

change your file or submit it after the due date then that will be the time I see.

Printing in MIPS:

Looking back at what we did to print in MIPS. You set up the correct registers to tell the system

what is going to be printed ($v0) and to pass the value that is going to be printed over ($a0). There are

three operations we have to call to get something printed, two setup operations and then the actual call

to the system. They will look something like this:

li $v0, 1 #load immediate instruction, 1 is the service to print an integer

add $a0, $t0, $zero #load the desired value into $a0

syscall #call to the system

The other service that will help you besides 1 being the service to print an integer value is that 11 is the

service to print a character. Remember that we can use the decimal value for an ASCII character to

easily print it.

Sample Output (C and MIPS code):

C code:

#include "stdio.h"

int main(void) { //program starts here

int i = 2; //set i = 2

while( i != 2097152){ //as long as i isn’t 2097152 keep looping

printf("%d\n",i); //print off i (%d) followed by the new line character (\n)

i=i*2; //multiply i by 2

}

printf("%d\n",i); //print off the final value of i, same as above

return 0; //end the program

} Output (for both): 2

4

8

16

32

64

128

256

512

1024

2048

4096

8192

16384

32768

65536

131072

262144

524288

1048576

2097152