Download Spring 2009 CSE 30 Midterm Exam - Prof. Richard Ord and more Exams Computer Science in PDF only on Docsity!
Name _________________________
Student ID __________________ Signature_______________________
cs30x_______
CSE 30
Spring 2009
Midterm Exam
1. Number Systems ___________________ (15 points)
2. Binary Addition/Condition Code Bits/Overflow Detection ___________________ (12 points)
3. Branching ___________________ (21 points)
4. Bit Operations / C Runtime Environment ___________________ (17 points)
5. Parameter Passing and Return Values (Stack Variables) ___________________ (12 points)
6. Local Variables, The Stack and Return Values ___________________ (16 points)
7. Load/Store/Memory ___________________ (11 points)
SubTotal ___________________ (104 points)
Extra Credit ___________________ (12 points)
Total ___________________
1. Number Systems
Convert 0xFC16 (2โs complement, 16-bit word) to the following. (6 points)
binary ____________________________________ (straight base conversion)
octal 0 ___________________________________ (straight base conversion)
decimal ____________________________________ (convert to signed decimal)
Convert -444 to the following (assume 16-bit word). Express answers in hexadecimal. (6 points)
sign-magnitude 0x_______________________________________________
1โs complement 0x_______________________________________________
2โs complement 0x_______________________________________________
Convert +341 to the following (assume 16-bit word). Express answers in hexadecimal. (3 points)
sign-magnitude 0x_______________________________________________
1โs complement 0x_______________________________________________
2โs complement 0x_______________________________________________
2. Binary Addition/Condition Code Bits/Overflow Detection
Indicate what the condition code bits are when adding the following 8-bit 2โs complement numbers. (12 points)
N Z V C N Z V C N Z V C
4. Bit Operations / C Runtime Environment
What is the value of %l0 after each statement is executed? Express your answers in hexadecimal.
set 0xCAFE1234, %l
sra %l0, 15, %l
Value in %l0 is 0x_______________________________________ (2 points)
set 0xCAFE1234, %l
sll %l0, 9, %l
Value in %l0 is 0x_______________________________________ (2 points)
set 0xCAFE1234, %l
set 0x????????, %l
xor %l0, %l1, %l0! Value in %l0 is now Ox1234CAFE
Value set in %l1 must be this bit pattern 0x_______________________________________ (3 points)
Fill in the names of the 5 areas of the C Runtime Environment as laid out by the SPARC architecture.
Then state what parts of a C program are in each area. (10 points)
low memory
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
high memory
5. Parameter Passing and Return Values (Local Stack Struct Variable)
Write the equivalent unoptimized SPARC assembly language instructions to perform the following C code
fragment. You can assume just this one local variable. (12 points)
C SPARC assembly
/* Function Prototype */
char foo( short, unsigned short, int );
/* ... Other code ... */
/* Assume this local variable
is declared appropriately and is the only local var. */
struct fubar {
char a[3]; short b; unsigned short c[2]; int d;
} fb; /* Local variable fb */
/* ... Other code ... */
/*
Write the code for just this Put your SPARC Assembly code function call, saving the in the box below. return value appropriately */
fb.a[2] = foo( fb.b, fb.c[1], fb.d );
7. Load/Store/Memory Specify the hex values requested after those lines have been fully executed. (11 points)
.global main
.section ".data" fmt: .asciz "0x%08X\n"! prints value as hex 0xXXXXXXXX
c: .byte 0x
.align 2 s: .half 0xBEAD
.align 4 i1: .word 0x i2: .word 0x i3: .word 0x x: .word 0x
.section ".text" main: save %sp, -96, %sp
set x, %l set s, %l lduh [%l1], %l2 _____________________ Hex value in %l
stb %l2, [%l0+3] _____________________ Hex value in word labeled x
srl %l2, 4, %l2 _____________________ Hex value in %l stb %l2, [%l0]
set fmt, %o ld [%l0], %o call printf _____________________ Hex value in word labeled x nop (same as output of this printf)
set i1, %l set c, %l ldsb [%l1], %l2 _____________________ Hex value in %l
sth %l2, [%l0+2] _____________________ Hex value in word labeled i stb %l2, [%l0]
set fmt, %o ld [%l0], %o call printf _____________________ Hex value in word labeled i nop (same as output of this printf)
set i2, %l set i3, %l ld [%l1], %l2 _____________________ Hex value in %l
sth %l2, [%l0] _____________________ Hex value in word labeled i
sra %l2, 16, %l2 _____________________ Hex value in %l sth %l2, [%l0+2]
set fmt, %o ld [%l0], %o call printf _____________________ Hex value in word labeled i nop (same as output of this printf)
ret restore
Extra Credit (12 points)
What gets printed at each printf() statement given the following C program? (8 pts)
#include <stdio.h>
int main() { char a[] = "SPARC"; char *p = a;
printf( "%c", *p++ ); ______
printf( "%c", *(p+2) = p[3] ); ______
printf( "%c", *(p+3) = p[-1] ); ______
printf( "%c", *++p ); ______
printf( "%c", *p++ = *a + 2 ); ______
printf( "%c", ++*p++ ); ______
printf( "%d", ++p - a ); ______
printf( "\n%s\n", a ); _______________________
return 0; }
Using the Rt-Lt Rule, write the C variable definition for the variable named foo that is a pointer to an array of 7
elements where each element is a pointer to a char. (4 pts)
__________________________________________________________________
A portion of the Operator Precedence Table
Operator Associativity
++ postfix increment L to R -- postfix decrement
- indirection R to L ++ prefix increment -- prefix decrement & address-of
- multiplication L to R / division % modulus
. . .
= assignment R to L
Scratch Paper