关键词 > C语言代写

Computer Architecture and Assembly Lab (14:332:333) Spring 2023

发布时间:2023-04-11

Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Computer Architecture and Assembly Lab (14:332:333)

Spring 2023

Lab 5 Assignment

RISC-V Assembly

Due Date: 04/14/2023 -01:59 PM

Instructions:

Please answer all the questions below. You need to use the Venus RISC-V simulator for running and testing your code. Note that the simulator is 32-bit, and we do not consider overflow here. Upload your lab report with the department cover page and your source code using Sakai.

Exercises

1. [10 pts] Write a RISC-V program in Venus simulator that accepts an input integer ��� and uses two methods to compute a factorial:

· Recursive method: ���(���) = ��� * ���(��� − 1)

· Iterative/loop method: ���(���) = ���! = ��� * (��� − 1) * (��� − 2) * … * 2 * 1

You can assume ��� is always a positive number.

In the program, please have a main function that takes the value of the input, performs the factorial computations by the two methods, and prints the outputs of the two methods in the console.

· Verify your program for 3 different input values ��� and such that  ,   ,   ≠0

Note: you need to provide your own inputs and show screenshots of the outputs based on the given inputs. (Each method and function worth 10 pts.)

2. [15 pts] Write a RISC-V program in Venus simulator that splits the given array {4, 37, 0, 12, 1, 0, 6} into the following three sub-arrays:

· Array 1: the elements are the odd positive numbers

· Array 2: the elements are the even positive numbers

· Array 3: the elements are all zeros

In the program, please have a main function that obtains the value of the input, splits the array, and prints the three sub-arrays in the console. Your implementation should work for arrays of different values as well.

Note: you need to show screenshots of the outputs based on the given input. (Each array worth 10 pts.)

3. [25 pts] Write a RISC-V program in Venus simulator that computes matrix multiplication. You can assume all the entries in each matrix are positive numbers. In the program, you need to consider the following cases:

· When the number of rows for matrix A and the number of columns for matrix B are equal, compute the multiplication and obtain the product matrix C: ��� = ��� * ���.

· When the number of rows for matrix A and the number of columns for matrix are not equal, return the error code 99. Hint: please refer to the Ecall wiki page about returning the error code (https://github.com/kvakil/venus/wiki/Environmental-Calls).

In the program, please have a main function that checks the condition, perform the appropriate computations, and print the matrix C or the error code in the console.

· Verify your program for 3 input matrices   ,  ,  and such that the dimensions of   ,  ,  and are all unique.

· Demonstrate each possible case at least once.

· Your implementation should work for matrices of different values as well.

Note: you need to provide your own inputs and show screenshots of the outputs based on the given inputs. (The main function worth 15 pts, and the multiplication function worth 25 pts.) 

4. [50 pts] Use three available compilers that convert your C program to Assembly: 1) Venus, 2) Cornell, 3) Compiler Explorer (for a 64 bit architecture) with more information at: https://godbolt.org/ For the 3d compiler you have the options to explore with multiple versions. Your task is to run a number of instructions or program with all three compilers (including 2 versions in the 3d), document the results, collect the output and messages from the compilers/simulators and explore how and why there are differences.

You will be conducting experiments on executing the following instructions 1-6 [35 pts]

1)) beq x5, x8, 5008   // Out of range number entered in decimal. What is the output of the compiler?

2)) beq x5, x8, 1621  //are not supposed to enter odd number, is this detected by the compiler

                                       // or handled by the OS as exception? Is this handled on the spot or later

3)) jalr x0, 0(x1)     // having loaded to x1 an odd number and observing where jalr will

                                           // take us

4)) jal x3, label // where label is a number with range higher than 20 bits. Is the compiler

                                     //giving a warning,  or informing of error?

5)) bne x5, x7, label    // check how many bytes/what number the label is from two

                                                   // different  compilers.

6)) explore if all three simulators accept only arguments or also arguments and PC-relative addresses.

7)) Explore add I instructions:  [8 pts]

Adding the two hexadecimal numbers:
0x80000000 and 0x80000001
What is the output and why?

Now input the following commands using all three simulators:

addi x2, x0, 0x80000000

addi x3, x0, 0x80000001

add x5, x2, x3

Show the results. Is this program executed? If so, what are the results? What messages do your get from the simulators?

Now, implement the above in all three simulators the following way:

lui x1, 0x80000

addi x2, x1, 0x000

addi x3, x1, 0x001

add x4, x2, x3    

Show the results. Is this program executed? If so, what are the results? What messages do your get from the simulators?

Also, is the result what you would expect? Why yes or why not?

8) Explore indirect PC relative instructions:  [7 pts]

Use one instruction learned in class to insert the current PC of your program in register x11. Use the instruction you selected in the following program:

addi x5, x0, 1
addi x8, x0, 1
???????????     //substitute ??? with the instructions that enters current PC into x11
jalr x1, x11, -8
beq x5, x8, hello
addi x5, x0,1
addi x5,x0,2
hello:
addi x5,x0,2

Then, execute the updated program on all three simulators. Explain what the results shows and why. Essentially what does the program do? Are there any differences in the results across the three simulators/compilers you are using? Why yes, why not?