Logical Operations and Decision-Making Instructions in RISC-V

Beyond arithmetic, processors need to manipulate individual bits and alter their execution path based on conditions. This article covers the core logical operations used for bit manipulation and explains how conditional branching and looping are built from a small set of comparison-based instructions.

Logical OperationsConditional BranchingBitwise Instructions

~3 دقیقه مطالعه · آخرین به‌روزرسانی ۱۵ شهریور ۱۴۰۵

Logical Operations: Manipulating Bits Directly

Not every operation a processor performs deals with numbers as whole quantities. Sometimes software needs to manipulate specific bits within a value, which is done using Logical Operations.

  • AND produces a 1 in each bit position only where both operands have a 1, commonly used to clear specific bits, an operation called Masking.
  • OR produces a 1 in each bit position where at least one operand has a 1, commonly used to set specific bits.
  • Shift Left (sll) and Shift Right (srl) move all bits of a value by a fixed number of positions, which is also an efficient way to multiply or divide by powers of two.

An example of a logical AND instruction in RISC-V assembly:

and a, b, c

This instruction performs a bitwise AND between the values in registers b and c, storing the result in a.

Making Decisions: How Branching Works at the Hardware Level

A program rarely executes in one straight, unconditional line. Loops, if-statements, and function calls all require the processor to conditionally jump to a different point in the instruction sequence, a capability provided by Conditional Branch instructions.

The two fundamental branch instructions in RISC-V compare two register values directly:

beq a, b, Label
bne a, b, Label

beq (branch if equal) jumps to the specified label only if the two register values are equal, while bne (branch if not equal) jumps only if they differ. If the condition is false, execution simply continues with the next sequential instruction.

Building Loops and If-Statements from Branches

High-level constructs like if, while, and for do not exist at the hardware level. A compiler translates them into a sequence of comparisons and conditional branches.

Example: a simple high-level if-statement and its conceptual translation:

High-level:
if (i == j) f = g + h;

Translated concept:
bne i, j, Exit
add f, g, h
Exit:

Here, if i and j are not equal, execution jumps directly past the addition to the label Exit, skipping it. If they are equal, the branch is not taken and the addition executes normally.

Unconditional Jumps

Not all changes in execution flow depend on a condition. An Unconditional Jump instruction always transfers control to a specified location, regardless of any comparison, and is used to implement constructs such as the end of a loop body jumping back to its start.

Why This Small Set of Instructions Is Enough

Every complex control structure found in high-level programming languages — nested loops, switch statements, early returns — can be constructed by combining just these few primitives: comparison, conditional branching, and unconditional jumping. This is a direct demonstration of the "make the common case simple and let software build complexity on top" philosophy underlying instruction set design.

نوشته و پژوهش‌شده توسط دکتر شاهین صیامی

مقالات مرتبط

Common Misconceptions About Parallel Computing and the Book's Final Lessons

After covering everything from thread-level parallelism to warehouse-scale computing, it is worth correcting persistent misconceptions about parallel systems that even experienced engineers sometimes hold. This article addresses common fallacies about scaling and parallel hardware, then closes out the parallel processing chapter by tying together the full journey from a single instruction to a building full of cooperating machines.

ادامه

Real Stuff: Benchmarking CPUs Against GPUs and Multiprocessor Matrix Multiply

Comparing a CPU and a GPU fairly requires a model that accounts for both computational throughput and memory bandwidth limits together. This article introduces the roofline model used to compare real hardware like the Intel Core i7 and NVIDIA Tesla GPU, then shows how matrix multiplication is accelerated across multiple processors as the final practical application of this chapter's parallel concepts.

ادامه

Benchmarking Multiprocessors and Modeling Parallel Performance

Measuring the performance of a parallel system requires different tools and metrics than measuring a single-core processor. This article covers the specialized benchmarks used to evaluate multiprocessor systems, explains how to model scaling behavior as more processors are added, and revisits Amdahl's Law in the context of real-world performance measurement.

ادامه

Cluster Networking: Connecting to the World Outside

A cluster of machines is only useful if it can communicate efficiently both internally and with the outside world. This article covers the networking layers involved in cluster communication, the tradeoffs between latency and bandwidth at scale, and how clusters connect to external networks and users.

ادامه

Clusters, Warehouse-Scale Computers, and Network Topologies

Beyond a single chip, parallelism extends to entire buildings full of independent computers working together. This article explains the shift from shared memory multiprocessing to clusters of separate machines, introduces the concept of warehouse-scale computing, and covers the network topologies that connect these independent machines efficiently.

ادامه

An Introduction to GPUs: Massive Parallelism for Data-Heavy Workloads

A GPU takes the SIMD idea covered earlier in this series to an extreme scale, running thousands of lightweight threads simultaneously to process massive amounts of independent data. This article explains why GPUs are architecturally so different from CPUs, how their thread execution model works, and what kinds of workloads benefit most from this design.

ادامه