EMBEDDED SYSTEMS & MICROCONTROLLERS · PAGE 88
Assembly: Shifts, Multiply and Divide
Fast arithmetic tricks.
Shifting left by one doubles a number, and shifting right by one halves it, using one fast instruction. Small chips often have no divide instruction, so shifts matter.
Multiplying by 10 can be done as (x shifted left 3) plus (x shifted left 1). Check for overflow and carry when working with several bytes.
EXAMPLE
; generic teaching syntax: R0 = R0 * 10
MOV R1, R0
SHL R0, 3 ; x * 8
SHL R1, 1 ; x * 2
ADD R0, R1 ; x * 10