Electronic – AVR-GCC initialization code

assemblerassemblyavravr-gccdisassembly

Consider the following asm.S

.global main
main:
rjmp main

Compile and dump it with these commands:

avr-gcc -mmcu=atmega32u4 -g -o asm.elf asm.S
avr-objdump -S asm.elf

In initialization code, among other, there is this command:

ae: 1f be           out 0x3f, r1    ; 63

Why there is this command?

Best Answer

The full initialiation sequence I see is:

__ctors_end:
  eor   r1, r1
  out   0x3f, r1
  ldi   r28, 0xFF
  ldi   r29, 0x0A
  out   0x3e, r29
  out   0x3d, r28
  call  main
  jmp   _exit

Breaking it down:

  eor   r1, r1
  out   0x3f, r1

Register 0x3f is SREG -- the status register, which contains status bits like N, Z, and C, as well as the interrupt enable bit. This code clears the register. The register should already be cleared at reset, but a bootloader or a partial software reset may have left it in an inconsistent state.

  ldi   r28, 0xFF
  ldi   r29, 0x0A
  out   0x3e, r29
  out   0x3d, r28

Ports 0x3d and 0x3e are the high and low portions of the stack pointer. This code initializes the stack pointer to 0x0AFF, at the top of RAM. This is, again, identical to the reset state of the processor, so this is more of a precaution than a necessity.

  call  main
  jmp   _exit

This calls your main function, then jumps to _exit (an infinite loop) in case main returns.