Keil Logo

A51: Lookup Tables in Assembly


Information in this article applies to:

  • C51 All Versions

QUESTION

In assembly, I must create a lookup table and locate it at a fixed address. I will access the table in an assembly function that I must call from C. How should I proceed?

ANSWER

The example project attached to this knowledgebase article contains three source files:

  • TEST.C: Contains the C program which calls the assembler function.
  • ASM_TEST.ASM: Contains the assembler function which uses the lookup table.
  • LOOKUPTABLE.INC: Contains the lookup table.

The contents of each file is listed below.

To locate the lookup table at a fixed address of 0x1000, open Project - Options for Target - BL51 Locate. In the Code Segment field enter SEGMENT_NAME(start_address). For this example, you should enter LOOKUPTABLE(0x1000). For your application, you may need to look up the segment name from the map file (*.M51).

LOOKUPTABLE.INC
LOOKUPTABLE SEGMENT CODE

RSEG LOOKUPTABLE

TABLE:   DB 02H, 03H, 04H, 05H, 06H, 07H, 08H, 09H
ASM_TEST.ASM
NAME ASMTEST

$include(LOOKUPTABLE.INC)

?PR?TSEG?ASM_TEST SEGMENT CODE
PUBLIC  ASM_LOOKUP_ROUTINE

RSEG ?PR?TSEG?ASM_TEST
using   0

ASM_LOOKUP_ROUTINE:

   MOV DPTR, #TABLE    ; DPTR points to the start of the lookup table
   MOV A, #4           ; A is the offset from the start of the lookup table
   MOVC A, @A + DPTR   ; Moves the (A+1)th item into the Accumulator

   RET
   END
TEST.C
extern void ASM_LOOKUP_ROUTINE(void);

void main(void)
{
   ASM_LOOKUP_ROUTINE();     // calling lookup routine

   while(1);
}

SEE ALSO


ATTACHED FILES

Request the files attached to this knowledgebase article.

Last Reviewed: Thursday, February 25, 2021


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.