Download Computer Organization and Systems Programming - Midterm Exam | CSE 30 and more Exams Computer Science in PDF only on Docsity!
Name _________________________
Student ID __________________ Signature_______________________
cs30x_______
CSE 30
Spring 2010
Midterm Exam
1. Number Systems ___________________ (15 points)
2. Binary Addition/Condition Code Bits/Overflow Detection ___________________ (12 points)
3. Branching ___________________ (22 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 ___________________ (105 points)
Extra Credit ___________________ (12 points)
Total ___________________
1. Number Systems
Convert 0xFB99 (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 -341 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 +448 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 0xDEADBEEF, %l
sra %l0, 9, %l
Value in %l0 is 0x _______________________________________ (2 points)
set 0xDEADBEEF, %l
sll %l0, 11, %l
Value in %l0 is 0x _______________________________________ (2 points)
set 0xDEADBEEF, %l
set 0x????????, %l
xor %l0, %l1, %l0! Value in %l0 is now OxCAFEBABE
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( int, short, unsigned short );
/* ... Other code ... */
/* Assume this local variable
is declared appropriately and is the only local var. */
struct fubar {
char a[5]; short b; unsigned short c; 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[3] = foo( fb.d, fb.b, fb.c );
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 0xDD
.align 2 s: .half 0xCAFE
.align 4 i1: .word 0x i2: .word 0x i3: .word 0x x: .word 0xAAAA
.section ".text" main: save %sp, -96, %sp
set x, %l set s, %l lduh [%l1], %l2 _____________________ Hex value in %l
stb %l2, [%l0+1] _____________________ Hex value in word labeled x
srl %l2, 8, %l2 _____________________ Hex value in %l stb %l2, [%l0+2]
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 ldub [%l1], %l2 _____________________ Hex value in %l
sth %l2, [%l0+2] _____________________ Hex value in word labeled i stb %l2, [%l0+1]
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, 8, %l2 _____________________ Hex value in %l sth %l2, [%l0]
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[2] + 1 ); ______
printf( "%c", *(a+4) = *p ); ______
printf( "%c", *p++ ); ______
printf( "%c", *p-- ); ______
printf( "%c", *++p = p[2] + 2 ); ______
printf( "%c", *(p+2) ); ______
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 fubar that is a pointer to a function
that takes a single argument of type pointer to int and returns a pointer to a double. (4 pts)
__________________________________________________________________
A portion of the C Operator Precedence Table
Operator Associativity
++ postfix increment L to R -- postfix decrement [] array element () function call
- indirection R to L ++ prefix increment -- prefix decrement & address-of sizeof size of type/object (type) type cast
- multiplication L to R / division % modulus
. .
= assignment R to L
Scratch Paper