Bitwise operations: Difference between revisions
→{{header|68000 Assembly}}: also revised 6502 example
Puppydrum64 (talk | contribs) |
Puppydrum64 (talk | contribs) (→{{header|68000 Assembly}}: also revised 6502 example) |
||
Line 136:
</pre>
=={{header|6502 Assembly}}==
Bitwise operations are done using the accumulator and an immediate constant (prefixed with #) or a value at a specified memory location (no #.)
<lang 6502asm>LDA #$05
STA temp ;temp equals 5 for the following</lang>
;AND
<lang 6502asm>LDA #$08
AND temp</lang>
;OR
<lang 6502asm>LDA #$08
ORA temp</lang>
;XOR
<lang 6502asm>LDA #$08
EOR temp</lang>
;NOT
<lang 6502asm>LDA #$08
EOR 255</lang>
The 6502 doesn't have arithmetic shift right, but it can be replicated, provided the negative flag is set according to the value in the accumulator.
<lang 6502asm> LDA #$FF
CLC ;clear the carry. That way, ROR will not accidentally shift a 1 into the top bit of a positive number
BPL
SEC ;if the value in A is negative, setting the carry will ensure that ROR will insert a 1 into bit 7 of A upon rotating.
SKIP:
ROR</lang>
The 6502 can only rotate a value by one, not an arbitrary number. A looping routine is needed for rotates larger than 1.
Also, the 6502's <code>ROL</code> and <code>ROR</code> rotate instructions both rotate through the carry, unlike the instructions on other architectures with the same name. (68000, x86, and ARM all have a "ROR" command but it doesn't rotate through the carry on those CPUs.)
<lang 6502asm>LDA #$01
ROL ;if the carry was set prior to the ROL, A = 3. If the carry was clear, A = 2.</lang>
<lang 6502asm>LDA #$01
ROR ;if the carry was set prior to the ROR, A = 0x80. If clear, A = 0.</lang>
=={{header|68000 Assembly}}==
Like with most 68000 commands, you can specify a length parameter. Anything outside that length is unaffected by the operation.
;AND
<lang 68000devpac>MOVE.W D0,#$100
MOVE.W D1,#$200
AND.W D0,D1</lang>
;OR
<lang 68000devpac>MOVE.W D0,#$100
MOVE.W D1,#$200
OR.W D0,D1</lang>
;XOR
<lang 68000devpac>MOVE.W D0,#$100
MOVE.W D1,#$200
EOR.W D0,D1</lang>
;NOT
<lang 68000devpac>MOVE.W D0,#$100
NOT.W D1</lang>
;Left Shift
<lang 68000devpac>MOVE.W D0,#$FF
MOVE.W D1,#$04
LSL.W D0,D1 ;shifts 0x00FF left 4 bits</lang>
;Right Shift
<lang 68000devpac>MOVE.W D0,#$FF
MOVE.W D1,#$04
LSR.W D0,D1 ;shifts 0x00FF right 4 bits</lang>
;Arithmetic Right Shift
<lang 68000devpac>MOVE.W D0,#$FF00
MOVE.W D1,#$04
ASR.W D0,D1 ;shifts 0xFF00 right 4 bits, preserving its sign</lang>
;Left Rotate
<lang 68000devpac>MOVE.W D0,#$FF00
MOVE.W D1,#$04
ROL.W D0,D1</lang>
;Right Rotate
<lang 68000devpac>MOVE.W D0,#$FF00
MOVE.W D1,#$04
ROR.W D0,D1</lang>
;Left Rotate Through Extend Flag
<lang 68000devpac>MOVE.W D0,#$FF00
MOVE.W D1,#$04
ROXL.W D0,D1</lang>
;Right Rotate Through Extend Flag
<lang 68000devpac>MOVE.W D0,#$FF00
MOVE.W D1,#$04
ROXR.W D0,D1</lang>
=={{header|8051 Assembly}}==
|