Labsheet 4 Dec30043
Labsheet 4 Dec30043
Labsheet 4 Dec30043
PROGRAMME DTK 3
PRACTICAL WORK 4
EQUIPMENT : Personal Computer with ARM Keil µVision Microcontroller Development Kit (MDK)
software
INSTRUCTIONS:
THEORY :
FLAG CONDITION
CMP and CMN
Syntax
where:
cond
is an optional condition code (see Conditional execution).
Rn
is the ARM register holding the first operand.
Operand2
is a flexible second operand. See Flexible second operand for details of the options.
Usage
These instructions compare the value in a register with Operand2. They update the condition flags on
the result, but do not place the result in any register.
Page 2 of 15
The CMP instruction subtracts the value of Operand2 from the value in Rn. This is the same as
a SUBS instruction, except that the result is discarded.
The CMN instruction adds the value of Operand2 to the value in Rn. This is the same as
an ADDS instruction, except that the result is discarded.
Page 3 of 15
PROCEDURE:
PART A:
__Vectors
DCD 0x20084000 ; stack pointer value when stack is empty
DCD START
ALIGN
; The program
AREA MYCODE, CODE, READONLY
ENTRY
START
;;;;;;;;;;User Code Starts from the next line;;;;;;;;;;;;
MOVS R0, #5 ; SET UP PARAMETERS
MOVS R1, #3
GCD CMP R0, R1
BEQ END
BLT LESS
SUBS R0, R0, R1 ; could be SUB R0, R0, R1 for arm
B GCD
LESS
SUBS R1, R1, R0 ; could be SUB R1, R1, R0 for arm
B GCD
END
Page 4 of 15
Activiti A-1 Observation
1. Observe changes in register value. Complete the table with the required answer.
Instruction value
Before After
MOVS R0, #5 R0= R0=
MOVS R1, #3 R1= R1=
1 LOOP
ST
Observation: (1 mark)
N Z C V N Z C V
(1 mark)
R0= R0=
R1= R1=
BEQ END xPSR (MSB)=
N Z C V
Observation: (1 mark)
N Z C V
Observation: (1 mark)
Observation: (1 mark)
N Z C V N Z C V
(1 mark)
R0= R0=
Page 5 of 15
B GCD xPSR (MSB) =
N Z C V
Observation: (1 mark)
2ND LOOP
GCD CMP R0, R1 Calculation: (1 mark)
Observation: (1 mark)
R0= R0=
R1= R1=
BEQ END xPSR (MSB) =
N Z C V
Observation: (1 mark)
N Z C V
Observation: (1 mark)
________________
N Z C V N Z C V
(1 mark)
R1= R1 =
Page 6 of 15
Next instruction: xPSR (MSB) =
_________ N Z C V
Observation: (1 mark)
3rd LOOP:
GCD CMP R0, R1 Calculation: (1 mark)
Observation: (1 mark)
R0= R0=
R1= R1=
BEQ END xPSR (MSB)=
N Z C V
0 0 1 0
Observation: (1 mark)
N Z C V
0 0 1 0
Observation: (1 mark)
Observation: (1 mark)
R0= R0=
Page 7 of 15
B GCD xPSR (MSB)=
N Z C V
Observation: (1 mark)
4th LOOP
GCD CMP R0, R1 Calculation: (1 mark)
Observation: (1 mark)
N Z C V N Z C V
(1 mark)
R0= R0=
R1= R1=
N Z C V
Observation: (1 mark)
(30 marks)
Page 8 of 15
2. Draw a flowchart by referring the program above.
(10 marks)
Page 9 of 15
PART B
Activity B-1: BRANCH NOT EQUAL (BNE) INSTRUCTION
1. Below is a simple program by using BNE instruction that calculates the SUM as given in
formula below. Variable COUNT is saved in register R0 and initialized to 10. The SUM is
saved in R1.
__Vectors
DCD 0x20084000 ; stack pointer value when stack
is empty
DCD START
ALIGN
; The program
AREA MYCODE, CODE,READONLY
ENTRY
COUNT EQU 10
SUM EQU 0
START
LDR R0, =COUNT ; COUNT = 10
LDR R1, =SUM ; SUM = 0, initialized
LDR R2, =1 ; R2 stores initial value of i
MYLOOP
ADD R1, R2, R1 ; sum = i + sum
ADDS R2, R2, #1 ; increment i;
SUBS R4, R0, R2 ; R4 = R0 - R2 to check if COUNT is ZERO.
; if result is ZERO, Z Flag is set to 1
BNE MYLOOP ; if Z flag is not zero, repeat MYLOOP
ADD R1, R2, R1 ; make sure we also add the final COUNT value
STOP
B STOP
END ; End of the program
Page 10 of 15
Activiti B-1 Observation
1. Observe changes in register value. Complete the table with the required answer.
Instructions values
Before After
LDR R0, =COUNT R0= R0=
R1= R1=
ADDS R2, R2, #1 Calculation:
N Z C V N Z C V
Observation: (2 marks)
R2= R2=
N Z C V N Z C V
Observation: (2 marks)
N Z C V
Observation: (2 marks)
Page 11 of 15
(5 marks)
2. Explain how the MYLOOP is repeated. Relate to the xPSR flag status value.
(3 marks)
3. How many times the MYLOOP is executed throughout the whole program?
(1 marks)
N Z C V
(1 marks)
5. What is the final value of the following register when the instruction BNE is skipped
xPSR =
N Z C V
(1 marks)
Page 12 of 15
6. Draw flowchart to explain the flow of the programme.
(10 marks)
Page 13 of 15
QUESTION
1. Explain the comparison between Branch Not equal (BNE) and Branch Equal (BEQ)
instruction.
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
(2 marks)
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
(2 marks)
CONCLUSION
____________________________________________________________________________________________
____________________________________________________________________________________________
____________________________________________________________________________________________
____________________________________________________________________________________________
____________________________________________________________________________________________
____________________________________________________________________________________________
____________________________________________________________________________________________
___________________________________________________________________________________
(5 marks)
Page 14 of 15
Practical Work 4 Marks Form
No Activity Marks
6 Question /4
7 Conclusion /5
TOTAL /70
/20
Page 15 of 15