S
S
Serpens4122019-05-10 18:49:54
Atmel AVR
Serpens412, 2019-05-10 18:49:54

How to set the value of a variable in SRAM when it is declared?

Only recently I began to study the AVR assembler, under the Atmega8 controller, using the AVR-GCC compiler. And the question arose - how can you set the value of a variable in SRAM when declaring in the .data section? Under the AVRASM2 compiler, this is done, as I understand it, something like this:

.DSEG
variable: .byte 0xff

However, similar code for AVR-GCC does not lead to a similar result:
.data
variable: .byte 0xff

In fact, at address 0x0060 there is some kind of garbage that has nothing to do with what I wanted to put there. However, if we look at the size of the program using avr-size, we will see that we have 2 bytes occupied in the .data section:
AVR Memory Usage
----------------
Device: atmega8

Program:      64 bytes (0.8% Full)
(.text + .data + .bootloader)

Data:          2 bytes (0.2% Full)
(.data + .bss + .noinit)

If the variable value (0xff) is removed from the declaration, then the date section will contain 0 bytes. In the .lst file, we see this picture in the .data section:
Disassembly of section .data:

00800060 <_edata-0x2>:
  800060:	ff 00       	.word	0x00ff	; ????

Why does the compiler try to shove a word into memory, and why does this data (neither word nor byte) end up in memory? And is it worth declaring data like this at all, perhaps it is better to completely clear SRAM during initialization when the controller starts?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2019-05-10
@Serpens412

In fact, at address 0x0060 there is some kind of garbage that has nothing to do with what I wanted to put there.

Where did the address 0x0060 come from and how did you understand what was there?
TL;DR: code that copies data from FLASH to SRAM and resets .bss is not linked by default. gcc inserts a reference to undefined symbols __do_copy_dataand __do_clear_bssinto each translation unit that defines objects in the .data and .bss sections, respectively. In hand-written assembler files, they can be mentioned explicitly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question