Assembly Language Fundamentals

Intermediate
Version:
1.0

Arrays and Lookup Tables

Strings taught us how to process sequential data stored in memory. Now we’ll take the same idea and apply it to numbers and structured data. Arrays and lookup tables are two of the most useful memory structures in assembly programming — they allow you to store groups of values and retrieve them efficiently using an index.

In this lesson, we’ll focus on the core concept: indexing memory by offset. We won’t use pointers or indirect addressing yet — just simple calculations that show how to access and use array data.

What You’ll Learn

What Is an Array?

An array is simply a list of values stored one after another in memory. Each element occupies one or more bytes, and they are accessed by adding an offset to the base address.

Here’s a simple array of bytes:

numbers:
   DEFB 10, 20, 30, 40, 50

This places five bytes in memory:

Address → 10 20 30 40 50
Index   →  0  1  2  3  4

Accessing an Array Element

If you know the index of the value you want, you can access it by adding that index to the base address. Here’s how to load the third element (30):

   LD HL, numbers   ; HL points to the start of the array
   LD B, 2          ; Index (0-based)
   LD C, 0          ; Make BC = 2
   ADD HL, BC       ; HL = numbers + 2
   LD A, (HL)       ; A = 30

Explanation:

Looping Through an Array

One of the most common tasks is to process every element in a list. For example, here’s how to sum all five numbers:

   LD HL, numbers   ; Start of array
   LD B, 5          ; Number of elements
   LD A, 0          ; Clear accumulator

sum_loop:
   ADD A, (HL)      ; Add current element to A
   INC HL           ; Move to next element
   DJNZ sum_loop    ; Repeat until B = 0

This technique works for arrays of any size. All you need is:

Lookup Tables

A lookup table is just an array where each element represents something meaningful. Instead of using a series of conditionals to choose a value, you use the index to "look up" the result directly.

For example, suppose we want to convert numbers 0–3 into ASCII characters:

ascii_table:
   DEFB 'A', 'B', 'C', 'D'

Now, instead of a long series of CP and JR Z instructions, we can do this:

   LD A, 2           ; Index 2
   LD L, A
   LD H, 0
   LD DE, ascii_table
   ADD HL, DE
   LD A, (HL)        ; A = 'C'

This approach makes your code faster, shorter, and easier to update — changing the table data changes the program’s behaviour without touching the logic.

Two-Dimensional Arrays (Optional)

Arrays don’t have to be one-dimensional. For example, you might store a small table of values like this:

grid:
   DEFB 1,2,3
   DEFB 4,5,6
   DEFB 7,8,9

If each “row” is 3 bytes, then element (row, col) is at:

Address = base + (row * 3) + col

This pattern — base + offset — is at the heart of nearly all data access in assembly.

Exercises

  1. Basic Access:
    Create an array of 8 bytes. Write a program that loads the 5th element into register A.
  2. Summing Values:
    Write a routine that sums all the elements in a 10-byte array and stores the result in register A.
  3. Lookup Table Conversion:
    Create a lookup table that maps numbers 0–9 to their ASCII characters '0''9'. Write a routine that converts a number stored in A into its ASCII equivalent using the table.
  4. Challenge:
    Build a two-dimensional 3×3 table. Write a routine that retrieves the value at (row, column) using the formula base + (row * 3) + col.

Summary

Previous Module
Next Module