0% found this document useful (0 votes)
157 views10 pages

CSE 30 Spring 2010 Midterm Exam

This document is a midterm exam for a CSE 30 computer architecture course. It contains 7 sections testing knowledge of number systems, binary addition, branching, bit operations, parameter passing, local variables, and load/store instructions. The exam has 105 points for the main questions and 12 points of possible extra credit. Students are asked to show their work by filling in blanks in the document.

Uploaded by

bestesttestest
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
157 views10 pages

CSE 30 Spring 2010 Midterm Exam

This document is a midterm exam for a CSE 30 computer architecture course. It contains 7 sections testing knowledge of number systems, binary addition, branching, bit operations, parameter passing, local variables, and load/store instructions. The exam has 105 points for the main questions and 12 points of possible extra credit. Students are asked to show their work by filling in blanks in the document.

Uploaded by

bestesttestest
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

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 (2s 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_______________________________________________
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_______________________________________________

2. Binary Addition/Condition Code Bits/Overflow Detection


Indicate what the condition code bits are when adding the following 8-bit 2s complement numbers. (12 points)
11010111
+10101001
---------

00100001
+01001001
---------

N
Z
V
C
----------------|
|
|
|
|
-----------------

N
Z
V
C
----------------|
|
|
|
|
-----------------

10000000
+10000000
--------N
Z
V
C
----------------|
|
|
|
|
-----------------

3. Branching (22 points)


Translate the C code below into the equivalent unoptimized SPARC Assembly code. Just perform a direct
translation no optimizations. Use the local register mappings for the variables in assembly as specified.
_C_
/* Assume variables a and b have been
properly declared as ints. */

_SPARC ASSEMBLY_
! a is mapped to %l3
! b is mapped to %l6

if ( (a > b) || (a < 22) )


{
a = b + 44;
}
else
{
--b;
do
{
a = a % b;
} while ( a >= 88 );
}

4. Bit Operations / C Runtime Environment


What is the value of %l0 after each statement is executed? Express your answers in hexadecimal.
set
sra

0xDEADBEEF, %l0
%l0, 9, %l0

Value in %l0 is 0x_______________________________________ (2 points)

set
sll

0xDEADBEEF, %l0
%l0, 11, %l0

Value in %l0 is 0x_______________________________________ (2 points)

set
set
xor

0xDEADBEEF, %l0
0x????????, %l1
%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
4

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
function call, saving the
return value appropriately
*/

Put your SPARC Assembly code


in the box below.

fb.a[3] = foo( fb.d, fb.b, fb.c );

6. Local Variables, The Stack, and Return Values


Here is a C function that doesnt do much but allocate local variables, perform statements, and returns a value:
C
int fubar( int a, int b ) {
int *local_stack_var1;
int local_stack_var2[6];
*local_stack_var1++ = a;
local_stack_var1 = &local_stack_var2[2];
b = local_stack_var2[4];
*local_stack_var1 = 9990999;
return ( ++a + local_stack_var2[1] );

/*
/*
/*
/*
/*

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

! prints value as hex

0xXXXXXXXX

0xDD

.section ".text"
main:
save

%sp, -96, %sp

set
set
lduh

x, %l0
s, %l1
[%l1], %l2

_____________________

Hex value in %l2

stb

%l2, [%l0+1]

_____________________

Hex value in word labeled x

srl
stb

%l2, 8, %l2
%l2, [%l0+2]

_____________________

Hex value in %l2

set
ld
call
nop

fmt, %o0
[%l0], %o1
printf

_____________________

Hex value in word labeled x


(same as output of this printf)

set
set
ldub

i1, %l0
c, %l1
[%l1], %l2

_____________________

Hex value in %l2

sth
stb

%l2, [%l0+2]
%l2, [%l0+1]

_____________________

Hex value in word labeled i1

set
ld
call
nop

fmt, %o0
[%l0], %o1
printf

_____________________

Hex value in word labeled i1


(same as output of this printf)

set
set
ld

i2, %l0
i3, %l1
[%l1], %l2

_____________________

Hex value in %l2

sth

%l2, [%l0]

_____________________

Hex value in word labeled i2

sra
sth

%l2, 8, %l2
%l2, [%l0]

_____________________

Hex value in %l2

set
ld
call
nop

fmt, %o0
[%l0], %o1
printf

_____________________

Hex value in word labeled i2


(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
-------------------------------------+ addition
L to R
- subtraction
-------------------------------------.
.
-------------------------------------= assignment
R to L

Scratch Paper

Scratch Paper

10

You might also like