Tutorial 3 – C Programming in 6502 – Using Assembly


Previous two [here and here] tutorials hopefully bring you into the 8-bit 6502 NES programming. Before starting this tutorial, I am going to say a bit more about the 6502 NES hardware. The NES (Nintendo Entertainment System) has a 8-bit 6502 CPU (which can address 16-bit memory address from $0000 to $FFFF). It also comes with a 2K RAM (Yes, only 2K, so be wise to use memory), and a PPU (Picture Processing Unit).  Most basic games are size of 40K and there also are 60K, 80K, 128K and 256K games. The 6502 CPU is 8 bit and kinda slow, so the gaming performance is limited even the size can be extended by external storage (slot)

With C compiler for 6502, we can easily write C programs to generate the *.nes programs. We can embed inline 6502 assembly languages easily (6502 assembly programming is handy and helpful because most of the cases, we need to operate directly with the hardware). We can use asm() to include a one line assembly.

1
2
3
4
void main()
{
    asm("nop");
}
void main()
{
	asm("nop");
}

We can also write the assembly file in *.s extension

1
2
3
4
5
6
7
8
.export     _testasm
 
.segment    "CODE"
 
.proc   _testasm: near
    inc $F0
    rts
.endproc
.export		_testasm

.segment	"CODE"

.proc	_testasm: near
	inc $F0
	rts
.endproc

and include it in C program:

1
2
3
4
5
6
7
8
void testasm();
 
void main()
{
    while(1){
        testasm();  // call the assembled function
    }
}
void testasm();

void main()
{
	while(1){
		testasm();	// call the assembled function
	}
}

At command line, we just need to include the *.s for compilation.

cl65 -t nes -o test.nes test.c test.s

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
317 words
Last Post: Tutorial 2 - C Programming in 6502
Next Post: Tutorial 4 - C Programming in 6502 - Useful Macros

The Permanent URL is: Tutorial 3 – C Programming in 6502 – Using Assembly

6 Comments

  1. Milap Jhumkhawala
      • Milap Jhumkhawala
        • Milap Jhumkhawala
  2. sugar
    • sugar

Leave a Reply