Download SPARC Assembly Exam: Recursion, Bit Operations, Linkage, and Floating Point - Prof. Richar and more Exams Computer Science in PDF only on Docsity!
Name _________________________
Signature_______________________
cs30x_______
CSE 30
Fall 2001
Final Exam
1. Number Systems ___________________ (15 points)
2. Binary Addition/Condition Code Bits/Overflow Detection ___________________ (12 points)
3. Branching ___________________ (18 points)
4. Bit Operations ___________________ (10 points)
5. Recursion/SPARC Assembly ___________________ (10 points)
6. Local Variables, The Stack and Return Values ___________________ (20 points)
7. SPARC Subroutines and Calling Convention ___________________ (14 points)
8. Floating Point ___________________ (18 points)
9. Linkage, Scope, Lifetime, Data ___________________ (31 points)
10. Machine Instructions ___________________ (20 points)
11. I/O & Virtual Memory ___________________ (11 points)
12. Miscellaneous ___________________ (32 points)
SubTotal ___________________ (211 points)
Extra Credit ___________________ (10 points)
Total ___________________
1. Number Systems
Convert EDA6 16 (2’s complement, 16-bit word) to the following. (6 points)
binary _______________________________________
octal _____________________________________
decimal ____________________________________
Convert +474 10 to the following (assume 16-bit word). Express answers in hexadecimal. (3 points)
sign-magnitude _______________________________________________
1’s complement _______________________________________________
2’s complement _______________________________________________
Convert -293 10 to the following (assume 16-bit word). Express answers in hexadecimal. (6 points)
sign-magnitude _______________________________________________
1’s complement _______________________________________________
2’s complement _______________________________________________
4. Bit Operations
What is the value of %l0 after each statement is executed? Express your answers in hexadecimal.
set 0xAB7E9F43, %l
set 0x78CDCD78, %l
or %l0, %l1, %l
Value in %l0 is _______________________________________ (2 points)
set 0xAB7E9F43, %l
sra %l0, 9, %l
Value in %l0 is _______________________________________ (2 points)
set 0xAB7E9F43, %l
sll %l0, 11, %l
Value in %l0 is _______________________________________ (2 points)
set 0xAB7E9F43, %l
set 0x91817161, %l
bclr %l1, %l
Value in %l0 is _______________________________________ (2 points)
set 0xAB7E9F43, %l
set 0x928276B5, %l
btog %l1, %l
Value in %l0 is _______________________________________ (2 points)
5. Recursion/SPARC Assembly
Given main.s and fubar.s , what gets printed when executed? (10 points)
/ main.s / .global main
.section ".rodata"
.align 4 code: .word 0x43216153, 0x21764567, 0x61336E4A, 0x306F2020, 0x4C3E5220, 0x .word 0x6C760065, 0x6900734C, 0x
.section ".text" main : save %sp, -92 & -8, %sp
set code, %o mov 1, %o call fubar nop
ret restore
/ fubar.s / .global fubar
.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
7. SPARC Subroutines and Calling Convention (2 point each)
In most architectures, some of the Stack Frame is built by the calling function and some of the Stack Frame is
built by the called function. The same is generally true of the SPARC architecture. Fill in the blanks for the gen-
eral model of Stack Frame creation/function call convention.
The ______________ function calls the instruction to set the program counter with the saved return address.
The ______________ function deallocates the space of the Stack used for the arguments
The ______________ function allocates space for and possibly saves values of registers that it may use.
The ______________ function allocates space on the Stack for the arguments being passed in the function call.
The ______________ function places the return value in the architecture-specific return value area.
The ______________ function calls the instruction to save the current value of the program counter to be used as
the return address.
The ______________ function allocates space on the Stack for local variables and performs any initialization.
8. Floating Point
Convert 97.375 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 0xC3D25000 (single-precision IEEE floating-point representation) to fixed-point decimal.
fixed-point decimal __________________________________ (6 points)
Complete the following initializations of the parts of a single-precision IEEE floating-point representation:
(2 pts each)
void classifyFloatParts( float f ) {
unsigned int bits = *(unsigned int ) &f; / bits is the bit pattern of f */
unsigned int sign = ____________________________________; /* sign bit */
unsigned int exp = ____________________________________; /* exponent bits */
unsigned int frac = ____________________________________; /* mantissa/fraction bits */
... }
9. Linkage, Scope, Lifetime, Data
For the following program fragment, specify what C runtime area/segment will be used for each variable defini-
tion or statement: (31 points — 1 point each)
int a; ______________
int b = 4; ______________
static int c = 20; ______________
static int d; ______________
int
foo( int e ) { ______________ (foo) ______________ (e)
float f = 4.20; ______________
static int g = 420; ______________
int h; ______________
int *i; ______________
i = (int *) malloc( g ); ______________ (where i is pointing)
Fill in the letter corresponding to the correct Fill in the letter corresponding to the correct
scoping/visibility for each of the variables: lifetime for each of the variables:
A) Global across all modules/functions A) Exists from the time the program is loaded to
linked with this source file. the point when the program terminates.
B) Global just to this source file. B) Exists from the time function foo() is called to
C) Local to function foo(). the point when foo() returns.
a _______ a _______
b _______ b _______
c _______ c _______
d _______ d _______
e _______ e _______
f _______ f _______
g _______ g _______
h _______ h _______
i _______ i _______
foo _______ foo _______
12. Miscellaneous
Circle T for true; Circle F for false; +2 if correct, 0 if incorrect or blank, -1 if both T and F are circled!:
T or F One way to generally speed up string compares on a Big-Endian machine is to use word compares instead
of byte by byte compares. (2 points)
T or F With virtual memory, more than one process using the same virtual address space can reside in physical
memory at the same time. (2 points)
T or F Paging will free up physical memory faster than swapping. (2 points)
T or F The hidden bit in IEEE floating-point representation allows us to represent larger values. (2 points)
T or F When combining segmentation with paging, paging is done within segments. (2 points)
T or F Static linked binaries are usually smaller than dynamic linked binaries. (2 points)
T or F Doubling the clock rate on a specific CPU will mean all instructions will execute twice as fast. (2 points)
T or F The resulting Text segment of a program compiled on a CISC system is generally larger than that of the
same program compiled on a RISC system. (2 points)
Write this modified classifyFloatParts() in SPARC assembly. Allocate local variables on the Stack. (16 points)
unsigned int foo( float ptr ); / Function prototype for foo() */
void classifyFloatParts( float f1, float f2 ) { unsigned int bits1 = foo( &f1 ); /* Local Stack variable / unsigned int bits2 = foo( &f2 ); / Local Stack variable */ }
Extra Credit
What does the following SPARC assembly language program output?
.global main
.section ".rodata" fmt: .asciz "%c"
.align 4 foo: .word 0x43005321, 0x4567336E, 0x306F204C, 0x52207565, 0x6C766569, 0x734C
.section ".text" Optimized version main: main: save %sp, -92 & -8, %sp
mov 1, %l clr %l set foo, %l ba test nop
loop: set fmt, %o ldub [%l0+%l2], %o call printf, 2 nop
mov %l1, %o mov 2, %o call .mul nop
mov %o0, %l
inc %l
test: ldub [%l0+%l2], %o tst %o bne loop nop
set fmt, %o mov 0x0A, %o call printf, 2 nop
ret restore
Output ________________________
(2 points)
Now optimize the code to get the same result with the fewest cycles. Some optimizations are better than others.
You may not be able to eliminate all nops. Go for the fewest machine cycles assuming memory accesses are sev-
eral more cycles than other non-memory access instructions. You cannot change the overall algorithm. (8 points)
Scratch Paper
Scratch Paper