Nios® II Processor Reference Guide

ID 683836
Date 8/28/2023
Public
Document Table of Contents

8.5.1. add

Instruction add
Operation

rC ←  rA + rB

Assembler Syntax

add rC, rA, rB

Example

add r6, r7, r8

Description

Calculates the sum of rA and rB. Stores the result in rC. Used for both signed and unsigned addition.

Usage

Carry Detection (unsigned operands):

Following an add operation, a carry out of the MSB can be detected by checking whether the unsigned sum is less than one of the unsigned operands. The carry bit can be written to a register, or a conditional branch can be taken based on the carry condition. The following code shows both cases:

add rC, rA, rB

cmpltu rD, rC, rA

add rC, rA, rB

bltu rC, rA, label

# The original add operation

# rD is written with the carry bit

# The original add operation

# Branch if carry generated

Overflow Detection (signed operands):

An overflow is detected when two positives are added and the sum is negative, or when two negatives are added and the sum is positive. The overflow condition can control a conditional branch, as shown in the following code:

add rC, rA, rB

xor rD, rC, rA

xor rE, rC, rB

and rD, rD, rE

blt rD, r0,label

# The original add operation

# Compare signs of sum and rA

# Compare signs of sum and rB

# Combine comparisons

# Branch if overflow occurred

Exceptions

None

Instruction Type

R

Instruction Fields

A = Register index of operand rA

B = Register index of operand rB

C = Register index of operand rC

Bit Fields
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
A B C 0x31
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0x31 0 0x3A