This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How access data in the "data" section?



Hi all, I've a new question that could seem so simple, but I didn't find any way out, so I'm asking one more time help to the forum.


I've declared in my source just two section, a ".text" and a ".data" one, but it seems that I cannot acces the datas, and variables stored in the ".data" section, from within the text section.


Thi is the ".data" section:

.data
LCDVar:   .byte   0x03
                  .align  0x02

And this one should be the ".text":

              .text
main:    ldr     r3,LCDVar

Both section seems so simple but, when I'm in the the text section, assembler generates the error: "Address must be defined in the current section".
It seems that from the text section can't access the variables in the data.

I tried many workaround: "ldr r3,[LCDVar]", "ldr r3,#LCDVar","mov r3,LCDVar", a so more, but the error is similar.

So the question now is: How can be possible access the variables in the data section? In the assembly manual is written that the "ldr" instruction,
can access even the other section over its own, but I still don't.

Any help will be appreciated.

Thanks

  • Hello Leonardo,

    How are the .text and .data assembled in the memory map of the device?

    Regards
    Amit
  • I'm not sure to have understood deeply your question.
    I didn't change standard memory map or linker script of the MCU itself, so it's:
    FLASH: from: 0x0 ; length: 0x040000
    SRAM: from: 0x0 20000000; length: 0x08000
    Is the correct anwer to your question?
    Where can I check how the memory map is assembled?
    Thanks
  • Hello Leonardo,

    The location of the .text and .const would be given in the map file generated in your project.

    Regards
    Amit
  • The location of the .text and .const would be given in the map file generated in your project.

    The map file is generated by the linker, and would basically repeat the information from the linker file, which (usually) defines those sections.

    If you want to reference data from another module, you must declare them as public/extern. In IAR assembler syntax, that would be the EXTERN and PUBLIC keywords.

    I'm not coding in assembler, just reading it - like the startup code for C/C++ projects.

  • Referencing an external variables is done with the ".global" directive, but is useless for my problem.

    Here, linked is the map file for my source and, my variable: LCDVar is in the SRAM just after the Stack pointer.

    My only question is: how access to a symbol in the SRAM? I'm quite sure the "ldr" command is correct, but I accept every help.

    Thanks

    ******************************************************************************
                      TI ARM Linker Unix v5.2.5                    
    ******************************************************************************
    >> Linked Tue May 10 14:39:31 2016
    
    OUTPUT FILE NAME:   <TivaM4-Indicizzato-.out>
    ENTRY POINT SYMBOL: "_c_int00"  address: 00000375
    
    
    MEMORY CONFIGURATION
    
             name            origin    length      used     unused   attr    fill
    ----------------------  --------  ---------  --------  --------  ----  --------
      FLASH                 00000000   00040000  00000498  0003fb68  R  X
      SRAM                  20000000   00008000  00000220  00007de0  RW X
    
    
    SEGMENT ALLOCATION MAP
    
    run origin  load origin   length   init length attrs members
    ----------  ----------- ---------- ----------- ----- -------
    00000000    00000000    00000498   00000498    r-x
      00000000    00000000    0000026c   0000026c    r-- .intvecs
      0000026c    0000026c    0000022c   0000022c    r-x .text
    20000000    20000000    00000200   00000000    rw-
      20000000    20000000    00000200   00000000    rw- .stack
    20000200    20000200    00000020   00000020    rwx
      20000200    20000200    00000020   00000020    rwx .data
    
    
    SECTION ALLOCATION MAP
    
     output                                  attributes/
    section   page    origin      length       input sections
    --------  ----  ----------  ----------   ----------------
    .intvecs   0    00000000    0000026c     
                      00000000    0000026c     tm4c123gh6pm_startup_ccs.obj (.intvecs)
    
    .text      0    0000026c    0000022c     
                      0000026c    0000009c     rtsv7M4_T_le_v4SPD16_eabi.lib : memcpy_t2.obj (.text)
                      00000308    0000006c                                   : autoinit.obj (.text)
                      00000374    00000050                                   : boot.obj (.text)
                      000003c4    0000004c                                   : cpy_tbl.obj (.text)
                      00000410    00000044                                   : exit.obj (.text)
                      00000454    00000018                                   : args_main.obj (.text)
                      0000046c    00000014                                   : _lock.obj (.text)
                      00000480    0000000c     tm4c123gh6pm_startup_ccs.obj (.text)
                      0000048c    00000004     rtsv7M4_T_le_v4SPD16_eabi.lib : pre_init.obj (.text)
                      00000490    00000008     main.obj (.tramp.main.1)
    
    .cinit     0    00000000    00000000     UNINITIALIZED
    
    .init_array 
    *          0    00000000    00000000     UNINITIALIZED
    
    .data      0    20000200    00000020     
                      20000200    0000000a     main.obj (.data)
                      2000020a    00000002     --HOLE-- [fill = 0]
                      2000020c    00000008     rtsv7M4_T_le_v4SPD16_eabi.lib : _lock.obj (.data)
                      20000214    00000008                                   : exit.obj (.data)
                      2000021c    00000004                                   : stkdepth_vars.obj (.data)
    
    .stack     0    20000000    00000200     UNINITIALIZED
                      20000000    00000200     --HOLE--
    
    .TI.persistent 
    *          0    20000200    00000000     UNINITIALIZED
    
    
    FAR CALL TRAMPOLINES
    
    callee name               trampoline name
       callee addr  tramp addr   call addr  call info
    --------------  -----------  ---------  ----------------
    main                      $Tramp$TT$L$PI$$main
       20000205     00000490     00000464   rtsv7M4_T_le_v4SPD16_eabi.lib : args_main.obj (.text)
    
    [1 trampolines]
    [1 trampoline calls]
    
    
    GLOBAL SYMBOLS: SORTED ALPHABETICALLY BY Name 
    
    address   name                    
    -------   ----                    
    00000411  C$$EXIT                 
    20000200  LCDVar                  
    00000481  ResetISR                
    UNDEFED   SHT$$INIT_ARRAY$$Base   
    UNDEFED   SHT$$INIT_ARRAY$$Limit  
    20000200  __STACK_END             
    00000200  __STACK_SIZE            
    20000200  __STACK_TOP             
    UNDEFED   __TI_CINIT_Base         
    UNDEFED   __TI_CINIT_Limit        
    UNDEFED   __TI_Handler_Table_Base 
    UNDEFED   __TI_Handler_Table_Limit
    00000309  __TI_auto_init          
    20000214  __TI_cleanup_ptr        
    20000218  __TI_dtors_ptr          
    00000000  __TI_static_base__      
    0000026d  __aeabi_memcpy          
    0000026d  __aeabi_memcpy4         
    0000026d  __aeabi_memcpy8         
    ffffffff  __binit__               
    ffffffff  __c_args__              
    20000000  __stack                 
    00000455  _args_main              
    00000375  _c_int00                
    2000020c  _lock                   
    0000047b  _nop                    
    00000473  _register_lock          
    0000046d  _register_unlock        
    0000048d  _system_pre_init        
    20000210  _unlock                 
    00000415  abort                   
    ffffffff  binit                   
    000003c5  copy_in                 
    0000041d  exit                    
    00000000  g_pfnVectors            
    20000205  main                    
    2000021c  main_func_sp            
    0000026d  memcpy                  
    
    
    GLOBAL SYMBOLS: SORTED BY Symbol Address 
    
    address   name                    
    -------   ----                    
    00000000  __TI_static_base__      
    00000000  g_pfnVectors            
    00000200  __STACK_SIZE            
    0000026d  __aeabi_memcpy          
    0000026d  __aeabi_memcpy4         
    0000026d  __aeabi_memcpy8         
    0000026d  memcpy                  
    00000309  __TI_auto_init          
    00000375  _c_int00                
    000003c5  copy_in                 
    00000411  C$$EXIT                 
    00000415  abort                   
    0000041d  exit                    
    00000455  _args_main              
    0000046d  _register_unlock        
    00000473  _register_lock          
    0000047b  _nop                    
    00000481  ResetISR                
    0000048d  _system_pre_init        
    20000000  __stack                 
    20000200  LCDVar                  
    20000200  __STACK_END             
    20000200  __STACK_TOP             
    20000205  main                    
    2000020c  _lock                   
    20000210  _unlock                 
    20000214  __TI_cleanup_ptr        
    20000218  __TI_dtors_ptr          
    2000021c  main_func_sp            
    ffffffff  __binit__               
    ffffffff  __c_args__              
    ffffffff  binit                   
    UNDEFED   SHT$$INIT_ARRAY$$Base   
    UNDEFED   SHT$$INIT_ARRAY$$Limit  
    UNDEFED   __TI_CINIT_Base         
    UNDEFED   __TI_CINIT_Limit        
    UNDEFED   __TI_Handler_Table_Base 
    UNDEFED   __TI_Handler_Table_Limit
    
    [38 symbols]
    

  • As said before, I don't do any coding in assembler. And neither do others, obviously. That should mean my knowledge on this field is rather limited.

    But what catches my eye is the following:

    SECTION ALLOCATION MAP
    
     output                                  attributes/
    section   page    origin      length       input sections
    --------  ----  ----------  ----------   ----------------
    .TI.persistent 
    *          0    20000200    00000000     UNINITIALIZED


    You might have noticed that the length of the heap section (the C name), where your variable is located, is actually zero.
    In IAR and Keil syntax, one needs to declare a "variable" (i.e. a name for a specific memory location) with the keyword "DCD". Not sure about the gcc assembler syntax.

    In general, I would first create a small C project, set the appropriate compiler flags to create assembly files, and build it. That should serve as a good example for your "pure assembly" projects.
    Or, perhaps you find a good Cortex M/gcc assembly coding tutorial somewhere else.


    PS:
    There seems to be an issue with the formatting software on this forum. Changing the formatting inside a quote (different font, bold/italic, etc.) produces "additional" visible formatting instructions in the quote.
    Or did I something wrong ?
  • Hi,

    A couple of weeks ago, with your previous question, I have suggested for you an .asm example, which seems not being followed...

    As for actually question, try this:

    	;;your .asm file:
    	.global __stack
    __stack:	.usect "stack", STACK_SIZE*4,8
    
    ;;; your data section
    	.data
    LCDVar	.byte	033h
    ;;; put the asasembler into correct configuration
    	.thumb
    
    ;;; now the main function - same file
    
    	.text
    	.allign 4
    	.thumbfunct main
    main: 	.asmfunc
    	ldr	r3, LCDVar
    	b	.		;don't "exit to system"
    	.endasmfunc
    

    Please note this is not tested - check it out and report the behavior.

  • Hello Petrei

    Is the issue here because the .data is way far from the code calling it. In which case MOVH and MOVL must be used to load the address and then a LDR call?

    Regards
    Amit
  • Hi all,

    finally I found the solution to my little problem and I want to share with you all because This kind of problems regards the way the assembly manage the memory organization and not so much the assembly itself.

    In the end the solution was so simple, I understood that every declaration of data memory with the key words: bss, data or const couldn't exist in an indipendent code, and all of them need to be placed inside a "text" block.

    So I need to code exactly in this sequence:

    .text
    ;here I can place some initialization code or, instead even a "nop" is enough
    
    .data
    ;here declare all of my datas, but this block need to be placed inside a text section, this is the reason why the preceeding "text" is mandatory.
    
    .text
    ;after I have declared all of my datas, need to place a second "text" label to mark the end of the data section and notify the assembler the start of the code section again.

    Thanks you all.

    Leonardo

  • Hi,

    Tested and verified, linked with startup_ccs.c file:

    ;; your .asm file:
    ;;
    ;;
    ;;; put the asasembler into correct configuration
    	.thumb
    ;;; your data section
    	.data
    	.align	4
    	.global LCDVar
    LCDVar	.byte	033h
    
    ;;; now the main function - same file
    	.text
    	.align 4
    	.global	main
    LM	.field	LCDVar, 32
    
    	.thumbfunc main
    main: .asmfunc
    	ldr	r0, LM
    	ldr	r3, [r0]
    L:	b	L		;don't "exit to system"
    	.endasmfunc
    	.end