CS 354, version A
Fall 2004
Name:___________________
ID:___________________
Login:_____________________
Exam 2

No electronic devices may be used while taking this exam. Examples of devices not allowed are calculators, pagers, cell phones, wrist calculators/computers, laptop computers, pocket computers. Each student is allowed one 8.5 by 11 inch sheet of paper with handwritten notes. The notes may be on both sides of the paper.

Show all work, and do any/all calculations on the exam. Extra scratch paper may not be used.

Exam Score
Q1 = _____ / 15
Q2 = _____ / 25
Q3 = _____ /   5
Q4 = _____ / 15
Total = _____ / 60




Question 1 (15 points total)
Part A (5 points) Define the value of mask, such that the following MAL code fragment clears bits 14 and 4 of register $12, without changing the other bits. Bit numberings are defined as starting with 0 from the rightmost bit.


.data
mask:   .word  0x_______________________

.text
      lw   $8, mask
      and  $12, $12, $8



Part B (10 points) Show your work in doing the following 8-bit, two's complement arithmetic problems. If overflow occurs, write the word "OVERFLOW" next to the computed answer.


        01101010                     11101010
      + 11001100                   - 10101111
     -----------                  -----------









Question 2 (25 points total)
A program needs to use a stack of 100 elements, where each element contains 2 integers.

Part A (3 points) Write a MAL declaration for this stack.



Part B (4 points) Assume that a stack pointer is defined with
     sp: .word
The stack is defined such that the stack pointer contains the address of the full location at the top of the stack; alternatively stated, the stack pointer points to the element at the top of the stack. Write a MAL code fragment that initializes the stack pointer.



Part C (5 points) Assume that the stack is completely full. Write an expression for the stack pointer's value.



Part D (8 points) Draw a complete and detailed diagram of what this stack must look like after 3 push operations.















Part E (5 points) Write a MAL code fragment that pops the top element off this stack. One integer is to be placed into register $16, and the other integer is to be placed into register $17. This code fragment does not consider the case of an empty stack, and ignores that this might happen.







Question 3 (5 points)
Pretend that the second operand in the MIPS-like instruction


      sw   $8, ((0x0400aa04))

specifies an indirect addressing mode. If register $8 contains the hexadecimal value 0x0400aa12, show how memory changes after execution of this invented MIPS-like instruction.
                    memory
               address      contents
               -------      ---------
              0x0400aa00   0x0400aa20

              0x0400aa04   0x0400aa16

              0x0400aa08   0x0400aa12

              0x0400aa12   0x0400aa08

              0x0400aa16   0x0400aa04

              0x0400aa20   0x0400aa00


Question 4 (15 points)
Write a MAL code fragment that adds 6 to each element of the 20th row of a 50 by 60 array of integers. Assume that the array is stored in row major order. Include comments to describe the contents of registers.