












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













Prev:
Write programs to do arithmetic
Today:
Loops
Do Statement S forever
Infinite loop
Possible to add “If C then exit”
For i=n1 to n2 Do S
i = loop counter
S = Statement
While C Do S
Logical expression C is evaluated
Only while C if true, S will be executed
Repeat S until C
Figure 2.7 The Repeat ... Until looping construct
initialize C
S C
true
false
Three categories
of 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 -
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-
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
Instructions for Variable Initialization 1.
Next…
Shift & Rotation
Read Chapter 2.