Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Simple Assembly Interpreter Develop a simple assembly code interpreter for a the

ID: 3842895 • Letter: S

Question

Simple Assembly Interpreter

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

This processor only has seven instructions:

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.

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:

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 <stdio.h>
#include <string.h>

/*
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 <stdio.h>
#include <string.h>

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

   return 0;
}

Explanation / Answer

#include <iostream>
using namespace std;
#define DEBUG
#define MIN(a,b) (((a)<(b)) ? a : b)
int main () {
int i, j;
i = 100;
j = 30;  
#ifdef DEBUG
cerr <<"Trace: Inside main function" << endl;
#endif
#if 0
cout << MKSTR(HELLO C++) << endl;
#endif
cout <<"The minimum is " << MIN(i, j) << endl;
#ifdef DEBUG
cerr <<"Trace: Coming out of main function" << endl;
#endif
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote