I came across the following piece of 16-bit x86 code for multiplying a value by 40, using just shifts and additions:
; BX holds the value we want to multiply. ; The result is stored in AX. MOV AX, BX ; Multiply by 4 using two shifts SHL AX, 1 SHL AX, 1 ; Add the original value, this gives us BX * 5 ADD AX, BX ; Now multiply by 8 using three shifts for the final result SHL AX, 1 SHL AX, 1 SHL AX, 1
Now what I'd like to know is why this code uses multiple shifts in a row instead of just doing
SHL 2 and
SHL 3. It was almost certainly written by hand, so I assume there was some speed benefit or something. Does anyone have any insights?
The code was written in 1991 and was targeting 286 and 386 class machines.