Pointers to add numbers

The following pseudocode is an instruction to add numbers using pointers:

(rbx) + (rax) -> (rbx)

The pointers (rax)and (rbx) refer to the contents of memory cells whose addresses are stored in %RAX and %RBX CPU registers.

Add numbers in different environments

Now we’ll explore how we add numbers by using pointers in following languages:

Add numbers in C/C++ using pointers

The expression (rbx) + (rax) -> (rbx) is equivalent to the following C/C++ language expression:

*pb = *pb + *pa;

In this expression, the * operator is an instruction to retrieve memory contents pointed to by the pointers pa or pb (also called pointer dereference).

Add numbers in assembly language using pointers

In assembly language, we use the instruction ADD for the + operator, but we cannot use multiple memory addresses in a one-step instruction:

addl (%rax), (%rbx) # invalid instruction

We can only use one memory reference per line, and we therefore need to employ another register as a temporary variable:

(rax) -> register
(rbx) + register -> (rbx)

We write this sequence of instructions as:

Get hands-on with 1400+ tech skills courses.