#pragma DATA_SECTION 和 #pragma CODE_SECTION的使用

#pragma DATA_SECTION 和 #pragma CODE_SECTION的使用

The DATA_SECTION pragma allocates space for the symbol in a section called section name. 
The syntax for the pragma in C is:
#pragma DATA_SECTION (symbol, "section name");
The syntax for the pragma in C++ is:
#pragma DATA_SECTION ( "section name");
The DATA_SECTION pragma is useful if you have data objects that you want to link into an area separate from the .bss section.
This directive is illustrated in the following example.
Using the DATA_SECTION Pragma
a)    C source file
#pragma DATA_SECTION(bufferB, "my_sect")
char bufferA[512];
char bufferB[512]:
// 执行完此三条语句之后,数组bufferB被分配到段名为 "my_sect"的自定义段中

b)    C++ source file
char bufferA[512];
#pragma DATA_SECTION("my_sect")
char bufferB[512];

c)    Assembly source file

           .global _bufferA
           .bss     _bufferA,512,4
           .global _bufferB
_bufferB: .usect   "my_sect",512,4

///

The CODE_SECTION pragma allocates space for the func in a section named section name. The CODE_SECTION pragma is useful if you have code objects that you want to link into an area separate from the .text section.
The syntax of the pragma in C is:

#pragma CODE_SECTION (func, section name)

The syntax of the pragma in C++ is:

#pragma CODE_SECTION (section name)

The following example demonstrates the use of the CODE_SECTION pragma.

Using the CODE_SECTION Pragma

a)     C source file


char bufferA[80];
char bufferB[80];

#pragma CODE_SECTION(funcA, codeA)

char funcA(int i);
char funcB(int i);

void main()
{
char c;
c = funcA(1);
c = funcB(2);
}

char funcA (int i)
{
return bufferA;
}

char funcB (int j)
{
return bufferB[j];
}

b)     Assembly source file

.sect    ".text"         .global _main;***************************************************************
;* FNAME: _main                          FR SIZE:    2            *
;*                                                              *
;* FUNCTION ENVIRONMENT                                         *
;*                                                              *
;* FUNCTION PROPERTIES                                          *
;*                             0 Parameter,   1 Auto,   0 SOE      *
;***************************************************************

_main:

         ADDB       SP,#2
         MOVB       AL,#1                  ; |12|
         LCR        #_funcA                ; |12|
         ; call occurs [#_funcA] ; |12|
         MOV        *-SP[1],AL             ; |12|
         MOVB       AL,#1                  ; |13|
         LCR        #_funcB                ; |13|
         ; call occurs [#_funcB] ; |13|
         MOV        *-SP[1],AL             ; |13|
         SUBB       SP,#2
         LRETR
        ; return occurs

.sect    "codeA"
         .global _funcA
;***************************************************************
;* FNAME: _funcA                         FR SIZE:    1            *
;*                                                              *
;* FUNCTION ENVIRONMENT                                         *
;*                                                              *
;* FUNCTION PROPERTIES                                          *
;*                             0 Parameter,   1 Auto,   0 SOE      *
;***************************************************************

_funcA:
         ADDB       SP,#1
         MOV        *-SP[1],AL             ; |17|
         MOVZ       AR6,*-SP[1]            ; |18|
         ADD        AR6,#_bufferA          ; |18|
         SUBB       SP,#1                  ; |18|
         MOV        AL,*+XAR6[0]           ; |18|
         LRETR
         ;return occurs

          .sect    ".text"
          .global _funcB

;***************************************************************
;* FNAME: _funcB                         FR SIZE:    1            *
;*                                                              *
;* FUNCTION ENVIRONMENT                                         *
;*                                                              *
;* FUNCTION PROPERTIES                                          *
;*                             0 Parameter,   1 Auto,   0 SOE      *
;***************************************************************

_funcB:
         ADDB       SP,#1
         MOV        *-SP[1],AL             ; |22|
         MOVZ       AR6,*-SP[1]            ; |23|
         ADD        AR6,#_bufferB          ; |23|
         SUBB       SP,#1                  ; |23| 
         MOV        AL,*+XAR6[0]           ; |23|
         LRETR
         ;return occurs

// 从红色标记可以看出,在加入#pragma CODE_SECTION(funcA, codeA)语句之后,main()函数和_funcB函数被

// 分配到 ".text" 段中,而_funcA函数被分配到 "codeA" 段中。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值