Linux Format

Write and run C64-style 6502 code

How long do you think it took to modify a 2,200-line C program that runs on Windows H to have it run on Linux? Just one hour. The main changes were switching some string routines such as strcpy_s to strncpy, altering paths for includes and changing the high-speed timing code. It might take between a week and a month or longer to do that in assembly. And moving that C code from, say, Ubuntu on x86 to Raspberry Pi on ARM would take almost no time at all. In assembler, it would take a complete rewrite; more months of work.

Having tried to put you off modern assembly, let’s try some old-fashioned assembler from 40 years ago. With roughly 60 instructions, the 6502 CPU is simpler than modern CPUs, which can have several hundred.

What is a 6502?

Pretend it’s 1983 and you have bought a brand new Commodore 64 and assembler cartridge that will ‘compile’ 6502 assembler into machine code. In this article, we are learning some 6502 assembly language, writing a short program in it and then running it on a web emulator.

The 6502 has just three registers: A (known as the accumulator), X and Y. Each register is 8-bit, so can hold a number between 0 and 255. It’s easier to deal with values and addresses in base 16 – hexadecimal – so a byte holds a value

You’re reading a preview, subscribe to read more.

More from Linux Format

Linux Format4 min read
Linux
The #1 open source mag Future Publishing Limited, Quay House, The Ambury, Bath, BA1 1UA Email contact@linuxformat.com EDITORIAL Editor-in-chief Neil Mohr Art editor Fraser McDermott Production editor Katharine Davies Group editor-in-chief Graham Bar
Linux Format3 min read
Kernel Watch
Linus Torvalds announced the fourth RC (Release Candidate) for what will become Linux 6.9 in another few weeks. In his announcement, he noted that there was “Nothing particularly unusual going on this week – some new hardware mitigations may stand o
Linux Format1 min read
Sql At Fifty
Structured Query Language (SQL) is 50 years old. It is the only programming language from the ’70s that is still around and – unlike COBOL and Fortran, which are confined to limited niche roles – still thriving and expanding. In 2023, the IEEE declar

Related