Search code examples
assemblyx86-16micro-optimization

80286: Which is the fastest way to multiply by 10?


To multiply a number by any any multiple of 2, I'll shift it those many times.

Is there any such technique to multiply a number by 10 in less cycles?


Solution

  • The 80286 did not have a barrel shifter, that was introduced with the 80386. According to the timing tables in the Microsoft Macro Assembler 5.0 documentation (1987), SHL reg, immed8 takes 5+n cycles, whereas SHL reg, 1 takes 2 cycles. ADD reg, reg takes 2 cycles, as does MOV reg, reg. IMUL reg16, immed takes 21 cycles. Therefore, the fastest way to multiply by ten would appear to be:

               ;       // cycles
    shl ax, 1  ; *2    // 2
    mov bx, ax ; *2    // 4
    shl ax, 1  ; *4    // 6
    shl ax, 1  ; *8    // 8
    add ax, bx ; *10   // 10
    

    or, alternatively:

               ;      // cycles
    mov bx, ax ; *1   // 2
    shl ax, 1  ; *2   // 4
    shl ax, 1  ; *4   // 6
    add ax, bx ; *5   // 8
    shl ax, 1  ; *10  // 10
    

    Ten cycles either way.