1
\$\begingroup\$

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?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ Incidentally, do you know why if I add .org 0x100 and .byte 0xFF to example in OP, the dump contains ` 1c4: ff ...` - obviously assembler adds address of main to address specified in .org. So, is there a way to specify exact offset? \$\endgroup\$ Commented Jul 4, 2018 at 3:01
  • \$\begingroup\$ There is, but that would be way too long for a comment. You might want to check the linker. \$\endgroup\$
    – Turbo J
    Commented Jul 4, 2018 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.