The Lush Engine








Introduction
Background
Math
Introduction
mult8.asm
Objects
Palettes
Tiles
Utilities

8 bit Multiply Routine

The following file is a routine to multiply two 8 bit numbers. You can download the file as well if you don't want to cut and paste the text.

mult8.asm

To download the file using a PC, right-click the link and select the option "Save Target As" then save the file as you would any other file from windows.

Explanation

This is really quite simple. We take the multiplier and double it eight times. What this does is move the bits within the byte one place left each time. We also double the result the same way each step. But if the multipler has one bit shoved off the left hand edge, we add the multiplicand to the result.

It may seem bizarre, but get a piece of paper and work one out by hand. It works.

Source

;*****************************************************************
; Procedure Name  : multiply_8
;
; Author          : Marc Gale (Xalthorn or Eldara)
;
; Description     : Multiply two bytes together
;
; On Entry        : A = Multiplier
;                   B = Multiplicand
;
; During          : DE is used to store the result
;                   HL are preserved, used and then restored
;
; On Exit         : Nothing intentional.
;*****************************************************************
multiply_8:
 push   hl              ; preserve HL
 ld     hl,0            ; initialise result to zero
 ld     d,0             ; set up DE to hold the ...
 ld     e,b             ; ... multiplicand
 
 add    a,a             ; double the multiplier
 jr     nc,.m1          ; if we didn't need to carry, no effect
 add    hl,de           ; add the multiplicand to the result
.m1
 add    hl,hl           ; double the result
 add    a,a             ; this repeats until we've done it ...
 jr     nc,.m2          ; ... eight times, once per bit
 add    hl,de
.m2
 add    hl,hl
 add    a,a
 jr     nc,.m3
 add    hl,de
.m3
 add    hl,hl
 add    a,a
 jr     nc,.m4
 add    hl,de
.m4
 add    hl,hl
 add    a,a
 jr     nc,.m5
 add    hl,de
.m5
 add    hl,hl
 add    a,a
 jr     nc,.m6
 add    hl,de
.m6
 add    hl,hl
 add    a,a
 jr     nc,.m7
 add    hl,de
.m7
 add    hl,hl
 add    a,a
 jr     nc,.done
 add    hl,de
.done
 ld     d,h                     ; put the result in DE
 ld     e,l
 pop    hl                      ; restore HL
 ret

Buttons created using the interactive button maker at Coolarchive