Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Parsing through instructions

by DaveMonk (Acolyte)
on Apr 08, 2012 at 21:53 UTC ( [id://964037]=note: print w/replies, xml ) Need Help??


in reply to Re: Parsing through instructions
in thread Parsing through instructions

Ok so this is part of what I have:
byte 1 byte 2 (optional) bits [ 7 6 5 4 3 2 1 0 ] [ 7 6 5 4 3 2 1 0 ] opcode - - - mod - operand1 - - - - operand2 - - - - - - - -
And instructions:
opcode | instruction | operands (mod 0) | operands (mod 1) -------+-------------+------------------+----------------- 0x00 | jmp | r1 | r2:r1 0x01 | movr | r1, r2 | rx, imm 0x02 | movm | r1, [ds:r2] | [ds:r1], r2 0x03 | add | r1, r2 | r1, imm flags cmp r1, r2 instruction results in: r1 == r2 => fl = 0 r1 < r2 => fl = 0xff r1 > r2 => fl = 1 jmpe r1 => if (fl == 0) jmp r1 else nop
I am then given:
0x00 0x00 0x00 0x01 0x03 0x02 0x03 0x02 0x03 0x02
But pretty much like you said, I am in need of simulating a processor with given instructions.

Replies are listed 'Best First'.
Re^3: Parsing through instructions
by GrandFather (Saint) on Apr 08, 2012 at 22:14 UTC

    So the key information you need is that you need to use the bit operators to manipulate the bits in each byte to extract the interesting bit fields. In particular use & (bitwise and) and a bit mask to isolate bit fields, and use the bitwise shift operators >> and << to move fields of bits about. For example, to isolate the op code you'd do something like:

    use strict; use warnings; my $byte = 0b00100100; my $opCode = ($byte & 0b11100000) >> 5; printf "Opcode is 0x%02X\n", $opCode;

    Prints:

    Opcode is 0x01

    See perlop for details about the various Perl operators.

    True laziness is hard work
      Awesome man, but I still need some help figuring out how to do this for the other 8 instructions, and then run it (the asm code) and dump to a file. But I have been playing around with perlop :)

        There is nothing more I can tell you that wouldn't amount to writing the code for you. If you can't take it from here then you have been set a task that you are not currently equipped to handle. The rest of the problem is really just applying the mask and shift I've already demonstrated to get at the individual op code fields, and then process the data.

        I suggest you play for a while and generate some code of your own. If you are still struggling show us what you have written along with your input data (the sample data you showed already is very unlikely to be real) and the expected output, then describe how it doesn't work for you.

        True laziness is hard work

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-04-23 07:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found