6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri May 03, 2024 7:49 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Mon Apr 22, 2024 3:24 pm 
Offline

Joined: Sat Apr 20, 2024 4:01 pm
Posts: 1
ASM502
--------

[MODS: I hope this is the right place to most this??]

ASM502 is a 6502 assembler written in C99-compliant C code. It is small and light, and yet implements a useful subset of CA65 functionality.

Attachment:
asm502-115a.zip [62.49 KiB]
Downloaded 12 times


I can hear everyone shout "we dont need another 6502 assembler", and they are probably right! In defence ASM502 has a few interesting points - it is (fairly) modern, compatible with a useful subset of CA65, and it is small, it can even compile/run (with cc65) on a Commodore 64 or Plus 4.

ASM502 uses standard 6502 assembler syntax.

To compile a 6502 assembler source "myprog.s" use:
asm502 myprog.s myprog.prg
(Specify correct name/path at start, ie: ./asm502.elf myprog.s myprog.prg )
ASM502 generates the 2 byte header with the start address (from the .ORG statement in the code).
If you want to save the listing file use:
asm502 myprog.s myprog.prg >myprog.lst
Load "myprog.prg" on your emulator (or real comp) and start with RUN (or SYS XXXX etc).


Internally ASM502 works with 32 bit numbers (ie the default C integer) depending on the C compiler options used.
Numbers can be decimal (default), hex (precede with $) or binary (precede with %) ie:
.byte 160, $A0, %10100000 ; Number 160 in decimal, hex, binary..

Unary operators can preceed an expression:
<(expression) ; Low byte of expression
>(expression) ; High byte of expression
-(expression) ; Negate expression

ASM502 supports variables and can evaluate complex expressions. It uses C-style operators "<<,>>,=,!=,<,>,&,|,^,+,-,*,/,%".

expr1 << expr2 ; Shift expr1 left by expr2
expr1 >> expr2 ; Shift expr1 right by expr2
expr1 = expr2 ; True if equal
expr1 != expr2 ; True if not equal
expr1 < expr2 ; True if less
expr1 < expr2 ; True if more
expr1 & expr2 ; Bitwise AND
expr1 | expr2 ; Bitwise OR
expr1 ^ expr2 ; Bitwise EOR (EXCLUSIVE OR)
expr1 + expr2 ; Add
expr1 - expr2 ; Subtract
expr1 * expr2 ; Multiply
expr1 / expr2 ; Divide
expr1 % expr2 ; Modulus (remainder of division)

A variable must start with upper/lower case letter, then include numbers or underlines. ie:

My_var1 = (($123 + %1010 + 1234) * 2) & $ffff ; Assign const val to (My_var1)
.word (My_var1 >> 1) + (33 * $22), $abc1, 0 ; Use that in .word code definition..

Here are some example lines:

; Comments follow a semicolon.
label: ; lable can be a destination for a branch or jump or data.
lda #65 ; 6502 load accumulator with decimal 65.
.byte 1,2,3,4 ; Define 4 bytes.
.word $1234, $abcd ; Define 2 words (same as: .byte $34,$12,$cd,$ab)
.byte <label, >label ; Low and high byte of label


ASM502 supports a subset of assembler macro/conditional directives.

*=(expression) ; Set start-address of assembly code..
.org (expression) ; Set start-address of assembly code.. (alternative to *=.. )
.byte (expression),"strings".. ; Define some bytes in-line
.word (expression),(expression).. ; Define some 16 bit words inline (in lo/hi byte order)
.end ; Terminate assembly.

.if (expression) ; Conditional compile following code if (expression) non-zero..
; asm code here ..
.else ; Compile following code if (expression) is zero..
; asm code here..
.endif

.define var expression ; This is the same as "var=expression". more complex macro .define not (yet) implemented.

Most more complex directives (ie: macros) are not (yet) implemented.

Here is a more complete simple example program to print some text.

;---------------------------------------------------------------------------------------
; Simple program to print some text.

* = 828 ; Put code in C64 tape buffer, call with SYS 828.. (change for non-commodore computers..)
printchar = $ffd2 ; Routine to print accumulator as char on screen (change for non-commodore computers..)
IS_C64=1 ; Set variable IS_C64 to 1

printtext: ; jsr printtext starts this program.
ldx # 0
printloop: ; loop point.
ldy textdata,x ; get character to print
beq printdone ; Zero terminates print
txa
pha ; Save X on stack
tya
jsr printchar ; Print character in Accumulator
pla
tax ; Get X
inx
bne printloop
printdone:
rts

textdata:
.if IS_C64 ; Only compile following if IS_C64 is non zero
.byte "HELLO C64! ",0 ; Define some text bytes, zero at end.
.else
.byte "THIS IS NOT A C64! ",0
.endif
;---------------------------------------------------------------------------------------

to compile this program, cut and paste the above to a new text file called "testpr,s", then compile with:
asm502 testpr.s testpr.prg
Load "testpr.prg" on your emulator (or real comp) and start with SYS 828.

There are more extensive code examples included with ASM502.


ASM502 notes
--------------

The source code "asm502.c" is included, and released under the GNU GPL3 licence (see end of this document).

As things stand this program is a "work in progress". It started as an adaption of an earlier assembler of mine when I wanted something simpler and smaller than the CA65 (the CC65 assembler). Now CA65 is a very good assembler, but by necessity it has to deal with far more complexity due to it being part of a C compiler - segmentation, object files, library formats etc.

Compiling the ASM502 single-file source just needs a C99 compatible C compiler, and is as simple as:
cc asm502.c -o asm502 -Os # in Linux..

I have included pre-built binarys for 32 bit Windows and Linux, but if you have a C compiler I recommend that you just build a binary for your own system.

ASM502 is a work-in-progress, and has plenty of omissions, and some bugs too, but it is useable, so I thought I should put it out anyway - I havent done anything to it for a long while now.
The .include command is not included yet, but it would not be difficult to add. Other things like macro definitions and procedures would require more work, but are possible.

ASM502 is a two-pass assembler. As it stands it simply allocates the memory for the source file and loads it in one block, but it is designed so that it could compile the source line-by-line. That would be useful to make it more effective on platforms with limited memory.
It can, and has been compiled on minimal platforms like the Commodore 64, although some tweaks to memory allocation for arrays might need looking at first. For simplicity, source files on Commodore platforms should be converted to upper case - again, this could be fixed.

In the examples folder are portable assembler sources (+binaries) for FREAX (nice 2k game for c64/vic20/c16), LIFET (game of life in 126 bytes) and a few other odd demos and examples. These will also compile with CA65.

In the end I have found ASM502 interesting and useful, so maybe you will too!

=============================================================================
The source code is released as free software under the GNU GPL3 licence,
see GNU website: http://www.gnu.org/licenses/gpl-3.0.html
=============================================================================


Top
 Profile  
Reply with quote  
PostPosted: Mon Apr 22, 2024 3:35 pm 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1927
Location: Sacramento, CA, USA
I'm not prepared to compile and test it, but I glanced at your source and offer a thumbs-up. I'm not at all sure if it's a compliment, but our C coding styles are quite similar.

_________________
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!

Mike B. (about me) (learning how to github)


Top
 Profile  
Reply with quote  
PostPosted: Mon Apr 22, 2024 3:50 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10793
Location: England
Thumbs up from me to, just for the principle of the thing - a new assembler in simple C. The writer will have learnt a lot in doing this, most likely, and the code is out there for others to read and to learn from.

Welcome, and thanks for the offering!


Top
 Profile  
Reply with quote  
PostPosted: Tue Apr 23, 2024 3:33 am 
Offline
User avatar

Joined: Fri Aug 03, 2018 8:52 am
Posts: 746
Location: Germany
very nice! the fact that the compiler can run on an actual 6502 makes it already worth a lot.
though taking a peek at the code i'm a bit surprised you don't use <stdint.h> at all, considering how different regular c data types are between architectures (int is either 2 or 4 bytes between cc65 and gcc for example) and you designed this for portability, so i would very highly recommend to almost exclusively use stdint data types for any portable program to avoid nasty surprises with variable ranges. (especially since you have a define called "DEFINT" which is meant to be a specific amount of bits regardless of architecture. like why do that when stdint already solves that problem for you?)

also you didn't mention if it only supports NMOS 6502 opcodes or also the 65C02 opcodes. i would assume it's NMOS only, adding C02 opcodes shouldn't be that difficult though.
I'll take a closer look at the code later to see how exactly it works, maybe 65816 support could also be added (since if it compiles on the 6502 it will also compile on the 65816 with no problem, and then we'd have a modern-ish native assembler for the 65816, which would be sick!)

on a final note, you said C99, but looking at the code it appears to be pure C89 (it compiles on gcc with -std=c89). makes sense as CC65 itself is a C89 compiler (with some C99 stuff like single line comments) but then why say it's C99 when it doesn't make use of any of it's features?


Top
 Profile  
Reply with quote  
PostPosted: Tue Apr 23, 2024 5:02 am 
Offline

Joined: Mon Jan 19, 2004 12:49 pm
Posts: 677
Location: Potsdam, DE
Looking at the mnemonic name table, it doesn't include any of the cmos extensions, so it must be NMOS only at the moment. I haven't looked at the code in any detail, but adding the four character cmos mnemonics might be trivial - or tricky.

Neil


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: