6502 stack - johns hopkins university
TRANSCRIPT
![Page 1: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/1.jpg)
6502 Stack
Philipp Koehn
20 September 2019
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 2: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/2.jpg)
1
c64 emulator
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 3: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/3.jpg)
2PEEK and POKE
• POKE: directly write into memory
• PEEK: directly read memory value
• Example: write into screen memory
– POKE 1024,1
writes letter A into top left corner
– PRINT PEEK(1024)
returns 1
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 4: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/4.jpg)
3Character Encoding in Screen Memory
• What is the character encoding in screen memory?
• Let’s write a program
Address Bytes Command4200 A2 00 LDX #00
4202 8A TXA
4203 9D 00 04 STA 0400,X
4206 E8 INX
4207 D0 F9 BNE 4202
4209 60 RTS
• Run from BASIC: SYS 16896
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 5: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/5.jpg)
4Screenshot
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 6: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/6.jpg)
5
stack
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 7: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/7.jpg)
6Stack
• Useful data structure
• LIFO: Last in, first out
– PUSH 5
– PUSH 2
– PULL → 2
– PULL → 5
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 8: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/8.jpg)
76502 Stack in Memory
⇒ 01FF
01FE
01FD
01FC
01FB
01FA
01F9
01F8
01F7
01F6
01F5
01F4
01F3
01F2
... ...
0100
• 2nd page in memory reserved("page" = 256 bytes)
• Stack pointer
– current free position
– an address, e.g., 01FF
– register in CPU
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 9: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/9.jpg)
8Example
⇒ 01FF
01FE
01FD
01FC
01FB
01FA
01F9
01F8
01F7
01F6
01F5
01F4
01F3
01F2
... ...
0100
• PUSH 0A
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 10: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/10.jpg)
9Example
01FF 0A
⇒ 01FE
01FD
01FC
01FB
01FA
01F9
01F8
01F7
01F6
01F5
01F4
01F3
01F2
... ...
0100
• PUSH 0A– store 0A to 01FF– decrease stack pointer to 01FE
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 11: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/11.jpg)
10Example
01FF 0A
⇒ 01FE
01FD
01FC
01FB
01FA
01F9
01F8
01F7
01F6
01F5
01F4
01F3
01F2
... ...
0100
• PUSH 0A– store 0A to 01FF– decrease stack pointer to 01FE
• PUSH 55
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 12: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/12.jpg)
11Example
01FF 0A
01FE 55
⇒ 01FD
01FC
01FB
01FA
01F9
01F8
01F7
01F6
01F5
01F4
01F3
01F2
... ...
0100
• PUSH 0A– store 0A to 01FF– decrease stack pointer to 01FE
• PUSH 55– store 55 to 01FE– decrease stack pointer to 01FD
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 13: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/13.jpg)
12Example
01FF 0A
01FE 55
⇒ 01FD
01FC
01FB
01FA
01F9
01F8
01F7
01F6
01F5
01F4
01F3
01F2
... ...
0100
• PUSH 0A– store 0A to 01FF– decrease stack pointer to 01FE
• PUSH 55– store 55 to 01FE– decrease stack pointer to 01FD
• PULL
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 14: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/14.jpg)
13Example
01FF 0A
⇒ 01FE 55
01FD
01FC
01FB
01FA
01F9
01F8
01F7
01F6
01F5
01F4
01F3
01F2
... ...
0100
• PUSH 0A– store 0A to 01FF– decrease stack pointer to 01FE
• PUSH 55– store 55 to 01FE– decrease stack pointer to 01FD
• PULL– increase stack pointer to 01FE– retrieve 55 from 01FE
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 15: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/15.jpg)
14Example
01FF 0A
⇒ 01FE 55
01FD
01FC
01FB
01FA
01F9
01F8
01F7
01F6
01F5
01F4
01F3
01F2
... ...
0100
• PUSH 0A– store 0A to 01FF– decrease stack pointer to 01FE
• PUSH 55– store 55 to 01FE– decrease stack pointer to 01FD
• PULL– increase stack pointer to 01FE– retrieve 55 from 01FE
• PUSH 42
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 16: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/16.jpg)
15Example
01FF 0A
01FE 42
⇒ 01FD
01FC
01FB
01FA
01F9
01F8
01F7
01F6
01F5
01F4
01F3
01F2
... ...
0100
• PUSH 0A– store 0A to 01FF– decrease stack pointer to 01FE
• PUSH 55– store 55 to 01FE– decrease stack pointer to 01FD
• PULL– increase stack pointer to 01FE– retrieve 55 from 01FE
• PUSH 42– store 42 to 01FE– decrease stack pointer to 01FD
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 17: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/17.jpg)
16
6502 stack instructions
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 18: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/18.jpg)
17Basic Stack Manipulation
• Accumulator
– PHA: push accumulator to stack
– PLA: pull accumulator from stack
• Processor status (flags)
– PHP: push processor status to stack
– PLP: pull processor status from stack
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 19: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/19.jpg)
18Example
• Stack is a good place to safely store register values
• Example
PHA
TXA
PHA
TYA
PHA
(some code that changes registers)
PLA
TAY
PLA
TAX
PLA
(all registers back to original state)
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 20: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/20.jpg)
19Stack Pointer Instructions
• Read out stack pointer
• TSX: transfer stack pointer to X register
• TXS: transfer X register to stack pointer
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 21: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/21.jpg)
20Warning
• Stack is not very big (256 bytes)
• Too heavy use may lead to stack overflow
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 22: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/22.jpg)
21
sub routines
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 23: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/23.jpg)
22Subroutines
• Subroutines are small programs that do common things
– for instance: write a character at current cursor position
– this is in the C64 kernel at address FFD2
• Naive usage
LDA #41
JMP FFD2
• Why won’t that work?Subroutine does not know where to return to
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 24: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/24.jpg)
23Solution
• Use the stack!
• Jump to subroutine
– store current program counter to stack
– jump to subroutine address
• Return from subroutine
– retrieve return address from stack
– jump to retrieved address
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 25: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/25.jpg)
246502 Subroutine Instructions
• JSR: Jump to subroutine
• RTS: Return from subroutine
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 26: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/26.jpg)
25Example
01FF 0A
01FE 55
⇒ 01FD
01FC
01FB
01FA
01F9
01F8
01F7
01F6
01F5
01F4
01F3
01F2
... ...
0100
• 4400 LDA #41
• 4402 JSR FFD2
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 27: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/27.jpg)
26Example
01FF 0A
01FE 55
01FD 44
01FC 05
⇒ 01FB
01FA
01F9
01F8
01F7
01F6
01F5
01F4
01F3
01F2
... ...
0100
• 4400 LDA #41
• 4402 JSR FFD2
– program counter is 4405– store program counter
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 28: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/28.jpg)
27Example
01FF 0A
01FE 55
01FD 44
01FC 05
⇒ 01FB
01FA
01F9
01F8
01F7
01F6
01F5
01F4
01F3
01F2
... ...
0100
• 4400 LDA #41
• 4402 JSR FFD2
– program counter is 4405– store programm counter
• FFD2 JMP (0326)
• F1CA ...
• F207 RTS
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 29: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/29.jpg)
28Example
01FF 0A
01FE 55
⇒ 01FD 44
01FC 05
01FB
01FA
01F9
01F8
01F7
01F6
01F5
01F4
01F3
01F2
... ...
0100
• 4400 LDA #41
• 4402 JSR FFD2
– program counter is 4405– store programm counter
• FFD2 JMP (0326)
• F1CA ...
• F207 RTS
– retrieve program counter from stack– jump to retrieved address
• 4405 ...
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 30: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/30.jpg)
29
example
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 31: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/31.jpg)
30Recursion
• Recursively calling subroutines
• Canonical example: Fibonacci numbers
f(x) = f(x-1) + f(x-2)
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, ...
• limx→∞f(x+1)f(x) is Golden Ratio
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 32: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/32.jpg)
31CodeSTART TXA
BNE M01 ; f(0) = 0? no, continue
RTS ; yes
M01 DEX ; prepare for f(x-1) call
BNE M02 ; f(1) = 1? no, continue
RTS ; yes
M02 TXA ; save X on stack
PHA
JSR START ; result of f(x-1) in accumulator
TAY ; let’s put f(x-1) aside
PLA ; get X back from stack
TAX
TYA ; get f(x-1) back
PHA ; save that for now on stack
DEX ; prepare f(x-2)
JSR START
STA TEMP ; store f(x-2) for addition
PLA ; f(x-1) from stack
CLC
ADC TEMP ; f(x-1) + f(x-2)
RTS
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 33: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/33.jpg)
32
some more instructions
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 34: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/34.jpg)
33Logic
• Standard Boolean operations
– AND: bitwise AND
– ORA: bitwise OR
– EOR: bitwise XOR
• Operations impact negative and zero flag
• BIT: bitwise AND, but do not store result
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 35: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/35.jpg)
34Compare
• Compare register value
– CMP: compare accumulator
– CPX: compare X register
– CPY: compare Y register
• Does not change register value
• Sets flags as in a subtraction(e.g., if values match, set zero flag)
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 36: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/36.jpg)
35
some quirky things
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 37: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/37.jpg)
36Decimal Mode
• Decimal mode: pretend that hex numbers are really decimal numbers
LDA #07
CLC
ADC #04
• Normally results in 0B
• But in decimal mode: result 11
• Instructions
– SED: set decimal mode
– CLD: clear decimal mode
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019
![Page 38: 6502 Stack - Johns Hopkins University](https://reader030.vdocuments.site/reader030/viewer/2022012419/6173c207228e7d3c543d6442/html5/thumbnails/38.jpg)
37NOP
• NOP: No Operation
• Does nothing
• Useful as filler
e.g., when deleting instructions
Philipp Koehn Computer Systems Fundamentals: 6502 Stack 20 September 2019