0
\$\begingroup\$

I am trying to understand How bad a 'void main(void)' is from the following link: tech notes. But I have difficulties in understanding two assembly languages examples, the first one is shown below:

int main(void)
{
 return 42;
}

|main|:
       mov ip,sp
       stmfd sp!,{rfp, fp, ip, lr, pc}
       sub fp,ip,#4
       cmps sp,sl
       bllt |x$stack_overflow|
       bl   |__main|
       mov r0,#42
       ldmdb fp,{rfp,fp,sp,pc}^

I have found some explanations from ARM v7-M Architecture Application Level Reference Manual. so I know that stmfd is like PUSH here. But I still have many questions.

  • Why do we call the first six instructions the initialization and stack checking?
  • What do ip, rfp, fp, and sl mean?
  • Why does it sub 4?
  • What is bllt? I just find bl in reference manual
  • Where does 'x$stack_overflow' come from?
  • Why does it use ldmdb?

If you can give me a tutorial or reference manual about ARM assembly languages, I will be very happy. Because I think there is another more difficult assembly language example from that link.

\$\endgroup\$

closed as off-topic by Leon Heller, tcrosley, Dave Tweed, Joe Hass, Matt Young Mar 12 '14 at 15:09

  • This question does not appear to be about electronics design within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ Where did you get this example? What compiler was used? It's not ARM v7-M code so you need to know what architecture it was compiled for, and then get the appropriate manuals from ARM for that architecture. \$\endgroup\$ – Joe Hass Mar 12 '14 at 11:28
  • 4
    \$\begingroup\$ This question appears to be off-topic because it is not about electronic design. \$\endgroup\$ – Leon Heller Mar 12 '14 at 12:36
  • \$\begingroup\$ @LeonHeller I think a embedded system designer has to understand assembly languages. I have asked some questions about STM32 which is a MCU with cortex-m3 core. More knowledge about assembly language can help me design more efficient system \$\endgroup\$ – oilpig Mar 13 '14 at 10:44
  • \$\begingroup\$ @JoeHass please refer to my link from my question 'tech notes'.It used GCC. It just says that it compiles it to ARM assembly language.... I do not waht the architecture is \$\endgroup\$ – oilpig Mar 13 '14 at 10:48
2
\$\begingroup\$

The custom on this website is to ask one question per eh, question, but I'll try to asnwer them all.

  • why do we call the first six instructions the initialisation and stack checking?

Because that is what they do. These instructions are insterted by the compiler, they are not caused by the C-level statement(s) in the main() that the user wrote.

  • what does ip rfp fp sl mean?

Looks look aliases (alternate names) for registers that have a specific purpose.

  • why does it sub 4?

To make room on the stack for something that it is not using. Looks like a missed opportunity for the optimizer :(

  • what is bllt? I just find bl in reference manual

lt is a condition: on these ARM chips ANY instruction can be conditional. In this case it is branch-and-link-iff-less-than.

  • where does 'x$stack_overflow' come from?

It is supplied by the runtime library, and called when the stack is (about to be) exceeded.

  • why does it use ldmdb?

that is the load-multiple-registers-from-a-full-descending-stack instruction. in other words: pop.

PS: for the bare-metal microcontroller softeware I mostly write the requirement that main() must return an int is a royal PITA. There is no OS or script or whatever to return to, so who cares what main() returns? main() should be an infinite loop, but the compiler insists on a return 0; after it. And then it complains about an unreachable statement :(

\$\endgroup\$
  • \$\begingroup\$ Thanks a lot, I really admire your professional answers....where can i obtain these knowledge....i am really afraid of assembly languages when I write MCU code \$\endgroup\$ – oilpig Mar 13 '14 at 10:54
  • \$\begingroup\$ Some general knowledge about computer architecture, and compiler working. Lots of books at that level. Add knowledge of the specific assembler, the book by Furber (ARM System-on-Chip Architecture) has this info, but it has a lot more. \$\endgroup\$ – Wouter van Ooijen Mar 13 '14 at 13:26
  • \$\begingroup\$ The extra four bytes are for 64 bit alignment for double variables. \$\endgroup\$ – Simon Richter May 6 '16 at 5:35

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