Home / Tutorials / PIC Tutorial / How to Use Lookup Tables in PIC Assembly
Lookup Tables with PIC

How to Use Lookup Tables in PIC Assembly

Programming a microcontroller in assembly languages takes time and to reduce that time, embedded systems engineers often have tricks up their sleeves. One of those tricks is the use of lookup tables. In this post, I will share how I use lookup tables in assembly language for PIC microcontrollers.

The RETLW Opcode

Lookup tables are possible in assembly primarily because of the RETLW opcode. This instruction means “return with a literal in w” and is used like this:

Here, 0x04 is the literal value placed inside the W register before returning. Since the opcode returns to something, it is usually inside a subroutine.

Take note that RETLW reads the stack for the address to which to return. So a GOTO followed by RETLW will result in a stack underflow error.

Lookup Table Example

The most common lookup table is the one for seven segment displays. Frugal engineers don’t want to use decoders in using seven segment displays and so connect them directly to PIC pins. A lookup table is used to convert the numbers to their equivalent for seven segment displays.

Below is an example table:

The binaries in this code are the seven-segment equivalent for digits zero to nine, assuming a common cathode displays and LSB at segment ‘a’.

Now if I want to display the digit seven (connected to PORTB), for example, I would do it like this:

What this does is move a literal value 7 to W then call the lookup table. On the table, the value 7 adds to the current value of the program counter which is the first line of the table. The program then jumps 7 lines towards

The program now returns to the caller with W equal to the seven segment equivalent for the digit 7.

Example Project: Decade Counter

Let’s say we use two seven segment displays to count from zero to ninety-nine. One way to implement this is to assign variables to the ones and tens digits. As the ones digit go past 9, the tens digit increments by one. Using lookup tables make this project easier.

Lookup Tables with PIC

Check Also


PIC16F877A – More Microcontroller Features

We have managed to blink a LED, use interrupts and timers with the PIC16F84A. Sadly, …

One comment

  1. Very helpful. I thank you.

Leave a Reply

Your email address will not be published.