Assembly Language Logical Shift

From TRCCompSci - AQA Computer Science
Revision as of 12:20, 21 January 2020 by Admin (talk | contribs) (Multiplication example)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Logical Shift Left

A Logical Shift Left will shift the binary pattern and add a zero at the least significant place value. For example the binary for 4 is '00100' a logical shift to the left of one place will give '01000' which is now 8. A logical shift to the left by one digit will multiply by 2. A logical shift of 2 places on '00100' will give '10000' which is 16, therefore a logical shift to the left by two digits will multiply by 4.

Logical Shift Right

A Logical Shift Right by a single digit will half the value, this essentially removes the least significant place value and shifts the other digits to the right. If the value represented is 13 ie '01101' a logical shift to the right of one place will remove the least significant place value so it will now be '00110' which is 6. 13 divided by 2 is 6 remainder 1. A logical shift to the right of two place will turn 13 '01101' into '00011' which is 3, ie 13 divided by 4 (the remainder will be 1).

Multiplication example

This example uses a Logical Shift to perform multiplication. R2 is used to store the second value of the multiplication, and for every shift we subtract 2 from this. We then compare it with #1, because if it is greater we still need to do a shift, if it is less than we have finished the calculation. However if the comparison is equal, we need to finally add the first number to the total.

      MOV R0, #13
      MOV R1, #9
      MOV R2, R1
      MOV R3, R0
LOOP: LSL R3,R3,#1
      SUB R2,R2,#2
      CMP R2,#1
      BGT LOOP
      BLT END
      ADD R3,R3,R0
END:  HALT