# Lab1: RV32I Simulator ###### tags: `Computer Architecture 2021` ## Single number This problem is based on [LeetCode 344](https://leetcode.com/problems/reverse-string/). Write a function that reverses a string. The input string is given as an array of characters s ## C Code ```cpp void reverseString(char* s, int sSize){ char tem; for(int i=0;i<=sSize/2-1;i++) { tem=s[i]; s[i]=s[sSize-i-1]; s[sSize-i-1]=tem; } } ``` ## Assembly code ### 1.use address ```cpp= #use address .data arr: .word 104,101,108,108,111 #hello in AscII space: .string " " len: .word 5 .text main: la s1,arr #load the array lw s2, len #load the length of array li s5,4 #define the unit offset mul s6,s2,s5 #get the size of byte of array sub,s6,s6,s5 #get the end of array address add,s1,s6,s1 #get the address of last item of array jal ra, loop1 #jump to loop1 ecall loop1: addi t0,t0,1 #i++ lw a0, (0)s1 #get the item at the address li a7,1 #print the item ecall la a0,space li a7,4 #print space ecall sub s1,s1,s5 #get the pre address of array blt t0,s2,loop1 #if i<len jump to loop ``` ### 2.two arrays ```cpp= .data arr: .word 104,101,108,108,111 #hello in AscII arr2: .word 11,12,12,16,17,15 #nums[5]={1,2,3,4,5} len: .word 6 .text main: la s1,arr la s7,arr2 lw s2, len li s5,4 mul s6,s2,s5 sub,s6,s6,s5 add,s1,s6,s1 jal ra, loop1 jal ra, out ecall loop1: addi t0,t0,1 lw a0,0(s1) add t2,a0,x0 lw a0,0(s7) sw a0,0(s1) add a0,t2,x0 sw a0,0(s7) add s7,s7,s5 sub s1,s1,s5 blt t0,s2,loop1 out: sub s7,s7,s6 sub s7,s7,s5 loop2: addi t1,t1,1 lw a0, (0)s7 add s7,s7,s5 li a7,1 ecall blt t1,s2,loop2 ``` ### 3.One array ```cpp= .data arr: .word 104,101,108,108,111 #hello in AscII output: .string " output is" space: .string " " len: .word 5 .text main: la s1 arr lw s2, len li s5,4 li s7,1 li s8,2 slli s8,s2,2 #max end address gap sub,s8,s8,s5 #max end item address gap jal ra, reverse jal ra, out ecall reverse: div s9,s8,s2 #size/2 sub s9,s9,s7 #size/2-1 loop1: addi t0,t0,1 #i++ sub t3,t0,s7 #i-1 add t4,t3,t3 #2(i-1) slli t4,t4,2 #2(i-1)*4(bytes) sub t6,s8,t4 #gap for exchange item lw a0,0(s1) add t2,a0,x0 #temp=x[i] add,s1,t6,s1 #x[max-i] lw a0,0(s1) #a0=x[max-i] sub s1,s1,t6 #x[i] sw a0,0(s1) #x[i]=a0 add s1,s1,t6 #x[max-i] add a0,t2,x0 sw a0,0(s1) #x[max-i]=temp sub s1,s1,t6 #x[i] add s1,s1,s5 #x[i+1] blt t0,s9,loop1 #if i<(size/2-1) jump to loop out: la a0 ,output li a7,4 slli s10,t0,2 #get the final address gap of s1 in loop1 sub s1,s1,s10 #minus offset ecall loop2: addi t1,t1,1 #i++ lw a0, (0)s1 #a0=x[i] add s1,s1,s5 #x[i+1] li a7,1 ecall la a0,space li a7,4 ecall blt t1,s2,loop2 # if i<len go to loop2 ``` ## Pipeline feature in Ripes ## Hazard issue I haven't found out how to initialize the start address of array ## Reference [hrh47's computer organization note](https://hackmd.io/@8bFA57f7SRG-K7AAT8s62g/ryv1NT3S#%E7%AC%AC18%EF%BD%9E21%E8%AC%9B-Pipelining-75)