SHIFT
Perform a logical or arithmetic shift of the operand's value.
| Opcode | Bytes | Cycles | Form | Example |
|---|---|---|---|---|
| $70 | 3 | 3 | shift left b:_, {s_byte} | shift left b:1, 123 |
| $70 | 3 | 3 | shift right unsigned b:_, {s_byte} | shift right unsigned b:1, 123 |
| $70 | 3 | 3 | shift right signed b:_, {s_byte} | shift right signed b:1, 123 |
| $71 | 3 | 3 | shift left p:_, {s_byte} | shift left p:2, 123 |
| $71 | 3 | 3 | shift right unsigned p:_, {s_byte} | shift right unsigned p:2, 123 |
| $71 | 3 | 3 | shift right signed p:_, {s_byte} | shift right signed p:2, 123 |
| $72 | 3 | 3 | shift left i:_, {s_byte} | shift left i:4, 123 |
| $72 | 3 | 3 | shift right unsigned i:_, {s_byte} | shift right unsigned i:4, 123 |
| $72 | 3 | 3 | shift right signed i:_, {s_byte} | shift right signed i:4, 123 |
| $73 | 3 | 3 | shift left i:_, i:_ | shift left i:4, i:8 |
| $74 | 3 | 3 | shift right unsigned i:_, i:_ | shift right unsigned i:4, i:8 |
| $75 | 3 | 3 | shift right signed i:_, i:_ | shift right signed i:4, i:8 |
| Condition flag | Output |
|---|---|
| zero (ZF) | If the result is zero, this flag is 1; otherwise it is 0.* |
| negative (NF) | This flag is updated with the highest bit of the result.* |
| overflow (OF) | This flag is not affected by shift. |
| carry (CF) | This flag is always updated with the last bit that was shifted out.* |
* Except when the shift count is zero; see below.
Notes
-
The result is stored back into the same operand.
-
shift left i:4, 1is a fast and compact way to multiply by 2. -
shift right signed i:4, 1orshift right unsigned i:4, 1is a fast and compact way to divide by 2. Note that for negative values,shift right signedrounds toward negative infinity rather than toward zero, which differs from the usual integer division operation. -
For constant shifts, the same opcode appears in multiple rows of the table above. This is because the shift variant is encoded in the same operand byte that stores the shift count:
Operand byte
(binary)Shift variant Operation 00xx_xxxxshift left…logical shift left (LSL) 01xx_xxxxshift right unsigned…logical shift right (LSR) 10xx_xxxxshift right signed…arithmetic shift right (ASR) -
The value
11xx_xxxxis reserved for future use; its behavior is undefined. -
For constant shifts, the shift count is stored in the low bits of this operand byte. For example,
shift left…, 3has an operand byte$03, whereasshift right signed…, 3has an operand byte$83. -
For all shift forms, the shift count is masked according to the register type. The mask is
$7forb:,$fforp:, or$1ffori:. For example, ifi:8contains 32, thenshift left i:4, i:8shifts by 0 (since32 AND $1f = 0), not by 32. -
If the shift count is zero, no operation is performed, and the flags are unchanged, but the instruction still requires 3 clock cycles. This behavior is typical of most CPUs.
-
Chombit does not currently support circular shift variants such as rotate right (ROR) or rotate right with extend (RRX), as they are rarely needed except in specialized optimizations. They may be introduced in the future, however.