Section 9.9: Integer Multiplication (Frame 8)                     [prev][home][next]

The following CSC-1 program implements the above multiplication algorithm.

   0:            LOD    C      ; This first section shifts the
   1:            SHL           ; multiplier over 8 bits so that
   2:            SHL           ; we can use the N bit to see if
   3:            SHL           ; we must add or not.
   4:            SHL
   5:            SHL
   6:            SHL
   7:            SHL
   8:            SHL
   9:            STD    CTEMP
  10:  LOOP:     LOD    CTEMP   ; Load the shifted multiplier
  11:            JP     NOADD   ; If MSB>=0, then don't add
  12:            JZ     NOADD   ;
  13:            LOD    A       ; If MSB=1, add multiplicand in B
  14:            ADD    B       ; to the product in A.
  15:            STD    A
  16:  NOADD:    LOD    CTEMP   ; Load and shift multiplier left
  17:            SHL            ; by one bit.
  18:            STD    CTEMP
  19:            LOD    CTR     ; Subtract 1 from counter.
  20:            SUB    ONE
  21:            JZ     ENDLOOP ; If counter is 0, we're done!
  22:            STD    CTR     ;
  23:            LOD    A       ; Else shift product left one bit
  24:            SHL
  25:            STD    A
  26:            JMP    LOOP    ; Go back to top of loop
  27:  ENDLOOP:  HLT
  27:
  28:  A:        NUM    0       ; Product (16 bits)
  29:  B:        NUM    13      ; Multiplicand (8 bits)
  30:  C:        NUM    26      ; Multiplier
  31:  CTR:      NUM    8       ; Used to count down
  32:  CTEMP:    NUM    0       ; Holds copy of C which gets
  32:                           ; destroyed
  33:  ONE:      NUM    1       ; Constant +