谁动了SP起始地址?

[复制链接]
2477|6
 楼主| wangjun403 发表于 2011-12-28 20:59 | 显示全部楼层 |阅读模式
ST, AC, ck, Stack, AN
KEIL环境,small模式编译

  1. $NOMOD51
  2. ;------------------------------------------------------------------------------
  3. ;  This file is part of the C51 Compiler package
  4. ;  Copyright (c) 1988-2005 Keil Elektronik GmbH and Keil Software, Inc.
  5. ;  Version 8.01
  6. ;
  7. ;  *** <<< Use Configuration Wizard in Context Menu >>> ***
  8. ;------------------------------------------------------------------------------
  9. ;  STARTUP.A51:  This code is executed after processor reset.
  10. ;
  11. ;  To translate this file use A51 with the following invocation:
  12. ;
  13. ;     A51 STARTUP.A51
  14. ;
  15. ;  To link the modified STARTUP.OBJ file to your application use the following
  16. ;  Lx51 invocation:
  17. ;
  18. ;     Lx51 your object file list, STARTUP.OBJ  controls
  19. ;
  20. ;------------------------------------------------------------------------------
  21. ;
  22. ;  User-defined <h> Power-On Initialization of Memory
  23. ;
  24. ;  With the following EQU statements the initialization of memory
  25. ;  at processor reset can be defined:
  26. ;
  27. ; <o> IDATALEN: IDATA memory size <0x0-0x100>
  28. ;     <i> Note: The absolute start-address of IDATA memory is always 0
  29. ;     <i>       The IDATA space overlaps physically the DATA and BIT areas.
  30. IDATALEN        EQU     80H
  31. ;
  32. ; <o> XDATASTART: XDATA memory start address <0x0-0xFFFF>
  33. ;     <i> The absolute start address of XDATA memory
  34. XDATASTART      EQU     0     
  35. ;
  36. ; <o> XDATALEN: XDATA memory size <0x0-0xFFFF>
  37. ;     <i> The length of XDATA memory in bytes.
  38. XDATALEN        EQU     0      
  39. ;
  40. ; <o> PDATASTART: PDATA memory start address <0x0-0xFFFF>
  41. ;     <i> The absolute start address of PDATA memory
  42. PDATASTART      EQU     0H
  43. ;
  44. ; <o> PDATALEN: PDATA memory size <0x0-0xFF>
  45. ;     <i> The length of PDATA memory in bytes.
  46. PDATALEN        EQU     0H
  47. ;
  48. ;</h>
  49. ;------------------------------------------------------------------------------
  50. ;
  51. ;<h> Reentrant Stack Initialization
  52. ;
  53. ;  The following EQU statements define the stack pointer for reentrant
  54. ;  functions and initialized it:
  55. ;
  56. ; <h> Stack Space for reentrant functions in the SMALL model.
  57. ;  <q> IBPSTACK: Enable SMALL model reentrant stack
  58. ;     <i> Stack space for reentrant functions in the SMALL model.
  59. IBPSTACK        EQU     0       ; set to 1 if small reentrant is used.
  60. ;  <o> IBPSTACKTOP: End address of SMALL model stack <0x0-0xFF>
  61. ;     <i> Set the top of the stack to the highest location.
  62. IBPSTACKTOP     EQU     0xFF +1     ; default 0FFH+1  
  63. ; </h>
  64. ;
  65. ; <h> Stack Space for reentrant functions in the LARGE model.      
  66. ;  <q> XBPSTACK: Enable LARGE model reentrant stack
  67. ;     <i> Stack space for reentrant functions in the LARGE model.
  68. XBPSTACK        EQU     0       ; set to 1 if large reentrant is used.
  69. ;  <o> XBPSTACKTOP: End address of LARGE model stack <0x0-0xFFFF>
  70. ;     <i> Set the top of the stack to the highest location.
  71. XBPSTACKTOP     EQU     0xFFFF +1   ; default 0FFFFH+1
  72. ; </h>
  73. ;
  74. ; <h> Stack Space for reentrant functions in the COMPACT model.   
  75. ;  <q> PBPSTACK: Enable COMPACT model reentrant stack
  76. ;     <i> Stack space for reentrant functions in the COMPACT model.
  77. PBPSTACK        EQU     0       ; set to 1 if compact reentrant is used.
  78. ;
  79. ;   <o> PBPSTACKTOP: End address of COMPACT model stack <0x0-0xFFFF>
  80. ;     <i> Set the top of the stack to the highest location.
  81. PBPSTACKTOP     EQU     0xFF +1     ; default 0FFH+1  
  82. ; </h>
  83. ;</h>
  84. ;------------------------------------------------------------------------------
  85. ;
  86. ;  Memory Page for Using the Compact Model with 64 KByte xdata RAM
  87. ;  <e>Compact Model Page Definition
  88. ;
  89. ;  <i>Define the XDATA page used for PDATA variables.
  90. ;  <i>PPAGE must conform with the PPAGE set in the linker invocation.
  91. ;
  92. ; Enable pdata memory page initalization
  93. PPAGEENABLE     EQU     0       ; set to 1 if pdata object are used.
  94. ;
  95. ; <o> PPAGE number <0x0-0xFF>
  96. ; <i> uppermost 256-byte address of the page used for PDATA variables.
  97. PPAGE           EQU     0
  98. ;
  99. ; <o> SFR address which supplies uppermost address byte <0x0-0xFF>
  100. ; <i> most 8051 variants use P2 as uppermost address byte
  101. PPAGE_SFR       DATA    0A0H
  102. ;
  103. ; </e>
  104. ;------------------------------------------------------------------------------

  105. ; Standard SFR Symbols
  106. ACC     DATA    0E0H
  107. B       DATA    0F0H
  108. SP      DATA    81H
  109. DPL     DATA    82H
  110. DPH     DATA    83H

  111.                 NAME    ?C_STARTUP


  112. ?C_C51STARTUP   SEGMENT   CODE
  113. ?STACK          SEGMENT   IDATA

  114.                 RSEG    ?STACK
  115.                 DS      1

  116.                 EXTRN CODE (?C_START)
  117.                 PUBLIC  ?C_STARTUP

  118.                 CSEG    AT      0
  119. ?C_STARTUP:     LJMP    STARTUP1

  120.                 RSEG    ?C_C51STARTUP

  121. STARTUP1:

  122. IF IDATALEN <> 0
  123.                 MOV     R0,#IDATALEN - 1
  124.                 CLR     A
  125. IDATALOOP:      MOV     @R0,A
  126.                 DJNZ    R0,IDATALOOP
  127. ENDIF

  128. IF XDATALEN <> 0
  129.                 MOV     DPTR,#XDATASTART
  130.                 MOV     R7,#LOW (XDATALEN)
  131.   IF (LOW (XDATALEN)) <> 0
  132.                 MOV     R6,#(HIGH (XDATALEN)) +1
  133.   ELSE
  134.                 MOV     R6,#HIGH (XDATALEN)
  135.   ENDIF
  136.                 CLR     A
  137. XDATALOOP:      MOVX    @DPTR,A
  138.                 INC     DPTR
  139.                 DJNZ    R7,XDATALOOP
  140.                 DJNZ    R6,XDATALOOP
  141. ENDIF

  142. IF PPAGEENABLE <> 0
  143.                 MOV     PPAGE_SFR,#PPAGE
  144. ENDIF

  145. IF PDATALEN <> 0
  146.                 MOV     R0,#LOW (PDATASTART)
  147.                 MOV     R7,#LOW (PDATALEN)
  148.                 CLR     A
  149. PDATALOOP:      MOVX    @R0,A
  150.                 INC     R0
  151.                 DJNZ    R7,PDATALOOP
  152. ENDIF

  153. IF IBPSTACK <> 0
  154. EXTRN DATA (?C_IBP)

  155.                 MOV     ?C_IBP,#LOW IBPSTACKTOP
  156. ENDIF

  157. IF XBPSTACK <> 0
  158. EXTRN DATA (?C_XBP)

  159.                 MOV     ?C_XBP,#HIGH XBPSTACKTOP
  160.                 MOV     ?C_XBP+1,#LOW XBPSTACKTOP
  161. ENDIF

  162. IF PBPSTACK <> 0
  163. EXTRN DATA (?C_PBP)
  164.                 MOV     ?C_PBP,#LOW PBPSTACKTOP
  165. ENDIF

  166.                 MOV     SP,#?STACK-1

  167. ; This code is required if you use L51_BANK.A51 with Banking Mode 4
  168. ;<h> Code Banking
  169. ; <q> Select Bank 0 for L51_BANK.A51 Mode 4
  170. #if 0   
  171. ;     <i> Initialize bank mechanism to code bank 0 when using L51_BANK.A51 with Banking Mode 4.
  172. EXTRN CODE (?B_SWITCH0)
  173.                 CALL    ?B_SWITCH0      ; init bank mechanism to code bank 0
  174. #endif
  175. ;</h>
  176.                 LJMP    ?C_START

  177.                 END


执行过红色部分的时候,SP变成了0x0b

程序代码
  1. #include <reg52.h>

  2. int tmp ;

  3. int main(void)
  4. {
  5.         int i = 0;

  6.         return 0;
  7. }


求大虾解释下
highgear 发表于 2011-12-28 22:11 | 显示全部楼层
MOV     SP,#?STACK-1
 楼主| wangjun403 发表于 2011-12-29 09:15 | 显示全部楼层
这里我知道,当时我想红色标注的,结果没有成功(才发现)
为什么这里会修改SP?为什么要修改?
icecut 发表于 2011-12-29 09:18 | 显示全部楼层
这是启动代码...或者说自动生成的代码.
至于为什么,你最好先打好基础再看....

不是有一句话能说明白的
highgear 发表于 2011-12-29 10:06 | 显示全部楼层
看这:
http://www.keil.com/support/docs/1219.htm

After reset, the stack pointer is initialized to 07h. The stack will start growing up from address 8h.

The Keil C compiler uses internal DATA memory for your variables and also allows you to use register banks 1, 2, and 3. If the stack pointer were not adjusted, calls to functions and interrupts would overwrite your variables.

Therefore, the last thing the startup code does is to set the stack pointer to the end of all your internal DATA variables.
icecut 发表于 2011-12-29 10:32 | 显示全部楼层
ls太棒了...不知你看明白没有.至于为什么这么设置,你还需继续思考
 楼主| wangjun403 发表于 2011-12-29 11:01 | 显示全部楼层
5# highgear

看那意思SP起始地址是根据内部ram使用多少来决定的?

那不是要等到程序编译后,启动代码才知道如何修改?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:数学才不是浮云

0

主题

629

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部