Writing an assembler in Python

  • Thread starter Giorgos Tzampanakis
  • Start date
G

Giorgos Tzampanakis

I'm implementing a CPU that will run on an FPGA. I want to have a
(dead) simple assembler that will generate the machine code for
me. I want to use Python for that. Are there any libraries that
can help me with the parsing of the assembly code?
 
P

Paul Rubin

Giorgos Tzampanakis said:
I'm implementing a CPU that will run on an FPGA. I want to have a
(dead) simple assembler that will generate the machine code for
me. I want to use Python for that. Are there any libraries that
can help me with the parsing of the assembly code?

One "dead simple" option is the re module.
 
L

Lawrence D'Oliveiro

Giorgos said:
I'm implementing a CPU that will run on an FPGA. I want to have a
(dead) simple assembler that will generate the machine code for
me.

Let me suggest an alternative approach: use Python itself as the assembler.
Call routines in your library to output the code. That way you have a
language more powerful than any assembler.

See <http://github.com/ldo/crosscode8> for an example.
 
M

mk

Giorgos said:
I'm implementing a CPU that will run on an FPGA. I want to have a
(dead) simple assembler that will generate the machine code for
me. I want to use Python for that. Are there any libraries that
can help me with the parsing of the assembly code?

I'm not sure about your field of application (never done anything like
that), but I found pyparsing highly usable.

Regards,
mk
 
R

Robert Kern

Not a bad idea, has anyone tried this for x86 machine code?

http://www.corepy.org/

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
L

Lawrence D'Oliveiro

SyntaxError: Non-matching "#end if" in crosscode8.py:345

What mismatch? Line 345 is the last line of this routine:

def org(self, addr) :
"""sets the origin for defining subsequent consecutive memory contents."""
self.curpsect.setorigin(self.follow(addr))
self.lastaddr = self.curpsect.origin
return self # for convenient chaining of calls
#end org
 
A

Albert van der Horst

I'm implementing a CPU that will run on an FPGA. I want to have a
(dead) simple assembler that will generate the machine code for
me. I want to use Python for that. Are there any libraries that
can help me with the parsing of the assembly code?

I have a pentium assembler in perl on my website below.
(postit-fixup principle).
You could borrow some idea's, if you can read perl.
The main purpose is to have a very simple and straightforward
assembler at the expense of ease of use.

Groetjes Albert
 
H

Holger Mueller

Giorgos Tzampanakis said:
I'm implementing a CPU that will run on an FPGA. I want to have a
(dead) simple assembler that will generate the machine code for
me. I want to use Python for that. Are there any libraries that
can help me with the parsing of the assembly code?

Why coding assembler if you can type in hexdumps...

scnr
Holger
 
G

Gregory Ewing

I wrote a PIC assembler in Python once. I didn't bother
with any parsing libraries. I used a regular expression
to split the input into tokens, then wrote ad-hoc
parsing code in Python.
 
M

member thudfoo

I wrote a PIC assembler in Python once. I didn't bother
with any parsing libraries. I used a regular expression
to split the input into tokens, then wrote ad-hoc
parsing code in Python.

I used Plex.

The lexer is here: http://pastebin.com/9Rm4rDfu

The target for the assembler is a toy single-byte processor.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top