Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Writing an assembler in Python

2,157 views
Skip to first unread message

Giorgos Tzampanakis

unread,
Feb 22, 2010, 9:24:00 PM2/22/10
to
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?

Paul Rubin

unread,
Feb 22, 2010, 9:33:40 PM2/22/10
to

One "dead simple" option is the re module.

Lawrence D'Oliveiro

unread,
Feb 22, 2010, 10:08:19 PM2/22/10
to
In message <Xns9D28186AF890Cfd...@127.0.0.1>, Giorgos
Tzampanakis wrote:

> 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.

Ed Keith

unread,
Feb 22, 2010, 10:47:26 PM2/22/10
to pytho...@python.org, Lawrence D'Oliveiro
> Subject: Re: Writing an assembler in Python
> --
> http://mail.python.org/mailman/listinfo/python-list
>

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

-EdK

Ed Keith
e_...@yahoo.com

Blog: edkeith.blogspot.com



Tim Roberts

unread,
Feb 23, 2010, 12:57:22 AM2/23/10
to

Yes, indeed. I have implemented TWO different FPGA-based microassemblers
in Python using the essentially undocumented but magically delicious
re.Scanner class. Simple and flexible.
--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Robert Kern

unread,
Feb 23, 2010, 11:27:49 AM2/23/10
to pytho...@python.org
On 2010-02-22 21:47 PM, Ed Keith wrote:
>> Subject: Re: Writing an assembler in Python
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
> 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

mk

unread,
Feb 23, 2010, 10:00:18 AM2/23/10
to pytho...@python.org

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

Regards,
mk

Anh Hai Trinh

unread,
Feb 23, 2010, 10:14:16 AM2/23/10
to
On Feb 23, 10:08 am, Lawrence D'Oliveiro <l...@geek-

central.gen.new_zealand> wrote:
>
> 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.

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

Lawrence D'Oliveiro

unread,
Feb 23, 2010, 6:11:26 PM2/23/10
to
In message <da970fce-bd6b-4eb3...@k5g2000pra.googlegroups.com>, Anh Hai Trinh wrote:

> On Feb 23, 10:08 am, Lawrence D'Oliveiro <l...@geek-central.gen.new_zealand> wrote:
>>
>> 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.
>
> 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

Albert van der Horst

unread,
Mar 2, 2010, 1:48:59 PM3/2/10
to
In article <Xns9D28186AF890Cfd...@127.0.0.1>,

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
--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

Holger Mueller

unread,
Mar 2, 2010, 4:26:59 PM3/2/10
to

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

scnr
Holger
--
http://www.kati-und-holger.de/holgersblog.php

Gregory Ewing

unread,
Mar 3, 2010, 1:52:02 AM3/3/10
to
> In article <Xns9D28186AF890Cfd...@127.0.0.1>,
> Giorgos Tzampanakis <gt...@hw.ac.uk> wrote:
>
>>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 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.

--
Greg

member thudfoo

unread,
Mar 3, 2010, 11:43:24 AM3/3/10
to pytho...@python.org
> --
> http://mail.python.org/mailman/listinfo/python-list
>

I used Plex.

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

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

iansu...@gmail.com

unread,
Jun 19, 2018, 8:41:22 PM6/19/18
to
What does the code look like to insert assembler into python and how does that code send information back to python.

It seems you wrote that python is a good compiler for assembly. If possible I want to add assembly to my python.

Steven D'Aprano

unread,
Jun 19, 2018, 10:36:01 PM6/19/18
to
Who are you talking too?




--
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson

Dan Stromberg

unread,
Jun 19, 2018, 10:54:51 PM6/19/18
to
On Tue, Jun 19, 2018 at 7:33 PM, Steven D'Aprano <
steve+comp....@pearwood.info> wrote:

> On Tue, 19 Jun 2018 17:41:11 -0700, iansuderman wrote:
>
> Who are you talking too?
>

If Python does inline assembler, this is the first I've heard of it.

You can do something kind of like this (but nicer) using
https://pypi.org/project/numba/ Just make sure to use it in "no-python"
mode, not "object" mode.

HTH

> <https://mail.python.org/mailman/listinfo/python-list>

dieter

unread,
Jun 20, 2018, 1:16:57 AM6/20/18
to
iansu...@gmail.com writes:

> What does the code look like to insert assembler into python and how does that code send information back to python.

Python is a "high level" language: it tries hard to hide many
"low level" details such as addresses and memory management.
Thus, it is quite far away from low level assembler code.

You can use something like "cython" to compile something
similar to Python source code into C/C++ code - either
for direct optimizations or to interface with C/C++
libraries. In the second case, the C/C++ functions can include
assembly code (provided this is supported by the compiler).

"cython" handles most of the difficulties of Python's C API,
thus typically providing for (rather) safe Python-C/C++ interaction.
I suggest to read its documentation.

0 new messages