IFs, LOOPs and DBRA
IF
Simple IF
a)HLL b) 68K
x:=x+1 ADDQ.W #1,X
IF A=7 THEN CMPI.W #7,A
B:=3; BNE NEXT
C:=4; MOVEQ #3,B
END IF MOVEQ #4,C
x:=X+2; NEXT:
ADDQ.W #2,X
b) At HLL
IF X=2 THEN CODE:=2;
CODE:=1; change to IF X=2 THEN
ELSE CODE:=1;
CODE:=2; END IF
END IF
This simplifies the code.
c) GENERAL LOOP
HLL 68K
IF (I>GT> 0) THEN CMPI.W #0,I
CODE(A) BLE ELSE
ELSE CODE(A)
CODE(B) BRA NEXT
END IF ELSE:
CODE(B)
NEXT NEXT:
d) I never want to see:
STUFF STUFF
BRA NEXT replace with NEXT:
NEXT:
You want to reduce the number of branches not put in superflous ones.
Loops
There are two types of loops in the world - REPEAT loops and WHILE loops.
Repeat Loops are always executed once which is a rare for loops. You must
always check yourself to see if it is safe to use a repeat loop. A while
loop can be executed zero times and is always safe and very common in
programming.
REPEAT Loops
a) Standard Repeat Loop
REPEAT: A read with a sentinel is
JSR DECIN suitable for a reat loop.
CMP.W '-1',d0
BNE REPEAT
b) General Loop
Loop:
CODE(A) Since CODE(A) always executed this
CMPI.W #0,I is a repeat loop.
BLT NEXT
CODE(B)
BRA LOOP
While Loop
a) OK VERSION b) Better Version
PREVIOUS CODE PREVIOUS CODE
Loop: BRA CHECK
CMP --- LOOP:
BEQ ENDLOOP CODE(C)
CODE(C) CHECK:
BRA LOOP CMP ---
ENDLOOP: BNE LOOP
NEXT CODE NEXT CODE
Some General Points about Loops
- Most loops are "top-tested" (like WHILE loops in Pascal or
C). This is correct most of the time.
- The loop condition should be tested before executing the body of the
loop for the first time, not after. This allows the body of the loop to
be executed 0 times, if that is appropriate. In some instances the
repeat-until loop is the loop of choice.
- In assembler language, a conditional branch will test the loop condition.
- This does not mean that the conditional branch should appear at the
start of the loop in assembler language, though. Usually, it should be
placed at the bottom, and there should be an unconditional branch to the
bottom of the loop to get it started. Occassionally, if the loop is
very long, this procedure is not followed.
- What's the difference?
- In the first one, there are two branches inside the loop.
- In the second one, there is only one branch inside the loop. The other
one is outside the loop where it is only done once, not every iteration.
- Particularly in today's pipelined processors, it is very important
to minimize the number of branches, and simplify loop structures.
The DBRA Instruction 68000k
- The syntax of the dbra (decrement and branch) instruction is
dbra Di,Addr
- This instruction decrements Di.w and branches to Addr if the result
is not -1.
- It is the equivalent of the following 3 instructions
sub.w #1,Di
cmp.w #-1,Di
bne Addr
- This is a very typical CISC instruction. In a program for a RISC machine,
you would use the three separate instructions.
- Its purpose is to make "count-controlled loops" easier. (That
is, if you know in advance exactly how many times a loop should execute,
use this instruction.)
- To execute a loop exactly N times, use this code:
move.w N,D2 ;Get N into a D reg, if not there already
bra EndL ;Start at the dbra instruction at the bottom
Loop: ...
...
EndL: dbra D2,Loop
- This is the "right" kind of loop, with only one branch in
it, at the bottom. The loop is entered by branching to the "dbra"
at the bottom.
- This also explains why the dbra instruction checks for -1 and not 0.
- If you want to use the values in the register , they are
N-1,N-2,...1,0.
- Here are two incorrect ways to do it:
move.w N,D1
Loop: ...
...
dbra Loop
- The above loop executes N+1 times, not N times.
- OK, so how about decrementing N first to fix that problem?
move.w N,D1
sub.w #1,D1
Loop: ...
...
dbra Loop
- The above loop contains a huge bug. If N=0 the loop will not
be executed 0 times (which is correct). Instead, it will be executed 65536
times!! (The first time through, the "dbra" will decrement -1
and get -2. This is not -1 so it will keep going. The value in D1 will
have to count all the way down to -32768, overflow up to +32767, and continue
down until it finally reaches -1 the hard way.)
- Beware of quick fixes!!