[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Writing an assembler in Python

Giorgos Tzampanakis

2/23/2010 2:24:00 AM

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?
11 Answers

Paul Rubin

2/23/2010 2:34:00 AM

0

Giorgos Tzampanakis <gt67@hw.ac.uk> writes:
> 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.

Lawrence D'Oliveiro

2/23/2010 3:08:00 AM

0

In message <Xns9D28186AF890Cfdnbgui7uhu5h8hrnuio@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/cros... for an example.

Tim Roberts

2/23/2010 5:57:00 AM

0

Paul Rubin <no.email@nospam.invalid> wrote:
>
>Giorgos Tzampanakis <gt67@hw.ac.uk> writes:
>> 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.

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, timr@probo.com
Providenza & Boekelheide, Inc.

mk

2/23/2010 3:00:00 PM

0

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

Chick

2/23/2010 3:14:00 PM

0

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/cros... for an example.

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

Robert Kern

2/23/2010 4:28:00 PM

0

On 2010-02-22 21:47 PM, Ed Keith wrote:
>> Subject: Re: Writing an assembler in Python
>> 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/cros... for
>> an example.
>> --
>> http://mail.python.org/mailman/listinfo/p...
>>
>
> Not a bad idea, has anyone tried this for x86 machine code?

http://www.c...

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

Lawrence D'Oliveiro

2/23/2010 11:11:00 PM

0

In message <da970fce-bd6b-4eb3-bb66-3ca52cc0fd4a@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/cros... 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

3/2/2010 6:49:00 PM

0

In article <Xns9D28186AF890Cfdnbgui7uhu5h8hrnuio@127.0.0.1>,
Giorgos Tzampanakis <gt67@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 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...

Holger Mueller

3/2/2010 9:27:00 PM

0

Giorgos Tzampanakis <gt67@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?

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

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

Gregory Ewing

3/3/2010 6:52:00 AM

0

> In article <Xns9D28186AF890Cfdnbgui7uhu5h8hrnuio@127.0.0.1>,
> Giorgos Tzampanakis <gt67@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