Title: DOWHILE Loop General Form
1DO-WHILE Loop General Form
2Example Multiply 2 Numbers (in 2000 and 2002)
3Which program is correct and why?
- If the value in 2002 is 0
- -gt DO-WHILE is incorect
- The sub.w instruction is executed before the bne
test - Therefore the loop executes 65536 times, not 0
times - Important to decide which loop construct fits
your problem domain
4How would you fix the DO-WHILE version?
- Need some way to test the value in 2002 first
and skip the DO-WHILE loop if value0 - -gt We need Conditional Statements
5IF THEN ELSE General Form
6Example
If contents of 2000 is 0 then set 2002 to 0
otherwise set it to 1.
value (2000) if (value 0) result 0
else result 1 (2002) result
7Assembly Implementation
8Fix the problem with the DO-WHILE loop
Loop only executed if count is not 0
9Example Normalise a 16-bit Number
- -gt Bits are shifted left until MSB 1, shift
count stored as the exponent - 0001 0010 -gt 1001 0000, exponenet 11
10Solution in English
- Set exponent to 0.
- Load number to normalise.
- Shift the number to the left until MSB 1 (i.e
number is negative). - Increment exponent at each shift.
- Store number and exponent.
11Pseudo Code
WHILE loop or the DO-WHILE loop? Number might
already be negative (normalized) -gt execute loop
0 times -gt use WHILE loop.
exp 0 num (2000) while (MSB(Num) !
1) num num ltlt 1 exp exp 1 (2002)
num (2004) exp
12Convert to Assembly Language
How do we determine if the number is negative? -gt
Examine the N flag, bmi and bpl branch depending
on the value of the N flag
move.w 0,d0 exp 0 move.w 2000,d1
load num while bmi endwhile while(ve)
add.w 1,d0 exp1 lsl.w 1,d1
numltlt1 bra while endwhile
move.w d1,2002 store num move.w d0,2004
store exp
13Note
- Always ensure that the last instruction which
affects the CCR, prior to conditional branching,
is testing the appropriate value. - In the example above only 2 instructions which
affect the CCR execute before the bmi endwhile. - move.w 2000,d1
- lsl.w 1,d1
- Both of these generate CCR values based on the
current value of num. This is precisely the
behaviour we want.