Jump to content

Bitwise operations: Difference between revisions

No edit summary
Line 136:
</pre>
=={{header|6502 Assembly}}==
Integer one is in the accumulator, integer two is in zero page memory location "temp". Both are considered to be unsigned.
<lang 6502asm>
AND temp ;ANDs accumulator with temp.
 
OR temp ;ORs accumulator with temp.
 
EOR temp ;XORs accumulator with temp.
 
EOR #$FF ;bitwise NOT on the accumulator.</lang>
 
The 6502 can only shift left or right by one. Shifting by a variable number requires a loop.
There is also no arithmetic shift right on the 6502, only logical. It can also be replicated with a few commands.
 
<lang 6502asm>multi_ASL:
LDX temp
CPX #$08
BNE loop_ASL
;value will be zero anyway so don't bother
LDA #0
RTS
 
loop_ASL:
ASL
DEX
BPL loop_ASL
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
multi_LSR:
LDX temp
CPX #$08
BNE loop_LSR
;value will be zero anyway so don't bother
LDA #0
RTS
 
loop_LSR:
LSR
DEX
BPL loop_LSR
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
multi_ASR
LDX temp
CPX #$08
BNE loop_ASR
;value will be zero anyway so don't bother
LDA #0
RTS
 
loop_ASR:
LSR
DEX
BPL loop_ASR
RTS
 
ASR:
CLC ;clear the carry
PHA
PLA ;sets flags according to accumulator
BPL skip
SEC ;set the carry, the carry rotates into bit 7 during the ROR
skip:
ROR ;top bit will be the same as it was before.
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
multi_ROL:
LDX temp
loop_ROL:
LSR
DEX
BPL loop_ROL
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
multi_ROR:
LDX temp
loop_ROR:
LSR
DEX
BPL loop_ROR
RTS</lang>
 
=={{header|8051 Assembly}}==
1,489

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.