Statement: Multiply two 8-bit numbers stored in memory locations 2200H and 2201H by repetitive addition and store the result in memory locations 2300H and 2301H
Sample problem:
(2200H) = 03H
(2201H) = B2H
Result = B2H + B2H + B2H = 216H
= 216H
(2300H) = 16H
(2301H) = 02H
Source program
LDA 2200H
MOV E, A
MVI D, 00 : Get the first number in DE register pair
LDA 2201H
MOV C, A : Initialize counter
LX I H, 0000 H : Result = 0
BACK: DAD D : Result = result + first number
DCR C : Decrement count
JNZ BACK : If count 0 repeat
SHLD 2300H : Store result
HLT : Terminate program execution
(2200H) = 03H
(2201H) = B2H
Result = B2H + B2H + B2H = 216H
= 216H
(2300H) = 16H
(2301H) = 02H
Source program
LDA 2200H
MOV E, A
MVI D, 00 : Get the first number in DE register pair
LDA 2201H
MOV C, A : Initialize counter
LX I H, 0000 H : Result = 0
BACK: DAD D : Result = result + first number
DCR C : Decrement count
JNZ BACK : If count 0 repeat
SHLD 2300H : Store result
HLT : Terminate program execution
This seems inefficient...
ReplyDeleteWhen doing longhand arithmetic, you only need, at most, as many additions as you have number positions.
Since you work off decrementing a counter, it would seem you could have as many as 256 adds, with an average case of 128.
Further, you only need to "add" if the bit you're multiplying each time is a 1. You always need a shift, but if the multiplier is a zero, you can skip the addition.
This comment has been removed by the author.
ReplyDeleteMVI C,00
ReplyDeleteLDA 2000
MOV B,A
LDA 2001
MOV D,A
MVI A,00
UP ADD B
DCR D
JNZ UP
JNZ DN
INR C
STA 2000
MOV A,C
STA 2201
HLT