Analysis of the startup code provided by 51 MCU Keil (STARTUP.A51)

Publisher:chi32Latest update time:2020-09-23 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Startup code generated by Keil C
Program Listing 1.1 AT89C51 startup code

$NOMOD51 //Do not use the default special function register 51

;------------------------------------------------------------------------------

; This file is part of the C51 Compiler package

; Copyright (c) 1988-2002 Keil Elektronik GmbH and Keil Software, Inc.

;------------------------------------------------------------------------------

; STARTUP.A51: This code is executed after processor reset.

; It is clear that this code will be executed first after the processor is powered on and reset

;

; To translate this file use A51 with the following invocation:

;

; A51 STARTUP.A51

;

; To link the modified STARTUP.OBJ file to your application use the following

; BL51 invocation:

;

; BL51 , STARTUP.OBJ

; BL51 is the linker used by Keil. This is the command line format.

;------------------------------------------------------------------------------

;

; User-defined Power-On Initialization of Memory

; Below we will define some symbolic constants related to memory initialization. Compared with ICCAVR, Keil's explicit declaration of symbolic constants is easier to understand.

; With the following EQU statements the initialization of memory

; at processor reset can be defined:

;

; ; the absolute start-address of IDATA memory is always 0

IDATALEN EQU 80H ; the length of IDATA memory in bytes.

;IDDATALEN refers to the 128-byte RAM on the chip, I refers to internal

XDATASTART EQU 0H ; the absolute start-address of XDATA memory

XDATALEN EQU 0H ; the length of XDATA memory in bytes.

; XDATA refers to the off-chip SRAM space, X refers to external

PDATASTART EQU 0H ; the absolute start-address of PDATA memory

PDATALEN EQU 0H ; the length of PDATA memory in bytes.

; PDATA refers to program memory, p refers to program

; Notes: The IDATA space overlaps physically the DATA and BIT areas of the

; 8051 CPU. At minimum the memory space occupied from the C51

; run-time routines must be set to zero.

; Note: The register space and memory space of the 8051 CPU are not uniformly addressed, but are distinguished by instructions, so the addresses of different storage areas may overlap.

;------------------------------------------------------------------------------

;

; Reentrant Stack Initialization

;

; The following EQU statements define the stack pointer for reentrant

; functions and initialized it:

; Keil C does not use the stack to pass parameters by default, so the function is not reentrant (for the concept of reentrancy, see the section on thread safety and reentrant functions). Keil requires the user to explicitly declare whether the function has the reentrant attribute in order to initialize the stack for the C function call.

; Stack Space for reentrant functions in the SMALL model.

IBPSTACK EQU 0 ; set to 1 if small reentrant is used.

IBPSTACKTOP EQU 0FFH+1 ; set top of stack to highest location+1.

;

; Stack Space for reentrant functions in the LARGE model.    

XBPSTACK EQU 0 ; set to 1 if large reentrant is used.

XBPSTACKTOP EQU 0FFFFH+1; set top of stack to highest location+1.

;

; Stack Space for reentrant functions in the COMPACT model.  

PBPSTACK EQU 0 ; set to 1 if compact reentrant is used.

PBPSTACKTOP EQU 0FFFFH+1; set top of stack to highest location+1.

;Stack in different memory modes. There are three mode settings in Keil compiler:

Small: All variables are placed in the internal RAM area

Compact: All variables are placed in the lower 256 bytes of external RAM by default (addressable by R0)

Large: All variables are placed in external RAM (DPTR addressing)

This is caused by the 51 processor's numerous addressing modes. Different addressing modes have different efficiencies.

;------------------------------------------------------------------------------

;

; Page Definition for Using the Compact Model with 64 KByte xdata RAM

;

; The following EQU statements define the xdata page used for pdata

; variables. The EQU PPAGE must conform with the PPAGE control used

; in the linker invocation.

;

PPAGEENABLE EQU 0 ; set to 1 if pdata object is used.

;

PPAGE EQU 0 ; define PPAGE number.

;

PPAGE_SFR DATA 0A0H ; SFR that supplies uppermost address byte

; (most 8051 variants use P2 as uppermost address byte)

;

;------------------------------------------------------------------------------

; Standard SFR Symbols

ACC DATA 0E0H

B DATA 0F0H

SP DATA 81H

DPL DATA 82H

DPH DATA 83H

                NAME ?C_STARTUP

?C_C51STARTUP SEGMENT CODE

?STACK SEGMENT IDATA

                RSEG ?STACK

                DS 1

                EXTRN CODE (?C_START)

                PUBLIC ?C_STARTUP

                CSEG AT 0 //Starting point of the code segment

?C_STARTUP: LJMP STARTUP1 //Jump to the STARTUP1 program area.

                RSEG ?C_C51STARTUP

STARTUP1:

//IF ENDIF is a conditional compilation command. The code is compiled only when the condition is true.

IF IDATALEN <> 0 //If there is IDATA, clear the data in the IDATA area, similar to clearing the BSS area

                MOV R0,#IDATALEN - 1

                CLR A

IDATALOOP: MOV @R0,A

                DJNZ R0,IDATALOOP

ENDIF

//If there is an external data area, clear the area from XDATASTART to XDATASTART + XDATALEN in the external data area

//Since there are many storage area types and compilation models for 51, the following code clears the corresponding area according to different configurations

IF XDATALEN <> 0

                MOV DPTR,#XDATASTART

                MOV R7,#LOW (XDATALEN)

IF (LOW (XDATALEN)) <> 0

                MOV R6,#(HIGH (XDATALEN)) +1

ELSE

                MOV R6,#HIGH (XDATALEN)

ENDIF

                CLR A

XDATALOOP: MOVX @DPTR,A

                INC DPTR

                DJNZ R7,XDATALOOP

                DJNZ R6,XDATALOOP

ENDIF

IF PPAGEENABLE <> 0

                MOV PPAGE_SFR,#PPAGE

ENDIF

IF PDATALEN <> 0

                MOV R0,#LOW (PDATASTART)

                MOV R7,#LOW (PDATALEN)

                CLR A

PDATALOOP: MOVX @R0,A

                INC R0

                DJNZ R7,PDATALOOP

ENDIF

IF IBPSTACK <> 0

EXTRN DATA (?C_IBP)

                MOV ?C_IBP,#LOW IBPSTACKTOP

ENDIF

IF XBPSTACK <> 0

EXTRN DATA (?C_XBP)

                MOV ?C_XBP,#HIGH XBPSTACKTOP

                MOV ?C_XBP+1,#LOW XBPSTACKTOP

ENDIF

IF PBPSTACK <> 0

EXTRN DATA (?C_PBP)

                MOV ?C_PBP,#LOW PBPSTACKTOP

ENDIF

//This is where the stack pointer is initialized

                MOV SP,#?STACK-1

; This code is required if you use L51_BANK.A51 with Banking Mode 4

; EXTRN CODE (?B_SWITCH0)

; CALL ?B_SWITCH0 ; init bank mechanism to code bank 0

                LJMP ?C_START

                END

From the above code, we have found some operations that are the same as the ATmega8 initialization code: initializing the stack and bss area. Since the storage space and addressing mode of AT89C51 are more complex than ATmega8, many initialization codes based on different configurations are added to the initial code.

Reference address:Analysis of the startup code provided by 51 MCU Keil (STARTUP.A51)

Previous article:Analysis of 51 MCU stack
Next article:How the serial port works

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号