555 words
3 minutes
Addition using fully register

Addition is one of the most fundamental operations in assembly language. In x86 architecture, the add instruction performs arithmetic addition on registers, memory, or immediate values.

This guide covers:

  • Syntax and behavior
  • 32-bit and 64-bit examples
  • Overflow and flags (CF, OF, ZF, SF)
  • Common pitfalls and best practices
  • Practice challenges (easy and hard)

Syntax of add#

The add instruction performs arithmetic addition:

add destination, source
  • Operation: destination = destination + source
  • Rules:
    • Operands must be the same size (e.g., both 32-bit or both 64-bit).
    • Source can be a register, immediate value, or memory operand.
    • Destination can be a register or memory operand.
    • Memory-to-memory addition (e.g., add [mem1], [mem2]) is not allowed.

Register Overview#

x86 architecture provides registers of varying sizes. This guide focuses on full registers (32-bit and 64-bit):

64-bit32-bit16-bit8-bit High8-bit Low
RAXEAXAXAHAL
RBXEBXBXBHBL
RCXECXCXCHCL
RDXEDXDXDHDL

Basic Addition Examples#

Register-to-Register Addition#

32-bit Example:

mov eax, 10
mov ebx, 20
add eax, ebx ; eax = 10 + 20 = 30
  • Result: EAX = 30, EBX = 20 (unchanged)

64-bit Example:

mov rax, 100
mov rbx, 200
add rax, rbx ; rax = 100 + 200 = 300
  • Result: RAX = 300, RBX = 200 (unchanged)

Immediate Value Addition#

Add a constant to a register:

mov eax, 50
add eax, 25 ; eax = 50 + 25 = 75
  • Result: EAX = 75

Memory and Register Addition#

Add a value from memory to a register:

mov eax, [myVar] ; Load value from memory
add eax, 5 ; eax = [myVar] + 5

Add a register value to memory:

add [myVar], eax ; [myVar] = [myVar] + eax

Note: Direct memory-to-memory addition is not supported.

Status Flags#

The add instruction updates the FLAGS register:

FlagDescriptionSet When
CFCarry FlagUnsigned overflow (carry out)
OFOverflow FlagSigned overflow
ZFZero FlagResult is zero
SFSign FlagResult is negative

Example (unsigned overflow):

mov eax, 0xFFFFFFFF ; Largest 32-bit unsigned number
add eax, 1 ; Wraps around
  • Result: EAX = 0, CF = 1, ZF = 1, OF = 0

Signed vs. Unsigned Addition#

Assembly treats signed and unsigned values the same; only the flags and interpretation differ.

Example (signed overflow):

mov eax, 0x7FFFFFFF ; Largest 32-bit signed int (2147483647)
add eax, 1 ; Causes signed overflow
  • Result: EAX = 0x80000000 (-2147483648), OF = 1

Chained Addition#

Add multiple values sequentially:

Single Register:

mov eax, 5
add eax, 10
add eax, 15 ; eax = 5 + 10 + 15 = 30

Multiple Registers:

mov eax, 10
mov ebx, 20
mov ecx, 30
add eax, ebx
add eax, ecx ; eax = 10 + 20 + 30 = 60

Common Pitfalls#

  1. Mismatched Operand Sizes:

    add ax, eax ; Invalid: 16-bit vs. 32-bit
  2. Memory-to-Memory Operations:

    add [mem1], [mem2] ; Invalid: not allowed
  3. Unintended Overwrites:

    add eax, ebx ; Modifies EAX, not EBX

Optimization with lea#

The lea (Load Effective Address) instruction can perform addition without affecting flags:

lea eax, [ebx + ecx * 2] ; eax = ebx + 2 * ecx
  • Advantage: Faster for complex expressions and preserves flags.
  • Use Case: Optimizing arithmetic in performance-critical code.

Instruction Summary#

InstructionDescription
add eax, ebxAdds EBX to EAX
add eax, 5Adds immediate value 5 to EAX
add eax, [mem]Adds memory value to EAX
add [mem], eaxAdds EAX to memory
add rax, rbx64-bit register addition

Practice Challenges#

Easy Challenges (chal 4)#

  1. Add 10 and 25 using EAX and EBX. Store the result in EAX.
  2. Add 0xFFFFFFFF and 1 in EAX. Identify which flags are set.
  3. Add AL = 0xFF and BL = 0x02. What is the result in AL? What are CF and OF?

Hard Challenges (chal 5)#

  1. Add three 64-bit numbers in RAX, RBX, and RCX. Store the result in RAX.
  2. Detect signed overflow:
    mov eax, 0x7FFFFFFF
    add eax, 1
    ; Check OF flag (e.g., using JO instruction)
  3. Add two large numbers from memory ([a] and [b]) and store the result in a register using mov and add.
Addition using fully register
https://frqblog.vercel.app/posts/addition-using-fully-register/
Author
frqblog
Published at
2025-07-30
License
CC BY-NC-SA 4.0