Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

tutorial arm

Tianshu Huang edited this page Oct 4, 2018 · 5 revisions

ARM Assembly

All of your code gets compiled to assembly! Assembly is essentially a human-readable translation of machine code (which is what you flash to your TM4C) consisting of lots of binary instructions which tell your processor what to do.

Why should I care?

After you take 319k, you should hopefully never have to write raw assembly again. Why, then, should you (and must you) learn assembly?

With simple tasks such as those you would usually find in embedded systems, compiler output is fairly predictable. With time sensitive tasks that are written in C, awareness of the machine code being executed therefore gives you a sense for how long code should take to execute, albeit only precise to an order of magnitude. While this shouldn't be important for Robotathon, for more complex tasks (such as playing 44.1kHz audio -> only 1814 clocks ~ 900 instructions/tick!), this awareness can be critical.

Compilation and an example

So what goes on behind the scenes when you run make, asking the compiler to compile your code for the TM4C?

Here's the basic process: the compiler turns your high level (C or C++) code into ARM assembly, then the assembler turns that assembly into machine code, and finally the linker links all those files of machine code together along with libraries to create your final executable file. The same holds when compiling for your computer as well; it is just assembled into x86 assembly instead of ARM assembly.

Here's an example of what the compilation process looks like:

The C code:

int main() {
    int arm;
    int leg = 1;
    arm = 2;
    leg = arm + leg;
    
    return 0;
}

The corresponding assembly:

NOTE: You can see the assembly of your own code by running make asm if you are using RASTemplate. Otherwise, generally the -S option on most compilers yields assembly.

main:
        str     fp, [sp, #-4]!  ; Main and some stack and frame pointer setup
        add     fp, sp, #0
        sub     sp, sp, #12
        mov     r3, #1          ; leg = 1
        str     r3, [fp, #-8]
        mov     r3, #2          ; arm = 2
        str     r3, [fp, #-12]
        ldr     r2, [fp, #-8]   ; leg = arm + leg
        ldr     r3, [fp, #-12]
        add     r3, r2, r3
        str     r3, [fp, #-8]
        mov     r3, #0          ; return 0
        mov     r0, r3         
        add     sp, fp, #0      ; cleanup and exit main
        ldr     fp, [sp], #4
        bx      lr

Wow, our 6 lines of C just turned into 16 lines of assembly!

So what's up with all of those three letter things, and all those two letter things?

The three letter things you see at the start of almost every line are called instructions, and the two letter things like r0 and fp are called registers. Registers are the places that a processor can take or store values for manipulation, it's much like the ans variable in your calculator, but there is more than one. The instructions are written almost like functions with parameters which can be registers or small numbers. For example mov r3, #1 tells the processor to move the number 1 to register 3.

0_o That looks very confusing and a lot harder to write than C/C++; why would you ever want to read or write assembly?

Why or why not assembly?

Assembly makes you in control of every single thing your processor does and as a consequence -

Assembly is best for:

  • Time sensitive tasks: where every nanosecond of run time counts (this is why drone ESC and flight controller firmware is usually written in assembly)
  • Very computationally intensive tasks: where you need to use every clock cycle to the best of your ability
  • The insane: who would like for their code to be a couple of orders of magnitude harder to write and debug
  • Making things break: namely making your TM4C lock up or loop infinitely
  • Helping you pass EE 306 and EE 319K

Assembly is not so good at:

  • Cross-platform compatibility and reusablility
  • Readability
  • Being your friend

All the ARM Thumb instructions

Click here for a quick reference guide to all the instructions which the TM4C supports (it does not support the full size ARM instruction set, just Thumb).

Challenge: Figure out what all the instructions in the assembly of our program do! The ones in the very beginning and end are especially tricky so you may have to read up on:

  • The stack
  • Stack frames
  • The link register
  • Branching in ARM assembly
  • AAPCS
Clone this wiki locally