Home | Projects | Notes > Computer Architecture & Organization > Structure of an ARM Assembly Program
There are differences between the textbook and the Raspberry Pi assembler directives. (We will be using the ones for the Raspberry Pi.)
Structure of an ARM Program for the Raspberry Pi: (Try to follow this format.)
xxxxxxxxxx
591@ Structure of an ARM Program for the Raspberry Pi
2
3@ --------------------------------------------------------------------------------------
4.text @ Defines a read-only section. (Code starts here)
5 @ Everything from here to .data (read/write) section, you don't
6 @ want the code to be modified or overwritten by accident.
7 @ Attempt to write in this section will produce 'seg fault'.
8 @ .text tells the assembler to switch to the text segment
9 @ (where code goes).
10
11@ --------------------------------------------------------------------------------------
12.global main @ Defines the location of the first instruction to be executed.
13 @ This is the memory address that will be loaded into the PC.
14main:
15
16 @ Make sure to have 'exit:' statement in this area so your program
17 @ terminates at some point.
18
19@ --------------------------------------------------------------------------------------
20.data @ Defines a read/write section.
21 @ .data tells the assembler to switch to the data segment
22 @ (where data goes).
23
24.balign 4 @ Forces a word boundary.
25 @ 4 specifies the number of bytes that must be aligned to.
26 @ (this number must be power of 2).
27 @ It means, the next piece of memory that I declare after this directive
28 @ need to start at a memory location that is divisible by 4. It has to be
29 @ aligned with the memory locations that are divisible by 4.
30
31Q: .word 9 @ Defines a label Q at current memory location as word-size and
32 @ sets it to a decimal 9.
33 @ .word allocates 4 byte memory space to hold data.
34 @ .hword allocates 2 byte memory space to hold data.
35 @ .byte allocates 1 byte memory space to hold data.
36
37Str: .asciz "This is a sample string.\n"
38 @ '.asciz' adds terminating null character at the end so you don't have to
39 @ include it manually.
40 @
41 @ `.ascii` does NOT add terminating null character so you have to
42 @ explicitly add it at the end of the string.
43 @ (e.g., .ascii "This is a sample string.\0\n")
44 @ ∴ Just use '.asciz' and make your life eaiser.
45
46Array: .skip 4x10 @ Defines a label 'Array' at current memory location and reserves
47 @ 40 bytes of memory.
48 @ Note that '.skip' does NOT initialize the allocated memory; Thus
49 @ 'Array' will contain whatever garbage values it was previously
50 @ set to.
51
52@ --------------------------------------------------------------------------------------
53.global XXX @ Defines any external calls like printf, scanf, etc.
54
55@ --------------------------------------------------------------------------------------
56.end @ Defines the end of the assembly code.
57 @ There is no more assembly code after this point in the file.
58 @ Can be replaced by a blank line.
59@ --------------------------------------------------------------------------------------
Labels such as
main:
,.end
,Str
represent addresses! Be aware how these are called in the main section.
C Code
xxxxxxxxxx
41X = P - Q;
2
3if (X >= 0) { X = P + 5; }
4else { X = P + 20; }
ARM Assembly Code (for the Raspberry Pi)
xxxxxxxxxx
811@ ---------------------------------------------------------------------------
2@ Case 1: P = 12, Q = 9, results in X = 17 (executes 'if' part)
3@ ---------------------------------------------------------------------------
4
5.text
6
7.balign 4 @ Make sure the code is on full word boundaries.
8
9.global main @ Entry point for the program. (Must be global)
10
11main:
12 ldr r0, =P @ Load the address of P
13 ldr r0, [r0] @ Load the value of P
14 ldr r1, =Q @ Load the address of Q
15 ldr r1, [r1] @ Load the value of Q
16 ldr r3, =X @ Load the address of X
17 subs r2, r0, r1 @ Do the subtraction and set the CCR flags
18 bpl then @ If positive, add 5
19 add r2, r0, #20 @ Else (if negative), add 20
20 b exit @ Done with calculation
21
22then:
23 add r2, r0, #5
24
25exit:
26 str r2, [r3] @ Write the result back to X in memory
27 mov r0, r2 @ Put x in r0 so it can be echo printed
28 bx lr @ Stop
29
30.data
31
32.balign 4 @ Force to the next whole word.
33X: .word 0 @ Define one word for X and initialize to 0
34
35.balign 4 @ Force to the next whole word.
36P: .word 12 @ Define one word for P and initialize to 12
37
38.balign 4 @ Force to the next whole word.
39Q: .word 9 @ Define one word for Q and initialize to 9
40
41
42@ ---------------------------------------------------------------------------
43@ Case 2: P = 12, Q = 14, results in X = 32 (executes 'else' part)
44@ (Same logic with changed data value, Q = 14)
45@ ---------------------------------------------------------------------------
46
47.text
48
49.balign 4 @ Make sure the code is on full word boundaries.
50
51.global main @ Entry point for the program. (Must be global)
52
53main:
54ldr r0, =P @ Load the address of P
55ldr r0, [r0] @ Load the value of P
56ldr r1, =Q @ Load the address of Q
57ldr r1, [r1] @ Load the value of Q
58ldr r3, =X @ Load the address of X
59subs r2, r0, r1 @ Do the subtraction and set the CCR flags
60bpl then @ If positive, add 5
61add r2, r0, #20 @ Else (if negative), add 20
62b exit @ Done with calculation
63
64then:
65add r2, r0, #5
66
67exit:
68str r2, [r3] @ Write the result back to X in memory
69mov r0, r2 @ Put x in r0 so it can be echo printed
70bx lr @ Stop
71
72.data
73
74.balign 4 @ Force to the next whole word.
75X: .word 0 @ Define one word for X and initialize to 0
76
77.balign 4 @ Force to the next whole word.
78P: .word 12 @ Define one word for P and initialize to 12
79
80.balign 4 @ Force to the next whole word.
81Q: .word 14 @ Define one word for Q and initialize to 14
C Code
xxxxxxxxxx
91/* Calculates 1 + 2 + 3 + ... + 19 + 20 */
2
3int counter = 1;
4int rsum = 0;
5
6while (counter < 21)
7{
8 rsum += counter;
9}
ARM Assembly Code (for the Raspberry Pi)
xxxxxxxxxx
231@ ---------------------------------------------------------------------------
2@ Calculates 1 + 2 + 3 + ... + 19 + 20
3@ ---------------------------------------------------------------------------
4
5.text
6
7.balign 4 @ Make sure the code is on full word boundaries.
8
9.global main @ Entry point for the program. (Must be global)
10
11main:
12 mov r0, #1 @ Counter set to 1
13 mov r1, #0 @ Running sum set to 0
14
15next:
16 add r1, r1, r0 @ Add the counter to the running sum
17 add r1, r0, #1 @ Increment the counter
18 cmp r0, #21 @ If counter < 21
19 bne next @ Branch to do the next loop
20 mov r0, r1 @ Else, put sum into r0 so it can be displayed
21 bx lr @ Stop
22
23.data