I am new to IAR and Embedded Programming. I was debugging the following C code, and found that R0
gets to hold the address of counter1
through ??main_0
, while R1
gets to hold address of counter2
through [PC,#0x20]
. This is completely understandable, but I cannot get why it was assigned to R0
to use LDR Rd, -label
while R1
used LDR Rd, [PC Offset]
and what is the difference between the two approaches?
I only knew about literal pools after searching but It didn't answer my question. In addition, where did ??main_0
get defined in the first place?
int counter1=1;
int counter2=1;
int main()
{
int *ptr;
int *ptr2;
ptr=&counter1;
ptr2=&counter2;
(*ptr);
(*ptr2);
counter2;
return 0;
}
CodePudding user response:
??main_0
is not "defined" as such, it's just an auto-generated label for the address used here so that when reading the disassembly you don't have to remember that address 0x8c
is that counter pointer. In fact it would make sense to have the other counter pointer as ??main_1
and I'm not sure why it shows the bare [PC, #0x20]
instead. As you can see on page 144/145 of the IAR assembly reference, those two forms are just different interpretations of the same machine code. If the disassembler decides to assign a label to an address, it can show the label form, otherwise the offset form.
The machine code of the first instruction is 48 07
, which means LDR.N R0, [PC, #0x1C]
. The interpretation as ??main_0
(and the assignment of a label ??main_0
to address 0x8c
in the first place) is just something the disassembler decided to do. You cannot know what the original assembly source (if it even exists and the compiler didn't directly compile to machine code) looked like and whether it used a label there or not.