Download Problems on Computer Organization and Programming - Final Test | CSE 30 and more Exams Computer Science in PDF only on Docsity!
Login: cs30x_______ Name _________________________
Student ID ____________________ Signature_______________________
CSE 30
Winter 2009
Final Exam
1. Number Systems / C Compiling Sequence ___________________ (15 points)
2. Binary Addition/Condition Code Bits/Overflow Detection ___________________ (12 points)
3. Branching ___________________ (20 points)
4. Bit Operations ___________________ (13 points)
5. Recursion/SPARC Assembly ___________________ (10 points)
6. Local Variables, The Stack, Return Values ___________________ (20 points)
7. More Recursive Subroutines ___________________ (10 points)
8. Floating Point ___________________ (12 points)
9. Machine Instructions ___________________ (20 points)
10. Linkage, Scope, Lifetime, Data ___________________ (32 points)
11. Load/Store/Memory ___________________ (11 points)
12. Miscellaneous ___________________ (27 points)
SubTotal ___________________ (202 points)
Extra Credit ___________________ (15 points)
Total ___________________
1. Number Systems
Convert 0xFB76 (2’s complement, 16-bit word) to the following. (6 points)
binary_____________________________________ (straight base conversion)
octal_______________________________________ (straight base conversion)
decimal____________________________________ (convert to signed decimal)
Convert 42910 to the following (assume 16-bit word). Express answers in hexadecimal. (3 points)
sign-magnitude_______________________________________________
1’s complement_______________________________________________
2’s complement_______________________________________________
Convert -392 10 to the following (assume 16-bit word). Express answers in hexadecimal. (6 points)
sign-magnitude_______________________________________________
1’s complement_______________________________________________
2’s complement_______________________________________________
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
What is the value of %l0 after each statement is executed? Express your answers in hexadecimal.
set 0xFACECAFE, %l
set 0x86715309, %l
and %l0, %l1, %l
Value in %l0 is _______________________________________ (2 points)
set 0xFACECAFE, %l
sra %l0, 14, %l
Value in %l0 is _______________________________________ (2 points)
set 0xFACECAFE, %l
sll %l0, 13, %l
Value in %l0 is _______________________________________ (2 points)
set 0xFACECAFE, %l
set 0x????????, %l
xor %l0, %l1, %l0! Value in %l0 is now Ox
Value set in %l1 must be this bit pattern _______________________________________ (3 points)
set 0xFACECAFE, %l
set 0x86715309, %l
or %l0, %l1, %l
Value in %l0 is _______________________________________ (2 points)
set 0xFACECAFE, %l
srl %l0, 10, %l
Value in %l0 is _______________________________________ (2 points)
5. Recursion/SPARC Assembly
Given main.s and fubar.s, what gets printed when executed? (10 points)
.global main /* main.s */
.section ".rodata"
.align 4 code: .word 0x43216153, 0x21764567, 0x61336E4A, 0x306F2020, 0x4C265220, 0x2075652B .word 0x6C762B65, 0x6943734C, 0x
.section ".text" main: save %sp, -92 & -8, %sp
set code, %o mov 1, %o call fubar nop
ret restore
.global fubar /* fubar.s */
.section ".rodata" fmt: .asciz "%c"
.section ".text" fubar: save %sp, -(92 + 1) & -8, %sp
inc %i
cmp %i0, %g be end nop
ldub [%i0 + %i1], %l cmp %l0, %g be end nop
stb %l0, [%fp - 1]
add %i1, 2, %o mov %i0, %o call fubar nop
set fmt, %o ldub [%fp - 1], %o call printf nop
end: ret restore
What gets printed? ______________________________
7. More Recursive Subroutines / Drawing Stack Frames
What is the output of the following program? (10 pts)
#include <stdio.h>
int BBB( int );
int AAA( int a ) { int result1;
printf( "a = %d\n", a );
if ( a >= 10 ) return 1; else { result1 = BBB( a + 1 ) + a; printf( "result1 = %d\n", result1 ); return result1; } }
int BBB( int b ) { int result2;
printf( "b = %d\n", b );
if ( b >= 10 ) return 3; else { result2 = AAA( b + 3 ) + b; printf( "result2 = %d\n", result2 ); return result2; } }
int main( int argc, char *argv[] ) { printf( "AAA = %d\n", AAA( 2 ) );
return 0; }
Put output here
8. Floating Point
Convert 165.75 10 (decimal fixed-point) to binary fixed-point (binary) and single-precision IEEE floating-point
(hexadecimal) representations.
binary fixed-point __________________________________ (2 points)
IEEE floating-point __________________________________ (4 points)
Convert 0xC356E000 (single-precision IEEE floating-point representation) to fixed-point decimal.
fixed-point decimal __________________________________ (6 points)
9. Machine Instructions
Translate the following instructions into SPARC machine code. Use hexadecimal values for your answers. If
an instruction is a branch, specify the number of instructions away for the target (vs. a Label).
ldub [%i5 + %l4], %o2 ___________________________________ (5 points)
sub %l3, 15, %i4 ___________________________________ (5 points)
Translate the following SPARC machine code instructions into SPARC assembly instructions.
0xD43FBFF8 ___________________________________ (5 points)
0x3EBFFFF7 ___________________________________ (5 points)
11. 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 0xCC
.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 ldsh [%l1], %l2 _____________________ Hex value in %l
stb %l2, [%l0+1] _____________________ Hex value in word labeled x
srl %l2, 12, %l2 _____________________ Hex value in %l stb %l2, [%l0+3]
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]
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
stb %l2, [%l0] _____________________ Hex value in word labeled i
sra %l2, 12, %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
12. Miscellaneous
Optimize the following piece of SPARC Assembly to eliminate the nop instructions. Follow the optimization
techniques used in class to fill the delay slots with useful instructions already in the code. (6 pts)
Just fill delay slots. Do not try to
make sense of the code.
L1:
cmp %l1, 42 bl L nop
add %l1, 4, %l
L2: cmp %l1, 99 bl L nop
What is Rick's favorite drink in the morning? (1 pt) ________________________________________________
Given the following program, order the printf() lines so that the values that are printed when run on a Sun
SPARC Unix system are displayed from smallest value to largest value. (2 points each)
void foo( int, int ); /* Function Prototype */
int a = 24;
int main( int argc, char *argv[] ) {
int b = 99; prints int c; ___________ smallest value
foo( c, b ); ___________
/* 1 / (void) printf( "c --> %p\n", &c ); ___________ / 2 / (void) printf( "foo --> %p\n", foo ); / 3 / (void) printf( "b --> %p\n", &b ); ___________ / 4 / (void) printf( "argc --> %p\n", &argc ); / 5 */ (void) printf( "malloc --> %p\n", malloc(50) ); ___________ }
void foo( int d, int e ) {
int f = 42; static int g; ___________
/* 6 / (void) printf( "g --> %p\n", &g ); ___________ / 7 / (void) printf( "e --> %p\n", &e ); / 8 / (void) printf( "a --> %p\n", &a ); ___________ prints / 9 */ (void) printf( "f --> %p\n", &f ); largest value
Hexadecimal - Character
| 00 NUL| 01 SOH| 02 STX| 03 ETX| 04 EOT| 05 ENQ| 06 ACK| 07 BEL|
| 08 BS | 09 HT | 0A NL | 0B VT | 0C NP | 0D CR | 0E SO | 0F SI |
| 10 DLE| 11 DC1| 12 DC2| 13 DC3| 14 DC4| 15 NAK| 16 SYN| 17 ETB|
| 18 CAN| 19 EM | 1A SUB| 1B ESC| 1C FS | 1D GS | 1E RS | 1F US |
| 20 SP | 21! | 22 " | 23 # | 24 $ | 25 % | 26 & | 27 ’ |
| 28 ( | 29 ) | 2A * | 2B + | 2C , | 2D - | 2E. | 2F / |
| 38 8 | 39 9 | 3A : | 3B ; | 3C < | 3D = | 3E > | 3F? |
| 40 @ | 41 A | 42 B | 43 C | 44 D | 45 E | 46 F | 47 G |
| 48 H | 49 I | 4A J | 4B K | 4C L | 4D M | 4E N | 4F O |
| 50 P | 51 Q | 52 R | 53 S | 54 T | 55 U | 56 V | 57 W |
| 58 X | 59 Y | 5A Z | 5B [ | 5C \ | 5D ] | 5E ^ | 5F _ |
| 60 ‘ | 61 a | 62 b | 63 c | 64 d | 65 e | 66 f | 67 g |
| 68 h | 69 i | 6A j | 6B k | 6C l | 6D m | 6E n | 6F o |
| 70 p | 71 q | 72 r | 73 s | 74 t | 75 u | 76 v | 77 w |
| 78 x | 79 y | 7A z | 7B { | 7C | | 7D } | 7E ~ | 7F DEL|
A portion of the Operator Precedence Table
Operator Associativity
++ postfix increment L to R
-- postfix decrement
[] array element
* indirection R to L
++ prefix increment
-- prefix decrement
& address-of
* multiplication L to R
/ division
% modulus
+ addition L to R
= assignment R to L
Scratch Paper