Lec 14
Lec 14
• The instruction sll shifts all bits in the 32-bit data word
to the left the specified number of places, from 1 to 31.
• Vacated positions are automatically filled with zeroes.
After an n-bit left shift, the n right positions will be 0.
• The n bits shifted out of the word are lost.
2 Lecture #14: Shift and Rotate, Procedures, and the Stack © N. B. Dodge 8/17
Erik Jonsson School of Engineering and
The University of Texas at Dallas
Computer Science
Left 3 bit Shift right 3 (srl) Each bit shifted 3 places right
positions
filled with
zeroes
Right 3 bit positions “lost”
• The MIPS instruction srl shifts all the bits in the 32-bit
data word to the right from 1 to 31 places.
• Vacated positions are filled with zeroes. At the end of
an n-bit right shift, the n left positions will be 0.
• Bits shifted out are eliminated. After an n-bit right
shift, the original n bits at the right are lost.
4 Lecture #14: Shift and Rotate, Procedures, and the Stack © N. B. Dodge 8/17
Erik Jonsson School of Engineering and
The University of Texas at Dallas
Computer Science
6 Lecture #14: Shift and Rotate, Procedures, and the Stack © N. B. Dodge 8/17
Erik Jonsson School of Engineering and
The University of Texas at Dallas
Computer Science
Rotate Instructions
• Rotate instructions are similar to shift instructions.
• In a shift instruction, an n-bit shift to left or right
results in n bits being discarded.
• Further, the bit positions on the opposite end are
vacated or filled with 0’s (srl, sll) or 1’s (sra only).
• Rotate instructions are shifts that do not eliminate bits.
• For a left rotate (rol), bits shifted off the left end of a
data word fill the vacated positions on the right.
• Likewise, for a right rotate (ror), bits “falling off” the
right end appear in the vacated positions at left.
• Note that there are NO arithmetic rotates.
8 Lecture #14: Shift and Rotate, Procedures, and the Stack © N. B. Dodge 8/17
Erik Jonsson School of Engineering and
The University of Texas at Dallas
Computer Science
9 Lecture #14: Shift and Rotate, Procedures, and the Stack © N. B. Dodge 8/17
Erik Jonsson School of Engineering and
The University of Texas at Dallas
Computer Science
• For the three-bit rotate left (rol) shown above, the three
left-most bits are shifted off the left side of the data
word, but immediately appear as the three right-most
bits, still in the same sequence, left-to-right.
• All the other bits in the word are simply shifted left
three places, just as in a shift left (sll) instruction.
• Note that no bits are lost.
10 Lecture #14: Shift and Rotate, Procedures, and the Stack © N. B. Dodge 8/17
Erik Jonsson School of Engineering and
The University of Texas at Dallas
Computer Science
Masking Examples
13 Lecture #14: Shift and Rotate, Procedures, and the Stack © N. B. Dodge 8/17
Erik Jonsson School of Engineering and
The University of Texas at Dallas
Computer Science
Program 1
• Write the following short program:
– A single data declaration – chars: .word 0x21445455
– Write a very short program to use a rotate right instruction to
output the four bytes of the word above as four ASCII
characters.
– Don’t bother to make this a loop; simply write the linear
instructions to output the four bytes. The program will be less
than 15 instructions (including directives). Hints:
• Use syscall 11 for the outputs; it outputs the bottom 8 bits of $a0
as an ASCII character.
• Output the first character before rotating.
• Use rotate right and output the other three characters in the
correct order to get the desired output.
• What is output?
16 Lecture #14: Shift and Rotate, Procedures, and the Stack © N. B. Dodge 8/17
Erik Jonsson School of Engineering and
The University of Texas at Dallas
Computer Science
Subroutines or Procedures
• A subroutine or procedure (sometimes referred to as a
subprogram) is a segment of a larger program that is
typically relatively independent of that program.
1. Common functions or capabilities are often required by
multiple software subsystems or modules in a larger program.
2. Rather than have each module duplicate redundant functions,
common modules are created that can be called when needed
by any module or subsystem of the overall program.
3. Such reusable modules are quite common in large programs.
4. The reuse requirement means that these modules must be
carefully written.
18 Lecture #14: Shift and Rotate, Procedures, and the Stack © N. B. Dodge 8/17
Erik Jonsson School of Engineering and
The University of Texas at Dallas
Computer Science
Subroutines (2)
• Subroutine requirements:
– Defined by its inputs and outputs only (very similar to
computer hardware or logic design).
– Can be debugged using simulated inputs.
– As long as the subroutine “meets the “spec,” it should “plug
into” and operate well with the larger program.
• Modern programming involves “hierarchical design:
– Large programs are structured in layers.
– Executive layers supervise overall operation, while middle
layers (“middle managers”) summon “worker” modules.
– These lowest-layer modules perform actual functions.
– Many of these are procedures.
19 Lecture #14: Shift and Rotate, Procedures, and the Stack © N. B. Dodge 8/17
Erik Jonsson School of Engineering and
The University of Texas at Dallas
Computer Science
20 Lecture #14: Shift and Rotate, Procedures, and the Stack © N. B. Dodge 8/17
Erik Jonsson School of Engineering and
The University of Texas at Dallas
Computer Science
The Stack
• There are several bookkeeping activities when calling
subroutines:
– The calling program must pass arguments (that is, data to be
processed) to the procedure, and get the result(s) returned.
– The procedure must protect existing register data, which will
be required by the calling program, when it resumes.
– Many procedures are recursive, that is, capable of calling
themselves. A recursive procedure must be written very
carefully.
• Possible multi-level procedure calls means that data
must be preserved across procedure calls.
• The stack is ideal for this use.
21 Lecture #14: Shift and Rotate, Procedures, and the Stack © N. B. Dodge 8/17
Erik Jonsson School of Engineering and
The University of Texas at Dallas
Computer Science
22 Lecture #14: Shift and Rotate, Procedures, and the Stack © N. B. Dodge 8/17
Erik Jonsson School of Engineering and
The University of Texas at Dallas
Computer Science
23 Lecture #14: Shift and Rotate, Procedures, and the Stack © N. B. Dodge 8/17
Erik Jonsson School of Engineering and
The University of Texas at Dallas
Computer Science
25 Lecture #14: Shift and Rotate, Procedures, and the Stack © N. B. Dodge 8/17
Erik Jonsson School of Engineering and
The University of Texas at Dallas
Computer Science
* Follows our “stack pointer points to the last filled location” convention.
26 Lecture #14: Shift and Rotate, Procedures, and the Stack © N. B. Dodge 8/17
Erik Jonsson School of Engineering and
The University of Texas at Dallas
Computer Science
28 Lecture #14: Shift and Rotate, Procedures, and the Stack © N. B. Dodge 8/17
Erik Jonsson School of Engineering and
The University of Texas at Dallas
Computer Science
29 Lecture #14: Shift and Rotate, Procedures, and the Stack © N. B. Dodge 8/17
Erik Jonsson School of Engineering and
The University of Texas at Dallas
Computer Science
Frame Construction
Stack Memory Stack Memory Stack Memory
$sp Data $fp Data $sp Data
Data Data Data
“Empty” Frame 1 “Empty”
“Empty” Frame 2 “Empty”
“Empty” Frame 3 “Empty”
“Empty” Frame 4 “Empty”
“Empty” $sp Frame 5 “Empty”
“Empty” Frame 6 “Empty”
“Empty” “Empty” Procedure “Empty”
variables
“Empty” “Empty” stored as “Empty”
necessary
Before Procedure Call During Procedure After Procedure Call
31 Lecture #14: Shift and Rotate, Procedures, and the Stack © N. B. Dodge 8/17
Erik Jonsson School of Engineering and
The University of Texas at Dallas
Computer Science
32 Lecture #14: Shift and Rotate, Procedures, and the Stack © N. B. Dodge 8/17
Erik Jonsson School of Engineering and
The University of Texas at Dallas
Computer Science
Program 2
• Declare the following four numbers in your data
declaration (use “.word”).
num1: .word 34527
num2: .word 98564
num3: .word 12953
num4: .word 68577
• Then write a program to print out the numbers in
reverse order, using the stack. Do not use a loop to do
this.
• Remember to output a CR/LF between each number,
so that each appears on a new line:
li $v0,11
li $a0, 0x0a Outputs a CR/LF
syscall
33 Lecture #14: Shift and Rotate, Procedures, and the Stack © N. B. Dodge 8/17
Erik Jonsson School of Engineering and
The University of Texas at Dallas
Computer Science
Program 3
• The preceding program was rather long, because we did not use a
loop to make it more compact. Using the same data:
num1: .word 34527 num2: .word 98564
num3: .word 12953 num4: .word 68577
• Rewrite the program to reverse the order of the numbers and
print out as before, but use two short loops to (a) store the words
in the stack, and (b) print them out.
• As in Program 2, you still need to output a CR/LF between
numbers, so that each appears on a new line.
• Hints:
– You will need to load the address of the first number (num1) into a
register, and then address it (and the other numbers) by the indirect-
register-plus-offset method.
– You will need a counter for each loop to determine when you have
stored (and later output) four numbers.
35 Lecture #14: Shift and Rotate, Procedures, and the Stack © N. B. Dodge 8/17