1

I am trying to learn assembly coding for arm cortex m4.

__asm volatile (
                "   LDR r0,=MY_NUM          \n" /* Restore the context. */
                "                                       \n"
                ".align 2                   \n"
                "MY_NUM: .word 0x12345678       \n" 
                );

screenshot

I am getting "error #29 : expected an expression" at the ldr instruction. How to resolve it?

artless noise
  • 21,212
  • 6
  • 68
  • 105
  • 3
    Advice: don't use inline assembly. That's a complicated thing best left after you have learned standalone assembly ;) – Jester Mar 21 '17 at 19:01
  • Thanx for response, – Tejal Bhardwaj Mar 21 '17 at 19:42
  • Thanx for response, but i want to work with assembly coding for cortex m4, how can i approach? I saw inline assembly in FreeRTOS port, i was trying to implement it in my codes. – Tejal Bhardwaj Mar 21 '17 at 19:44
  • @TejalBhardwaj - if you wish to work with Assembly for Cortex-M4... then you'll just write stand alone ARMv7 Assembly. The vendor for your specific Cortex-M4 based hardware may have provided all the sample files you need; specifically with respect to startup code; which is required to "boot up" - assuming you have a baremetal environment. – InfinitelyManic Mar 21 '17 at 20:00
  • @Jester what is alternative for inline assembly, coz i want to use assembly coding within C coding – Tejal Bhardwaj Mar 22 '17 at 03:32
  • 1
    If you need it within C, then inline is the only way. However you can write entire asm functions in separate file and call them from C, if that's acceptable. – Jester Mar 22 '17 at 09:53

1 Answers1

0

You are using ARMCC which does not have a genuine inline assembler, but instead has a compiled language similar to assembly. Directives such as .align and .word have no meaning to it.

The code is almost the right syntax for GCC + GNU AS, but like most assemblers GNU AS requires anything that is not a label to be indented by one space or tab character. You need a space before the .align directive. Whilst that would compile it would have undefined behaviour as it modifies 2 registers and condition codes without informing the compiler (which assumes the register are unchanged), and if built for thumb mode reads a byte from an unspecified address.

0x12345678 disassembles as:

0:   12345678        eorsne  r5, r4, #120, 12        ; 0x7800000

Or in thumb mode as:

0:   5678            ldrsb   r0, [r7, r1]
2:   1234            asrs    r4, r6, #8
Timothy Baldwin
  • 3,551
  • 1
  • 14
  • 23