gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

ASM > variables in GAS

#9821 - Omega81 - Tue Aug 19, 2003 2:01 am

Hello I was wondering if anyone knew how to create variables in GAS. From the -S output I can see that GCC create two label; one for storing the varible names and another for storing there size. Then in the code, the varibles are writen and read from as array element. Here is a code strip
Code:

ldr   r2, .L2
   .....
   ldr   r2, .L2+4
   mov   r3, #55
   str   r3, [r2, #0]
   ldr   r2, .L2+8
   mov   r3, #33792
   add   r3, r3, #147
   str   r3, [r2, #0]
   ldr   r4, .L2+8
   ldr   r0, .L2+8
   ldr   r3, .L2
   ldr   r2, .L2+4
        ......
   ldr   r3, .L2
   ldr   r3, [r3, #0]
   mov   r0, r3
        ....
.L3:
   .align   2
.L2:
   .word   frn
   .word   sun
   .word   too
.Lfe1:
   .size   main,.Lfe1-main
   .comm   frn, 4, 32
   .comm   sun, 4, 32
   .comm   too, 4, 32


is this how I will have to create variables cause I was hoping I could ust use the variable name directly just have them defined in a .data section or something.

Any throughs?
_________________
Keep it real, keep it Free, Keep it GNU

#9830 - NEiM0D - Tue Aug 19, 2003 1:42 pm

This is how to preserve a space in a section (naturally ram) for variables:

Code:
variable1:              .space 4
variable2:              .space 2
variable3:              .space 1
variable4:              .space 700



To initialise some variables, you will have to define the section (rom or ram) and then:

Code:
variable5:          .long 0x12345678
variable6:          .byte 4,242,43
variable7:          .hword 65530



ps: dont forget to align where needed.

#9831 - Omega81 - Tue Aug 19, 2003 2:30 pm

Sorry if this is a stupid question but how do I define the location(Ram or Rom) were I want to put the variable? the code below......
Code:
 
variable1:              .space 4
 


isn't it the same as

Code:

variable1:           .comm 7, 32


where 32 is the alignment (32it).
_________________
Keep it real, keep it Free, Keep it GNU

#9856 - sajiimori - Wed Aug 20, 2003 7:00 am

You can specify where to put things by putting a .section directive above them. The actual locations of the sections depend on your link script, but .text is usually rom, and .data and .bss are usually in iwram.

Code:

.section .text

variable_in_rom: .word 0x1234

.section .data

variable_in_ram: .word 0xABCD


I think you might actually be able to just type .text instead of .section .text, but I don't remember.