Arm is RISC based architecture which can be seen a lot in embedded domain. There are billions of arm devices currently active. It ranges from automotive to smartphone processor and now in your laptop thanks to Apple. That being said let us dive right in.
Basic understanding of C programming will come in handy.(If not you might have to do some googling here and there)
Where to code?
So most of us are on x86_64 arch machines. So we need to rely on an emulator. Cpulator is a good place to get started. in Architecture select ARMv7 and in System DE1-SoC.
So here are some terms you have to be familiar with just have a look and then you can always
GPRs (General Purpose Registers)
There are 16 (R0,R1,R2....R15) registers available. We can used them in computations and storing values. Out of these 3 has some special purpose (R13,R14,R15) and some regs will have temporary special assignments.
Link Register (R13):
This holds return address; So I will assume that you know functions in higher level languages; A function is called from a certain context (main function for c /c++), and once all the instructions of called function functions are executed, control is transferred to main function or function(or context) which called the function. So we need to store the address where the control has to return. Link reg stores the same
Stack Pointer register (R14):
SP stores address of top of the stack. I usually don't mess with it (Although it's totally mess-able :) ).
Program counter (R15)
PC stores address of next instruction to be executed so if you override this you can jump between points in your program. Or PC is used for teleportation.
That is all for terminologies
Current Program Status Register:
CPSR register contains flags indicating current status of our program.
CFSR FLAG DESCRIPTION  N Negative condition code flag  Z Zero condition code flag  C Carry condition code flag  V Overflow condition code flag  Q Cumulative saturation bit [26:25] IT[1:0] If-Then execution state bits for the Thumb IT (If-Then) instruction  J Jazelle bit [19:16] GE Greater than or Equal flags [15:10] IT[7:2] If-Then execution state bits for the Thumb IT (If-Then) instruction  E Endianness execution state bit: 0 - Little-endian, 1 - Big- endian  A Asynchronous abort mask bit  I IRQ mask bit  F FIRQ mask bit  T Thumb execution state bit [4:0] M Mode field
Let's LOAD our bags and get MOVing
I am sorry for bad puns :)
Let's look at a pretty basic program and we'll learn to talk in assembly.
.global _start// EXTERNALLY ACCESSIBLE _start label _start: //STARTING POINT OF PROGRAM YOU CAN CHANGE _start TO ANYTHING MOV R0,#12 // MOVE VALUE IMMEDIATELY AT RIGHT TO R0 MOV R1,#11 // MOVE VALUE IMMEDIATELY AT RIGHT TO R1 ADD R2,R0,R1// ADD R0,R1 AND STORE IT IN R2 i.e, R2 = R0 + R1
Copy paste this in your emulator (cpulator) and click on compile and load.Now Click on step into three times and check reg values in your left. R0 has 12 (C in hex) stored and R1 has 11(B in hex) and R2 stores sum of R0 and R1 which is 23 (17 in hex).
If I have to read this program then, .global makes _start accessible outside of this current file.So it can be used as a starting point.
Syntax of label
So start is analogous to main function.
Now we move values into regs and add them and store them in a separate reg. This is the basic structure of a assembly program.Note that in comments I have used the word immediate;It's intentional and will become clear in next section. Now let us see different ways to move things around.
We can move data in different ways in assembly.
- We can move a value immediately into a register
- We can move a address directly into a register
- We can move values directly between registers
- Or we can move values in a indexed way
If...Read more »