Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Simple Assembly Interpreter Develop a simple assembly code interpreter for a theoretical processor with the following properties. This processor only has seven instructions: lod imm32

Simple Assembly Interpreter

Develop a simple assembly code interpreter for a theoretical processor with the following properties.

This processor only has seven instructions:

lod imm32 ; Load a 32-bit immediate value lod mem32 ; Load a 32-bit value from memory add imm32 ; Add a 32-bit immediate add mem32 ; Add a 32-bit value from memory sub imm32 ; Subtract a 32-bit immediate sub mem32 ; Subtract a 32-bit value from memory sto mem32 ; Stores the result into a single memory location 

You can assume this simple processor has only one register and 256 32-bit memory locations. Your simple assembler will given an array containing instructions and will convert the instructions into x86 code on the fly. You should be able to do this through a combination of C and inline assembly. The only parts that have to be in assembly are the code elements for the above instructions. For instance, a lod instruction could become a mov eax instruction.

The mem32 addresses are in terms of bytes. For instance, an address of 0x10 would refer to the 17th byte, or the 5th integer (0 based index).

Immediate values are hex numbers without a 0x in front. Memory address are hex numbers with the 0x in front.

char *linesOfCode[] = {  "lod 0x10", // mem32, hex value  "add 1010", // imm32, hex value  "sub 11", // imm32, hex value  "sto 0x14" // mem32, hex value  }; 

As noted, your project can use a combination of C and assembly. You can use C string processing functions from the C standard library.

Your function should look like the following:

int simpleAssembler(char** linesOfCode, int lineCount, int* memory); 

linesOfCode is an array of strings containing the code lines you are to execute. memory is a 256 element 32-bit integer array that is given to you. lineCount is the count of the number of lines in the linesOfCode array.

simpleAssembler should return a -1 if an error has occurred and 0 otherwise.

// Description: Main module, tests simpleAssembler function // Date: 5/11/2017

#include #include

/* lod imm32 ; Load a 32-bit immediate value lod mem32 ; Load a 32-bit value from memory add imm32 ; Add a 32-bit immediate add mem32 ; Add a 32-bit value from memory sub imm32 ; Subtract a 32-bit immediate sub mem32 ; Subtract a 32-bit value from memory sto mem32 ; Stores the result into a single memory location */

// Prototype int simpleAssembler(char** linesOfCode, int lineCount, int* memory);

//------------------------------------------------- int main() { int memory[256]; char *linesOfCode[] = { "lod 1010", "lod 0x10", "add 1010", "sub 11h", "sto 0x14" };

int ret = simpleAssembler(linesOfCode, 5, memory); if (ret != 0) printf("simpleAssembler error ");

return 0; }

// Module Name : simpleAssembler.c // Description: Code for simpleAssembler Interpreter // Date: 5/11/2017

#include #include

int simpleAssembler(char** linesOfCode, int lineCount, int* memory) { for (int i = 0; i < lineCount; i++) { printf("%d: %s ", (i+1), linesOfCode[i]); }

return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Datacasting How To Stream Databases Over The Internet

Authors: Jessica Keyes

1st Edition

007034678X, 978-0070346789

More Books

Students also viewed these Databases questions