Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How can I read assemble code ?

by xiaoyafeng (Deacon)
on Oct 12, 2007 at 05:11 UTC ( [id://644376]=perlquestion: print w/replies, xml ) Need Help??

xiaoyafeng has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, recently I've read Computer Systems: A Programmer's Perspective.ISBN 9780130340740 This is a wonderful work for a new person to program such as me, and It make me see the codes and applications from a brand new perspective which I've never image before!
In the book, author use C and assemble code to illustrate relation of software and hardware and tell us all code in variety of advance language should be translated into machine language that CPU can understand.
OK, Back to my question, as I understand, perl code also need to be compiled and excuted(like C). So Is there a way to see assemble code translated by perl compiler as gcc -S does?

Please answer or point out my misunderstanding whether the question I pose is right or not.
Thanks in advance!!!

I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Replies are listed 'Best First'.
Re: How can I read assemble code ?
by ady (Deacon) on Oct 12, 2007 at 06:27 UTC
    First you'll have to understand, that Perl program execution is a mixture of parsing, interpretation and compilation :
    Quote
    The perl executable you are using has two distinct stages. First comes the frontend, which is certainly a compiler of sorts. It compiles your perl program source into a parse tree. This compiler then performs various optimizations such as one would find in any other compiler, including throwing out unreachable code, reducing constant expressions to their results, and loading in certain library definitions. It is at this point that the use statements get run, since they are semantically equivalent to BEGIN{} blocks wrapping a require and an import() class-method call against the included module.
    End of compilation.
    Next comes the backend, which is certainly an interpreter of sorts; let's call it a PP interpreter for now, just because. While what it actually executes is a parse tree and not byte code per se, still we would not go wrong in classifying this backend as a byte-code interpreter (like java or python). This is useful in particular when it comes to distinguishing these languages from ``pure'' interpreters, such as most shell and tcl implementations you happen to run. This is where any requires not wrapped in BEGINs occur.. . .br>

    From the frontend (the ``source-code to parse-tree'' compiler), you can get at the backend (the PP interpreter) via a BEGIN subroutine. Likewise, to go the other way (get back to the compiler from the interpreter), you can use an eval("string") or a s/foo/bar/ee notation. By the way, despite appearances to the contrary, it turns out that an eval { BLOCK } and s/foo/bar/e are not actually hooks back to the compiler; it already handled them long ago and far away.)


    Put another way
    Quote
    When the Perl compiler is fed a Perl program, the first task it performs is lexical analysis: breaking down the program into its basic syntactic elements (often called tokens). If the program is:
    print "Hello, world!\n";
    the lexical analyzer breaks it down into three tokens: print, "Hello, world!\n", and the final semicolon. The token sequence is then parsed, fixing the relationship between the tokens. In Perl, the boundary between lexical analysis and parsing is blurred more than in other languages. (Other computer languages, that is.
    . . .
    Once a program has been parsed . . ., it is compiled into a tree of opcodes representing low-level operations, and finally that tree of operations is executed--unless you invoked Perl with the -c ("check syntax") switch, which exits upon completing the compilation phase. . . . As the tree of opcodes constituting a compiled Perl program is executed, Perl values are created, manipulated, and destroyed.

    For details, see:
    Programming Perl: Internals and Externals
    Perl in a nutshell: The Perl Interpreter
    Introduction to the Perl Compiler-Translator
    quote:
    Perl has always had a compiler: your source is compiled into an internal form (a parse tree) which is then optimized before being run. Since version 5.005, Perl has shipped with a module capable of inspecting the optimized parse tree (B), and this has been used to write many useful utilities, including a module that lets you turn your Perl into C source code that can be compiled into an native executable.

    The B module provides access to the parse tree, and other modules (``back ends'') do things with the tree. Some write it out as bytecode, C source code, or a semi-human-readable text. Another traverses the parse tree to build a cross-reference of which subroutines, formats, and variables are used where. Another checks your code for dubious constructs. Yet another back end dumps the parse tree back out as Perl source, acting as a source code beautifier or deobfuscator. Because its original purpose was to be a way to produce C code corresponding to a Perl program, and in turn a native executable, the B module and its associated back ends are known as ``the compiler'', even though they don't really compile anything. Different parts of the compiler are more accurately a ``translator'', or an ``inspector'', but people want Perl to have a ``compiler option'' not an ``inspector gadget''. What can you do?

    Best regards,
    Allan Dystrup
Re: How can I read assemble code ?
by chromatic (Archbishop) on Oct 12, 2007 at 06:00 UTC
    Back to my question, as I understand, perl code also need to be compiled and excuted(like C).

    That's not exactly how it works. The way perlcc worked was to write a C program that contained the Perl interpreter as well as the source code in a serialized format. The program launched the Perl interpreter, then fed it the source code. If you think about what it takes to run Perl, you'll realize that you need the whole interpreter around in case you have an eval STRING construct of some kind. There's no direct translation of Perl statements to assembly language, which I think is what you wanted.

    The whole point is moot though because perlcc never worked very well and no one wants to maintain it.

Re: How can I read assemble code ?
by roboticus (Chancellor) on Oct 12, 2007 at 10:51 UTC
    xiaoyafeng:

    You might want to try the B::Terse and similar modules. They will show you the bytecode generated by the perl compiler. It's not exactly assembler, but can be very useful in learning some of the fundamentals of the system, much like the native assembly language is very useful in understanding many concepts.

    Trivial example:

    $ perl -MO=Terse -e 'print "foo!\n"' LISTOP (0x10122248) leave [1] OP (0x10033b50) enter COP (0x100253d8) nextstate LISTOP (0x10122220) print OP (0x1002fcf0) pushmark SVOP (0x100252e0) const [2] PV (0x10010f90) "foo!\n" -e syntax OK

    ...roboticus

      Terse was replaced with Concise (and now Terse is just a wrapper that calls Concise). Concise, by default shows a bit more info:
      $ perl -MO=Concise -e 'print "foo!\n"' 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 5 <@> print vK ->6 3 <0> pushmark s ->4 4 <$> const[PV "foo!\n"] s ->5 -e syntax OK
      Or in execution order, instead of the syntax tree:
      $ perl -MO=Concise,-exec -e 'print "foo!\n"' 1 <0> enter 2 <;> nextstate(main 1 -e:1) v 3 <0> pushmark s 4 <$> const[PV "foo!\n"] s 5 <@> print vK 6 <@> leave[1 ref] vKP/REFC -e syntax OK
      See B::Concise for what all that densely packed info means.
Re: How can I read assemble code ?
by GrandFather (Saint) on Oct 12, 2007 at 08:59 UTC

    An example may help. Ook! is a small language which makes it fairly easy to write an interpreter for it. For a sample see my attempt at a Perl Ook interpreter. Note that there is no translation to assembly language. A minor translation to an internal representation of the language tokens makes the implementation cleaner, but doesn't generate anything that is new or different than the Ook! source code.

    So, how does it work? It works by creating a mini-computer in software (a virtual machine) and runs the code in that environment - no assembly required (or even batteries).


    Perl is environmentally friendly - it saves trees
Re: How can I read assemble code ?
by Gangabass (Vicar) on Oct 12, 2007 at 05:53 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://644376]
Approved by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-25 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found