Tutorial 2: ARM addressing mode, ARM branches¶
Note
This is for practice, not for marks. Treat this as additional study material to help you develop your knowledge and skills on these topics. During the tutorial session, the TA will explain how to solve these problems.
For this tutorial, we will use basic RISC operations as seen in the Lecture 2 (Instruction set architecture), slides 1–55.
Question 1¶
Write an ARM assembly program that compares the value stored in R1, with number 2 and 3 respectively. The program should increment the number stored in R1 by 1 if R1 is greater than 3, decrement the number stored in R1 by 1 if the content of R1 is less than 2.
Question 2¶
The following is a C-like pseudo code that will check which of the initial number is bigger
int main(){
int gn =0;
int x = 4;
int y=8;
if(x<y)
{
gn = y;
}
else{
gn=x;
}
return gn;
}
Translate the following code into ARM assembly code that will use both conditional and unconditional branches to find the greatest number.
Question 3¶
Track the values of R0 and R1 after each of the instructions in the code fragment below. (show only the right most six bits of each register and write ?? in any slot where the value is unknown) Describe what happens to the values in R0 and R1 as a result of the three EOR instructions
.global _start
_start:
MOV R0, #11
MOV R1, #13
EOR R0, R0, R1
EOR R1, R0, R1
EOR R0, R0, R1
.end
Question 4¶
In each of the following instructions, determine the effective memory address (in terms of R2,R3) the content of which will be stored in R1. Note that all the instructions are independent.
Instruction 1
LDR R1 [R2, #4]
Instruction 2
LDR R1 [R2,R3]
Instruction 3
LDR R1 [R2,R3,LSL #3]
Instruction 4
LDR R1 [R2,R3, LSL #3]!
Question 5¶
Consider the following ARM assembly code and report the final value of the Register R0. Explain what does the program achieve?
.global _start
_start:
MOV R0, #10
MOV R1, #12
fn1:
CMP R0,R1
BEQ end
BLT fn2
SUBS R0, R0, R1
B fn1
fn2:
SUBS R1,R1,R0
B fn1
.end
Question 6¶
Consider the 4 byte array defined by arr=[3, 6, 4, 7]. Identify the content of the out array after the program has finished executing.
.global _start
arr: .word 3, 6, 4, 7
out: .space 16
_start:
LDR R0, =arr
LDR R1, =out
ADD R0, R0, #12
MOV R2, #4
LOOP:
LDR R3, [R0]
STR R3, [R1], #4
SUB R0, R0, #4
SUB R2, R2, #1
CMP R2, #0
BNE LOOP
.end