US
13 MIN READ TIME
6502 Assembler

How to build a 6502 assembler

David Bolton shows how to create a 6502 assembler in C#, highlighting the problems you need to overcome.

Part One!

Don’t miss next issue, subscribe on page 16!

Part One!

Don’t miss next issue, subscribe on page 16!

OUR EXPERT David Bolton worked as a games developer back in the ’80s, programming in both Z80 and 6502 assembly language on ZX Spectrum, MSX, Amstrad CPC-64 and CBM-64/CBM-16 home computers.

QUIICK TIP

If you get a ‘Symbol Lookup error’ when you run as65, you may have to uninstall the Dotnet Snap and install Dotnet using Apt.This command removes it: sudo snap remove – purge dotnetsdk . Then use the Apt command in the Quick Tip opposite.

QUIICK TIP

If you get a ‘Symbol Lookup error’ when you run as65, you may have to uninstall the Dotnet Snap and install Dotnet using Apt.This command removes it: sudo snap remove – purge dotnetsdk . Then use the Apt command in the Quick Tip opposite.

This article shows you how to create a 6502 assembler, which we’re calling as65. It won’t be quite as powerful as the xa65 assembler used in earlier articles (www.floodgap.com/retrotech/ xa – see LXF313) but it’ll be our own!

To get started, here are the steps it will go through when assembling: 1. Read source file and options. If there are any include files specified, read those in. 2. Do the first pass, evaluate labels. 3. If there were no errors in the first pass, do the second pass. Emit the machine code to disk. We use two passes because of page-zero support. If we didn’t have page zero, all addresses would be 16-bit. Accessing page-zero means instructions are one byte shorter and run slightly faster.

The problem is when a label is referred to before it is defined. If the memory location accessed is in page zero, the instruction will be two bytes long and this will affect the location of labels that follow.

The first pass builds up a dictionary of labels. By the time it has done the first pass, it knows the value of every label and can then generate correct code on the second pass.

Rules to follow

If you follow these rules, it will be less problematic.

1. Declare any zero-page memory locations before you use them. Think of these as the variables that your program will use.

2. Labels used for subroutines or branching forward can be called before they are declared. After pass one is done, the length of all instructions will have been determined.

You might ask why are we even using page zero, because instructions that access zero-page locations are two bytes long not three, and execute in three clock cycles not four.

Unlock this article and much more with
You can enjoy:
Enjoy this edition in full
Instant access to 600+ titles
Thousands of back issues
No contract or commitment
Try for 99c
SUBSCRIBE NOW
30 day trial, then just $9.99 / month. Cancel anytime. New subscribers only.


Learn more
Pocketmags Plus
Pocketmags Plus

This article is from...


View Issues
Linux Format
September 2024
VIEW IN STORE

Other Articles in this Issue


LINUX FORMAT
LINUX FORMAT
The #1 open source mag Future Publishing Limited,
WELCOME
MEET THE TEAM
This issue, we’re introducing readers to Linux Mint 22 and its Cinnamon desktop; in the name of open source choice, what other desktop should people try and why?
Lightning strikes
Linux isn’t a magic bullet. It still has
REGULARS AT A GLANCE
CrowdStrike issues hit Microsoft and Linux
Microsoft wasn’t the only victim of faulty CrowdStrike updates. Weeks before the worldwide outage, Debian and RHEL were also hit.
Nvidia officially goes open source
The R560 driver marks Nvidia’s full transition to open source GPU kernel modules.
Google backs down on cookies
Chrome will allow users to make “informed choices”.
SUITE PRIVACY
Italo Vignoli is one of the founders of
EARLY ADOPTER
David Stokes is a technology evangelist at Percona.
Jellyfish devs say help others
Lead developer Joshua Boniface has asked users to support clients instead.
Switzerland is open source
New law requires all government apps to be open source.
Ext4 gets faster
The latest Linux 6.11 comes with performance optimisation for ext4.
Distro watch
What’s behind the free software sofa?
ATOMIC STEAM
Ludovico de Nittis is a software engineer at
FILTER TIPS
Jon Masters is a kernel hacker who’s been
Kernel Watch
Jon Masters summarises the latest happenings in the Linux kernel, so that you don’t have to.
Answers
Got a burning question about open source or the kernel? Whatever your level, email it to answers@linuxformat.com
Mailserver
WRITE TO US Do you have a burning
HotPicks
THE BEST NEW OPEN SOURCE SOFTWARE ON THE PLANET
THE LINUX SMART HOME
We aim to keep the lights on the smarter way with Home Assistant, Zigbee and low-cost IKEA kit.
REVIEWS
HighPoint Rocket 1608A
For when Shane Downing really needs 56GB/s of storage speed.
Q4OS 5.5
Nate Drake is on cue to explore the latest Q4OS and comes away amazed at its speed and simple setup.
Pardus 23.2
Nate Drake offers readers a chunk of Turkish Delight as he tours this Debian-based distro, coded with full government approval.
Nobara Project 40
Nate Drake delves into this modified Fedora distro. Is this the ultimate in Linux gaming or should you say “no Nobara”?
Mabox Linux 24.07
A spin of Manjaro, which is a spin of Arch, is leaving the normally centred Nate Drake in a right tizzy.
Men of War 2
Often found wondering what war is good for, Jake Tucker is reminded by The Management that it looks after his pay cheque and pension fund.
ROUNDUP
Remote desktops
Ever wish you had the desktop of another computer right in front of you? Michael Reed checks out five applications that can do this and more.
The verdict Remote desktops
We’ve ended up with five options that can
FRESH MINT 22!
Fresh Mint 22!
Linux Mint is in season and Jonni Bidwell has been sniffing it voraciously. Join him on a fragrant voyage of discovery.
Mint is for its users
Discover why Linux Mint is your new favourite Linux flavour.
How to install Mint
Get Mint booted, explore the live environment, then get this herbaceous powerhouse installed forthwith.
Getting to know Mint
Explore the freshest of fresh installs and tweak it to perfection.
X marks the apps
Perfect your install and discover the new apps on the block.
Pi USER
Pi Foundation releases AI teaching guidance
Worry no more about how to teach about ethical generative AI – the Pi Foundation has you covered.
Fritzing 1.0.3
Les Pounder spends most of his work hours looking at circuit diagrams, then carries on doing the same thing after work.
Sovol SV08
Big, super-fast and open source, it looks as though Denise Bertacchi has found her perfect printer!
Simulate your Pi Pico projects with Wokwi
Claiming he’s saving resources, Les Pounder simulates an electronics project because he is too lazy to get up and build it for real.
IN DEPTH
PIPEWIRE
Matt Holder delves into the depths of our audio stack to learn about PipeWire and what came before it.
TUTORIALS
Try a tiny text editor
Shashank Sharma lives by the try-it-before-you-dismiss-it code. That’s how he finds new tools. It’s also how he discovered a fondness for kimchi.
Navigate your way around the filesystem
LINUX BASICS
Geek out with NixOS
Mats Tage Axelsson shows you how easy it is to use NixOS and the challenges of installing new applications. All 100,000 packages.
Expand your storage with high-speed SSDs
RAID
Upgrade it: Solid-state drives
RAID
Take local control of multiple AI models
OLL AMA
Record your games on the Steam Deck
STEAM DECK
ADMINISTERIA
Get to grips with crontabs on Linux
Ever the busy boy, Stuart Burns needs things to run on time, so he’s here to show you how to set up schedules.
AngelVPN
Always a good boy, Sam Dawson thinks he’s rather smitten with this devilishly good service.
PureDome VPN
A VPN service that’s aimed at business types like Mike Williams, who prefers to do his private business in private.
WHATEVER HAPPENED TO SYMBIAN?
The Symbian operating system had a lot going for it, and even dabbled with open source, so why did it fail? Mike Bedford investigates.
CODING ACADEMY
Code your own Wordle-like game
PYTHON
ADVERTISEMENT
GO COMPARE
Linux format
www.linuxformat.com
Techradar
www.techradar.com/pro/linux
LINUX FORMAT
magazinedirect.com
LINUX FORMAT
www.magazinesdirect.com
LINUX FORMAT
www.magazinesdirect.com
Teachradar
www.teachradar.com
LINUX FORMAT
LINUX FORMAT
magazinesdirect.com
Code club
www.codeclub.org.uk
EFF
EFF.ORG
THE BRAIN TUMOUR CHARITY
thebraintumourcharuty.org
Chat
X
Pocketmags Support