231 Fall 2007 -- Exam 3 Name: ______________________ Matching. Indicate the letter of the best description. (1 pt. each) 1. _____ .text section a. memory region for stack frames b. memory region for uninitialized data 2. _____ .data section c. memory region for machine instructions d. memory region for assembly language source 3. _____ .bss section e. memory region for dynamically allocated data f. read-only memory region for initialized data 4. _____ "heap" section g. read-write memory region for initialized data h. write-only memory region for initialized data 5. _____ "stack" section i. register holding address of top of stack j. register holding the address of the current 6. _____ .rodata section stack frame for displacement addressing of local variables 7. _____ signed load k. register holding address of next instruction l. load instruction that fills most significant 8. _____ unsigned load bits of word-length registers with zeros when loading less than a full word 9. _____ stack pointer m. load instruction that fills most significant bits of word-length register with the sign 10. _____ actual parameter bit of the data item when loading less than a full word 11. _____ a00, a10, a20, ... n. parameter defined in subroutine header o. parameter passed to subroutine from caller 12. _____ a00, a01, a02, ... p. row-major memory order for an array q. column-major memory order for an array 13. _____ .align 2 r. memory alignment directive for bytes s. memory alignment directive for halfwords t. memory alignment directive for words u. memory alignment directive for doublewords -- note: ignore these -- 14. _____ msb has largest addr 15. _____ msb has smallest addr v. big-endian memory order w. little-endian memory order ---- Consider this C-like program int a = 5; /* global variable */ int funny_add( int b, int c ){ a = a - 4; b = b - 3; c = c - 2; return( a + b + c ); } void main(void){ int d = 1, e; e = funny_add( a, d ); } Show final values after calls to funny_add() for the variables listed below, by column, according to the specified parameter passing methods. (18 pts. total) b:call by value b:call by value-result b:call by reference c:call by value c:call by value-result c:call by reference 16. a _____ _____ _____ 17. d _____ _____ _____ 18. e _____ _____ _____ 19. Consider the C programs and identify the parameter calling method. (4 pts.) c01 int a; int a; c02 int main(void){ int main(void){ c03 a = 0; a = 0; c04 a = sub( a ); a = sub( &a ); c05 return 0; return 0; c06 } } c07 int sub( int x ){ int sub( int *x ){ c08 return( x + 1 ); return( *x + 1 ); c09 } } x is call by __________ x is call by __________ 20. Consider the SPARC programs and identify the parm. calling method. (4 pts.) a01 main:save %sp,-112,%sp main:save %sp,-112,%sp a02 sethi %hi(a),%l0 sethi %hi(a),%l0 a03 st %g0,[%l0+%lo(a)] st %g0,[%l0+%lo(a)] a04 or %l0,%lo(a),%o0 ld [%l0+%lo(a)],%o0 a05 call sub call sub a06 nop nop a07 st %o0,[%l0+%lo(a)] st %o0,[%l0+%lo(a)] a08 clr %i0 clr %i0 a08 ret ret a10 restore restore a11 a12 sub:ld [%o0],%o0 sub:add %o0,1,%o0 a13 add %o0,1,%o0 retl a14 retl nop a15 nop input %o0 input %o0 is call by __________ is call by __________ 21. In the programs in question 20 above, consider the st %g0,[%l0+%lo(a)] instruction in the third line of each. (2 pts. each) a. What operation does the mnemonic "st" represent? ____________ b. How many bytes will "st" transfer? ____________ c. Will the bytes be transferred to memory or to a register?____________ d. What is in register %l0? ____________________________________ e. What does %lo(a) represent? ____________________________________ 22. Fill in the blanks for generic subroutine actions. (1 pts. per blank) caller: push parameter2 on stack push parameter1 on stack call subroutine /* pushes return address onto the stack */ subroutine: /* callee save */ push _____________ to save push old fp set new ______ as current ______ subtract from ______ to make room for locals set ______ to current ______ pop ________________________ pop _____________ to restore return /* pops return address from stack */ clean parameters off stack 23. Identify the five items in a generic stack frame (10 pts.) 24. Show the addressing arithmetic expression for calculating the byte address of element a[i] in an array declared in C as "int a[100]". (No SPARC code is necessary for this question. Use the address "a" as the base address of the array and use 0-origin indexing.) (2 pts.) 25. Give the SPARC code to implement the statement "a[i] = 0;" for a global array declared as "int a[100];". Assume that registers %base_a_r and %i_r have already been initialized. You may define other registers as needed. (4 pts.) 26. Give the SPARC code for the funny_add() function using call-by-value. ("a" is a global variable allocated in a .data section.) (10 pts.) // elsewhere .section ".data" .global a int a = 5; a: .word 5 ... ... // funny_add.c .section ".text" .global funny_add extern int a; .global a int funny_add( int b, int c ){ funny_add: a = a - 4; b = b - 3; c = c - 2; return( a + b + c ); } 27. Give the SPARC code for the following C procedure "sum". Assume that the program has initialized values of "a" before calling sum. (15 pts.) int a[10]; .section ".bss" a: .skip 40 ... .section ".text" ... sum( a, 10 ); set a, %o0 mov 10, %o1 ... call sum nop int sum( int x[], int n ){ ... int i, sum = 0; .global sum for( i=0; i void swap( TYPE & x, TYPE & y ){ TYPE temp = x; x = y; y = temp; } The following code is generated for swap() by "g++ -S" for different data types. when instantiated with when instantiated with int a,b; char c,d; ... ... swap(a,b); swap(c,d); the resulting code is: the resulting code is: a01 save %sp, -120, %sp save %sp, -120, %sp a02 st %i0, [%fp+68] st %i0, [%fp+68] a03 st %i1, [%fp+72] st %i1, [%fp+72] a04 ld [%fp+68], %g1 ld [%fp+68], %g1 a05 ld [%g1], %g1 * ldub [%g1], %g1 a06 st %g1, [%fp-20] * stb %g1, [%fp-17] a07 ld [%fp+68], %i5 ld [%fp+68], %i5 a08 ld [%fp+72], %g1 ld [%fp+72], %g1 a09 ld [%g1], %g1 * ldub [%g1], %g1 a10 st %g1, [%i5] * stb %g1, [%i5] a11 ld [%fp+72], %i5 ld [%fp+72], %i5 a12 ld [%fp-20], %g1 * ldub [%fp-17], %g1 a13 st %g1, [%i5] * stb %g1, [%i5] a14 ret ret a15 restore restore A. What is the parameter passing method? Explain how this can be observed from the SPARC code. (up to 4 pts.) B. Explain why some load/stores (those marked with asterisks above: a05-a06, a09-a10, a12-a13) are ldub and stb in the char-instantiated code. (up to 3 pts.) C. Explain why not all load/stores are ldub and stb in the char-instantiated code. (up to 3 pts.)