input assembly program, then simulates its execution, and finally prints the content of registers and the main memory

Online tutoring services

Need help with this question or any other Computer Science assignment help task? Click on the button below to to hire an expert cheap.

I finsh some of them, but I cant execute it and print the answer, so I need someone to finish the remaining part

Get Help With a similar task to - input assembly program, then simulates its execution, and finally prints the content of registers and the main memory

Login to view and/or buy answers.. or post an answer
Additional Instructions:
Tutorial for Assignment 2.pdf Tutorial for Assignment 2 Zhang Jiahao 18042204R@connect.polyu.hk 1 Task • In this assignment, you need to implement a simulator for assembly program. • Example: Input text file (assembly program) Result Output irmovq $-5, %r0 irmovq $11, %r1 L1: addq %r0, %r1 rmmovq %r0, 0(%r1) halt Your simulator %r0..7: -5 6 0 0 0 0 0 0 M0..15: 0 0 0 0 0 0 -5 0 0 0 0 0 0 0 0 0 2 Task: example 3 irmovq $2, %r0 irmovq $3, %r1 cmpq %r1, %r0 jl L1 irmovq $4, %r2 L1: addq %r0, %r1 rmmovq %r0, 0(%r1) halt Register %r0 0 %r1 0 %r2 0 %r3 0 … … Memory M0 0 M1 0 M2 0 M3 0 M4 0 M5 0 M6 0 M7 0 … … Condition code CF 0 ZF 0 SF 0 OF 0 Task: example 4 Condition code CF 0 ZF 0 SF 0 OF 0 irmovq $2, %r0 irmovq $3, %r1 cmpq %r1, %r0 jl L1 irmovq $4, %r2 L1: addq %r0, %r1 rmmovq %r0, 0(%r1) halt Register %r0 2 %r1 0 %r2 0 %r3 0 … … Memory M0 0 M1 0 M2 0 M3 0 M4 0 M5 0 M6 0 M7 0 … … Task: example 5 Condition code CF 0 ZF 0 SF 0 OF 0 irmovq $2, %r0 irmovq $3, %r1 cmpq %r1, %r0 jl L1 irmovq $4, %r2 L1: addq %r0, %r1 rmmovq %r0, 0(%r1) halt Register %r0 2 %r1 3 %r2 0 %r3 0 … … Memory M0 0 M1 0 M2 0 M3 0 M4 0 M5 0 M6 0 M7 0 … … Task: example 6 Condition code CF 0 ZF 0 SF 1 OF 0 irmovq $2, %r0 irmovq $3, %r1 cmpq %r1, %r0 jl L1 irmovq $4, %r2 L1: addq %r0, %r1 rmmovq %r0, 0(%r1) halt Register %r0 2 %r1 3 %r2 0 %r3 0 … … Memory M0 0 M1 0 M2 0 M3 0 M4 0 M5 0 M6 0 M7 0 … … Task: example 7 Condition code CF 0 ZF 0 SF 1 OF 0 irmovq $2, %r0 irmovq $3, %r1 cmpq %r1, %r0 jl L1 irmovq $4, %r2 L1: addq %r0, %r1 rmmovq %r0, 0(%r1) halt Register %r0 2 %r1 3 %r2 0 %r3 0 … … Memory M0 0 M1 0 M2 0 M3 0 M4 0 M5 0 M6 0 M7 0 … … Task: example 8 Condition code CF 0 ZF 0 SF 1 OF 0 irmovq $2, %r0 irmovq $3, %r1 cmpq %r1, %r0 jl L1 irmovq $4, %r2 L1: addq %r0, %r1 rmmovq %r0, 0(%r1) halt Register %r0 2 %r1 5 %r2 0 %r3 0 … … Memory M0 0 M1 0 M2 0 M3 0 M4 0 M5 0 M6 0 M7 0 … … Task: example 9 Condition code CF 0 ZF 0 SF 1 OF 0 irmovq $2, %r0 irmovq $3, %r1 cmpq %r1, %r0 jl L1 irmovq $4, %r2 L1: addq %r0, %r1 rmmovq %r0, 0(%r1) halt Register %r0 2 %r1 5 %r2 0 %r3 0 … … Memory M0 0 M1 0 M2 0 M3 0 M4 0 M5 2 M6 0 M7 0 … … Task: example 10 Condition code CF 0 ZF 0 SF 1 OF 0 irmovq $2, %r0 irmovq $3, %r1 cmpq %r1, %r0 jl L1 irmovq $4, %r2 L1: addq %r0, %r1 rmmovq %r0, 0(%r1) halt Register %r0 2 %r1 5 %r2 0 %r3 0 … … Memory M0 0 M1 0 M2 0 M3 0 M4 0 M5 2 M6 0 M7 0 … … %r0..7: 2 5 0 0 0 0 0 0 M0..15: 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 Result Output Solution: overview • There are four main functions • Input: read data from a text file and store them into a string array (see page 13) . • Parse: parse each line and find the keyword and operators, e.g., “addq, %r0, %r1”, addq is the keyword, %r0 and %r1 are two operators. • Simulate: execute instructions and simulate them on register and memory arrays (two integer arrays). • Output: print results • There are four structures Global variables: string S[100], int reg[8], int mem[16], int flag[4] 11 Solution: pseudo-code int main(int argc, char ** argv){ Read(argv[1]);//S[i] denotes the ith instruction Initialize();//Initialize register, memory and flags int i=0; while (!isHalt(i)){ T = Parse(S[i]); if (T is a jump instruction){ i = ExecuteJump(T,i); } else { Execute(T); i++; } } Output(); // Print results; } 12 Solution: read data • string S[100] • getline(fs,S[i]); • fs.eof(); note: be careful about this function • No folder name 13 Solution: parse instruction line • Find instruction keywords • Total 12 keywords • Find operators • “halt”: no operators • “addq”, “subq”, “cmpq”: rA, rB, two operators • “je”, “jne”, “jg”, “jl”: Dest, one operators • “rrmovq”, “irmovq”, “rmmovq”, “mrmovq”: two operators • S[i].find(keyword[j]); • !!! Care the formats: e.g. “irmovq $-5, %r0” • !!! Care characters: whitespaces, line break, tab, parentheses, e.g., “rmmovq %r0, 0(%r1)” 14 Solution: execute function • For each execution, three cases: • Computation and move instruction: I. execute it; II. update flags if it is “cmpq”; • Jump instruction: I. check flags; II. find the label position; III. goto the step and execute it first; • Halt instruction: end this program; 15 Solution: output results • Print registers; • Print memory; • Care print formats: • %r0..7 • M0..15 • whitespaces, negative signs, … • No more other output: debug information 16 Additional: • Output results on screen • Check test cases before submission: • Try to compile and run your program on apollo • Compile your program with “gcc SimXXX.c –o SimXXX” • The execution command is: “./SimXXX caseYYY.txt”, where “caseYYY.txt” is the input filename. • Be careful to use three party libraries. • It is a black-box testing: you get scores if and only if your output is correct. 17 Thanks! 18 sim.c #include <stdio.h> #include <time.h> #include <string.h> #include <stdlib.h> #define LAB 10 #define SIZE 200 #define LINE 30 void storprint(int regis[8],int mem[16]); //main int main(int argc, char **argv){ clock_t start = clock(); char first[SIZE],second[LINE][SIZE],checker; char *filename = argv[1]; int rA,rB = 0; //FILE READ //declare variables FILE *fp = fopen(filename,"r"); char input; int sum_str(char *str); int i=0,j=0,k=0,linum=0,linecheck=0; int currlab,a=1; char com[LINE][5],lab[LINE][1],inti[LINE][10],dest[LINE][10]; int linlab[LAB][LINE][SIZE]; //declare register and memory space int regis[8],mem[16]; //initialise register and memory for (i=0;i<8;i++){ regis[i]=0; } for (i=0;i<16;i++){ mem[i]=0; } i=0; //reset i for later use //showing error when open file if (fp == NULL){ printf("ERROR"); return 1; } //read file and put into single string array else { while ((input = fgetc(fp)) != EOF) { if ((input != ' ')&&(input!='\t')) { //printf("%c", input); first[i]=input; i++; } } fclose(fp); } //printf("%c%c%c\n",first[0],first[1],first[2]); //seperate each line into new array //regonzie the labels, commands, init and dest for (j=0;j<i;j++){ a=1; if (first[j]=='\n'){ if (com[linum][0]=='h'){ printf("\nend of program\n"); } else { printf("\nLine number = %d\ninstruction = %s\nSrc = %s\nDest = %s\nCurrent label=%d",linum ,com[linum],inti[linum],dest[linum],currlab); } puts("\n------------"); linum++; k=0; while ((first[j+a]!='$')&&(first[j+a]!='%')&&(first[j+a]!='\n')&&(first[j+a]!='L')){ com[linum][a-1]=first[j+a]; com[linum][a]='\0'; a++; } } if ((first[j]=='%')&&(first[j-1]!=',')&&(com[linum][0]!='h')&&(com[linum][0]!='j')){ while(first[j+a]!=','){ inti[linum][a-1]=first[j+a]; inti[linum][a]='\0'; a++; } } if(first[j]=='$'&&(com[linum][0]!='h')&&(com[linum][0]!='j')){ while(first[j+a]!=','){ inti[linum][a-1]=first[j+a]; inti[linum][a]='\0'; a++; } } if((first[j]=='%')&&(first[j-1]==',')&&(com[linum][0]!='h')&&(com[linum][0]!='j')){ while(first[j+a]!='\n'){ dest[linum][a-1]=first[j+a]; dest[linum][a]='\0'; a++; } } if((com[linum][0]=='j')&&(first[j]=='L')){ while(first[j+a-1]!='\n'){ dest[linum][a-1]=first[j+a-1]; dest[linum][a]='\0'; a++; } } if ((first[j]==',')&&(com[linum][0]=='r')&&(com[linum][1]=='m')){ while(first[j+a]!='\n'){ dest[linum][a-1]=first[j+a]; dest[linum][a]='\0'; a++; } a=1; } if((com[linum][0]=='r')&&(com[linum][1]=='m')&&(first[j-1]=='q')&&(first[j]=='%')){ while(first[j+a]!=','){ inti[linum][a-1]=first[j+a]; inti[linum][a]='\0'; a++; } } } if(strcmp(com[linum],"cmpq") == 0){ } //print command storprint(regis,mem); clock_t end = clock(); double execute = (double)(end - start) / CLOCKS_PER_SEC; printf("\nExecution time: %.8f\n",execute); } //printing function with format void storprint(int regis[8],int mem[16]){ int i; printf("%r0..7: "); for (i=0;i<8;i++){ printf("%2d",regis[i]); } printf("\nM0..15:"); for (i=0;i<16;i++){ printf("%2d",mem[i]); } } assign2_sample_cases.zip case001.txt L0: irmovq $5, %r0 irmovq $3, %r1 rrmovq %r1, %r2 rmmovq %r0, 1(%r1) halt case002.txt L0: irmovq $1, %r0 irmovq $2, %r1 irmovq $3, %r2 addq %r0, %r1 addq %r1, %r2 subq %r0, %r2 halt case003.txt L0: irmovq $2, %r0 irmovq $1, %r1 cmpq %r0, %r1 jl L1 halt L1: subq %r1, %r0 rmmovq %r0, 0(%r1) halt case004.txt irmovq $0, %r6 irmovq $0, %r7 cmpq %r6, %r7 je L2 L1: irmovq $7, %r7 rmmovq %r7, 7(%r7) halt L2: irmovq $6, %r6 rmmovq %r6, 6(%r6) halt case005.txt irmovq $-12 , %r1 irmovq $18, %r2 rmmovq %r1, -15( %r2) mrmovq 15(%r1 ), %r0 halt output001.txt %r0..7: 5 3 3 0 0 0 0 0 M0..15: 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 output002.txt %r0..7: 1 3 5 0 0 0 0 0 M0..15: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 output003.txt %r0..7: 1 1 0 0 0 0 0 0 M0..15: 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 output004.txt %r0..7: 0 0 0 0 0 0 6 0 M0..15: 0 0 0 0 0 0 0 0 0 0 0 0 6 0 0 0 output005.txt %r0..7: -12 -12 18 0 0 0 0 0 M0..15: 0 0 0 -12 0 0 0 0 0 0 0 0 0 0 0 0 comp1411_assign2.docx COMP1411 (Spring 2020) Introduction to Computer Systems Assignment 2: Programming Extended Deadline: 10:00am, 11th May, 2020 Notes: · Please submit your assignment in Blackboard. · Please follow the naming convention as stated in Section 3. Section 1. Description In this assignment, you need to implement a simulator for assembly program based on a modified version of the Y86-64 architecture. For details about Y86-64, please refer to the instructions in lecture 6 and chapter 4.1 in the textbook. For simplicity, we use the following setting: · We use the following registers: %r0, %r1, %r2, %r3, %r4, %r5, %r6, %r7. · Each register can store a 8-bit signed integer (instead of 64-bit). The value range is -128..127. · The main memory address is an integer in the range 0..15. We only consider the following instructions in this assignment and have the following remarks. · rA and rB denote registers. · Dest is an address label. · V is a constant in decimal format (e.g., $2). · Only the instruction cmpq can update condition codes. · The notation D(rB) refers to the memory location at the address rB+D. Instruction Operands Meaning halt stop execution addq rA, rB compute rB+rA, then store the result in rB subq rA, rB compute rB–rA, then store the result in rB cmpq rA, rB compare rB and rA, then update condition codes je Dest jump to Dest if the previous comparison result is rB==rA jne Dest jump to Dest if the previous comparison result is rBrA jg Dest jump to Dest if the previous comparison result is rB>rA jl Dest jump to Dest if the previous comparison result is rB<rA rrmovq rA, rB move rA to rB irmovq V, rB move the constant V to rB rmmovq rA, D(rB) move rA to memory (at the address rB+D) mrmovq D(rB), rA move memory (at the address rB+D) to rA Section 2. Sample input and output Your simulator takes an assembly program as input, then simulates its execution, and finally prints the content of registers and the main memory to the screen. Your simulator should initialize the content of registers and memory to 0 before executing an assembly program. In the following, we show the sample input (in a file) and the sample output (on screen) of your simulator. Please follow the format of sample output (on screen) exactly. Sample input (in a file “case000.txt”) irmovq $-5, %r0 irmovq $11, %r1 L1: addq %r0, %r1 rmmovq %r0, 0(%r1) halt Assume that there is an instruction in each line. It is optional to add an address label (e.g., L0, L1, L2, L3, …) in front of an instruction. Assume that the address labels at different lines must be different. Sample output (on screen) %r0..7: -5 6 0 0 0 0 0 0 M0..15: 0 0 0 0 0 0 -5 0 0 0 0 0 0 0 0 0 The first line shows the content of registers. The second line shows the content of the main memory. The values are displayed in the decimal format and separated by space. Section 3. Submission and grading You shall submit a C program SimXXX.c or a C++ program SimXXX.cpp in Blackboard. (note that XXX denotes your student ID) Tutor will test your simulator by using 10 test cases. Each test case is worth of 10 marks. Tutor will compile your simulator in our department’s apollo server as follows. gcc SimXXX.c –o SimXXX or g++ SimXXX.cpp –o SimXXX Tutor will run your simulator as follows, where “caseYYY.txt” is a text file (assembly program). ./SimXXX caseYYY.txt The following assumptions are true for all test cases used for grading. 1. Each test case file has at most 100 instructions, i.e., 100 lines. 2. Each line has at most 128 characters. 3. Each test case contains at most 10 address labels. These address labels are L0, L1, …, L9. Different address labels are used for different lines. 4. The time limit of each test case is 5 seconds. If your simulator cannot print results within the time limit, then it is regarded as incorrect. 5. You must be careful about whitespaces (e.g., tabs, line break characters), commas, colons and others. We provide an extreme test case for this (case005.txt). 6. All contents (in input and output) are case sensitive. 7. All numbers are integersin the range -128..127. 8. All operations will be in the range of memory and registers. 3

Related Questions

Similar orders to input assembly program, then simulates its execution, and finally prints the content of registers and the main memory
5
Views
0
Answers
Make code and test for a pac man type game.
At the very least I would need the StudentAttackerController.Java code. The code would need to be tested by the “TestAgent - Scored” configuration that would run 100 trials and give an average score at the end. The average score for the code need to have a score 6500 or higher....
14
Views
0
Answers
Gameboard with hidden tiles; three guesses to pick all safe squares; gameboard based on files from user input (included)
For this assignment, you will be writing a game. The rules of the game are simple you have a game board with hidden tiles. You have to make three guesses if you pick all safe squares, you win, if you select a bad square, you lose. All game boards have are square, meaning both sides have the same length. The game board size will never be larger than 25 or below 1. The gameboard contains only 4 possible values (3 are requirements, the 4th is for bonus). If the square contains a 'S' it is safe. Meaning you have successfully made one guess. If the square contains a 'B' you lose. Meaning the game is over. If the square contains a 'R' you must pick again, this guess doesn’t count towards your guesses. If the square contains a 'C' it safely clears the area. (See bonus information in code) A game board file has the format ( for a size of 3. ) 3 SSS SRS SBS The game ends after successfully guessing three safe squares or guessing a losing square. After each guess, your program should display the contents of the gameboard square and print the corresponding message. Assignment Requirements Unlike previous examples, this assignment provides a starting point. Would you please c...
7
Views
0
Answers
C++ computer science programming project
Add the ability for your Canadian Experience scene to have two running machines. At some point in your animation you will indicate that a machine is to start running. You might have your character pretend to flip a switch, for example. From there on the machine will run continuously. You will create your code in the library MachineLib. It is an additional project in your solution. You will get your solution working using the MachineDemo project, also an additional project in your solution. MachineDemo will allow you to work on just the machines, getting them working first. Then you will integrate them into your project. I attach some of instructions...
18
Views
0
Answers
this assignment is to do it on Mysql workbench
use Mysql workbench to creat all queries see the watched file and follow everything on it it has everything you need to know. please let me know if you have any question...