21 Feb 2013

Homework 4 -- CS 429 Spring 2013

We have covered Chapter 3 of B+O (Computer Systems: A Programmer's Perspective (2nd Edition), Randal E. Bryant, David R. O'Hallaron, 2010), particularly the parts on function calls, as well as the CDC 6600.

Topics: function calls, parameters, addressing modes, input/output, interrupts, machine language, assemblers

  1. Assume we pass parameters on the stack by pushing the actual parameter values on the stack in the order that they occur. So a call to foo(a,b,c) will push first a, then b, then c on the stack. Then we execute a "call" instruction which pushes the return address on the stack and jumps to the function. For a function declared as "int enter(char c, int n)" on a 32-bit machine, and a call "a = enter('3', b)", where b has the value 49, show the contents of the stack when we are at the first instruction in the function "enter".

  2. Does your answer to the previous question change if you are told that stack elements are always 32-bit (4-byte) aligned?

  3. Assume that stack elements are 4-byte aligned, and you allocate a local array of characters by "char a[6]", and then run the code "for (i=0; i <= 6; i++) a[i] = ' ';".

  4. Name 4 addressing modes.

  5. What would be an advantage of SP relative addressing? PC relative addressing?

  6. My computer has no explicit input/output instructions, just something called "memory mapped I/O". How do I access the registers for my I/O device?

  7. If I use a stack to save the return address and parameters for calling functions and procedures, would it make sense to do the same for interrupts? What would I save on the stack for an interrupt?

  8. The condition code is generally not saved for a call to a subroutine, since it is typically generated and then used immediately. An interrupt is like a subroutine call. Does this mean we can ignore saving and restoring the condition code for interrupts?

  9. Assume we have the following function:
    int total_sum(int *a, int n)
    {
       int sum = 0;  int i;
       for (i = 0; i < n; i++) sum = sum + a[i];
       return(sum);
    }
    
    Assuming the CDC 6600, and parameter a is passed in X0, parameter n is passed in X1, and the value should be passed back in X0, write the assembly language for this function. Try to keep sum and i in a register to save on the space and time of putting it in memory. Now convert that assembly language to machine language.

  10. If our calling convention says that functions return their values in the A register, where do procedures return their values?