ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-list
python-list
Re: list/dictionary as case statement ?
by Hendrik van Rooyen other posts by this author
Jan 2 2007 10:44PM messages near this date
Re: list/dictionary as case statement ? | Re: list/dictionary as case statement ?
"Stef Mientki" <S.Mientki-nospam@[...].nl>  wrote:

>  
>  If I'm not mistaken, I read somewhere that you can use 
>  function-names/references in lists and/or dictionaries, but now I can't 
>  find it anymore.
>  
>  The idea is to build a simulator for some kind of micro controller (just 
>  as a general practise, I expect it too be very slow ;-).
>  
>  opcodes ={
>     1: ('MOV', function1, ...),
>     2: ('ADD', function2, ),
>     3: ('MUL', class3.function3, )
>     }
>  
>  def function1
>     # do something complex
>  
>  
>  Is this possible ?

the short answer is : "Yes"

a slightly longer answer depends on what you want to do

If you want to write a simple assembler, your dict will look something like:

Mnemonics_to_Opcodes = 
{"MOV": [Functionnameforopcodeandtwooperands,movopcode],
"NOP":[Functionnameforopcodeonly,nopopcode],
"SETB":[Functionnameforopcodeandoneoperand,setbopcode],
"JMP":[Functionnameforopcodeandoneoperand,jmpopcode],
...
}

if you want to write a simulator only that "executes" the hex or binary,
then your dict needs to be something like this:

Opcodes =
{movopcode:[MovFunction,movinstructionlen],
nopopcod:[NopFunction,nopinstructionlen],
setbopcode:[SetbFunction,setbinstructionlen],
jmpopcode:[JmpFunction,jmpinstructionlen],
...
}

and then you write an "instruction fetcher" based on the
Program Counter that uses the length data in the dict and
calls the various functions with the operands as fetched from
memory. The easiest is to make the PC a global...

It works well - and it is surprisingly fast too...
And its easy if the opcodes are all say one byte,
else you need an opcode length field too, and fancier
parsing.

This is not the hassle with this kind of thing - the hassle comes
in trying to simulate the behaviour of the I/O of a real system...

hth - Hendrik


-- 
http://mail.python.org/mailman/listinfo/python-list
Thread:
Stef Mientki
Bruno Desthuilliers
Bjoern Schliessmann
Tom Plunket
Stef Mientki
Bjoern Schliessmann
Mrab
Tom Plunket
Hendrik van Rooyen
Gary Herron
Stef Mientki
Grant Edwards

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved