Unit 5.2 -- Do Loop Example 2:  Dumb Multiplication

PROG

Illustrate multiplication of A by B

PED

Just another DO LOOP example

Pseudo

R1:=0 (counter)
R2:=0 (sum)
WHILE (R1 <> B) DO
  add A to R2
  R1:=R1+1
END WHILE
Store R2 into SUM

SF

BE can be used to branch on equality

9.html
                           Unit 5.2,
                      Dumb Multiplication

There  are  three  ways  to  perform  multiplication,  i.e.,
computing  A*B where A and B are memory locations containing
integers.

We can multiply these two numbers in three different manners
on the IBM-360 architecture:

a)   we  can do "dumb multiplication"  That is we can add  A
     to itself B times.

     For example, if A were 3 and B were 5, we could compute
     3+3+3+3+3.

     This is done using a do loop.

b)   we can use the M or MR instructions.  These are left as
     an exercise (manual problem) for the interested reader.
     Not  all  computers, including some  modern  computers,
     support a multiply instructions.

c)   We  can use shifts and adds to do the multiply.   These
     are  discussed much later in the course as part of  the
     "bit diddling" section.

The pseudocode and example illustrates the multiplication of
A  by  B  by  method  a.  I decided to specify  the  use  of
registers specifically.

R2  contains the sum as we add successive values of  A.   At
the beginning of the loop R1 contains the number of times  A
has  been  added to R2.  Initially, it is zero  as  we  have
added  no  A's to R2.  When R1 equals B that means  we  have
added  B A's to R2.  Thus, we stop when R1 is strictly  less
than B.

The  conversion of this pseudocode to ASSEMBLER is straight-
forward.  The program computes SUM:=A*B;

R1  and R2 are both zeroed with SR statements at lines 9 and
10,  respectively.

The comparison of R1 to B is done at line 12 and 13.

A  is added to R2 at line 14.  And R1 is incremented at line
15.

At line 16, we return to the begining of the loop.

When register two finally contains A*B, we end up at line 18
where it is stored into SUM.