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?
1 Answer 1
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.
-
\$\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 ofmain
to address specified in.org
. So, is there a way to specify exact offset? \$\endgroup\$Igor Liferenko– Igor Liferenko2018年07月04日 03:01:25 +00:00Commented 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– Turbo J2018年07月04日 16:11:16 +00:00Commented Jul 4, 2018 at 16:11
Explore related questions
See similar questions with these tags.