Difference between revisions of "Introducing the Instruction Set Part 4"

From Intellivision Wiki
Jump to: navigation, search
(Moving On)
Line 1: Line 1:
This short tutorial examines the shift instructions.  If you haven't  
+
This short tutorial examines the shift instructions.  If you haven't read [[Introducing the Instruction Set Part 1|Part 1]] or [[Introducing the Instruction Set Part 2|Part 2]] yet, you might consider doing so first.
  
 
= Shift and Rotate Instructions =
 
= Shift and Rotate Instructions =

Revision as of 23:14, 8 October 2007

This short tutorial examines the shift instructions. If you haven't read Part 1 or Part 2 yet, you might consider doing so first.

Shift and Rotate Instructions

The CP1610 offers a rich variety of shift and rotate instructions. These are useful for bit manipulation and certain types of mathematics. Shifting left by one position is equivalent to multiplying by 2. Shifting right by one position is roughly equivalent to dividing by 2. The shift instructions only operate on R0 through R3. You cannot use them with R4 through R7. Overall, CP-1600 offers 8 separate shift instructions. Each can shift by one or two positions.

Click on a given mnemonic to see how it operates.

  SLL   Rx[, 2]Shift Logical Left
  SLR   Rx[, 2]Shift Logical Right
  SAR   Rx[, 2]Shift Arithmetic Right
  SWAP  Rx[, 2]Swap bytes
  SLLC  Rx[, 2]SLL into Carry
  SARC  Rx[, 2]SAR into Carry
  RLC   Rx[, 2]Rotate Left thru Carry
  RRC   Rx[, 2]Rotate Right thru Carry

Logical shifts (SLL, SLR, SLLC) fill the newly-opened positions with zeros. Arithmetic shifts (SAR, SARC) fill the newly opened positions with copies of bit 15. Logical right shifts can be thought of as "unsigned divide by 2", whereas arithmetic right shifts can be thought of as "signed divide by 2, rounding toward negative."

You can combine shift and add instructions to perform simple multiplications. The following example shows how to multiply the value in R0 by 20, using R1 as a temporary variable.

    SLL  R0, 2   ; Multiply R0 by 4
    MOVR R0, R1  ; Save a copy of original value * 4
    SLL  R0, 2   ; Multiply R0 by 4 again (original value * 16)
    ADDR R1, R0  ; Add (value * 4) to (value * 16), giving (value * 20)

Right shifts divide by powers of two, but they also truncate the fractional portion. You can combine a right shift with an ADCR to get a rounded result instead:

    SARC R0, 1   ; Signed divide by 2.  Shifted away bit goes to 'C'
    ADCR R0      ; Add 1 if shifted away bit was 1.  (Round towards positive)

Rotate instructions combined with shift instructions make it easy to shift values longer than 16 bits. The following examples show how to shift the 32 bit number held in R1:R0 left and right by 1 and 2 positions. (R1 holds bits 16..31 of the 32-bit number.)

    ; Shift R1:R0 left by 1 position
    SLLC R0, 1   ; Shift lower half left, extra bit to 'C'
    RLC  R1, 1   ; Shift upper half left, pulling lower bit from 'C'
    
    ; Shift R1:R0 left by 2 positions
    SLLC R0, 2   ; Shift lower half left, extra bits to 'C', 'O'
    RLC  R1, 2   ; Shift upper half left, pulling lower bit from 'C', 'O'
    
    ; Shift R1:R0 right by 1 position (arithmetic right shift)
    SARC R1, 1   ; Shift upper half left, extra bit to 'C'
    RRC  R0, 1   ; Shift lower half left, pulling lower bit from 'C'
    
    ; Shift R1:R0 left by 2 positions (arithmetic right shift)
    SARC R1, 2   ; Shift upper half right, extra bits to 'C', 'O'
    RRC  R0, 2   ; Shift lower half right, pulling lower bit from 'C', 'O'


Moving On

At this point, you may wish to continue with the remaining parts of this tutorial:

Or, you can return to the Programming Tutorials index.