A simple asm-to-ophis syntax converter in Python

Topics pertaining to the emulation or simulation of the 65xx microprocessors and their peripheral chips.
Post Reply
scotws
Posts: 576
Joined: 07 Jan 2013
Location: Just outside Berlin, Germany
Contact:

A simple asm-to-ophis syntax converter in Python

Post by scotws »

As one of the people using the Ophis assembler (https://github.com/michaelcmartin/Ophis), I'm stuck with lots of code that is written in a more "standard" syntax. So I've put together a small Python script to help me convert from one format to another: asm2ophis (included).

What it does is convert things like "ALIAS = NUMBER" to ".alias ALIAS NUMBER", add semicolons to labels, and justify comments the way I prefer them, etc. One of the biggest problems was converting "unmarked" inline comments -- remarks that don't start with a ";" such as in VTL2 -- to the Ophis syntax. It's only half-automatic, the end result needs to be edited by hand for a whole bunch of things, but it does relieve me of a lot of the time-consuming grunt work. What it does not do is include any of the more advanced Ophis features. As a Python program, it should run on any platform. I've tried to comment as much as I can.

So far, I've only used it with VTL 2.0 and FIG Forth, two of the large programs I hope to incorporate in the Übersquirrel. Anything else is bound to create new problems. A more serious version of this tool would have to first figure out which assembler format is being used first and then approach the whole problem appropriately, but for the moment, I'm getting by with this -- pardon my German -- half-assed script.

I've included a file with the raw output of processing the VTL02a2b source so people can get a feeling for what this can't or cannot do. Below is the listing of the problems asm2ophis spit out on the command line when this was generated. You'll notice a bunch of warnings about "inc" instructions -- there are so many variations even in the syntax of those two files I just ask the user to handle them ("inc", "inc a", "inc <alias>", "inc <address>"). This is true for lsr, asl, dec, and a few others.

There is a whole bunch of stuff that could be improved very quickly, but I'll be doing other things for the next couple of days and thought I'd offer what I had here in case somebody could use this. Of course, I'm grateful for suggestions and corrections.

Code: Select all

Line 2: Error: Found unexpected dot-command .lf
Line 3: Error: Found unexpected dot-command .cr
Line 4: Error: Found unexpected dot-command .tf
Line 157: Error: Found unexpected dot-command .or
Line 163: Warning: Found '/', convert manually.
Line 167: Warning: Found '/', convert manually.
Line 270: Warning: Opcode 'dec' found, convert manually
Line 284: Warning: Opcode 'inc' found, convert manually
Line 286: Warning: Opcode 'inc' found, convert manually
Line 320: Warning: Opcode 'dec' found, convert manually
Line 321: Warning: Opcode 'dec' found, convert manually
Line 400: Warning: Opcode 'inc' found, convert manually
Line 401: Warning: Opcode 'inc' found, convert manually
Line 415: Warning: Opcode 'inc' found, convert manually
Line 480: Warning: Opcode 'rol' found, convert manually
Line 486: Warning: Opcode 'rol' found, convert manually
Line 570: Warning: Opcode 'dec' found, convert manually
Line 595: Warning: Opcode 'inc' found, convert manually
Line 604: Warning: Opcode 'inc' found, convert manually
Line 606: Warning: Opcode 'inc' found, convert manually
Line 661: Warning: Opcode 'lsr' found, convert manually
Line 663: Warning: Opcode 'ror' found, convert manually
Line 663: Warning: Found '/', convert manually.
Line 666: Warning: Opcode 'asl' found, convert manually
Line 668: Warning: Opcode 'rol' found, convert manually
Line 687: Warning: Opcode 'asl' found, convert manually
Line 689: Warning: Opcode 'rol' found, convert manually
Line 690: Warning: Opcode 'rol' found, convert manually
Line 691: Warning: Opcode 'rol' found, convert manually
Line 701: Warning: Opcode 'inc' found, convert manually
Line 702: Warning: Opcode 'dec' found, convert manually
Line 716: Warning: Opcode 'inc' found, convert manually
Line 724: Warning: Opcode 'inc' found, convert manually
Line 728: Warning: Opcode 'rol' found, convert manually
Line 747: Warning: Opcode 'asl' found, convert manually
Line 752: Warning: Opcode 'asl' found, convert manually
Line 753: Warning: Opcode 'rol' found, convert manually
Line 809: Warning: Found '/', convert manually.
Line 815: Warning: Opcode 'inc' found, convert manually
Line 838: Warning: Found '/', convert manually.
Line 850: Warning: Opcode 'inc' found, convert manually
Warning: File may contain unmarked comments (line 752 for instance).

Done. Wrote 870 lines.
Attachments
vtl02-test.asm
(24.95 KiB) Downloaded 171 times
asm2ophis-b01.py.zip
(7.18 KiB) Downloaded 169 times
User avatar
barrym95838
Posts: 2056
Joined: 30 Jun 2013
Location: Sacramento, CA, USA

Re: A simple asm-to-ophis syntax converter in Python

Post by barrym95838 »

I feel slightly bad that I didn't use a more 'universal' source syntax for VTL02 ... I especially regret not placing
leading semi-colons on the end-of-line comments, as that would have eliminated >90% of the drudgery. I plan
to continue using San's cross-assembler, but I will be more considerate to others in future endeavors.

Thanks,

Mike
scotws
Posts: 576
Joined: 07 Jan 2013
Location: Just outside Berlin, Germany
Contact:

Re: A simple asm-to-ophis syntax converter in Python

Post by scotws »

Nah, it's fine, I learned a lot doing this. VTL02 is so (amazingly) short I probably could have done this all by hand in the same time, but you know how it is. -- At some point, I'll actually get around to getting it running with my kernel (which isn't ready yet), and then will probably go through again to see about adapting it to the 65c02. Again, amazing little program.
User avatar
barrym95838
Posts: 2056
Joined: 30 Jun 2013
Location: Sacramento, CA, USA

Re: A simple asm-to-ophis syntax converter in Python

Post by barrym95838 »

scotws wrote:
... running with my kernel (which isn't ready yet), and then will probably go through again to see about adapting it to the 65c02...
Hello again, scotws ... [bump]

As someone who knows and uses the 'c02 (others welcome to comment as well), do you like and use the TRB and TSB instructions much? I see an immediate and frequent use for BRA, PHX, PHY, PLX, PLY, STZ, and a few others, but I'm not so sure about those two, probably because my brain is entrenched in '02 mode from years of habit.

The reason that I'm asking is that I have a couple of op-code slots left in my 65m32 map, and I want to know if I should stick a 32-bit version of those two in there, or leave those slots open for something else that might wind up being more important.

Thanks,

Mike
User avatar
BigDumbDinosaur
Posts: 9427
Joined: 28 May 2009
Location: Midwestern USA (JB Pritzker’s dystopia)
Contact:

Re: A simple asm-to-ophis syntax converter in Python

Post by BigDumbDinosaur »

barrym95838 wrote:
As someone who knows and uses the 'c02 (others welcome to comment as well), do you like and use the TRB and TSB instructions much?
I use TRB and TSB in a lot of places.
x86?  We ain't got no x86.  We don't NEED no stinking x86!
User avatar
GARTHWILSON
Forum Moderator
Posts: 8773
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: A simple asm-to-ophis syntax converter in Python

Post by GARTHWILSON »

I use them quite a bit in I/O.
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
scotws
Posts: 576
Joined: 07 Jan 2013
Location: Just outside Berlin, Germany
Contact:

Re: A simple asm-to-ophis syntax converter in Python

Post by scotws »

I actually don't think I have used them at all yet, for the reason you just mentioned: Not on the '02, so don't think that way yet. I keep meaning to sit down and work through a bunch of examples to get comfortable with them, and then go back and look at my code again.
Martin_H
Posts: 837
Joined: 08 Jan 2014

Re: A simple asm-to-ophis syntax converter in Python

Post by Martin_H »

I just stumbled onto this thread.

I just re-purposed my Python TinyComp compiler's tokenizer to read 6502 assembler, and then pretty print it into the Ophis format. I haven't uploaded it to GitHub yet, but if there's interest I could upload it to my TinyComp repo.

As an aside I should probably back port the pretty print feature into TinyComp as that's kind of a useful feature.
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: A simple asm-to-ophis syntax converter in Python

Post by BigEd »

Please do share your code!
Martin_H
Posts: 837
Joined: 08 Jan 2014

Re: A simple asm-to-ophis syntax converter in Python

Post by Martin_H »

OK I uploaded it to my FIG Forth Github repo: https://github.com/Martin-H1/FigForth

It contains the original FIG FORTH source, my finished port, and asm2oph.py which converted about 95% of the source. It couldn't handle the equate to .alias directive, ASL A to ASL, .org to .origin, and .advance. But after processing and a few passes with Ophis everything looked reasonable to me. The Makefile contains a target Fig6502_asm.oph which runs the command:

Code: Select all

python asm2oph.py FIG6502.ASM > Fig6502_asm.oph
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: A simple asm-to-ophis syntax converter in Python

Post by BigEd »

Thanks!
Post Reply