Sample Exercises for Quiz 2

SPARC Pipelining, SPARC Assembly Language Instructions, Branching,
Control Statements, Filling Delay Slots, Annulled Branches,
Bitwise Logical Operations, Intro to the Data Segment, Intro to The Stack

List the SPARC Assembly instructions that are delayed control transfer 
instructions that we normally fill the following instruction (the delay slot) 
with a nop instruction (before any optimization).


Why do we need to be aware of this delay slot?  What kind of instructions
cannot be inserted into a delay slot?


What other non-delayed control transfer instruction might cause a cycle to 
be wasted with a processor stall?  Give an example of how this could occur.
How could we avoid wasting a cycle in this particular situation?


What are the Integer Condition Code (icc) bits?
What does it mean if there is a 0 or a 1 in each bit?


Translate the following C control statements into equivalent SPARC assembly.
Give both unoptimized and optimized versions.  Use only the "preferred"
looping structure as specified in the Notes (the way real compilers
translate loops).

x = 3;
while ( x > 17 ) {
   stmt1;
   stmt2;
   ++x;
}
stmt1_after_while;
stmt2_after_while;

--------------------------

x = 3;
do {
   stmt1;
   stmt2;
   ++x;
} while ( x > 17 );
stmt1_after_do-while;
stmt2_after_do-while;

--------------------------

for ( x = 0; x < 22; ++x ) {
   stmt1;
   stmt2;
}
stmt1_after_for;
stmt2_after_for;

--------------------------

/* No statements above here */
if ( x < 16 ) {
   stmt1;
   stmt2;
}
stmt1_after_if;
stmt2_after_if;

--------------------------

/* No statements above here */
if ( x < 16 ) {
   stmt1;
   stmt2;
} else {
   stmt3;
   stmt4;
}
stmt1_after_if-else;
stmt2_after_if-else;

--------------------------

x = 5555 * 12;

--------------------------

x = 4444 / 13;

--------------------------

x = 6666 % 14;

--------------------------

x = fubar( 35, -6006 );

--------------------------

What is the value (in hex) of %o3 after each set of instructions:

set  0x12873465, %o3
set  0x56128734, %o4
and  %o3, %o4, %o3


set  0x12873465, %o3
set  0x56128734, %o4
or   %o3, %o4, %o3


set  0x12873465, %o3
set  0x56128734, %o4
xor  %o3, %o4, %o3


set  0xDEADBEEF, %o3
srl  %o3, 11, %o3


set  0xDEADBEEF, %o3
sra  %o3, 8, %o3


set  0xDEADBEEF, %o3
sll  %o3, 11, %o3


set  0xDEADBEEF, %o3
set  0X12345678, %o4
bset %o4, %o3


set  0xDEADBEEF, %o3
set  0X12345678, %o4
bclr %o4, %o3


set  0xDEADBEEF, %o3
set  0X12345678, %o4
btog %o4, %o3


Know how to use tst and btst instructions.

Write the SPARC assembly bit checking instructions to test if bit 31
(most significant bit) of the value in register %o2 is set and branch
to a label named negative if it is.  In one case, use btst.
In another case, usa a bit mask and use one of AND, OR, or XOR.


What bit mask would you use to check if every evenly divisible by 4 bit
is on? Bits are numbered from 31 (most significant bit) to 0 (least
significant bit).

What is the difference between an instruction like add and addcc?


Indicate which conditional branch instructions would transfer control to
loop if used in place of the ba (branch always) instruction below.
In other words, if you were to replace the ba instruction with
bl would it still branch to loop?  What about ble?
What about bg? Etc. for all the conditional branches ...

mov    7, %l0                        mov    7, %l0
addcc  %l0, -2, %l0                  cmp    %l0, 5
ba     loop                          ba     loop
nop                                  nop

Conditional Branch Instructions
           bl      ble
           bg      bge
           be      bne
           blu     bleu
           bgu     bgeu
           bneg    bpos
           bvs     bvc
           bcs     bcc
           bz      bnz


Intro to The Data Segment

List the pseudo-ops that are used to allocate space for the various basic
data types (char -> double)? What are the alignment restrictions of the
various basic data types (char -> double)?

Write the SPARC Assembly language instructions to allocate the following
*global* variables:

	char	a = 'A';
	long	b = 0x08675309;
	char	c = '\n';	/* Use the decimal ASCII value for this one */
	double	d = -14.65;
	char	e = '\x0A';
	short	f = 420;
	char fmt[] = "CSE30 Rocks\n"

Intro to The Stack

What are some of the reasons why we have to use the runtime Stack for local
variables (as opposed to just mapping all local variables to the local
registers in the register set window)?

Write the save instruction using the formula given in the Notes and
book to allocate 5 integers as local variables on the Stack.