Absolute address location of variables and functions in Keil C51

Publisher:技术掌门Latest update time:2016-12-14 Source: eefocusKeywords:Keil Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Variable absolute address positioning


1) When defining a variable, use the _at_ keyword plus the address.


unsigned char idata myvar _at_ 0x40; 

Define the variable myvar at 0x40 of idata. You can find the following line in the M51 file:   


IDATA 0040H 0001H ABSOLUTE ; indicates that a variable is located at the absolute address 0x0040 of idata. 

2) Use KeilC compiler to define variables with absolute addresses, the method is to be checked.


 


2. Function absolute address positioning


1) Write a function myTest in the program


void myTest(void)

{

  // Add your code here

2) Use KeilC compiler to locate the function of absolute address, open Project -> Options for Target menu, select BL51 Locate tab, enter in Code:?PR?myTest?MAIN(0x4000) to locate the function myTest to 0x4000 in the program area, and compile again.


3) How to locate multiple functions at once


Similarly, write another function myTest1 in the program


void myTest1(void)

{

  // Add your code here

  In the Code: field of the BL51 Locate tab of the Options for Target menu, enter: ?PR?myTest1?MAIN(0x3900), ?PR?myTest?MAIN(0x4000) to locate the function myTest1 at 0x3900 in the program area, define the function myTest at 0x4000 in the program area, and recompile.


The following can be found in the M51 file:


Copy code

3.obj TO Reader RAMSIZE (256) CODE (?PR?MYTEST1?MAIN (0X3900), ?PR?MYTEST?MAIN (0X4000))

  3665H 029BH *** GAP ***

CODE 3900H 0014H UNIT?PR?MYTEST1?MAIN

  3914H 06ECH *** GAP ***

CODE 4000H 0014H UNIT?PR?MYTEST?MAIN 

Copy code

4) Function call


The method of directly calling functions in a program will not be explained here. Here we will focus on the method of using function pointers to call functions at absolute addresses.


  (1) Define the prototype of the function to be called typedef void (*CALL_MYTEST)(void); This is the prototype of a callback function, and the parameter is empty.


  (2) Define the corresponding function pointer variable CALL_MYTEST myTestCall = NULL;


  (3) The function pointer variable is assigned to point to the function at the absolute address we located myTestCall = 0x3900; points to the function myTest1


  (4) Function pointer call


if (myTestCall != NULL)

{

    myTestCall(); // Call the function myTest1 at the function pointer, set the PC pointer to 0x3900

  Check the bin file generated by the compilation. You can see the content of myTest1 at 0x3900 and the content of myTest at 0x4000.


(5) Other notes: If there is no content in the program space from 0x3000 to 0x3900, when the address pointer of myTestCall points to 0x3800 (between 0x3000 and 0x3900), execution will start from 0x3900. The method of calling a function in AP in Load is similar to this, but the corresponding parameter passing may require another method.


 


  Definition of Segment 


  RSEG is a segment selection instruction. To understand its meaning, you need to understand the meaning of segment.


  A segment is a storage unit for program code or data objects. Program code is placed in the code segment, and data objects are placed in the data segment. There are two types of segments: absolute segments and relocation segments. Absolute segments are specified in assembly language, and their addresses will not change when connected with L51. They are used to access the i/o of a fixed memory, or to provide the entry address of an interrupt vector. The address of the relocation segment is floating, and its address is determined by L51 when connecting the program module. The segments generated by C51 when compiling the source program are all relocation segments, which have segment names and storage types. Absolute segments do not have segment names.


After saying so much, you may still not understand what a segment means. Don't worry, just keep reading.


For example, you wrote a function void test_fun(void) { ...} in C and saved it in test.c. After compiling it with the compiler, you can see in the .SRC FILE: 


?PR?test_fun?TEST SEGMENT CODE //(Put the function in the code segment) 

When writing this function body:


RSEG ?PR?test_fun?TEST //Select the located code segment as the current segment test_fun:

……//code 

So the expression pattern of the function is: ?PR?function name?file name


The function names are divided into:


1: Function without parameters? PR? Function name? File name


2: Function with parameters?PR?_Function name?File name


3: Re-enter the function?PR?_?function name?file name


For example, you define a global variable


unsigned char data temp1,temp2; 

unsigned char xdata temp3;  

In the test.c file, the compiler will divide each file into 0 or more global data segments, and global variables of the same type are stored in the same segment. So the above will be compiled as follows:


Copy code

RSEG ? DT ? TEST

. temp1: DS 1

. temp2: DS 1

;

RSEG ?XD? TEST

. temp3: DS 1 

Copy code

Copy code

// Below is the representation of the global segment of each type of data

?CO? File name //Constant section

?XD? FILE_NAME //XDATA data segment

?DT? FILE_NAME //DATA data segment

?ID? FILE_NAME //IDATA…..

?BI? FILE_NAME //BIT …..

?BA? FILE_NAME //BDATA….

?PD? FILE_NAME //PDATA…..

Copy code

  You should understand the meaning of segment after reading this. You may ask, what is the use of this? It is used when you need to write a part of the program in assembly language. Put the function written in assembly language in this file, rename it to xxx.a51, and write it according to the above rules. Just compile it.


  Now that we know the meaning of segment, let's go back to the usage of SEG. There are two types of segment selection instructions in A51: relocation segment selection instruction and absolute segment selection instruction. They are used to select whether the current segment is a relocation segment or an absolute segment. Using different segment selection instructions will locate the program in different address spaces.


  1. The selection instruction for relocation segment is: RSEG segment name


It is used to select a previously defined relocation segment as the current segment. The usage is just like the example above, where a function segment is declared first and then the function segment is written.


  2. Absolute segment selection instruction


Copy code

CSEG [AT absolute address expression] //absolute code segment

DSEG [AT absolute address expression] // internal absolute data segment

XSEG [AT absolute address expression] //External absolute data segment

ISEG [AT absolute address expression] //Internal indirect addressing absolute data segment

BSEG [AT absolute address expression] // absolute bit addressing segment 

Copy code

Here is an example of their usage:


For example, if we write a serial port interrupt program, the starting address is 0x23.


CSEG AT 0X23

LJMP serialISR

RSEG ?PR?serialISR?TEST

. serialISR:  

  The assembly function uses the variables in the same project C file. For example, if ICFLAG is defined in the C file, the definition in the assembly file is


EXTERN ICFLAG ; define external variables 

 


  Define a function, such as


Copy code

CARDATR:

    ...........

    RET


GLOBAL CARDATR 

Copy code

 To call the CARDATR function in the same project file, you should define the function


extern void CARDATR(void); 

 


C18 specifies the absolute address of data


For example:


#pragma udata overlay RECBUFS =0x190 //200

UINT8 NUMBER;

UINT8 REC_BUF[31];

#pragma udata

 


Keywords:Keil Reference address:Absolute address location of variables and functions in Keil C51

Previous article:The _at_ keyword in KEIL C51
Next article:Using _at_ absolute address positioning in keil

Recommended ReadingLatest update time:2024-11-16 15:00

C51 MCU interrupt number and interrupt vector
1. Interrupt number 2. The use of interrupt and using in C51 interrupt The basic structure of the 8051 series MCU includes: 32 I/O ports (4 groups of 8-bit ports); two 16-bit timer counters; full-duplex serial communication; 6 interrupt sources (2 external interrupts, 2 timer/counter interrupts, 1 se
[Analog Electronics]
C51 MCU interrupt number and interrupt vector
A Simple Study on the Delay Program of Keil C of 51 Single Chip Microcomputer
In the past, when we used assembly language to write microcontroller programs, this problem was relatively easy to solve. For example, if we used a 51 with a 12MHz crystal oscillator and wanted to delay for 20us, we could just use the following code to meet general needs: mov r0, #09h loop: djnz r0, loop The instructi
[Microcontroller]
MCU driver TEA5767 FM stereo radio C51 general source code
TEA5767 FM stereo radio, source code for C51 MCU. The module is less than 2 yuan on Taobao. It is recommended to use Keil uVision5 to compile. I used this version to compile and pass. The circuit schematic is as follows: The microcontroller source program is as follows: #include REG52.H #include "Radio.H" #include
[Microcontroller]
MCU driver TEA5767 FM stereo radio C51 general source code
Notes on using Keil C51 MCU development environment
It is best to use Keil to write C51 programs. You can also use Weifu, but Weifu's editing capabilities are far inferior. I am used to writing code with Keil, and then using Weifu hardware simulation (only WAVE simulation head). However, Keil is easy to use, but the key to writing code is still the C51 level, that is
[Microcontroller]
Notes on using Keil C51 MCU development environment
How to see the program occupying STM32 flash and SRAM from the Keil compilation result
Program Size: Code=114956 RO-data=20528 RW-data=808 ZI-data=702360   FromELF: creating hex file... "..\OBJ\MALLOC.axf" - 0 Error(s), 0 Warning(s). Build Time Elapsed:  00:00:05 From the compilation information above, we can see that the FLASH size occupied by our code is: 135484 bytes (114956 +20528), and the SRAM s
[Microcontroller]
Keil C51 extends C language keyword eight: far
The emergence of far is to support the newly emerged 8051 family enhanced MCUs, which may have more than 64KB of memory. Use far to access the extended RAM, and use const far to access the extended ROM. NXP 51MX architecture 51 MCU provides access to up to 8MB of code and xdata storage space through universal pointe
[Microcontroller]
Keil software delay
There are four common delay methods in C language: Figure 1 C language delay   Figure 1 shows four common delay methods used in our programming language, two of which are inaccurate delays and two more accurate delays. Both the for statement and the while statement can change the delay time by changing the range
[Microcontroller]
Keil software delay
IO port simulation SPI communication C51 program
/**************************  Resources used in the file 1. Port: P0.4, P0.5, P0.6, P0.7 2. Call delay_ms function ******************************/ /*************************     Simulate SPI interface I/O definition ****************************/ sbit spi_cs=P0^1;   sbit spi_di=P0^2;   sbit spi_clk=P0^3; sbit spi_do=P0^
[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号