Programming Loops in Computer Systems: Types, Mechanisms, and Implementation, Study notes of Microcomputers

An in-depth exploration of programming loops, their types (finite and infinite), and looping mechanisms (do, for, while, and repeat) in the context of computer systems. The implementation of these loops using conditional branch instructions and the condition code register (ccr).

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-xov
koofers-user-xov 🇺🇸

9 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ECE 3120
Computer Systems
Programming Loops
Manjeera Jeedigunta
http://blogs.cae.tntech.edu/msjeedigun21
Tel: 931-372-6181, Prescott Hall 120
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Programming Loops in Computer Systems: Types, Mechanisms, and Implementation and more Study notes Microcomputers in PDF only on Docsity!

ECE 3120

Computer Systems

Programming Loops

Manjeera Jeedigunta

http://blogs.cae.tntech.edu/msjeedigun

Email: [email protected]

Tel: 931-372-6181, Prescott Hall 120

Prev: „

Write programs to do arithmetic

Today: „

Loops

Do Statement S forever

Infinite loop

Possible to add “If C then exit”

Figure 2.4 An infinite loop

S

For i=n1 to n2 Do S

i = loop counter

S = Statement

1) Initialize the loop counter

2) Compare the loop counter with the limit

3) Perform the operations in S if loop counter within the limit

4) Increment the loop counter ‘i’ and go to step 2

While C Do S

Logical expression C is evaluated „

Only while C if true, S will be executed

C
S
true
false
Figure
T
he
W
hile
... Do
looping
construct

1) Initialize the logical expression C

2) Evaluate the logical expression C

3) If C is true perform the functions specified by S,

go back to 2, if not exit

4) Exit

Repeat S until C

Figure 2.7 The Repeat ... Until looping construct

initialize C

S C

true

false

1) Initialize loop counter

2) Execute statement S

3) Evaluate C, if true go to step 2, else exit

Unary (unconditional) branch:

always execute

Simple branches:

branch is taken when a specific bit of CCR is in a specific

status

Unsigned branches:

branches are taken when a comparison or test of unsigned

numbers results in a specific combination of CCR bits

Signed branches:

branches are taken when a comparison or test of signed

quantities results in a specific combination of CCR bits

Three categories

of Branches

Short Branches:

in the range of -128 ~ +127 bytes

Long Branches:

in the range of 64KB

bit-conditional branches

Branch Instructions

Four types of branch instructions:

T a b l e 2 .2 S u m m a r y o f s h o r t b r a n c h i n s t r u c t i o n s

M n e m o n i c

F u n c t i o n

U n a r y B r a n c h e s

B R A B R N

B r a n c h a l w a y sB r a n c h n e ve r

E q u a t i o n o r O p e r a t i o n

S i m p l e B r a n c h e s

M n e m o n i c

F u n c t i o n

B C C

B C S B E Q

B M I B N E

B P L B V C

B V S

B r a n c h i f c a r r y c l e a rB r a n c h i f c a r r y s e tB r a n c h i f e q u a lB r a n c h i f m i n u sB r a n c h i f n o t e q u a lB r a n c h i f p l u sB r a n c h i f o ve r f l o w c l e a rB r a n c h i f o ve r f l o w s e t

1 = 11 = 0

U n s i g n e d B r a n c h e s

M n e m o n i c

F u n c t i o n

B H I

B H SB L O

B L S

B r a n c h i f h i g h e rB r a n c h i f h i g h e r o r s a m eB r a n c h i f l o w e rB r a n c h i f l o w e r o r s a m e

C = 0C = 1Z = 1

N

= 1

Z = 0

N

= 0

V = 0V = 1

E q u a t i o n o r O p e r a t i o n E q u a t i o n o r O p e r a t i o n

C +

Z =

0

C = 0C = 1

C +

Z =

1

M n e m o n i c

F u n c t i o n

E q u a t i o n o r O p e r a t i o n

S i g n e d B r a n c h e s

B G EB G T

B L EB L T

B r a n c h i f g r e a t e r t h a n o r e q u a lB r a n c h i f g r e a t e r t h a nB r a n c h i f l e s s t h a n o r e q u a lB r a n c h i f l e s s t h a n

N

V =

0

Z + ( N

V ) =

0

Z + ( N

V ) =

1

N

V =

1

Compare and Test Instructions -

Condition flags need to be set up before conditional branch instruction should

be executed.

The 68HCS12 provides a group of instructions for testing the condition flags.

Table 2.4 Summary of compare and test instructions

Mnemonic

Function

Compare instructions

CBA

CMPACMPB

CPD

CPS

CPX

CPY

Compare A to BCompare A to memoryCompare B to memoryCompare D to memoryCompare SP to memoryCompare X to memoryCompare Y to memory

Operation

(A) - (B) (A) - (M)(B) - (M)

(D) - (M:M+1)

(SP) - (M:M+1)

(X) - (M:M+1)(Y) - (M:M+1)

Test instructions

Mnemonic

Function

TST

TSTATSTB

Test memory for zero or minusTest A for zero or minusTest B for zero or minus

Operation(M) - $

(A) - $00(B) - $

Decrementing & Incrementing Instructions

DEC, DECA,DECB,DES,DEX,DEY

INC,INCA,INCB,INS,INX,INY

ldaa i

adda #

staa i

Loop Primitive Instructions-

68HCS12 provides a group of instructions that either decrement or increment aloop count to determine if the looping should be continued.

The range of the branch is from $80 (-128) to $7F (+127).

Table 2.5 Summary of loop primitive instructions

Mnemonic

Function

DBEQ cntr, relDBNE cntr, rel

IBEQ cntr, relIBNE cntr, rel

TBEQ cntr, relTBNE cntr, rel

Decrement counter and branch if = 0(counter = A, B, D, X, Y, or SP)Decrement counter and branch if

0

(counter = A, B, D, X, Y, or SP)Increment counter and branch if = 0(counter = A, B, D, X, Y, or SP)Increment counter and branch if

0

(counter = A, B, D, X, Y, or SP)Test counter and branch if = 0(counter = A, B, D, X, Y, or SP)Test counter and branch if

0

(counter = A, B, D, X, Y, or SP)

Equation or Operation

counter

(counter) - 1

If (counter) = 0, then branchelse continue to next instructioncounter

(counter) - 1

If (counter)

0, then branch

else continue to next instructioncounter

(counter) + 1

If (counter) = 0, then branchelse continue to next instructioncounter

(counter) + 1

If (counter)

0, then branch

else continue to next instructionIf (counter) = 0, then branchelse continue to next instructionIf (counter)

0, then branch

else continue to next instruction

Note. 1.

cntr

is the loop counter and can be accumulator A, B, or D and register X, Y, or SP.

rel

is the relative branch offset and is usually a label

Example 2.15’

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

N 8-bit elements using the

repeat S until C

looping construct.

N
equ
org
max_val
ds.b
org
ldaa
array
; set array[0] as the temporary max max
staa
max_val
ldx
#array+N-1 ; start from the end of the array
ldab
#N-
; set loop count to N - 1
loop
ldaa
max_val
cmpa
0,x
bge
chk_end
ldaa
0,x
staa
max_val
chk_end
dexdbne
b,loop
; finish all the comparison yet?
forever
bra
forever
array
db
db
end

Instructions for Variable Initialization 1.

[

opr

[]

where

opr

is specified using the

extended

or

index

addressing modes. The

specified memory location (1 bye) is cleared.

[

[

[]

Accumulator B is cleared to 0

Next…

Shift & Rotation

Read Chapter 2.