Write a table lookup program for a single-chip microcomputer

Publisher:cloudsousou6Latest update time:2018-05-11 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

For complex calculations, the speed of the microcontroller is too slow. The best way is to manually calculate all the results in advance, store them in ROM in sequence, and then directly check the results from ROM when the program is executed.


The 51 MCU has two table lookup instructions:

    MOVC A, @A + PC
    MOVC A, @A + DPTR

The first one can avoid DPTR, but the "offset" is quite difficult to calculate because it involves the basic concept of instruction byte number and table lookup, which is a headache for many people.
In response to this problem, Zuoerlundao has developed a method to automatically calculate the offset, which can avoid the tedious manual calculation process.


The following is an online question that was answered by Doerlundao:

The data pre-stored in 30H ~ 39H are 1, 3, 5, 7, 9, 2, 4, 6, 8, 10.
Use MOVC A, @A + PC instruction to write a square table lookup program to calculate the square of the number in 30H ~ 39H on the chip and send it to 40H ~ 49H unit.


The program written by erlundao is as follows:
;-------------------------------------------
    org 0000h

    mov 30h, #1 ; store some data first
    mov 31h, #3
    mov 32h, #5
    mov 33h, #7
    mov 34h, #9
    mov 35h, #2
    mov 36h, #4
    mov 37h, #6
    mov 38h, #8
    mov 39h, #10

    call SQRT ;Call subroutine to find the square of the above 10 numbers
                         ;The unit part starting at 40H is omitted
    sjmp $
;-------------------------------------------
;Subroutine to find the square using table lookup method
SQRT:
    MOV R0, #30H ;Source value starting address
    MOV R1, #40H ;Destination starting address
    MOV R2, #10 ;Total 10 numbers
LOOP:
    MOV A, @R0
    ADD A, #S_TAB - ($ + 3) ;Automatically calculate the offset
    MOVC A, @A + PC ;Look up the table and find the square
    MOV @R1, A
    INC R0
    INC R1
    DJNZ R2, LOOP
    RET
;-------------------
S_TAB:
    DB 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196
;-------------------------------------------
;End


The questioner's comment on the answer: Yes, that's correct, but what does #S_TAB - ($ + 3) mean?


========================================


Algorithm description:


At the beginning of S_TAB, a series of "Square Numbers" is stored using the DB pseudo-instruction. - Baidu's layout is very poor, the quotation marks are displayed too small, and the book title marks are used instead.

So, counting from S_TAB, the 0th byte is 0 squared, the 1st byte is 1 squared, and so on, the 14th byte is 14 squared.

If the PC in the table lookup instruction MOVC A, @A + PC is equal to S_TAB, then we can find the square of A from the table.

Haha, this is a bit like "Standing Long Jump". You stand on the springboard and jump as far as your strength allows.


In fact, when executing the table lookup instruction MOVC A, @A + PC, PC is not equal to S_TAB.

What to do? Just think of long jump, add the run-up and you'll be done. Everyone should be able to calculate the number of steps or meters for the run-up.


Before executing the table lookup instruction, calculate the difference between S_TAB and the PC when executing the table lookup instruction, and add it to A.

This number, called the offset, is added to A, which is equivalent to the "run-up".


To calculate this offset, we need to count the number of bytes between S_TAB and the PC when the table lookup instruction is executed.

If you are not familiar with machine language, it is quite difficult to calculate the offset manually.

Moreover, if the instructions are modified, they have to be recalculated, which is very troublesome.


Fortunately, Zuoerlundao has invented a method to automatically calculate the offset.


In the program, the following two lines:

    ADD A, #S_TAB - ($ + 3) ; 2 bytes
    MOVC A, @A + PC ; 1 byte

That is, the work of automatically calculating the offset and looking up the table is completed.


The calculation formula in the program is: #S_TAB - ($ + 3), which can also be written as: #S_TAB - $ - 3.


The following is a description of each part:

$: represents the current address where the ADD instruction is located;

$ + 3: the PC value when executing the table lookup instruction, plus 3, which means these two instructions occupy a total of three bytes;

S_TAB - ($ + 3): Calculate the difference between the table head address and the address when looking up the table;


According to the method introduced in the microcontroller textbook, using the MOVC A, @A + PC instruction requires manual calculation of the "difference between the address of the table lookup instruction and the beginning of the table."

Anyone who has used this method knows how difficult it is.

Therefore, most people would rather waste a DPTR than calculate the difference.


The formula developed by Zuo Er Lun Dao has both theory and practical methods, which greatly facilitates microcontroller programmers and is worthy of wide promotion.


Keywords:MCU Reference address:Write a table lookup program for a single-chip microcomputer

Previous article:Use the timer interrupt of the microcontroller to periodically output a rectangular wave
Next article:52 MCU timer/counter programming summary

Recommended ReadingLatest update time:2024-11-16 21:26

Serial port reception problems encountered by PIC microcontrollers
When using the serial port of the PIC16F1825/9 chip to receive data, a problem is encountered. When the serial port receives data quickly, the serial port may freeze. That is, the program runs normally, the serial port sends data normally, but the serial port cannot receive data. After consulting the PIC data manual,
[Microcontroller]
Serial port reception problems encountered by PIC microcontrollers
Design of water temperature and water level controller based on PIC16C72 single chip microcomputer control
Solar water heaters are rapidly entering thousands of households due to their advantages of energy saving, environmental protection, and low cost. The water temperature and water level controllers that are matched with solar water heaters are very convenient for users to use. The product has a good market prospect and
[Microcontroller]
Design of water temperature and water level controller based on PIC16C72 single chip microcomputer control
Torque Loading Controller Design with RTX and TM4C Microcontrollers
introduction Eddy current dynamometer is mainly used to test the power of the engine. It can also be used as a loading device for gearboxes, reducers, and transmissions to test their transmission power. The stability of the torque loading controller and the accuracy of the measurement will directly affect the analysis
[Microcontroller]
Torque Loading Controller Design with RTX and TM4C Microcontrollers
The difference between PIC microcontroller and 51 microcontroller
(1) Bus structure: The bus structure of the MCS-51 microcontroller is the von Neumann type. The computer fetches instructions and data in the same storage space, and the two cannot be performed at the same time; while the bus structure of the PIC microcontroller is the Harvard structure. The instruction and data space
[Microcontroller]
How to choose low-power MCU for smart hardware development
This article compares the typical low-power MCU series on the market and analyzes that the MCU series based on the ARM. Cortex M0+ core is most suitable for the development of wearable medical devices. Device developers should pay close attention to its development trends, combine existing market demand, product syste
[Microcontroller]
How to choose low-power MCU for smart hardware development
Points to note when 51 MCU communicates with PC
After two days of work, I finally got the 232 communication working and wrote down a summary. First, the baud rate should be set to the same. Second, in the device manager on the PC, set the USB to 232 serial port to COM2, and the baud rate should be set the same as written in the software. The following is a writ
[Microcontroller]
Functions and usage of parallel P2 port of MCS-51 microcontroller
The parallel P2 port of the MCS-51 series microcontroller also has two functions. For microcontrollers with internal program memory (such as customized 8051), the P2 port can be used as an input port or an output port, directly connected to the input/output device; it can also be used as The system extended address bu
[Microcontroller]
Direct software architecture for 8-bit MCU systems (C51 series)
1. Preparation of startup documents $ NOMOD51 ; Ax51 macro assembler control command: disable predefined 8051 ; Customize the storage area that needs to be initialized after power-on ; The starting address of the indirect addressing area IDATA is fixed to 0 IDATALEN EQU 8OH; specifies the length
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号