For this assignment, we will use a program which computes the sum of the first 3 powers of 10. The C version appears in Listing 1, and the MIPS Assembly version in Listing 2. The line numbers on the left of Listing 2 are for reference only, and are not part of the program.
You do not have to type this program in yourself. The program already resides here. Simply copy this file into your own directory and use it.
I. Run the simulator in single steps. What is printed
on the spim console?
Do you notice any discrepancies between the given program and the instructions
executed? What are the discrepancies?
II. Complete the following table by examining various registers
and memory location contents after executing the appropriate instructions.
Try to use the breakpoint command.
Location | Line 16 | Line 23
first iteration |
Line 23
second iteration |
Line 39 |
$25 | ||||
$24 | ||||
$8 | ||||
8($sp) | ||||
4($sp) |
Below is a program written in C. You can also find a separate
file for this C program here.
However, this program is short enough that you can also type it in, and
we ask you to do that in the Getting Started exercise so you can practice
with emacs.
#include <stdio.h>
main() { int i; int sum = 1; int power = 1;
for (i = 1; i <= 2; i = i + 1) { power = power * 10; sum = sum + power; } printf ("The sum of the first 3 powers of 10 is %d\n", sum); }Take a look at the assembly code (prob0.s) and the C code (prob0.c) to begin to see the relationship and differences between operations in a high-level language and operations in assembly language.
|
.data
str: .asciiz "The sum of the first 3 powers of 10 is: " newline: .asciiz "\n" .text
main:
li $2, 4
li $2, 1
li $2, 4
lw $31, 0($sp)
.end main |
# tells assembler that this is data
# tells assembler the following is code
# Allocate space for storage on stack (4 words)
# Load value of power (1st time it's 1)
# Use syscall 1 (print_int)
# Get return address
|