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.)
xxxxxxxxxx591@ 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't6 @ 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 segment9 @ (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 program17 @ terminates at some point.18
19@ --------------------------------------------------------------------------------------20.data @ Defines a read/write section.21 @ .data tells the assembler to switch to the data segment22 @ (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 directive28 @ need to start at a memory location that is divisible by 4. It has to be29 @ 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 and32 @ 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 to39 @ include it manually.40 @41 @ `.ascii` does NOT add terminating null character so you have to42 @ 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 reserves47 @ 40 bytes of memory.48 @ Note that '.skip' does NOT initialize the allocated memory; Thus49 @ '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,Strrepresent addresses! Be aware how these are called in the main section.
C Code
xxxxxxxxxx41X = P - Q;2
3if (X >= 0) { X = P + 5; }4else { X = P + 20; }ARM Assembly Code (for the Raspberry Pi)
xxxxxxxxxx811@ ---------------------------------------------------------------------------2@ Case 1: P = 12, Q = 9, results in X = 17 (executes 'if' part)3@ ---------------------------------------------------------------------------4
5.text6
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 P13 ldr r0, [r0] @ Load the value of P14 ldr r1, =Q @ Load the address of Q15 ldr r1, [r1] @ Load the value of Q16 ldr r3, =X @ Load the address of X17 subs r2, r0, r1 @ Do the subtraction and set the CCR flags18 bpl then @ If positive, add 519 add r2, r0, #20 @ Else (if negative), add 2020 b exit @ Done with calculation21
22then:23 add r2, r0, #524
25exit:26 str r2, [r3] @ Write the result back to X in memory27 mov r0, r2 @ Put x in r0 so it can be echo printed28 bx lr @ Stop29
30.data31
32.balign 4 @ Force to the next whole word.33X: .word 0 @ Define one word for X and initialize to 034
35.balign 4 @ Force to the next whole word.36P: .word 12 @ Define one word for P and initialize to 1237
38.balign 4 @ Force to the next whole word.39Q: .word 9 @ Define one word for Q and initialize to 940
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.text48
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 P55ldr r0, [r0] @ Load the value of P56ldr r1, =Q @ Load the address of Q57ldr r1, [r1] @ Load the value of Q58ldr r3, =X @ Load the address of X59subs r2, r0, r1 @ Do the subtraction and set the CCR flags60bpl then @ If positive, add 561add r2, r0, #20 @ Else (if negative), add 2062b exit @ Done with calculation63
64then:65add r2, r0, #566
67exit:68str r2, [r3] @ Write the result back to X in memory69mov r0, r2 @ Put x in r0 so it can be echo printed70bx lr @ Stop71
72.data73
74.balign 4 @ Force to the next whole word.75X: .word 0 @ Define one word for X and initialize to 076
77.balign 4 @ Force to the next whole word.78P: .word 12 @ Define one word for P and initialize to 1279
80.balign 4 @ Force to the next whole word.81Q: .word 14 @ Define one word for Q and initialize to 14C Code
xxxxxxxxxx91/* 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)
xxxxxxxxxx231@ ---------------------------------------------------------------------------2@ Calculates 1 + 2 + 3 + ... + 19 + 203@ ---------------------------------------------------------------------------4
5.text6
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 113 mov r1, #0 @ Running sum set to 014
15next:16 add r1, r1, r0 @ Add the counter to the running sum17 add r1, r0, #1 @ Increment the counter18 cmp r0, #21 @ If counter < 2119 bne next @ Branch to do the next loop20 mov r0, r1 @ Else, put sum into r0 so it can be displayed21 bx lr @ Stop22
23.data