Jump to content
43oh

Alignment in Thumb Assembly


Recommended Posts

I'm currently working with a Cortex-M0, which implements a subset of the thumb instruction set.

 

As a learning exercise, I decided to just implement a "blinky" project in assembly, in which I was ultimately successful, but with an unanswered question.

 

Apparently there are some instruction alignment requirements, but I don't seem to know about them, or how to find out about them.  Is there a good resource for this?  I'm relatively new to assembly programming, but these intricacies are what tend to hold me up.  My assembler (gcc arm toolchain) would spit out dozens of errors about alignment.  It took me hours of debugging, and lo and behold I throw a single NOP in the code, and everything works.  I'm sure this is one of the "well if you're programming in assembly, you should know about this already", but I'm self-taught and have probably missed out on some of the fundamentals.

 

Any help is appreciated!

 

Link to post
Share on other sites

I can't answer your question directly, but I've generally found that a good way to figure out what the assembler expects is to write a trivial application in C that does what you want, then compile it with -S and look at the generated assembly. Probably the directives you need will be revealed by that.

Link to post
Share on other sites

Hello,

 

I am somewhat in the same case as you.

I give you a link which was useful to me : http://pygmy.utoh.org/riscy/cortex/led-stm32.html

it speaks a little about alignement and directive .thumb

.thumb ; (same as saying '.code 16')

 

i've adapted this exemple to cortex M0 (stm32F0discovery)

if you need i can put it somewhere.

For the cortex M0 -mcpu=cortex-m0 flag must be added to gcc

 

Link to post
Share on other sites

I can't answer your question directly, but I've generally found that a good way to figure out what the assembler expects is to write a trivial application in C that does what you want, then compile it with -S and look at the generated assembly. Probably the directives you need will be revealed by that.

 

That's actually what I did!  It really didn't help all that much unfortunately since the secret sauce was adding a nop to make the alignment work out.  It did however help me to get the correct directives defined, which was extremely helpful.

 

Hello,

 

I am somewhat in the same case as you.

I give you a link which was useful to me : http://pygmy.utoh.org/riscy/cortex/led-stm32.html

it speaks a little about alignement and directive .thumb

.thumb ; (same as saying '.code 16')

 

i've adapted this exemple to cortex M0 (stm32F0discovery)

if you need i can put it somewhere.

For the cortex M0 -mcpu=cortex-m0 flag must be added to gcc

 

Website must be down, though I did take a look, and it did provide a good example.  The problem is this didn't really outline the problem that I was running into, and that was just that a nop (or probably any additional instruction) just magically fixed my code.

 

Maybe this is just one of the reasons people tell you not to write in assembly. :)

Link to post
Share on other sites

"The Definitive Guide to the ARM Cortex-M0"  by Joseph Yiu is a good book that gets into the m0 architecture. It provides practical coding examples in both 'C' and assembler.  There are some passages about alignment concerns.  If you post some code, we can try and figure out which rules you are violating.

 

-rick

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...