You are on page 1of 34

Instructions: Language

of Computer
Lecture # 6
Course Instructor: Dr. Afshan Jamil
Outline
• Logical operations
• Instructions for making decisions
• Other comparison operators
• Procedures
• Procedure calling
• Data flow in RISC-V
• Procedure call instructions
• Function calls and stacks
• Stacks
• RISC-V stack
Logical Operations
Operation C++ RISC-V
instructions
Shift left logical << sll, slli
Shift right logical >> srl, srli
Shift right arithmetic >> sra, srai
Bit-by bit AND & and, andi
Bit-by-bit OR | or, ori
Bit-by-bit NOT ~ xori
Bit-by-bit XOR ^ xor, xori
3
CONTD…
CONTD…
CONTD…
• slli x11,x19,4 // reg x11= reg x19<< 4 bits
– x19= 00000011 //3
– x11=00110000 //48
• Shifting left by i bits gives the same result as multiplying
by 2𝑖 ,
• srli x11,x19,4 // reg x11 = reg x19 >> 4 bits
– x19= 11110000 //240
– X11=00001111 //15
• Shifting right by i bits gives the same result as dividing
by 2𝑖 ,
CONTD…
CONTD…
CONTD…
• and x9,x10,x11 // reg x9= reg x10 & reg x11
• x9= 1101 1111
• x10= 0010 0011
• x11= 0000 0011
• or x9,x10,x11 // reg x9= reg x10| reg x11
• x9= 1101 1101
• x10= 0010 0001
• x11= 1111 1101
CONTD…
• xor x9,x10,x11 // reg x9 = reg x10 ^ reg x11
• x9= 1101 1111
• x10= 0010 0011
• x11= 1111 1100

• xori x9,x10,63 // reg x9 = reg x10 ^ 63


• x9= 1101 1100
• x10= 1111 1111
• x11= 0010 0011
Instructions for making decisions

• An instruction that tests a value and that allows for a


subsequent transfer of control to a new address in the
program based on the outcome of the test.
• beq rs1, rs2, L1 (branch on equal)
– if (rs1 == rs2) goto/branch to; instruction labeled L1;
• bne rs1, rs2, L1 (branch on not equal)
– if (rs1 != rs2) goto/branch to instruction labeled L1;

11
CONTD…

• EXAMPLE RISC-V Assembly code:


• C code: • five variables f through j correspond
to the five registers x19 through x23
if (i==j)
bne x22, x23, Else
f = g+h; add x19, x20, x21
else beq x0,x0,Exit
Else: sub x19,x20,x21
f = g-h; Exit:

12
CONTD…
EXAMPLE
C code:
while(save[i]==k)
i += 1; Assume that i and k
RISC-V Assembly code correspond to registers
x22 and x24 and the
Loop: sll x10, x23, 3 base of the array save
add x10, x10, x25
lw x9, 0(x10) is in x25.
bne x9, x24, Exit
addi x22, x22, 1
beq x0,x0, Loop
Exit:
13
Other comparison operators
• Branch if less than (blt) instruction compares the values
in registers rs1 and rs2 and takes the branch if the value
in rs1 is smaller, when they are treated as two’s
complement (signed) numbers.
• blt rs1, rs2, L1 (branch if less than)
– if (rs1 < rs2) goto/branch to instruction labeled L1;
• Branch if greater than or equal (bge) takes the branch if
the value in rs1 is atleast the value in rs2.
• bge rs1, rs2, L1 (branch if greater than
or equal)
– if (rs1 >= rs2) goto/branch to instruction labeled L1;
CONTD…
• Branch if less than, unsigned (bltu) takes the branch if
the value in rs1 is smaller than the value in rs2 when the
values are treated as unsigned numbers.
• bltu rs1, rs2, L1 (branch if less than
unsigned)
– if (rs1 < rs2) goto/branch to instruction labeled L1;
• Branch if greater than or equal, unsigned (bgeu) takes
the branch in the opposite case.
• bgeu rs1, rs2, L1(branch if greater than
or equal,unsigned)
– if (rs1 >= rs2) goto/branch to instruction labeled L1;
Procedures
• Procedure is a stored subroutine that performs a
specific task based on the parameters with which it
is provided.
• A procedure or function is one tool programmers
use to structure programs, both to make them easier
to understand and to allow code to be reused.
• Procedures allow the programmer to concentrate on
just one portion of the task at a time; parameters act
as an interface between the procedure and the rest
of the program and data, since they can pass values
and return results.
Functions/Procedures
• Invoking a function changes the control flow of a
program twice.
1. Calling the function
2. Returning from the function
• The program that initiates a procedure and provides the
necessary parameter values is called caller.
• A procedure that executes a series of stored instructions
based on parameters provided by the caller and then
returns control to the caller is called callee.

17
Procedure calling

Steps required
1. Place parameters in a place where procedures can
access them (registers).
2. Transfer control to procedure.
3. Acquire storage resources needed for procedure.
4. Perform procedure’s operations
5. Place result value in a place were calling program
can access it (registers).
6. Return to place of call.
18
Data Flow in RISC-V
• RISC-V follows the following convention
for procedure calling in allocating its 32
registers:
• x10–x17: eight parameter registers in
which to pass parameters or return values.
• x1: one return address register to return to
the point of origin.

19
Procedure call instructions
• Procedure call: jump and link
jal x1,ProcedureAddress
– Address of following instruction put in register x1.
– This “link,” stored in register x1, is called the return
address
– Jumps to target address.
• Procedure return: jump and link register
jalr x0, 0(x1)
20
Function Calls and Stacks
• Notice function calls and returns occur in a stack-like order: the
most recently called function is the first one to return.
• 1. Someone calls A
• 2. A calls B
• 3. B calls C
• 4. C returns to B
• 5. B returns to A
• 6. A returns
Here, for example, C must return to B before B can return to A.

21
22
Stack
• Suppose a compiler needs more registers for a
procedure than the eight argument registers.
• Any registers needed by the caller must be
restored to the values that they contained before
the procedure was invoked.
• This situation is an example in which we need to
spill registers to memory.
• The ideal data structure for spilling registers is a
stack—a last-in first-out queue.
CONTD…
• A stack needs a pointer to the most recently
allocated address in the stack to show where the
next procedure should place the registers to be
spilled or where old register values are found.
• In RISC-V, the stack pointer is register x2, also
known by the name sp. The stack pointer is adjusted
by one doubleword for each register that is saved or
restored.
Stack and Function call
• It’s natural to use a stack for function call storage. A block of
stack space, called a stack frame, can be allocated for each
function call.
• When a function is called, it creates a new frame onto the
stack, which will be used for local storage.
• Before the function returns, it must pop its stack frame, to
restore the stack to its original state.
• The stack frame can be used for several purposes.
– Caller- and callee-save registers can be put in the stack.
– The stack frame can also hold local variables, or extra
arguments and return values.

25
The RISC-V Stack
Example
• procedure adds 10 to input parameter
int main()
{ int i, j;
i = 5;
j = add10(i);
i = j;
}
int add10(int i)
{ return (i + 10);}

27
CONTD…
• main:
addi x19, x0, 5
add x10, x19, x0
jal x1,add10
add x20, x11, x0
add x19, x20, x0

28
CONTD…
• add10:
addi sp, sp, -8
sd x6, 0(sp)
addi x6, x10, 10
add x11, x6, x0
ld x6, 0(sp)
addi sp, sp, 8
jalr x0, 0(x1)
Example

long long int leaf_example (long long


int g, long long int h, long long int
i, long long int j)
{
long long int f;
f = (g + h) −(i + j);
return f;
}
CONTD…
• The parameter variables g, h, i, and j correspond
to the argument registers x10, x11, x12, and x13,
and f corresponds to x20.
• leaf_example:
addi sp, sp, -24
sd x5, 16(sp)
sd x6, 8(sp)
sd x20, 0(sp)
CONTD…

• add x5, x10, x11


• add x6, x12, x13
• sub x20, x5, x6
• addi x10, x20, 0
CONTD…
• ld x20, 0(sp)
• ld x6, 8(sp)
• ld x5, 16(sp)
• addi sp, sp, 24
• jalr x0, 0(x1)

You might also like