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

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
25
Views
1
Answers
JavaScript Assignment
Assignment details in the Word file attached. I just need the files with the code, I will put them into the website myself....
106
Views
1
Answers
46
Views
1
Answers
C++ queue
Use C++ and implement your own queue using linked list that should be templated....
31
Views
1
Answers
creating a website
Description Use Notepad to create a web site about any subject that interests you. In a Mac use the app, Text Edit. The web site must consist of exactly two linked web pages that use the same external style sheet. (If this is not clear to you, please revi...