Monash University >
School
of Computer Science and Software Engineering >
CSE1303 >
Part B >
Tutorials > Tutorial B4
CSE1303 Computer Science
Semester 2, 2003
Part B
Tutorial B4
This tute covers material from lectures B10 to B11.
Attempt the questions marked (*) like
this before the tutorial.
If you have specific
questions about unmarked questions, you can ask the tutor about them
during the tutorial.
Arithmetic
- Write the following infix expressions in Reverse Polish
Notation. Write "get-X" if a variable X is read from,
and "put-X" if it is assigned to.
- (*) 2 * 3
- (*) 1 + 2 * 3
- 2 * 3 + 1
- C = 5 * (F - 32) / 9
- (*) F = C * 9 / 5 + 32
- n * (n + 1) / 2
- s = (L >> 31) & 1
- Write MIPS code to evaluate the following expressions (the same
as above). Assume that all variables are 32-bit signed globals. Try very hard to
conserve register usage; that is, don't use $t2
unless $t0 and $t1 contain values you need, and
re-use registers at the first possible opportunity (but don't
change the order of expression evaluation: a * b isn't
b * a). Remember that
you can use a source register as the destination register in the
same instruction. For this question, avoid using immediate arithmetic instructions;
instead, place the immediate value in a register with the
li instruction.
- (*) 2 * 3
- (*) 1 + 2 * 3
- 2 * 3 + 1
- C = 5 * (F - 32) / 9
- (*) F = C * 9 / 5 + 32
- n * (n + 1) / 2
- s = (L >> 31) & 1
- (*) Compare your answers to the above two questions.
Take special note of the order of operations for a given expression.
Fill in the following table mapping Reverse Polish operations with
the corresponding MIPS opcodes.
| RPN operation | MIPS opcode
|
|---|
| + | add
|
| - | sub
|
| * |
|
| / |
|
| number |
|
| get-var |
|
| put-var |
|
- (*) Examine the MIPS code versions of your
expressions. Look at the three-register arithmetic instructions
(add, mul, etc.).
What do you notice about the two source registers (in relation to
each other)?
What do you notice about the destination register (in relation to
each other)? Is this relationship always true?
- (*) Suggest an algorithm which, given a Reverse Polish
Notation expression, can produce MIPS code that can compute it.
Input/output
- (*) Write a MIPS program which prints your name (a constant
string), then exits.
- (*) Write a MIPS program which reads a number from the keyboard,
then prints the equivalent negative number.
- (*) Consider the following line of C. Write MIPS code that does
the same thing. Assume that n is a global variable.
printf("The answer is %d.\n", n);
Memory
- Draw memory diagrams corresponding to these C declarations.
Assume that all variables are global, and that int is the
same size as long.
- (*)
int x = 5;
int y;
- (*)
int a[4] = { 0, 1, 4, 9 };
-
char str[16] = "String example";
-
char *str = "String example";
- Draw memory diagrams corresponding to these C declarations.
Assume that all variables are local, and that int is the
same size as long. Show the values of $sp and
$fp and where they point. Assume that the value of
$sp before the function begins is 0x7FFF0000.
- (*)
int x = 5;
int y;
- (*)
int i = 42;
int j = 104;
int *iptr = &i;
-
int a[4] = { 0, 1, 4, 9 };
Instructions
- Consider the following instruction:
la $t0, -8($fp)
Explain why this la pseudoinstruction is equivalent
to this addiu instruction:
addiu $t0, $fp, -8
Local variables
- (*) Translate the following C code into MIPS assembly
language. Try to make your translation as faithful as possible;
that is, do not try to optimize the code.
#include <stdio.h>
#include <stdlib.h>
int main() {
int a;
int b;
int q;
int r;
puts("Enter two numbers: ");
scanf("%d", &a);
scanf("%d", &b);
q = a / b;
r = a % b;
printf("Quotient is %d\n", q);
printf("Remainder is %d\n", r);
exit(0);
}
[ Top |
Home ]
Last modified 2002-07-03