CIS 2400 (Fall 2024) Home Schedule Assignments Tools & Refs HW 07: RISC-V Programming

This assignment gives students practice writing assembly programs and interacting with lowl-level representations of strings and arrays.

Collaboration

For assignments in CS 2400, you will complete each of them on your own or solo. However, you may discuss high-level ideas with other students, but any viewing, sharing, copying, or dictating of code is forbidden. If you are worried about whether something violates academic integrity, please post on Ed or contact the instructor.

Instructions

In this assignment you will write a number of short RISC-V assembly programs. In order to do this assignment, you will only need a text editor.

When writing a program in assembly you will find it helpful to work your algorithm out on paper perhaps with the aid of a flow chart before you start writing the code. You should also write down what variables you need to maintain in your program and which registers will store those values; this is known as a register allocation.

Your asm file should have a label called .END to mark the end of the program.

Remember again that your assembly programs must be saved as text files. You can create and edit text files with an editor like vim, emacs, nano, Sublime, and many others In other words, do NOT submit a .pdf, .docx or other types of non-text documents..

Problem 1: is_even

For this problem you can assume that a0 (alias for x10) is set to a 2C value. Your job is to determine wheter or not the 2C value is even and report the answer in x10. (Yes, this is the same register as the input, this register is typically used as the first argument to a function / subroutine and is also usually where the “return value” is stored.) If the value is even, then you should set x10 to be 1, otherwise it should be 0. Your ASM file should titled is_even.s.

Problem 2: swap

for this problem you should should assume that a0 and a1 are each set to be pointers to 32-bit values in memory. Your job is to swap the values at each address (e.g. *a0 = *a1; and vice-versa.) Your ASM file should titled swap.s.

Problem 3: parity

For this problem your job is to write a program that considers a 32 bit field initially stored in a0. Your program should determine the number of 1 bits in this 32 bit field. Specifically after your program has run x10 should contain the number of 1 bits in a0. Egs if the 32 bit field contains 6 1 bits x10 should be set to 6. Your ASM file should titled parity.asm.

Problem 4: toupper

Your job is to write a program that takes a null-terminated string and convert any lower-case ascii characters ('a' to 'z') to upper case. Non lowercase letters should remain the same. You may want to refer to an ASCII table to see which range of values are considered a lower case number, and how you should convert it to upper case. At the beginning of your program, a0 should contain the address of the start of the string to be copied. You can assume that a0 is a properly formatted string that has its end marked by the null-terminator character (which is equal to 0). After your program has run, the string that a0 initially points to should have any characters within it that were lowercase now converted to uppercase. Note that you are allowed to modify the address stored in a0 over the duration of your program. While you are developing your code, you may find it useful to refer to the strlen.s example mentioned above. Your asm file should be entitled toupper.s.

Problem 5: timer

your job is to write a program that takes a 2C number as input in a0 and then executes exactly that many instructions over the course of the subroutine before hitting the end. You can asseume that a0 is a muliple of 10 and greater than or equal to 10. You are free to modify a0 over the course of the subroutine. HINT: recall there is a psuedo-instruction nop which is equivalent to addi x0,x0,0 which does nothing! Your asm file should be entitled timer.s.

Problem 6: sort_array

Your job is to write a program that takes in an array of 32-bit 2C numbers stored in memory and sorts that array in increasing arithmetic order (from smallest to largest). At the beginning of your program, a0 should contain the address of the start of the array to be sorted and a1 should contain the length of the array. You can assume that the length of the array (a1) will be non-negative. After your program has run the array in data memory should be completely sorted with the smallest element being the first element and the largest being the last. You can use any sorting algorithm you like. For example, if an array of [0, 2, -7, 5] is provided, the array should look like [-7, -0, 2, 5] after the function has ended. While you are developing your code, you may find it useful to refer to the sum_numbers.s example mentioned earlier in the specification. Your asm file should be entitled sort_array.s.

Compiling and Testing

For this assignment, you will only be able to test your submission by walking through it yourself or by using the autograder once it is up. Writing it out is very helpeful practice and will be useful for future assignments (and maybe the final).

Submission

Please submit all of your asm files and their corresponding script files to Gradescope This should include:

Make sure that the files you submit follow the instructions laid out above. Most notably: