0% found this document useful (0 votes)
11 views4 pages

RISC-V Arithmetic and Logical Instructions

Copyright
© All Rights Reserved
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)
11 views4 pages

RISC-V Arithmetic and Logical Instructions

Copyright
© All Rights Reserved
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

Lab 4.

Arithmetic and Logical Instructions

Goals
After this laboratory exercise, you should know how to use arithmetic, logical and shift
instructions. In addition, you should also understand overflow in arithmetic operation
and how to detect it.

References
- RISC-V documents, textbook.
- The RISC-V Instruction Set Manual: [Link]

Preparation

Assignments at Home and at Lab

Home Assignment 1
The arithmatic was introduced in Laboratory Exercises 02, this assignment describes a
special situation when adding two integers – the overflow.

Support: The sum of two 32-bit integers may not be representable in 32 bits. In this
case, we say that an overflow has occurred. Overflow is possible only with operands of
the same sign.
For two nonnegative (negative) operands, if the sum obtained is less (greater) than
eitheir operand, overflow has occurred

The following program dectects overflow based on this rule. Two operands are stored
in register s1 and s2, the sum is stored in register s3. If overflow occur, t0 register is set
to 1 and clear to 0 in otherwise.

# Laboratory Exercise 4, Home Assignment 1


.text
# TODO: Initialize s1 and s2 in different cases

# Algorithm for determing orverflow condition


li t0, 0 # No overflow is default status
add s3, s1, s2 # s3 = s1 + s2
xor t1, s1, s2 # Test if s1 and s2 have the same sign
blt t1, zero, EXIT # If not, exit
slt t2, s3, s1
blt s1, zero, NEGATIVE # Test if s1 and s2 is negative?
beq t2, zero, EXIT # s1 and s2 are positive
# if s3 > s1 then the result is not overflow
j OVERFLOW
NEGATIVE:
bne t2, zero, EXIT # s1 and s2 are negative
# if s3 < s1 then the result is not overflow
Hanoi University of Science and Technology
School of Information and Communications Technology

OVERFLOW:
li t0, 1 # The result is overflow
EXIT:

Home Assignment 2
The basic logical operation includes and, or, xor, not.
These instructions operates with bits of source operands and write the result to
destination operand.

There are versions of immediate format of and, or, xor using with a source register
and a 12-bit immediate value.

Some useful ways with logical operations:


i. and is used to extract some bits from a register or value
ii. and is used to clear some bits of a register or value
iii. or is used to set some bits for a register.

The following program demonstrates how to use logical instructions to extract


information from one register. We can extract one bit or more according to the mask we
use. Read this example carefully and explain each lines of:

# Laboratory Exercise 4, Home Assignment 2


.text
li s0, 0x12345678 # Test value
andi t0, s0, 0xff # Extract LSB of s0
andi t1, s0, 0x0400 # Extract bit 10 of s0

Home Assignment 3
The next logical instructions in this assignment are shift instructions including sll
(shift left logical), srl (shift right logical), and sra (shift right arithmetic).

1
Hanoi University of Science and Technology
School of Information and Communications Technology

RISC-V supports the immediate format of the shift instructions (slli, srli, and
srai), with 5-bit constant.
Note that, shifting left (right) n-bit is equivalent to multiply (divide) by 2n.
This example show how the shift operations used to implement other instructions, such
as multiply by a small power of 2

# Laboratory Exercise 3, Home Assignment 3


.text
li s0, 1 # s0 = 1
sll s1, s0, 2 # s1 = s0 * 4

Assignment 1
Create a new project to implement the Home Assigment 1. Compile and upload to
simulator. Initialize two operands (register s1 and s2), run this program step by step,
observe memory and registers value.

Assignment 2
Write a program to do the following tasks:
▪ Extract MSB of s0
▪ Clear LSB of s0
▪ Set LSB of s0 (bits 7 to 0 are set to 1)
▪ Clear s0 (s0=0, must use logical instructions)

MSB: Most Significant Byte


LSB: Least Significant Byte

s0 = 0x 1 2 3 4 5 6 7 8

MSB LSB
Assignment 3
Pseudo instructions in RISC-V are not-directly-run-on-RISC-V-processor instructions
which need to be converted to real-instructions of RISC-V. Re-write the following
pseudo instructions using real-instructions understood by RISC-V processors:

a. abs s0, s1
s0 = abs($s1)
2
Hanoi University of Science and Technology
School of Information and Communications Technology

b. move s0, s1
s0 = s1
c. not s0
s0 = bit_invert(s0)
d. ble s1, s2, label
if (s1 <= s2)
j label

Assignment 4
To dectect overflow in additional operation, we also use other rule than the one in
Assignment 1. This rule is: when add two operands that have the same sign, overflow
will occur if the sum doesn’t have the same sign with either operands. You need to use
this rule to write another overflow detection program.

Assignment 5
Write a program that implement multiply by a small power of 2. (2, 4, 8, 16, etc for
example).

Conclusions
What is benefit of using shift instructions for implementing multiplication comparing
with multiply instructions (RV32M: RISC-V 32-bit Multiplier/Divider) ?

Common questions

Powered by AI

Logical instructions are used for manipulating bits within registers. The 'and' instruction can extract or clear specific bits in a register by using a corresponding mask. The 'or' instruction is utilized to set specific bits in a register. The logical operations are fundamental in tasks such as bit masking and setting conditions within registers .

Logical instructions can be used to operate on bits in various ways. For instance, the 'and' operation can extract specific bits by using a mask, 'or' can set bits to one, 'xor' can toggle bits, and 'not' can invert bits. These operations enable fine-grained control over binary data, useful for tasks such as bit masking, condition setting, and binary arithmetic preparation .

Pseudo instructions in RISC-V are not directly executable by a RISC-V processor, as they serve as convenient shortcuts or abstractions for more complex sequences of actual instructions. They must be converted into a sequence of one or more real instructions that accomplish the same task on an RISC-V processor to ensure compatibility and execution. An example provided in the document includes converting the 'abs' pseudo instruction to actual RISC-V instructions .

According to Assignment 4, overflow in addition is detected by checking the signs of the operands and the result. If two operands with the same sign are added—both positive or both negative—and the sum has a different sign from the original operands, overflow has occurred. This method focuses on discrepancies between expected and resulting signs after the addition operation .

The shift right arithmetic instruction (sra) can be used to divide a signed integer by 2 by shifting all bits of the integer to the right. This operation effectively divides the integer by 2, maintaining the sign of the number due to the arithmetic nature of the shift, which fills the leftmost bits with the sign bit. This allows for proper division for both positive and negative numbers in signed integer arithmetic .

Shift operations in the RISC-V instruction set, such as 'sll' (shift left logical), can be used to multiply a number by a small power of two. Specifically, shifting a number left by n bits is equivalent to multiplying that number by 2^n. This method is more efficient in some cases compared to using the multiplication instruction itself, especially with small powers of two, as it reduces the computational cost .

Overflow occurs in arithmetic operations when the sum of two 32-bit integers cannot be represented within 32 bits. It specifically happens when two operands of the same sign (both nonnegative or both negative) yield a result that appears to contradict their initial sign; for example, two nonnegative integers resulting in a negative sum or vice versa. The document provides a program that detects overflow based on the rule: if two nonnegative operands produce a sum less than either operand, or two negative operands produce a sum greater than either operand, overflow has occurred .

Using shift instructions for multiplication, specifically by powers of two, is significant in RISC-V because it reduces the complexity and computational cost involved. Shifting a value left by n positions effectively multiplies it by 2^n, which can be a more resource-efficient operation than using the dedicated multiply instruction, especially for small integer calculations. This is because shift operations are simpler and faster than multiplication operations .

Using logical instructions to reset or manipulate specific bits within a register provides significant control over binary data while being computationally efficient. Instructions such as 'and', 'or', and 'xor' allow selective targeting of bits for setting, clearing, or toggling without affecting other data in the register. This efficiency in manipulating specific portions of binary representations is crucial for optimizing performance in tasks like extracting or setting flags, implementing state machines, and handling data in networking or graphics .

The provided program detects overflow using logical instructions by initially setting a default status indicating no overflow. The program checks the signs of the two integers by XORing them; if the result is non-negative, they have the same sign. Depending on whether the integers are positive or negative, the program compares the sum with one of the operands to see if the sum is out of bounds, indicating overflow. It then sets a register to indicate overflow if such a condition is detected .

You might also like