0% found this document useful (0 votes)
13 views5 pages

Solution 6 Iterative Constructsv.5

Uploaded by

testphishingguy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Solution 6 Iterative Constructsv.5

Uploaded by

testphishingguy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Computer organisation and programming course, Shenkar College of Design & Engineering

Solution of Homework 6: Iterative (loop) constructs


Using the language λass (we discussed in class) that consists of the following constructs:
 IF(condition) THEN ELSE FI;
 IF(condition) THEN FI
 Goto label
 You can label any line of code. For example:
label, IF (condition) ...ELSE Goto label;
 Any conditionals like: < > == != ≥ ≤

Implement the following Iterative Constructs:

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);

12 April 2022 1 Dr Yigal Hoffner


Computer organisation and programming course, Shenkar College of Design & Engineering

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;

'WHILE DO' and 'DO UNTIL' iterative construct implementation exercises:


Use the following template in questions 3-6:

To print a value (between 0-9) that is held in the Accumulator (AC):


...
ADD ASCII_offset // This only works for values
Out_Loop, SKO // in the AC between 0 and 9 !
BUN Out_Loop //
OUT // printf(Counter);
...
// Data
Counter, DEC 0 // Temporary loop counter
Size, DEC 3
//
Minus1, DEC -1
ASCII_offset, HEX 30 // ASCII value added to integer

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.

12 April 2022 2 Dr Yigal Hoffner


Computer organisation and programming course, Shenkar College of Design & Engineering

2. Transform and implement the following in the Mano Assembly language:

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);

12 April 2022 3 Dr Yigal Hoffner


Computer organisation and programming course, Shenkar College of Design & Engineering

3. Transform, implement in Mano & test the FOR loop:


int Start = -2; int End = 7; // try different values but keep: End – Start <10
int Number = 0; // Number must be between 0 -9
FOR (int Count = Start; Count ≤ End; Count++)
DO
printf(Number);
Number++;
OD;
Note that you are asked to print the variable Number. As long as the loop is not executed more
than 10 times, Number can be printed, as it will change between 0 ≤ Number ≤ 9;
So as in this example, going from Start = -2; End = 7; Number = 0; will work.

Start = -2; End = 7; Number = 0;


FOR (Count = start; Counter ≤ End; Count++)
DO
printf(Number);
Number++;
OD;

//FOR (int Count = Start; Count≤End; Count++)


LDA Start // Start = -2; End = 7; Number = 0;
Count = start;
STA Count // Count=Start; WHILE (Count ≤ End)
CLA DO
STA Number // Number = 0; printf(Number);
While_Loop, LDA End // WHILE (Count ≤ End) Number++;
CMA // Count++;
INC // OD;
ADD Count //
SPA //
BUN LoopBody //
BUN LoopEnd // DO
LoopBody, LDA Number //
ADD ASCII_offset // Start = -2; End = 7; Number = 0;
Out_Loop, SKO // Count = start;
BUN Out_Loop // While_Loop, IF (Count ≤ End)
OUT // printf(Number); THEN
ISZ Number // Number++; printf(Number);
Number++;
LDA Count //
Count++;
INC //
Goto While_Loop;
STA Count // Count++;
FI;
BUN While_Loop // OD;
LoopEnd, HLT //

// Data Start = -2; End = 7; Number = 0;


Start, DEC -2 // Count = start;
End, DEC 7 // While_Loop, IF (Count > End)
Count, DEC 0 // temporary loop counter
ASCII_offset, HEX 30 // ASCII value added to integer THEN Goto OD_Loop;
Number, DEC 0 ELSE
END printf(Number);
Number++;
Count++;
Goto While_Loop;
FI;
OD_Loop;

4. Straightforward, slow Multiplication: R = X * Y;


To be done… by me…

12 April 2022 4 Dr Yigal Hoffner


Computer organisation and programming course, Shenkar College of Design & Engineering

5. Multiplication: R = X · Y i.e. Result = Multiplicand x Multiplier


Write a program that multiplies two short unsigned integers and stores the result in a third integer
variable called R.
Note that multiplying two n bit numbers results in a number with 2n bits!

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)

Figure 1: Possible memory map configuration for (a) 8 x 8 bit multiplication


Version 1
// MultResult = 0;
// FOR (8 binary digits in multiplier)
// DO
// binary_digit = get next binary digit of multiplier from right to left;
// IF (binary_digit == 1)
// THEN MultResult = MultResult + multiplicand;
// FI;
// Shift(multiplicand) 1 place to left;
// END;

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.

12 April 2022 5 Dr Yigal Hoffner

You might also like