# Lab 4

There is no checkoff for lab this week. This is primarily practice for the midterm.

## Objectives

• TSW read and analyze C and RISC-V code that covers everything they have covered so far in the course.

## Exercise 1: Converting Number Bases in C

By this point, you should be comfortable converting between the standard decimal/hexadecimal/binary representations for numbers. Now, let’s try writing a generic number representation converter for unsigned numbers in C! In particular, you will be filling out two functions in numberrep.c. Note that the strategy that numberrep.c uses to convert between different bases is to first convert to decimal and then convert to the new base. This is not necessarily the best way to convert between two numbers. (What might be a better strategy for converting from hex to binary or vice versa?)

### Action Item

Finish the implementations of the functions in numberrep.c. Running make check should run without any Assertion errors in the output.

### Checkpoint

• Are you comfortable converting a number from one base to another?
• Are you comfortable with reading and writing C code that needs to dynamically manage memory?

## Exercise 2: RISC-V From Scratch

Now that you’ve implemented the number representation converter in C, let’s implement a subset of the program in RISC-V. Specifically, we will be implementing the validate_number function from the program. The starter code is in numberrep.s, which already contains the code for validate_number_char. You can click this magic link to load Venus with the starter code.

The general strategy for validate_number is to loop through the number and call validate_number_char each time with a character and a base as arguments. Note that since it is calling a function, you must follow the CALLEE/CALLER conventions for whichever registers you choose to use.

Your implementation of validate_number should set a0 to be 1 if the number is valid in this base or 0 if it isn’t. The starter code prints out the value of a0 for testing purposes.

### Action Item

Interpret what validate_number_char does and write down your interpretation. Then, finish the implementation of validate_number.

## Checkpoint

• Are you comfortable with writing and reading RISC-V functions?
• Are you comfortable with the CALLEE/CALLER conventions for registers?