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

EEC 170 Introduction to Computer Architecture Winter 2024

Project 1

Due: 10:00 am Monday 5 February.

Write functions in RISC-V assembly for the following problems and simulate in RARS: Refer to Getting started with RARSfor tips/suggestions. Also refer to the tutorial videos posted on Canvas.

Thisis the RARS Github homepage. The RARS simulator itself can be downloaded here. Make sure you have Java 8 or later installed. Download the .jar file and run it. You should see this window open up:

Note: I find that when I run RARS on a Mac, the file explorer doesn’t really work, so it’s not really usable. But that might just be my machine acting up.

Navigate to the Setting tab and make sure that either or both “Assemble all files in directory” or “Assemble all files currently open” are checked. Otherwise there is noway (that I know of) to assemble both the main file and your function files.

When you wish to test your code, navigate to main.asm and click the Assemble button (the crossed wrench and screwdriver). If the assembly fails, you probably have either an invalid command, or a function called by the main.asm can’t be found, either because it wasn’t assembled or it was misnamed.

IMPORTANT: For the main.asm file to successfully call your functions, you must do 3 things:

•   In main.asm, uncomment the “call ” line.

o #call nextfactor → call nextfactor

•   Make sure that the name of the your file is correct, i.e. that it isn’t still “function_template.asm”

o This one isn’t really so RARS can find your code, its so my autograder can.

•    In your function code, update lines 11 and 13 to have the function’s correct name instead of funcTemp

If the assembly succeeds, you can run the code by pressing the green arrow button. You can set breakpoints in the code by clicking the checkboxes on the far left of the commands. It typically takes some scrolling to find your code; try scrolling to the bottom.

You can also test your code from the command line, but I (Conor) typically use the GUI. For tips on using the command line with RARS, see the first link in this document.

RARS simulator has separate 32bit instructions and separate 64bit instructions depending on   what you are doing. This assignment is to be done with 32bit instructions. You can find on the RARS wikipage the separate set of instructions. (link)

Submit your function files to Gradescope. Submit only the function files, not the folder

containing them nor a zip of them. Important Note: they will be graded automatically. This

means that if you do not follow the naming and IO conventions given below, you will get a zero for that part of the assignment! I recommend against this.

Also, you will not be submitting your main.asm file, so if your functions depend on any changes you have made to the main.asm file we have provided, your functions will not behave correctly.

Your functions must be able to handle any valid input (what counts as a valid input is explained for each problem). We will choose avariety of test cases and your grade will be based on how

many test cases produce the correct output from your function. Note: more specifically, your

grade will be based on correct outputs above the expected number of correct outputs for random  guesses. So in the case of problems 5, Power of 2, which has only 2 possible correct outputs, you will only receive credit if more than ½ of the test cases produce correct outputs. The test cases will not be published.

Problems

1. Next Factor

Input a 32-bit unsigned integer i. Return the smallest unsigned integer that is equal to or greater than i and that is a multiple of 7, 11, and 13. The input and the correct output will always be representable by 32-bit unsigned integers.

●   Filename: nextfactor.asm

●   Function name: nextfactor

Input register: x10

●   Output register: x10

●   Examples:

○   nextfactor(1) = 1001

○   nextfactor(1001) = 1001

○   nextfactor(88087) = 88088

2. Smallest Byte

Input a 32-bit word made of 4 8-bit unsigned bytes. Choose the smallest byte value from those 4 bytes. Return a 32-bit word with all 4 bytes set to that smallest byte value.

●   Filename: smallestbyte.asm

●   Function name: smallestbyte

●   Input register: x10

●   Output register: x10

●   Examples:

○   smallestbyte(0xff01ee13) = 0x01010101

○   smallestbyte(0xeeeeeeee) = 0xeeeeeeee

3. Interleave

Input 2 32-bit unsigned integers, i0 and i1. Bits are counted from least significant to most

significant (bit 0, the rightmost bit, is the least significant bit). The output should have its even

bits set to the least significant 16 bits of input i0 and its odd bits set to the least significant 16 bits of input i1. Note the most significant bits of both inputs are ignored.

To illustrate, consider a smaller example (16-bit inputs and outputs):

i0 has bits abcdefghijklmnop, a is most significant,p is least significant

i1 has bits ABCDEFGHIJKLMNOP, A is most significant, P is least significant

The output should be IiJjKkLlMmNnOoPp

●   Filename: interleave.asm

●   Function name: interleave

Input register: x10, x11

●   Output register: x10

●   Examples:

○   interleave(0xffffffff, 0x00000000) = 0x55555555

○   interleave(0x01234567, 0x456789ab) = 0x90939c9f

4. First Majority Byte

Consider all possible bytes found within the input word [31:0]. Those bytes are [7:0], [8:1], [9:2] [31:24]. Return a signed integer set to the low bit index of the least significant byte that has at least 5 bits of its 8 bits set to 1. If no byte satisfies this property, return - 1.

A simplified visual example, with a 16-bit input:

Input: [0100110110011000]

Process:

●          [01001101[10011000]] >>> [7:0] contains 3 set bits

●          [0100110[11001100]0] >>> [8:1] contains 4 set bits

●          [010011[01100110]00] >>> [9:2] contains 4 set bits

●          [01001[10110011]000] >>> [10:3] contains 5 set bits, so the output should

be 3.

●   Filename: fmbyte.asm

●   Function name: fmbyte

Input register: x10

●   Output register: x10

●   Examples:

○   fmbyte(0x000000ff) = 0; the least significant byte with a majority of 1s is [7:0], so return that byte’s low bit index 0.

○   fmbyte(0x00000ff0) = 1; the least significant byte with a majority of 1s is [8:1], so return that byte’s low bit index =1.

○   fmbyte(0x33333333) = - 1; no byte has a majority of 1s

5. Power of 2

Your input is an unsigned integer. Your output is 1 if the input is a power of 2 and a 0 otherwise. 0 is not a power of 2.

●   Filename: pow2.asm

●   Function name: pow2

Input register: x10

●   Output register: x10

●   Examples:

○   pow2(0) = 0

○   pow2(1) = 1

○   pow2(4096) = 1

○   pow2(4097) = 0

6. Calculator

Your input is a 4-character string packed into a 32-bit unsigned integer, where the only allowed  (ASCII) characters are the digits 0 through 9, 4 arithmetic operators (+, -, *, /), and space. Space can appear anywhere in the string and can always be ignored. Numbers will never begin with 0,  unless the number is 0. The input will never have negative numbers. Return the arithmetic value of the input string. This is the hardest problem.

●   Filename: calculator.asm

●   Function name: calculator

Input register: x10

●   Output register: x10

●   Examples:

○   calculator(“3-44”) = -41

○   calculator(" 3 ") = 3

○   calculator(“0*12”) = 0

7. Valid Parentheses

(This problem is optional and will receive no credit [and will not be graded]. It is both an

interesting algorithm to code up and also an opportunity to practice writing a procedure call   and using a stack. We highly recommend you attempt to solve this problem so that you can get some experience with procedure calls and stacks. This problem is most elegantly (and simply) solved recursively.)

Given a pointer to a null-terminated string of byte characters, return 1 (true) if the string has

properly nested parentheses and 0 (false) if it does not. “Properly nested” means that every open parenthesis character ‘(’ is followed at some point later by a matching ‘)’, just as in a valid math equation. Not all characters in the string will be parentheses; you can ignore all other characters.

The following strings have properly nested parentheses:

• (())

• (()()(((()))))

• ()()()()

• (1+(2+(3+(4+5))))

The following strings do not:

• (

• )1(

• (((y))))((x)

Your program should expect a pointer to a null-terminated string in register x10 and must store the output (0 or 1) in register x10.