Docsity

Branch Instructions, Coding Examples, BRSET, Rotate Instructions | ECE 251, Study notes of Microprocessors

Partial preview of the text

Download Branch Instructions, Coding Examples, BRSET, Rotate Instructions | ECE 251 and more Study notes Microprocessors in PDF only on Docsity!

Lecture #

ECE251: Tuesday September 16 • Branch Instructions-continued• Several more coding examples• BRSET/BRCLR• Shift/Rotate instructions• Labs: #3 due this week. Do #4 this week; due nextweek.• Homework: #2 due Thursday.

Lecture #

Flowchart Example: Repeat–Until Flow Structure

Assembly Code

ORG^

LEN^

DS.B

;Length of Table

Table

DS.B

ORG^

LDX^

#Table CLRA L^

ASR 1,X+

;Table(i)=Table(i)/

;^

& increment pointer

INCA

; Increment counter

CPA LEN

; Done?

BLE^

L^

;If not, do again

{Continue processing}

Finishedwith table?

No

Yes

Table(i)=Table(i)/2Increment Counters

Problem: Divide each element in table

by 2, using shift-right inst.

Lecture #

Turn a sequence of ASCII digits into a number^ Complete (small) Flowchart Example:^4

Begin

from buffer^ Read Char

Digit? No

N=Char-$

from buffer^ Read Char

Yes

Digit? No

+(Char-$30)^ N=N*

Yes Done

Lecture #

Repeat Until Looping

Example 2.

Write a program to find the maximum element from an array of N 8-

bit elements using the

repeat S until C

looping construct. Start

max_val

←^ Array[N]←^ i N- max_val < array[i]?

yes

no

←^ i i - 1 i = 0?^ yes Stop

max_val no

←^ Array[i]

Lecture #

.lst file Generated

C:\Miniide\Untitled1.lst

  • generated

by MGTEK Assembler ASM

V1.22 Build 137 for WIN

(x86) -

Tue Sep

11 08:55:19 20071:

=

N^

equ^

20

2:^

=

org^

$

3:^0800

max_val

ds.b

1

4:^ 0801 01

03 05 06

13 29

array

dc.b

1,3,5,6,19,41,53,28,13,42,76,

0807 35

1C 0D 2A

4C 0E

5:^ 080D 14

36 40 4A

1D 21

dc.b^

20,54,64,74,29,33,41,

0813 29

2D 6:^

=

org^

$

7:^ 1000 B

0801

ldaa^

array

; set array[0] as

the^

temporary max

val

8:^ 1003 7A

0800

staa^

max_val

;^

ā€œ

9:^ 1006 CE

0814

ldx^

#array+N-

;^ start from the end

of the

array

10:^

1009 C

13

ldab^

#N-^

; set loop

count

to N

  • 1

11:^

100B B

0800

loop^

ldaa^

max_val

12:^

100E A

00

cmpa^

0,x

13:^

1010 2C

05

bge^

chk_end

14:^

1012 A

00

ldaa^

0,x

15:^

1014 7A

0800

staa^

max_val

16:^

1017 09

chk_end

dex

17:^

1018 04

31 F

dbne^

b,loop

; finish

all the

comparison

yet?

18:^

101B 20

FE^

forever

bra^

forever

; Better

to use swi.

Why?

19:^

end

Symbols:array

chk_end

Forever

*0000101b Loop^

*0000100b max_val

N^

Lecture #

BRSET & BRCLR

-^ These instructions combine 1) a mask operation,2) a compare operation, and 3) a conditionalbranch operation.^ BRCLR

mem

mask

rel_addr

  • Branches to a target location if all

(mem) bits

that are 1 in the mask

are cleared

BRSET

mem

mask

rel_addr

  • Branches to a target location if all

(mem) bits

that are 1 in the mask

are set

Lecture #7 10

BRSET

Example

-^ Suppose Data is memory location $3000 andthe content of this location is $4B. We execute:^ BRSET

Data

%

Exit

Does the branch to

Exit

take place?

(Data) = $4B = 0100 10110100 1011 • 0110 0000 = 01

0 0 0000

≠≠≠≠^

01^1

0 0000

Therefore branch is NOT taken.Give an example of (Data) for which branch willoccur.

Lecture #7 11

Use

BRCLR

in looping example

Example 2.

Write a program to compute the number of elements that are divisible by 4 in an array of N 8-bit elements. Use the

repeat S until C

looping construct.

Solution:

A number divisible by 4 would have the least significant two bits equal 0s. Use

BRCLR

to test both bits at the same time.^ N

equ^

org^

; Data Area

total

ds.b

array

dc.b

dc.b

org^

; Program area

clr^

total

; initialize total to 0

ldx^

#array ldab

#N

; use B as the loop count

loop

brclr

0,x,%00000011,yes

; check bits 1 and 0

bra^

chkend

; No, don’t include in total

yes^

inc^

total

; Yes, include in total

chkend

inxdbne

b,loop

; Done?

If not loop back.

swi^

; Done.

end

Shift and Rotate Instructions

The 68HC12 has shift and rotate instructions that apply to a memory location,accumulators A, B and D. If a memory operand is specified, the extended or indexaddressing mode must be used..There are three 8-bit arithmetic shift left instructions:

ASL^

opr^

-- memory location

opr^

is shifted left one place

ASLA

-- accumulator A is shifted left one place

ASLB

-- accumulator B is shifted left one place

The operation is:

C^

b7 ------------------------ b

The 68HC12 has arithmetic shift right instructions that apply to a memory location andaccumulators A and B.

ASR^

opr^

-- memory location

opr^

is shifted right one place

ASRA

-- accumulator A is shifted right one place

ASRB

-- accumulator B is shifted right one place

The operation is:

C

b7 ------------------------ b

The 68HC12 has one 16-bit arithmetic shift left instruction:

ASLD

The operation is:

C^

a7 ------------------------ a

b7 ------------------------ b

accumulator A

accumulator B

The 68HC12 has three

logical shift right

instructions that apply to 8-bit operands.

LSR^

opr^

-- memory location

opr^

is shifted right one place

LSRA

-- accumulator A is shifted right one place

LSRB

-- accumulator B is shifted right one place

The operation is

C

b7 ------------------------ b 0

The 68HC12 has one

16-bit logical shift right

instruction:

LSRD

The operation is:

C

0

a7 ------------------------- a

b7 ----------------------- b

accumulator A

accumulator B