JSON65: a JSON parser in 6502 assembly language

Programming the 6502 microprocessor and its relatives in assembly and other languages.
Post Reply
ppelleti
Posts: 9
Joined: 10 Aug 2018
Location: Ventura, CA USA
Contact:

JSON65: a JSON parser in 6502 assembly language

Post by ppelleti »

I just finished writing my first 6502 project in 25 years. It is a JSON parser for the 6502:

https://github.com/ppelleti/json65

The event-driven parser is written entirely in 6502 assembly language, although it provides a C API (meant to be called from cc65 programs). There are some helper routines (for things such as building up a tree representation) that are written in C.

Features:
  • Works on all members of the 6502 family. (No 65C02 instructions.)
  • Works on any platform supported by cc65.
  • Choose between an event-driven (SAX-style) parser, and a tree representation.
  • Supports incremental parsing (entire file does not need to fit in memory).
  • File size and line length are unlimited (although strings are limited to 255 bytes or less).
  • Keeps track of line number and column number (e. g. for error reporting).
  • Generates helpful error messages.
  • Fully reentrant. No global variables. You can use multiple parsers at once.
  • Accepts all common line endings (CR, LF, or CRLF) automatically.
whartung
Posts: 1004
Joined: 13 Dec 2003

Re: JSON65: a JSON parser in 6502 assembly language

Post by whartung »

That's really nice!

Did you write a C version first and port it, or just start with raw assembly?

Be curious how much slower a C version is.

Since json.s is < 1800 lines, fair to assume that this is 1500-2000 bytes of total code when assembled?
ppelleti
Posts: 9
Joined: 10 Aug 2018
Location: Ventura, CA USA
Contact:

Re: JSON65: a JSON parser in 6502 assembly language

Post by ppelleti »

whartung wrote:
Did you write a C version first and port it, or just start with raw assembly?
I just wrote it in assembly to begin with.
whartung wrote:
Since json.s is < 1800 lines, fair to assume that this is 1500-2000 bytes of total code when assembled?
Here are the sizes for all of the files:

Code: Select all

json65.o        2240 bytes
json65-string.o  291 bytes
json65-tree.o   1300 bytes
json65-quote.o   226 bytes
json65-print.o   710 bytes
json65-file.o   1378 bytes
total           6145 bytes
(My build script, run-tests.pl, parses the .map files and prints a size summary.)

If all you want is the event-driven parser, all you need is json65.o, so that's just a little over 2K. json65.s uses some tables in RODATA, so it could probably be made smaller by using a chain of CMP and branch instead of a lookup table.

Of the six total files, I wrote three in assembly language, and three in C. This wasn't particularly scientific, but I used assembly language for the ones that seemed more performance-critical (like examining each byte of a string one-by-one), and I used C for the files that had a lot of local variables and a lot of if statements, that I thought would be more annoying to write in assembly language.
Post Reply