# Assignment1: RISC-V Assembly and Instruction Pipeline
contributed by `ychu0406`
## Quiz 1 problem B
### C code
```javascript=
static inline bf16_t fp32_to_bf16(float s)
{
bf16_t h;
union {
float f;
uint32_t i;
} u = {.f = s};
if ((u.i & 0x7fffffff) > 0x7f800000) { /* NaN */
h.bits = (u.i >> 16) | 64; /* force to quiet */
return h;
}
h.bits = (u.i + (0x7fff + ((u.i >> 0x10) & 1))) >> 0x10;
return h;
}
```
### Assembly code
```:=
.data
float_val: .word 0x40490fdb
.text
.globl _start
_start:
la t0, float_val
lw t1, 0(t0)
# Check if NaN
li t2, 0x7fffffff
and t3, t1, t2
li t4, 0x7f800000
bgtu t3, t4, nan_case
# Normal case: rounding and truncating to bf16
li t5, 0x7fff
add t1, t1, t5
srl t8, t1, 16
j finish
nan_case:
# Handle NaN: shift and set quiet bit
srl t8, t1, 16
ori t8, t8, 0x40
finish:
la t0, result_bf16
sh t8, 0(t0) # Store lower 16 bits as bf16
li a7, 93
ecall
.data
result_bf16: .half 0x0000
```
## 58. Length of Last Word
- [Description in leedcode](https://leetcode.com/problems/length-of-last-word/description/)
- Given a string s consisting of words and spaces, return the length of the last word in the string.
- A word is a maximal
substring
consisting of non-space characters only.
- Example 1:
```
Input: s = "Hello World"
Output: 5
Explanation: The last word is "World" with length 5.
```
- Example 2:
```
Input: s = " fly me to the moon "
Output: 4
Explanation: The last word is "moon" with length 4.
```
- Example 3:
```
Input: s = "luffy is still joyboy"
Output: 6
Explanation: The last word is "joyboy" with length 6.
```
- Constraints:
```
1 <= s.length <= 104
s consists of only English letters and spaces ' '.
There will be at least one word in s.
```
### Solution
### C code
```c=
int lengthOfLastWord(char* s) {
int length = 0;
int i = strlen(s) - 1;
while (i >= 0 && s[i] == ' ') {i--;}
while (i >= 0 && s[i] != ' ') {length++;i--;}
return length;
}
```
### Assembly code
```:=
.data
str: .asciz "Hello World " # Test string
.text
.globl _start
_start:
la a0, str # Load address of the string into a0
call strlen # Get the length of the string
mv t0, a0 # Move the length into t0
addi t0, t0, -1 # Adjust to point to the last character
# Skip trailing spaces
skip_spaces:
lb t1, 0(t0) # Load byte at address t0
beq t1, zero, finish # If we reached the start of the string, stop
li t2, 32 # Load space character into t2
beq t1, t2, skip # If the character is space, continue skipping
j start_counting # Otherwise, start counting the length of the last word
skip:
addi t0, t0, -1 # Move to the previous character
j skip_spaces # Repeat the space skipping process
# Count the length of the last word
start_counting:
li t3, 0 # Initialize word length counter to 0
count_word:
lb t1, 0(t0) # Load byte at address t0
beq t1, zero, finish # If we reached the start of the string, stop
beq t1, t2, finish # If we hit a space, we're done with the word
addi t3, t3, 1 # Increment the word length counter
addi t0, t0, -1 # Move to the previous character
j count_word # Repeat counting the word
# Exit the program and return the result
finish:
mv a0, t3 # Move the length into a0 (return value)
li a7, 93 # Exit syscall number
ecall # Exit the program
# Helper function to compute string length
strlen:
mv t0, a0 # Copy the base address of the string into t0
li t1, 0 # Initialize length counter to 0
strlen_loop:
lb t2, 0(t0) # Load byte at address t0
beq t2, zero, strlen_done # If we reach null character, stop
addi t1, t1, 1 # Increment length counter
addi t0, t0, 1 # Move to the next character
j strlen_loop # Repeat the loop
strlen_done:
mv a0, t1 # Return the string length
ret # Return from function
```
### Assembly optimization
- 