Monash University > School of Computer Science and Software Engineering > CSE1303 > Part B > Tutorials > Tutorial B5

CSE1303 Computer Science
Semester 3 (summer), 2002
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.

  1. Selection and iteration

    1. Write a MIPS program which prints the numbers from 1 to 10.
    2. 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.

    3. (*) 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.
    4. (*) 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.
  2. Arrays

    1. (*) Modify your above C program so that instead of reading integers from the user, it scans an array that already exists in memory.
    2. (*) Translate this modified C program into MIPS. Try to make your translation as faithful to the original as possble.
  3. Branches and jumps

    1. 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.)

    2. 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 instruction following the branch instruction in memory (i.e., the address is PC-relative).

      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.)

      Suggest why the offset counts from the next instruction rather than the current one.

    3. 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.
  4. Pointers and arrays

    1. 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.
      1. (*) strlen: return the number of characters in a string.
      2. (*) strcpy: copy a string from one location to another.
      3. 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.
      4. strcat: concatenate (add) a string onto the end of another string.
      5. strchr: return a pointer to the first occurrence of a character in a string, or NULL if the character can't be found.
      6. (*) strcmp: Compare two strings, and return -1, 0 or 1 depending on their dictionary order.
    2. (*) 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.)
    3. (*) Rewrite the above C functions so that they do not use array accesses, but instead step along strings with a pointer.
    4. 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-01-28