Jameco Electronics Rabbit 3000 User Manual

Page 47

Advertising
background image

38

Rabbit 3000 Microprocessor

The

SBC

instruction can also be used to perform a sign extension.

; extend sign of l to HL

LD A,l

rla ; sign to carry

SBC A,a

; a is all 1’s if sign negative

LD h,a

; sign extended

The multiply instruction performs a signed multiply that generates a 32-bit signed result.

MUL ; signed multiply of BC and DE,

; result in HL:BC - 1 byte, 12 clocks

If a 16-bit by 16-bit multiply with a 16-bit result is performed, then only the low part of
the 32-bit result (BC) is used. This (counter intuitively) is the correct answer whether the
terms are signed or unsigned integers. The following method can be used to perform a 16
x 16 bit multiply of two unsigned integers and get an unsigned 32-bit result. This uses the
fact that if a negative number is multiplied the sign causes the other multiplier to be sub-
tracted from the product. The method shown below adds double the number subtracted so
that the effect is reversed and the sign bit is treated as a positive bit that causes an addition.

LD BC,n1

LD HL’,BC ; save BC in HL’

LD DE,n2

LD A,b

; save sign of BC

MUL

; form product in HL:BC

OR a

; test sign of BC multiplier

JR p,x1 ; if plus continue

ADD HL,DE ; adjust for negative sign in BC

x1:

RL DE

; test sign of DE

JR nc,x2 ; if not negative

; subtract other multiplier from HL

EX DE,HL’

ADD HL,DE

x2:

; final unsigned 32 bit result in HL:BC

This method can be modified to multiply a signed number by an unsigned number. In that
case only the unsigned number has to be tested to see if the sign is on, and in that case the
signed number is added to the upper part of the product.

The multiply instruction can also be used to perform left or right shifts. A left shift of n
positions can be accomplished by multiplying by the unsigned number 2^^n. This works
for n # 15, and it doesn’t matter if the numbers are signed or unsigned. In order to do a
right shift by n (0 < n < 16), the number should be multiplied by the unsigned number
2^^(16 – n), and the upper part of the product taken. If the number is signed, then a signed
by unsigned multiply must be performed. If the number is unsigned or is to be treated as
unsigned for a logical right shift, then an unsigned by unsigned multiply must be per-
formed. The problem can be simplified by excluding the case where the multiplier is
2^^15.

Advertising
This manual is related to the following products: