Automatically Initializing Data Segment Values in MAX-IDE

[09-11 23:02:11]   来源:http://www.88dzw.com  单片机学习   阅读:8704

文章摘要:In applications written in C or another high-level language and compiled into assembly code, the compiler typically handles the allocation of space for variables (as well as the process of initializing variables to predefined starting values) automatically. In this case, the user only needs to decla

Automatically Initializing Data Segment Values in MAX-IDE,标签:单片机开发,单片机原理,单片机教程,http://www.88dzw.com
In applications written in C or another high-level language and compiled into assembly code, the compiler typically handles the allocation of space for variables (as well as the process of initializing variables to predefined starting values) automatically. In this case, the user only needs to declare the variable, its type, and (optionally) its initial value. The compiler handles the rest.
   unsigned int c = 0x1234;
   
However, when writing applications directly in MAXQ assembly language, the allocation of space for variables and setting the variables to initial values must be performed explicitly. This detailed action allows tighter control of the resources available on the MAXQ microcontroller, but adds some complexity to the system.

For small assembly-based applications or those which do not require a large amount of working space, internal registers can be used to store all application variables. This approach provides two important advantages:
  1. Compact, fast code. Register variables can be read from, written to, or copied to another register variable in as little as one instruction cycle, depending on the location of the register. On MAXQ20-based microcontrollers, no more than two instruction cycles will generally be required in the worst case.
  2. Direct operations on variables. Some internal register locations can be operated on directly. For example, any of the 16 working accumulators, A[0] through A[15], can be selected (using the AP register) as the active accumulator, Acc. This means that if an operation needs to be performed on a variable stored in one of these registers, it can be performed directly on that register without having to copy the value out, perform the operation, and copy the value back in. Similarly, variables stored in the LC[0] and LC[1] registers can be used directly as loop counters by executing the djnz instruction.
A larger application, or an application requiring a larger number of working variables, can benefit from storing some or all of its variables in SRAM-based data memory. This method allows a much larger number of variables to be created, up to the limits imposed by the size of the data memory. Variables stored in this manner are accessed using one of the MAXQ20 core's standard data pointers, which can be used to read and write byte-sized or word-sized variables. (Note: all code examples in this application note assume that DP[0] is configured to operate in word mode.)
   move   DP[0], #0010h      ; Location of variable in data memory
   move   Acc, @DP[0]        ; Read variable
   add    #1                 ; Increment variable value by 1
   move   @DP[0], Acc        ; Store variable back in data memory
   
If a long series of calculations must be performed on a variable, the value of that variable can be copied into a working register, as shown in the example code above. All intermediate operations can be performed using that working register, and the value can be copied back out to the variable once calculations are complete.

Segment Declarations in MAX-IDE

Once the decision is made to store application variables in SRAM-based data memory, how do you determine where to store the variables?

Typically, all of the data memory is available for application use, except for the highest 32 bytes in memory which are used by the debugger. This means that declaring a variable is simply a matter of defining a location for it in data memory. This location is then used by code whenever the variable is read or written. The #define macro can be used to associate a variable location with a symbolic name.
#define VarA  #0020h
#define VarB  #0021h
#define VarC  #0022h

   move   DP[0], VarA        ; Point to VarA variable
   move   Acc, @DP[0]        ; Read value of variable
   move   DP[0], VarB        ; Point to VarB variable
   move   @DP[0], Acc        ; Copy VarA to VarB
   move   DP[0], VarC        ; Point to VarC variable
   move   @DP[0], #1234h     ; Set VarC = 1234h

上一页  [1] [2] [3] [4] [5]  下一页


Tag:单片机学习单片机开发,单片机原理,单片机教程单片机学习
分类导航
最新更新
热门排行