==================================================================== DR 6502 AER 201S Engineering Design 6502 Execution Simulator ==================================================================== Supplementary Notes By: M.J.Malone More 6502 Assembly Code ======================= There were several instructions not introduced in the first introductory file 'START65.DOC'. The remaining 6502 instructions will be discussed here. Operator Instructions --------------------- These are instructions of only one argument and include incrementing, decrementing and bit shifts. INC arg Increases the value of 'arg' by one DEC arg Decreases the value of 'arg' by one INX Increment .X INY Increment .Y DEX Decrement .X DEY Decrement .Y ROR arg Rotates the argument and C flag bits one to the right ROL arg Rotates the argument and C flag bits one to the left LSR arg Logical Shift Right: shifts bits one to the right ASL arg Arithmetic Shift Left: shifts bits one to the left The increment and decrement instructions are very useful when dealing with pointers and vectors. Since the .X and .Y are index registers used to index within memory spaces, the INX, INY, DEX and DEY instructions are used to move on the the next or previous memory entry. The .X and .Y are also used as quick counters and here again the increment and decrement are very useful. The bit rotating and shifting instructions are used both in arithmetic operations and bit pattern manipulation for encoding and decoding. An LSR can be thought of as dividing by 2 and the ASL as multiplying by 2. Along with the ADC and SBC, these instructions are key to multiplication and division. Flag Setting and Clearing ------------------------- The programmer is able to directly influence the flags through the following statements: CLC Clears the Carry Flag SEC Sets the Carry Flag CLD Clears 'Decimal' Mode SED Sets 'Decimal' Mode CLI Clears the Interrupt Disable Flag SEI Sets Interrupt Disable Flag BRK Break Instruction CLV Clears the Overflow Flag page 2 Carry Flag The carry flag is set and cleared by arithme tic and shift statements and is influenced by comparisons. In general the flag is used as follows: In ROL, ROR, ASL and LSR statements the carry flag is used as a 9th bit in the accumulator either above the most significant bit (MSB) or below the least significant bit (LSB). In SBC and ADC statements it is used as a 9th bit above the MSB to act as a carry or a borrow. Since comparisons use a subtraction, the carry flag reflects a similar state for CMP, CPX, and CPY as for the statement SBC. The carry flag is NOT used as a borrow-from in CMP, CPX or CPY. For example using CMP to compare #$05 and #$05 results in a result of zero regardless of the state of the carry flag before the comparison. After the comparison however, if a borrow was not required to do the comparison the carry flag will be set indicating no borrow has occurred. The above comparison will result in the carry flag being set. Increment instructions DO NOT influence the carry flag, nor do AND, ORA or EOR. Negative and Zero Flags Neither the negative or zero flag can be explicitly set or cleared in a SEx or CLx instruction. All instructions that move data to a location inside the processor or compare data within in any way except pulling from the stack influence the negative and zero flags. That means all instructions except JMP, JSR, RTS, Sets, Clears, Pushes, Pulls, STA, STX, STY and Branches alter the negative and zero flags. If the data being transferred is zero or if a calculation results in the number zero, regardless of the state of the carry flag, the zero flag will be set (Z=1). The negative flag will have the same logic state as the MSB of the data moved or the result of the calculation. As a result, the negative and zero flags will never both be set at the same time because a zero has all bits including the MSB clear. The zero flag and testing for zero: BNE (Z=0?) and BEQ (Z=1?) are quite straight forward. The negative flag has some peculiar properties. First note that there is no such thing as a negative number in the 6502. If required to judge if a number is negative, the 6502 will assume all numbers between #$FF and #$80 are negative correspond to the numbers -#$01 to -#$80 respectively. The numbers between #$00 and #$7F are assumed positive corresponding to +#$00 and +#$7F respectively. The comparison and subtraction instructions often cause students confusion in relation to the negative flag. Often a following will be required: ; ; If arg1