Category:6502 Assembly: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
Line 57: Line 57:


===Zero Page Indirect With Y===
===Zero Page Indirect With Y===
This one's a bit confusing. The values at a pair of consecutive zero page memory addresses are dereferenced, their order is swapped, the two values are concatenated into a 16-bit memory address, THEN the value of y is added to that address, and <i>the value at that address</i> is used as the operand. Whew! Let's break it up into steps.
This one's a bit confusing. The values at a pair of consecutive zero page memory addresses are dereferenced, their order is swapped, the two values are concatenated into a 16-bit memory address, THEN the value of y is added to that address, and <b><i>the value at that address</i></b> is used as the operand. Whew! Let's break it up into steps.


<lang 6502asm>LDA #$40
<lang 6502asm>LDA #$40

Revision as of 11:02, 25 August 2021


This page is a stub. It needs more information! You can help Rosetta Code by filling it in!
Language
6502 Assembly
This programming language may be used to instruct a computer to perform a task.
Official website
See Also:


Listed below are all of the tasks on Rosetta Code which have been solved using 6502 Assembly.

The MOS Technology 6502 is an 8-bit microprocessor that was designed by Chuck Peddle and Bill Mensch for MOS Technology in 1975. When it was introduced, it was the least expensive full-featured microprocessor on the market by a considerable margin, costing less than one-sixth the price of competing designs from larger companies such as Motorola and Intel. It was nevertheless fully comparable with them, and, along with the Zilog Z80, sparked a series of computer projects that would eventually result in the home computer revolution of the 1980s. The 6502 design, with about 4,000 transistors, was originally second-sourced by Rockwell and Synertek and later licensed to a number of companies. It is still made for embedded systems.

One of the first "public" uses for the design was the Apple I computer, introduced in 1976. The 6502 was next used in the Commodore PET and the Apple II. It was later used in the Atari home computers, the BBC Micro family, the Commodore VIC-20 and a large number of other designs both for home computers and business, such as Ohio Scientific and Oric.

The Ricoh 2A03/2A07 was created from a second source 6502 and used in the Nintendo Entertainment System in the US. The Ricoh 2A07 was used in the European model. Both use the same instructions as the 6502 but the Binary Coded Decimal mode does not function on either. The Decimal flag can still be set and cleared, but it will have no effect on any calculations. The functionality was replicated in software, as most NES games keep track of score and display it as decimal digits.

Registers

The 6502 has three main data registers: A (the accumulator), X, and Y. Most mathematical operations can only be done with the accumulator. X and Y are often limited to loop counters and offsets for indirect addressing. It also has the system flags, the stack pointer, and the program counter.

RAM

The first 256 bytes of the 6502's address space is known as "zero page RAM" and can be accessed more quickly than other sections of RAM. If a 8 bit address is used as an instruction parameter, it is actually referring to the zero page. (The high byte equals $00 and is thus omitted). This saves space, as the $00 high byte is not actually included in the bytecode, so a load/store to/from zero page takes one less byte than a load/store to/from anywhere else.

A True 8-Bit Computer

The 6502 is an 8-bit computer in the purest sense. Unlike the Z80, the 6502 is not capable of 16 bit operations within a single register. To work with a 16 bit number you will need to split it in two and work with each half individually.

Addressing Modes

Immediate

A constant value is directly used as the argument for a command. <lang 6502asm>LDA #3 ;load the number 3 into the accumulator AND #%10000000 ;bitwise AND the binary value 1000 0000 with the value in the accumulator SBC #$30 ;subtract hexadecimal 0x30 from the accumulator. If the carry flag is clear, also subtract 1 after that.</lang>

Zero Page

A zero page memory address is supplied as the argument for a command, and the actual operation is performed using the value stored within. This is similar to the dereference operator in C/C++. This is faster than other addressing modes that work with memory, and takes up fewer bytes in your program. Furthermore, certain commands work with zero page but not longer addresses.

For these examples, assume that the zero page memory address $05 contains #$40 (hexadecimal 0x40). <lang 6502> LDA $05 ;dereferences to whatever is stored at $05, in this case, #$40. #$40 is loaded into the accumulator. ADC $05 ;add the value stored at address $05 to whatever is stored in the accumulator. If the carry flag is set, add 1 to the result. ROR $05 ;rotate right the bits of the value stored at memory address $05. The value stored there changes from #$40 to #$20.</lang>

Absolute

A memory address stored outside the zero page is used as the argument for a command. This is slower and takes longer than the zero page. However, there are still certain things that absolute addressing is needed to do, such as jumping and reading/writing to or from memory-mapped ports.

<lang 6502asm>JMP $8000 ;move the program counter to address $8000. Execution resumes there. STA $2007 ;store the value in the accumulator into address $2007 (this is the memory-mapped port on the NES for background graphics)</lang>

Zero Page Offset By X/Y

A zero page memory address offset by X or Y. The value in X or Y is added to the supplied address, and the resulting address is used as the operand.

<lang 6502asm>LDX #$05 ;load 5 into X LDA $02,x ;load the value stored in $07 into the accumulator. (2 + 5 = 7) LDY #$04 ;load 4 into Y LDA $12,y ;load the value stored in $16 into the accumulator. ($12 + $4 = $16)</lang>

Absolute Offset By X,Y

An absolute memory address offset by X or Y. This works similar to the zero page version. However, not all commands work with this mode. For example, the LDX and LDY commands work with this mode, but STX and STY do not. (LDA and STA work with all addressing modes) <lang 6502asm>LDX #$15 LDY #$20 LDA $4000,x ;evaluates to LDA $4015 SBC $7000,y ;the accumulator is reduced by the value stored at $7020. If the carry is clear, 1 is subtracted from the result</lang>

Zero Page Indirect With Y

This one's a bit confusing. The values at a pair of consecutive zero page memory addresses are dereferenced, their order is swapped, the two values are concatenated into a 16-bit memory address, THEN the value of y is added to that address, and the value at that address is used as the operand. Whew! Let's break it up into steps.

<lang 6502asm>LDA #$40 STA $02  ; $02 contains #$40

LDA #$20 STA $03  ; $03 contains #$20, $02 contains #$40

LDY #$06  ; Y contains #$06

LDA ($02),y ; load the value at address $2040+y = load the value at address $2046</lang>

Note that for this mode, you are required to offset by Y. If you really don't want to offset by Y, load #0 into Y first.

Zero Page Indirect With X

This is similar to the one above. In fact, the only difference besides the register we use is the order of operations. Rather than adding Y after the dereference and concatenation, X is added BEFORE that step. X is placed inside the parentheses to show this. This mode is useful for writing to non-consecutive memory addresses in quick succession, by storing the addresses at consecutive zero page locations. Once again, let's break it down:

<lang 6502asm>LDA #$40 STA $06 LDA #$20 STA $07  ;$07 contains #$20, $06 contains #$40

LDX #$06 ;X contains #$06

LDA ($00,x) ;adds x to $00. Then the same thing happens as LDA ($06),y where y=0. This evaluates to LDA $2040, loading the accumulator

           ;with whatever value happens to be stored there.</lang>

Like before, you are required to use X in this mode. If you don't want to offset, just have X equal zero. In fact, when x and y both equal zero, ($HH,x) = ($HH),y for all 8-bit hexadecimal values $HH.



Citations

  1. Wikipedia: MOS Technology 6502
  2. Wikipedia: Ricoh 2A03
  3. [assembler and debugger in JavaScript

See Also

Subcategories

This category has the following 3 subcategories, out of 3 total.

Pages in category "6502 Assembly"

The following 129 pages are in this category, out of 129 total.