Labsheet 4 Dec30043

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

ELECTRICAL ENGINEERING DEPARTMENT

COURSE DEC 30043 MICROPROCESSOR FUNDAMENTALS

PROGRAMME DTK 3

PRACTICAL WORK 4

TOPIC : ASSEMBLY LANGUAGE : FLAGS IN APSR

OBJECTIVES : Upon completion of the lab activites, students should be able to

1. Apply assembly-level structured programming techniques using flag condition and


branches.
2. Apply assembly-level structured programming techniques using compare and
branch.

EQUIPMENT : Personal Computer with ARM Keil µVision Microcontroller Development Kit (MDK)
software

INSTRUCTIONS:

This lab activity should be done in two weeks.

Week 1: Activity 1 – Activity 2 (evaluated as practical assessment – 40 MARKS)

Week 2: Activity 3 – Activity 4 (evaluated as practical assessment – 40 MARKS)

Answer all questions at the end of the lab activity.

THEORY :

FLAG CONDITION
CMP and CMN

Compare and Compare Negative.

Syntax

CMP{cond} Rn, Operand2

CMN{cond} Rn, Operand2

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.

COMPARE AND BRANCH

Page 3 of 15
PROCEDURE:

PART A:

Activity A-1: BRANCH EQUAL

1. Write the programming code below.

; Always Start with Vector Table


; Vector Table Mapped to Address 0 at Reset
AREA RESET, DATA, READONLY

__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

GCD CMP R0, R1 Calculation: (1 mark)

Observation: (1 mark)

xPSR (MSB) = xPSR (MSB)=

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)

BLT LESS xPSR (MSB) =

N Z C V

Observation: (1 mark)

SUBS R0, R0, R1 Calculation: (1 mark)

Observation: (1 mark)

xPSR (MSB) = xPSR (MSB)=

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)

xPSR (MSB) = xPSR (MSB) =


( 1 mark)
N Z C V N Z C V

R0= R0=
R1= R1=
BEQ END xPSR (MSB) =

N Z C V

Observation: (1 mark)

BLT LESS xPSR (MSB)=

N Z C V

Observation: (1 mark)

Next instruction: Calculation: (1 mark)

________________

xPSR (MSB) = xPSR (MSB) =

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)

xPSR (MSB) = xPSR (MSB) =


(1 mark)
N Z C V N Z C V

R0= R0=
R1= R1=
BEQ END xPSR (MSB)=

N Z C V
0 0 1 0

Observation: (1 mark)

BLT LESS xPSR (MSB)=

N Z C V
0 0 1 0

Observation: (1 mark)

SUBS R0, R0, R1 Calculation: (1 mark)

Observation: (1 mark)

xPSR (MSB) = xPSR (MSB)=


(1 mark)
N Z C V N Z C V

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)

xPSR (MSB) = xPSR (MSB) =

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)

(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.

; Always Start with Vector Table


; Vector Table Mapped to Address 0 at Reset

AREA RESET, DATA, READONLY

__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

Figure 2: Assembly code for “activity2.s”

2. Execute 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=

LDR R1, =SUM R1= R1=

LDR R2, =1 R2= R2=


MYLOOP Calculation:
ADD R1, R2, R1

R1= R1=
ADDS R2, R2, #1 Calculation:

xPSR (MSB) = xPSR (MSB)=

N Z C V N Z C V

Observation: (2 marks)

R2= R2=

SUBS R4, R0, R2 Calculation: (1 mark)

xPSR (MSB) = xPSR (MSB)=

N Z C V N Z C V

Observation: (2 marks)

R4= 0x0000 0000 R4= 0x0000 0008

BNE MYLOOP xPSR (MSB)=

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)

4. What is the value of xPSR when BNE MYLOOP is executed?


xPSR =

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. Explain the comparison between BGT and BLT instruction.

____________________________________________________________________________________

____________________________________________________________________________________

____________________________________________________________________________________

____________________________________________________________________________________

(2 marks)

CONCLUSION

____________________________________________________________________________________________

____________________________________________________________________________________________

____________________________________________________________________________________________

____________________________________________________________________________________________

____________________________________________________________________________________________

____________________________________________________________________________________________

____________________________________________________________________________________________

___________________________________________________________________________________

(5 marks)

Page 14 of 15
Practical Work 4 Marks Form

No Activity Marks

1 Activiti A-1 Observation /30

2 Activiti A-1 Flowchart /10

3 Activiti B-1 Observation /5

4 Activiti B-1 Questions /6

5 Activiti B-2 Flowchart /10

6 Question /4

7 Conclusion /5

TOTAL /70

/20

~END OF LAB SHEET 4~

Page 15 of 15

You might also like