143,585 views
26 votes
26 votes
4. (30 points) Assume a string str and each character is stored as one byte. Convert the following high-level code into RISC-V assembly language. Assume that the array base address is stored in register a0, the index variable i is stored in register to and the count variable count is stored in the register ti. a) if (str[i] < 'O' || str[i] > '9') count = count + 1; b) if (str[i] >= 'a' && str[i] <= 'z') str[i] = str[i] 32;

User Iamlukeyb
by
2.3k points

1 Answer

9 votes
9 votes

Answer:

Here is a possible implementation of the first code snippet in RISC-V assembly language:

lb a1, 0(a0)

blt a1, 'O', skip1

bgt a1, '9', skip1

addi t1, t1, 1

skip1:

Here is a possible implementation of the second code snippet in RISC-V assembly language:

lb a1, 0(a0)

blt a1, 'a', skip2

bgt a1, 'z', skip2

addi a1, a1, 32

sb a1, 0(a0)

skip2:

Step-by-step explanation:

The first instruction loads the value of str[i] into register a1, using a0 as the base address and 0 as the offset (since i is stored in register t0). The second instruction checks if the value in a1 is less than 'a', and if so, it jumps to the skip2 label. The third instruction checks if the value in a1 is greater than 'z', and if so, it also jumps to the skip2 label. If none of these conditions are met, the fourth instruction adds 32 to the value in a1, and the fifth instruction stores the resulting value back into str[i], using a0 as the base address and 0 as the offset.

User Davislor
by
2.8k points