Monash University >
School
of Computer Science and Software Engineering >
CSE1303 >
Part B >
Tutorials > Tutorial B5
CSE1303 Computer Science
Semester 3 (summer), 2003
Part B
Tutorial B5
This tute covers material from lectures B12 to B13.
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.
Selection and iteration
- Write a MIPS program which prints the numbers from 1 to 10.
- When translating an if statement to MIPS, it is neater
to invert the sense of the comparison (e.g., turn "less than" into
"greater than or equal"). Write MIPS code that corresponds to the
following C statement, inverting the sense of the comparison as
shown in lectures:
if (g < h)
{
/* stuff */
}
Now try to write the same code without inverting the
comparison; that is, the only branch you can use is blt
(branch if less than). You may need to add additional jump
instructions and labels to your code.
Comment on the increase in the length in your program, and its
readability.
- (*) Write a C program which reads integers from the user
until zero is entered. Your program then prints out the minimum,
maximum, and average of the entered numbers. You may assume that at
least one number will be entered.
- (*) Write a MIPS program that is a translation
of your above C code. Try to make your translation as faithful to
the original as possible.
Arrays
- (*) Modify your above C program so that instead of
reading integers from the user, it scans an array that already
exists in memory.
- (*) Translate this modified C program into MIPS. Try
to make your translation as faithful to the original as possble.
Branches and jumps
- MIPS jumps are J-format (jump) instructions. The 26-bit address
encoded is the address to jump to, counted in words, not
bytes, from address zero (i.e., the address is absolute).
Suggest why it is not necessary to encode the address in bytes.
(Hint: how long are instructions in MIPS?)
Suggest an advantage to encoding the address in words. (Hint: if
two of the 26 bits are freed by not counting in bytes, they can be
used for another purpose.)
- MIPS branches are an I-format (immediate) instruction. The 16-bit immediate
value encoded represented as a signed offset, counted in
words, to add to the address of the current instruction
(i.e., the
address is PC-relative). Adding this offset with the
current value of PC produces the address of the target, branched-to,
instruction.
Is the justification for using words (rather than bytes) the same
as for jumps (above)?
Suggest why the offset is signed.
Estimate the range of addresses that can be branched to using a
16-bit offset.
Suggest a reason why branches use PC-relative addressing (with an
offset) rather than absolute (counting from address zero). (Hint:
it has to do with the range you computed above.)
- Unsigned versions of branch instructions exist for some
conditions: bltu (branch if less than unsigned),
bleu (branch if less than or equal unsigned),
bgtu (branch if greater than unsigned), and
bgeu (branch if greater than or equal unsigned).
However, there are not unsigned equivalents of beq (branch
if equal) or bne (branch if not equal). Suggest why this
is the case.
Pointers and arrays
- Write C functions to compute the following standard C string
operations. Check
your C Standard Library handout (or read their manual pages on Unix)
to determine the functions' parameters and return value.
- (*) strlen: return the number of
characters in a string.
- (*) strcpy: copy a string from one location
to another.
- strncpy: copy a string from one location
to another, stopping when either the end-of-string character is
reached or if n characters have been copied.
- strcat: concatenate (add) a string onto the end of
another string.
- strchr: return a pointer to the first occurrence of a
character in a string, or NULL if the character can't be found.
- (*) strcmp: Compare two strings, and return -1, 0 or 1
depending on their dictionary order.
- (*) Write MIPS code to compute the above functions.
(Just embed the code into your main program, since you don't
yet know how to write separate functions.)
- (*) Rewrite the above C functions so that they do not use array
accesses, but instead step along strings with a pointer.
- Write MIPS code corresponding to your modified versions of the C
string functions. Comment on whether these versions are shorter.
[ Top |
Home ]
Last modified 2002-07-03