Professional Documents
Culture Documents
Lecture 6
Instruction Set Overview
Logic Operations
Example 1
Branching(Flow Control) Instruction
• Loop
• If-else
• switch-case
• Function Call
Branching Instruction List
Loop
• Conditional branch or Combination of
conditional and unconditional branches
Example (Loop)
Loop Example Solution
If-else
• Combination of conditional and unconditional jump statements
Conditional_Jump_to_ifLabel1
Conditional_Jump_to_ifLabel2
else:
else_operation_statements
j bypass_others
ifLabel1:
ifLabel1_opertion_statements
j bypass_others
ifLabel2:
ifLabel1_opertion_statements
j bypass_others
bypass_others:
Switch-Case
• Jr $r is used alongwith conditional and
unconditional branch instructions.
• For Example for the C code given below
switch(x)
case 1: y=1+x
case 2: y=0
case 3: y=y*3;
Switch-Case(Continued)
• Compiler will make generate a vector table. An
array of addresses
addi $reg,$basereg,offset
jr reg
of jump table
# Each address is separated by 4 bytes = 1 word
add $t0,$t0,$x
jr $t0
Procedures (functions)
• Arranging a bunch of code performing a
specialized task as a separate entity
• jal Label and jr $ra instruction.
• jal Label jumps to Label while moving the
address of next instruction to $ra register
• jr $ra for returning from the procedure
• $a0-a3 are used for passing arguments
Procedures (functions)
• What if ?
– Called procedure (callee) calls another procedure
using jal Label2. Previous value of $ra will be
overwritten
– There are more the 4 arguments
– Callee uses the temporary registers used by caller
Stack
• Area of memory reserved for storing temporary
variables
• Grows from top to bottom
• $sp contains the address of last variable saved.
• In order to save a register(1 word) say $t0, update
$sp i.e. decrement $sp by 4 and,
• Use sw $t0,0($sp)
• After loading register back using lw increment satck
pointer by 4
Register usage convention
• $t register – Caller saved
• $s registers – Callee saved
• $a registers – Arguments
• $v registers – Return Value
Stack (Continued)
• Caller saves $t registers to stack before using jal,
so that callee can freely use them.
• Callee saves $s registers to stack before using
them and later retrieves them before return back
to caller.
• Arguments in excess of 3 are passed by $a and
stack.
• When callee calls another procedure, it saves
present $ra to stack
Frame pointer
• Points to first word of a procedure i.e at the
start of the procedure when $sp is
decremented for the first variable; move its
value to frame pointer $fp
• Since $sp keeps on changing during the
procedure $fp provides a stable base to access
local variables
Frame Pointer
Memory Allocation
• Global pointer $gp points to static data
segment; data that is used throughout the
execution of program (data in main()) or the
data declared using keyword static in C.
Communicating with People (Strings)