Changes

Jump to: navigation, search

6502 Math

1,705 bytes added, 01:54, 18 September 2023
Bitwise Operations
[[Category:6502]]The 6502 processor is limited to very simple math operations, and various processor flags affect these operations.
 
== Decimal Mode ==
 
The 6502 can perform math in binary or decimal mode.
 
In binary mode, operations are performed on a single 8-bit value. Numbers may be treated as signed or unsigned (the math is the same).
 
In decimal mode, the each byte is treated as two decimal digits - the lower 4 bits represent the lower digit, and the upper 4 bits represent the upper digit. Numbers are counted as positive, and values greater than 9 are invalid.
 
Decimal mode is selected by setting the D (Decimal) flag in the [[6502#Registers|Status Register]] using the <code>SED</code> instruction, and binary mode is selected by clearing the D flag using the <code>CLD</code> instruction.
 
The rest of this page deals with binary mode (decimal mode operates in mostly the same way).
== Addition ==
The Rotate Right and Rotate Left (ROR/ROL) instructions are like the LSR/ASL instructions, except that the Carry flag is rotated into one end of the byte, and the bit from the other end is rotated into the Carry flag. For example, ROR will shift C into the high bit and the low bit into C. Therefore, it is possible to perform a multi-byte rotate by stringing together ROR or ROL instructions.
 
== Bitwise Operations ==
 
The 6502 offers a basic set of [[Bitwise Operations]], including:
 
ROR Rotate right - 9-bit rotate (one byte plus C flag)
ROL Rotate left - 9-bit rotate left (one byte plus C flag)
ASL Arithmetic shift left - bit 7 -> C flag, bits 0:6 -> 1:7, 0 -> bit 0
LSR Logical shift right - bit 0 -> C flag, bits 7:1 -> 6:0, 0 -> bit 7
EOR Exclusive-OR (sometimes written XOR in other languages)
ORA OR (accumulator)
AND AND
 
A NOT operation can be performed using EOR with an immediate value of #$FF, and this can be combined with ORA and AND instructions to build NOR and NAND operations.
 
The BIT instruction performs a bitwise AND, sets the Z flag based on the result, and transfers bits 6 and 7 of the operand into the N and V flags.
 
== Multiplication and Division ==
 
There are no multiplication or division instructions on the 6502; it is up to the programmer to provide the appropriate logic.

Navigation menu