Getting Started with the Tiva Launchpad Board Part 3 Intro to Assembly
2023-09-01 | By Sarah Aman
License: Attribution Launchpad
As we continue with this series on the Tiva Launchpad board, we will talk about Assembly Language programming with the Tiva Launchpad board. To get started we’ll talk about the basic Assembly Language commands we will use. First, to write comments, we will use “;”. The semicolon in assembly works just like the “//” operation does in C Language programming. In assembly language, a pound sign “#” is used in front of any number that is not stored in a variable or register. This is used whether the number is decimal or hexadecimal. When programming in assembly language, we will store values in one of the built-in 16-bit registers that are used with the ARM processor. These are referred to as R with a number following (R1). The First command is ADD. ADD is used to perform addition operations that are unsigned. ADDS is used to perform addition operations that are signed. Here are some examples of how to use the ADD and ADDS operations.
- ADD R1, #0x6 ;R1 = R1 + 0x6
- ADD R4, R2 ; R4 = R4 + R2
- ADD R6, R5, R3 ; R6 = R5 + R3
- ADDS R6, R5, R3 ; R6 = R5 + R3 (the flags are also updated)
If you want to learn more about how your microcontroller performs signed and unsigned operations along with setting flags, I recommend doing more in depth research on the ARM microprocessor architecture. However, you don’t necessarily need to understand how this microcontroller is designed to work with it.
The next instruction is the MOV instruction. The MOV instruction is used for simple data transfers within the processor. This is illustrated with an immediate data value transferred to a register. Another data movement operation may be required to transfer the contents of one register to another register. Here are some Examples of how to use the MOV instruction.
- MOV R2, #0x123 ; move immediate value of 0x123 to R2
- MOV R4, #’A’ ; move ASCII value of A (0x41) to R4
- MOV R7, R3 ; move the contents of R3 to R7
While the MOV instruction is used to transfer data within the processor, the LDR and STR instructions allow data to be transferred between processor and memory. The load register (LDR) instruction is used to transfer data from memory to the processor register. The STR instruction is used to transfer data from the processor to memory. Here are some examples of how to use the LDR and STR instructions.
- LDR R2, [R1] ; load R2 with data from memory with the data pointed to by R1 (This is known as indirect addressing)
- LDR R0, =NUM1 ; load R0 with the value of NUM1 which is a constant
- STR R4, [R3] ; store R4 to a memory location addressed by R3
With the LDR and STR instructions it is common to use indirect addressing. Using [R1] instead of just using R1 retrieves the data from the memory address specified by the value of register R1.
The last set of basic assembly language instructions are unconditional and conditional branching. A conditional branch performs a comparison in addition to branching. The instruction B means unconditional branch. The CBZ instruction means compare, and branch if equal to zero. CBNZ means compare and branch if not equal to zero. These two instructions CBZ and CBNZ only branch if the condition is met. Otherwise, the program will just move to the next line of code. Here is an example of how to use these instructions.
- loop1 LDR R2, [R1] ; load R2 with data from memory pointed to by register R1
- …
- CBZ R5, label1 ; Compare R5 with zero. If the comparison result is true then branch to label1
- CBNZ R6, label2 ; Compare R6 with zero. If the comparison result is false then branch to label2
- …
- B loop1 ; jump to the memory location labeled as loop
- Label1
- MOV R3, #0x034
- Label2
- MOV R3, 0x00
Now that we have some familiarity with Assembly Language for an ARM microprocessor, we can write a program using what we have learned. To practice, we are going to write a program that adds five even numbers. Since the first five even numbers are 2, 4, 6, 8, and 10 we will add those. The sum of these numbers is 30. Creating an Assembly language project in Kiel is very similar to creating a C language project, however when selecting a file type to add to “Source Group 1” add a .s file instead of a .c file.
Here is the program we will be creating.
The first set of instructions are necessary for the compiler to process the Assembly Language file.
THUMB ; Marks the THUMB mode of operation
StackSize EQU 0x00000200 ; Define stack size of 1024 bytes
AREA STACK, NOINIT, READWRITE, ALIGN=3;
MyStackMem SPACE StackSize
AREA RESET, READONLY
EXPORT __Vectors
__Vectors
DCD MyStackMem + StackSize ; stack pointer for empty stack
DCD Reset_Handler ; reset vector
AREA MYCODE, CODE, READONLY
ENTRY
EXPORT Reset_Handler
Reset_Handler
MOV R0, #0 ; Initial value of sum
MOV R1, #2 ; First even number
MOV R2, #5 ; Counter for the loop iterations
The main section of the program contains all of the executable code.
lbegin
CBZ R2, lend ; If R2 != 0 continue with the next
; instruction
ADD R0, R1
ADD R1, #2
SUB R2, #1
B lbegin ; branch unconditionally to lbegin
lend
The end command is used to signify the end of the program.
END
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum