6502 Processor: Stack memory, Stack pointer, program pointer and details of 6502 instructions for jump and rotation

In this blog, I would like to share about program counter(PC), stack , stack pointer(SP) of 6502 processor. I will also share what I learnt regarding the instructions JSR, JMP, RTS, LSR,ASL,ROR, ROL, PHA and PLA while I experienced how they interact with the stack, SP, PC and the register status. Some of the information is from the documentation in the website of mass:werk

Program counter (PC)

Program counter (PC) is the pointer that keep track of the address of the next instruction to be executed in the memory.

The Stack

Regarding 6502 processor, the stack is located from $0100 to $01FF. If we store a value into the stack, value is stored starting from $01FF. However, we a 2-byte address is stored, the address closer to the $01FF is the high byte. For example, if we store $0123 to an empty stack. $01FF will be $01 and $01FE will be $23.

Stack pointer

Stack pointer (SP) is the pointer pointing to the top of stack memory. For 6502 processor, the pointer is pointing to $01FF before the program run.

Instructions

JSR: Jump to New Location Saving return address.
This instruction save the very last address of this instruction to the stack. Then, it set the program counter (PC) to the value provided in this instruction. Since it added value to the stack, SP will move forward for two bytes, i.e. minus 2. 

RTS: Return from subroutine.
This instruction set the program pointer (PC) to the address on the top of the stack. It also move SP will move backward for two bytes, i.e. add 2. 

JMP: jump to new location
This instruction simply move the PC to the address provided.

PHA: push Accumulator on Stack
This instruction store the value of the accumulator to the stack and move the stack point one byte forward, i.e. minus 1.

PLA: pull Accumulator from Stack
This instruction get the value of the accumulator to the stack and move the stack point one byte backward, i.e. add 1.

LSR: Logical shift right (Memory /  Accumulator)
If the right most bit is 1, it will set the carry flag. Otherwise, it unset the carry flag.
Then, it shift the value in the memory/Accumulator to right, then put the leftmost digit as 0

ASL: Shift Left One Bit (Memory /  Accumulator)
If the left most digit is 1, it will set the carry flag. Otherwise, it unset the carry flag.
Then, it shift the value in the memory/Accumulator to left, then put the rightmost digit as 0

ROR: Rotate One Bit Right (Memory /  Accumulator)
This instruction shift the value in the memory/Accumulator to right, then put the leftmost digit as the carry flag
If the original rightmost bit is 1, it will set the carry flag. 
If the original rightmost bit is 0, it will set the carry flag. 

ROR: Rotate One Bit Left(Memory /  Accumulator)
This instruction shift the value in the memory/Accumulator to left, then put the rightmost digit as the carry flag
If the original leftmost bit is 1, it will set the carry flag.
 If the original leftmost bit is 0, it will unset the carry flag.



Comments

Popular posts from this blog

SPO600 Project Stage 1 (Pt.1) - Create a GCC Pass

SPO600 Project Stage 2 (Pt.1) - GCC pass locating clone function

SPO600 Project Stage 2 (Pt.2) - GCC pass locating clone function -modified version