Solution 6 Iterative Constructsv.5
Solution 6 Iterative Constructsv.5
a) Until_L, IF (X>0)
UNTIL (X > 0)
THEN Goto Until_End; DO
ELSE A; A;
Goto Until_L; OD;
FI;
Until_End,
Or alternatively:
Until_L, IF (X≤0)
THEN A;
Goto Until_L;
FI;
b) Do_L, A;
IF (X>0) DO
THEN Goto Do_End; A;
ELSE Goto Do_L; UNTIL (X > 0);
FI;
Do_End;
Or alternatively:
Do_L, A;
IF (X≤0)
THEN Goto Do_L;
FI;
c) While_L, IF (X > 0)
THEN A; WHILE (X > 0)
Goto While_L; DO
FI; A;
OD;
d) Do_L, A; DO
IF (X > 0) A;
THEN Goto Do_L;
FI; WHILE (X > 0);
1. Transform the following FOR iterative construct into its equivalent WHILE construct:
Number = 0; Max = 9;
FOR (Counter = 0; Counter ≤ Max; Counter++)
DO
A;
OD;
Number = 0; Max = 9;
Counter = 0;
WHILE (Counter ≤ Max)
DO
A;
Counter++;
OD;
Number = 0; Max = 9;
Counter = 0;
While_L, IF (Counter ≤ Max)
THEN
A;
Counter++;
Goto While_L;
FI;
Number = 0; Max = 9;
Counter = 0;
While_L, IF (Counter > Max)
THEN Goto Od_L;
ELSE
A;
Counter++;
Goto While_L;
FI;
Od_L;
In your Mano CPU simulator – press the Button 'Keyboard/Screen CH0'. Two windows
should pop up on your screen. Quit the one simulating a Keyboard, leaving the virtual Screen
open for the exercise.
a) int Size = 3;
int Counter = Size;
WHILE (Counter> 0)
DO
printf(Counter);
Counter--;
OD;
b) int Size = 3;
int Counter = Size;
WHILE (Counter ≥ 0)
DO
printf(Counter);
OD;
Counter-; See the slides of Presentation 6
c) int Size = 3;
int Counter = Size;
DO
printf(Counter);
Counter--;
UNTIL (Counter<0);
d) int Size = 3;
int Counter = Size;
DO
printf(Counter);
Counter--;
UNTIL (Counter ≤ 0);
a. Single length arithmetic – you may assume that the two numbers are small enough so that
you do not need to store the result R as double-length integers (maximum value of the
numbers is 255 i.e. FF). (Figure 1 a)
We can use the CIR operation with the End-carry(E) to get the bits of the Multiplier one after the other.
Version 2
// Bits = 8; DigitCount = 0;
// MultResult = 0;
// WHILE (DigitCount < Bits)
// DO
// E = shift_right(multiplier); // use CIR
// IF (E==1)
// THEN MultResult = MultResult + multiplicand;
// FI;
// Shift(multiplicand) 1 place to left; // use CIL
// DigitCount++;
// END;
Please note: The FOR loop can also be implemented as a WHILE loop that tests for either the
Multiplier or the Multiplicand being zero.