DOWHILE Loop General Form - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

DOWHILE Loop General Form

Description:

1BA3 G Lacey Lecture 16. 2. Example: Multiply 2 Numbers (in $2000 ... Need some way to test the value in $2002 first and skip the DO-WHILE loop if value=0 ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 14
Provided by: isservice
Category:
Tags: dowhile | form | general | gerard | loop | way

less

Transcript and Presenter's Notes

Title: DOWHILE Loop General Form


1
DO-WHILE Loop General Form
2
Example Multiply 2 Numbers (in 2000 and 2002)
3
Which 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

4
How 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

5
IF THEN ELSE General Form
6
Example
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
7
Assembly Implementation
8
Fix the problem with the DO-WHILE loop
Loop only executed if count is not 0
9
Example 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

10
Solution 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.

11
Pseudo 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
12
Convert 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
13
Note
  • 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.
Write a Comment
User Comments (0)
About PowerShow.com