CSE 30 Spring 2010 Midterm Exam
CSE 30 Spring 2010 Midterm Exam
Student ID __________________
Signature_______________________
cs30x_______
CSE 30
Spring 2010
Midterm Exam
1. Number Systems
3. Branching
7. Load/Store/Memory
SubTotal
Extra Credit
Total
___________________
1. Number Systems
Convert 0xFB99 (2s complement, 16-bit word) to the following. (6 points)
binary
octal
decimal
Convert -341 to the following (assume 16-bit word). Express answers in hexadecimal. (6 points)
sign-magnitude 0x_______________________________________________
1s complement 0x_______________________________________________
2s complement 0x_______________________________________________
Convert +448 to the following (assume 16-bit word). Express answers in hexadecimal. (3 points)
sign-magnitude 0x_______________________________________________
1s complement 0x_______________________________________________
2s complement 0x_______________________________________________
00100001
+01001001
---------
N
Z
V
C
----------------|
|
|
|
|
-----------------
N
Z
V
C
----------------|
|
|
|
|
-----------------
10000000
+10000000
--------N
Z
V
C
----------------|
|
|
|
|
-----------------
_SPARC ASSEMBLY_
! a is mapped to %l3
! b is mapped to %l6
0xDEADBEEF, %l0
%l0, 9, %l0
set
sll
0xDEADBEEF, %l0
%l0, 11, %l0
set
set
xor
0xDEADBEEF, %l0
0x????????, %l1
%l0, %l1, %l0
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
4
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
function call, saving the
return value appropriately
*/
/*
/*
/*
/*
/*
statement
statement
statement
statement
statement
1
2
3
4
5
*/
*/
*/
*/
*/
Now write the equivalent unoptimized SPARC assembly language instructions to perform the equivalent. You
must allocate all local variables on the Stack. Perform each instruction literally. No short-cuts. Draw a line
between groups of instructions to indicate which instructions are associated with each C statement. (16 points)
SPARC assembly
fubar:
.global
fubar
.section
".text"
/* Your unoptimized code goes below this point */
7. Load/Store/Memory Specify the hex values requested after those lines have been fully executed. (11 points)
.global main
fmt:
.section ".data"
.asciz "0x%08X\n"
c:
.byte
s:
.align 2
.half
0xCAFE
i1:
i2:
i3:
x:
.align 4
.word
0x12345678
.word
0x12345678
.word
0x12345678
.word
0xAAAA0000
0xXXXXXXXX
0xDD
.section ".text"
main:
save
set
set
lduh
x, %l0
s, %l1
[%l1], %l2
_____________________
stb
%l2, [%l0+1]
_____________________
srl
stb
%l2, 8, %l2
%l2, [%l0+2]
_____________________
set
ld
call
nop
fmt, %o0
[%l0], %o1
printf
_____________________
set
set
ldub
i1, %l0
c, %l1
[%l1], %l2
_____________________
sth
stb
%l2, [%l0+2]
%l2, [%l0+1]
_____________________
set
ld
call
nop
fmt, %o0
[%l0], %o1
printf
_____________________
set
set
ld
i2, %l0
i3, %l1
[%l1], %l2
_____________________
sth
%l2, [%l0]
_____________________
sra
sth
%l2, 8, %l2
%l2, [%l0]
_____________________
set
ld
call
nop
fmt, %o0
[%l0], %o1
printf
_____________________
ret
restore
______
______
______
______
______
______
______
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
-------------------------------------+ addition
L to R
- subtraction
-------------------------------------.
.
-------------------------------------= assignment
R to L
Scratch Paper
Scratch Paper
10