Tuesday, July 14, 2009

How do you program an instruction set architecture in c++?

i have a program that's due this sunday and i have no idea how to start it.....it's one part of many....a small part...but i still don't know how to start it off

How do you program an instruction set architecture in c++?
It almost sounds as if your project is going to eventually become a "virtual machine", or better yet a simulated computer.





The first step is to determine your instruction set. The computer will probably have the ability to perform simple arithmetic, such as add, subtract, multiply, divide, shift, jump to memory locations, etc. Each of these operations will use a unique sequence of 'bits' to identify the specific operation. For example, you can say that your machine only supports only sixteen instructions. Because it is sixteen, we can identify each instructions with four bits:





MOV 0000 - move data


ADD 0001 - add data


SUB 0010 - subtract data


MPY 0011 - multiply data


DIV 0100 - divide data


AND 0101 - bitwise 'and'


OR 0110 - bitwise 'or'


XOR 0111 - bitwise 'exclusive or'


NOT 1000 - invert bits


JMP 1001 - unconditional jump


CMP 1010 - compare values


JEQ 1011 - jump if equal


JL 1100 - jump if less than


JG 1101 - jump if greater than


JLE 1110 - jump if less or equal


JGE 1111 - jump if greater or equal





Once you determine the instructions the computer will support, you need to determine your addressing modes. For example, an "add" instruction can work with two registers, two memory addresses, a memory address and a register, or indirect combinations of both. For each addressing mode, we will assign a sequence of bits. I'm going to choose only four addressing modes, "immediate" (where the data in the instruction is taken as a literal value), "memory" (where the data in the instruction is a memory address), "register" (where the data in the instruction is a register number) and "indirect" (where the value in a register is a memory address to where the data points). I will use two bits to identify the source addressing mode, an another two bits to identify the destination addressing mode. If the instruction only uses one destination, the second two bits would be used. This is now a total of four bits which we combine with the first four bits of the opcode. Below are the bits we will use for the modes:





Immediate - 00


Register - 01


Memory - 10


Indirect Register - 11





Now, we have defined a fairly competent instruction set that identifies the operations and addressing modes with only one byte of data!





Just to show "mnemonics" of the assembly language of our computer, with addressing modes, consider the following:





ADD 10, R2 - Add number 10 to register 2 (immediate/register)





ADD R1, R2 - Add register 1 to register 2 (register/register)





ADD @MEM1, R2 - Add value in memory at MEM1 to register 2 (memory/register)





ADD *R1, R2 - Add value pointed to by address in register 1 to register 2 (indirect/register)





Now, they would equate, in bits to the following:


ADD 10, R2 0001 00 01 (11 in hexadecimal)


ADD R1, R2 0001 01 01 (15 in hexadecimal)


ADD @MEM1, R2 0001 10 01 (19 in hexadecimal)


ADD *R1, R2 0001 11 01 (1D in hexadecimal)





Note that each instruction and addressing mode is one byte, but only part of the complete instruction. You can use the bitwise and (%26amp;) operator in C++ in order to mask off the opcode and addressing modes from that single byte (unsigned char). So:





opcode = byte %26amp; 0xF0; (1111 0000)


sourcemode = byte %26amp; 0xC0; (0000 1100)


destmode = byte %26amp; 0x03; (0000 0011)





Once we've determine the addressing mode for each one, the next sequence of bytes after the opcode and mode would be either the immediate value, a register value or a memory address. The remainder of each instruction can be either a fixed or variable number of bytes, that is your choice.





From there, you just process a stream of bytes and you can get the extract the operation, addressing mode, data for source and destination (or just destination if its a single operand). Next, your "virtual machine" needs to simulate the operations, keep track of the values in memory and in the registers, etc.





Hopefully this is enough to get you started.
Reply:Maybe i am wrong, but C++ doest not provide any support to interact with kernel directly. If you can use C, then it is possible by using ROM-BIOS functions like int86(),int86x(),intdosx()........

elephant ear

No comments:

Post a Comment