CS61C Lab 5

Assembly Pointers

Background

Goals

This lab gives you more practice with using the assembly language equivalent of C arrays and pointers, in particular with the technique of writing table-driven code. (This is like data-directed programming in CS 61A.) It also introduces you to characteristics of numerical representations at the machine level.

Reading

  • P&H Sections on MIPS and Procedures
  • You should familiarize yourself with the code provided for this assignment in order to make most productive use of lab time.

Exercises

Setup

Copy the contents of ~cs61c/labs/05 to a suitable location in your home directory.

$ mkdir ~/lab
$ cp -R ~cs61c/labs/05/ ~/lab

Exercise 1

Info

An instruction on the IBM 360, which we'll call scan, worked as follows. Given the address of the start of a string and the address of a 256-byte table of values, it essentially implemented the following C code:

char *sptr;
char table[256];
for (sptr = address of start of the string; table[*sptr] == 0; sptr++) {
}

That is, it scans the string looking for the first character whose corresponding table entry is nonzero.

The file lab5.s contains the framework of a program to skip past white space in a string; for exercise 1, you complete this program. Test your solution code with the following input lines:

  • an empty input line;
  • a line containing only blanks and tabs;
  • lines that start with each of the following characters: / (slash), 0..9, : (colon), @ (at sign), A..Z, [ (left bracket), ` (backquote), a..z, and { (left brace).

Part A

At the location in the program labeled by table, provide a table of bytes with the following contents.

  • Entries corresponding to the blank, tab, carriage return, and line feed characters should be 0.
  • The entry corresponding to the null character should be 1.
  • Entries corresponding to letters (either upper-case or lower-case), digits, and the underscore character should be 2.
  • All other entries should be 3.

ASCII tables are available online (search for "ascii table" at Google).

A useful abbreviation allowed by spim in the .byte directive is the notation value:count, which initializes the next count bytes with the given value. For example, the directive .byte 0:39 initializes the next 39 bytes with 0.

Part B

Supply the code that implements the scan instruction. Your code should be given the string address in register $s1 and the table address in register $s2; it should branch to the label scan_done when it finds a character in the string whose corresponding table value is nonzero, leaving the address of that character in $s1 and the corresponding table value in $v0.

Exercise 2

Verify that C ignores integer overflow in two ways:

  1. Add 1 to the largest possible positive signed integer, and print the result.
  2. Using mips-gcc with no optimizations (see lab 4, and use -O0), generate the assembly language equivalent of your program for (a) and examine the arithmetic instructions it uses.