In the multiplication algorithm discussed above we revised the way we multiplied number in lower classes, and gave an example of that method on binary numbers. We make a simple modification to the traditional algorithm before we proceed to formulate it in assembly language.

In the traditional algorithm we calculate all intermediate answers and then sum them to get the final answer. If we add every intermediate answer to accumulate the result, the result will be same in the end, except that we do not have to remember a lot of intermediate answers during the whole multiplication. The multiplication with the new algorithm is shown below.

1101 = 13 Accumulated Result
0101 = 5
----- 0 (Initial Value)
1101 = 13 0 + 13 = 13
0000x = 0 13 + 0 = 13
1101xx = 52 13 + 52 = 65
0000xxx = 0 65 + 0 = 65 (Answer)

We try to identify steps of our algorithm. First we set the result to zero. Then we check the right most bit of multiplier. If it is one add the multiplicand to the result, and if it is zero perform no addition. Left shift the multiplicand before the next bit of multiplier is tested. The left shifting of the multiplicand is performed regardless of the value of the multiplier’s right most bit. Just like the crosses in traditional multiplication are always placed to mark the ones, tens, thousands, etc. places. Then check the next bit and if it is one add the shifted value of the multiplicand to the result. Repeat for as many digits as there are in the multiplier, 4 in our example. Formulating the steps of the algorithm we get:

• Shift the multiplier to the right.
• If CF=1 add the multiplicand to the result.

• Shift the multiplicand to the right.
• Repeat the algorithm 4 times.

For an 8bit multiplication the algorithm will be repeated 8 times and for a sixteen bit multiplication it will be repeated 16 times, whatever the size of the multiplier is.

The algorithm uses the fact that shifting right forces the right most bit to drop in the carry flag. If we test the carry flag using JC we are effectively testing the right most bit of the multiplier. Another shifting will cause the next bit to drop in the next iterati on and so on. So our task of checking bits one by one is satisfied using the shift operation. There are many other methods to do this bit testing as well, however we exemplify one of the methods in this example.

In the first iteration there is no shifting just like there is no cross in traditional multiplication in the first pass. Therefore we placed the left shifting of the multiplicand after the addition step. However the right shifting of multiplier must be before the addition as the addition step’s exe cution depends upon its result.

We introduce an assembly language program to perform this 4bit multiplication. The algorithm is extensible to more bits but there are a few complications, which are left to be discussed later. For now we do a 4bit multiplication to keep the algorithm simple.

Example 4.1

01 ; 4bit multiplication algorithm
02 [org 0x100]
03 jmp start
04
05 multiplicand: db 13 ; 4bit multiplicand (8bit space)
06 multiplier: db 5 ; 4bit multiplier

51
07 result: db 0 ; 8bit result
08
09 start: mov cl, 4 ; initialize bit count to four
10 mov bl, [multiplicand] ; load multiplicand in bl
11 mov dl, [multiplier] ; load multiplier in dl
12
13 checkbit: shr dl, 1 ; move right most bit in carry
14 jnc skip ; skip addition if bit is zero
15
16 add [result], bl ; accumulate result
17
18 skip: shl bl, 1 ; shift multiplicand left
19 dec cl ; decrement bit count
20 jnz checkbit ; repeat if bits left
21
22 mov ax, 0x4c00 ; terminate program
23 int 0x21

04-06 The numbers to be multiplied are constants for now. The multiplication is four bit so the answer is stored in an 8bit register. If the operands were 8bit the answer would be 16bit and if the

7 operands were 16bit the answer would be 32bit. Since eight bits can fit in a byte we have used 4bit multiplication as our first example. Since addition by zero means nothing we skip the addition step if
14-16 the rightmost bit of the multiplier is zero. If the jump is not taken

the shifted value of the multiplicand is added to the result.
The multiplicand is left shifted in every iteration regardless of the

18 multiplier bit.

DEC is a new instruction but its operation should be immediately

19 understandable with the knowledge gained till now. It simply subtracts one from its single operand.
The JNZ instruction causes the algorithm to repeat till any bits of
20
the multiplier are left

Inside the debugger observe the working of the SHR and SHL instructions. The SHR instruction is effectively dividing its operand by two and the remainder is stored in the carry flag from where we test it. The SHL instruction is multiplying its operand by two so that it is added at one place more towards the left in the result.

0 comments