# Lab2:RISC-V Toolchain ###### tags: `RISC-V` `2022 Computer Architecture` ###### contributed by <[YongDa Su](https://github.com/YongDaSu/2022ComputerArchitecture/tree/main/Lab02)> ## [environment setup](https://hackmd.io/@2gMSvG25RxKj2-N_rAY6zg/B1eNc_Ggco) ## Question Selection ### **Motivation** In this Question, it use a for loop and a nest if loop, so I believe that this is a good one to practice how to optimize other peoples' program. ### **Question** Three Consecutive Odds from [王漢祺](https://hackmd.io/BF459yoPRe2oLbZHiy22xw?view) ([leetcode 1550](https://leetcode.com/problems/three-consecutive-odds/)) ### **Describe:** Given an integer array `arr`, return `true` if there are three consecutive odd numbers in the array. Otherwise, return `false`. ### **example:** ![](https://i.imgur.com/Ef3BaTb.jpg) ### **Program flow chart** ![](https://i.imgur.com/LP2pwGA.jpg) ### **C code** I change the input test, and find out some mistake. In the c program part, I could not improve the code properly, so I looked into the assembly part, I found some memory fault. In some cases of the array, the result was not correct. Then I fixed part, not only the memory fault but also improve some performance(by reducing some redundant registers and code). ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> bool threeConsecutiveOdds(int *arr, int arrSize) { int i; int count = 0; if (arrSize < 3) return false; for (i = 0; i < arrSize; i++) { if (*(arr + i) & 1) { if (++count == 3) return true; } else { count = 0; } } return false; } int main(int argc, char *argv[]) { int test1[2] = {2, 6}; int test2[9] = {1, 2, 34, 3, 4, 5, 7, 23, 25}; int test3[6] = {1, 3, 4, 7, 9, 12}; bool answer1 = threeConsecutiveOdds(test1, sizeof(test1) / sizeof(test1[0])); bool answer2 = threeConsecutiveOdds(test2, sizeof(test2) / sizeof(test2[0])); bool answer3 = threeConsecutiveOdds(test3, sizeof(test3) / sizeof(test3[0])); printf("The answer1 is %d\n", answer1); printf("The answer2 is %d\n", answer2); printf("The answer3 is %d\n", answer3); return 0; } /*** terminal output The answer1 is 0 The answer2 is 1 The answer3 is 0 -------------------------------- Process exited after 0.02739 seconds with return value 0 Press any key to continue . . . ***/ ``` ### **Assembly code(modify)** ```RISC-V .data arr1: .word 2, 6 arr2: .word 1, 2, 34, 3, 4, 5, 7, 23, 25 arr3: .word 1, 3, 4, 7, 9, 12 str0: .string "\nThe answer is false\n" str1: .string "\nThe answer is true\n" str2: .string " " #the size of array is known in advanced. arr1_size: .word 2 arr2_size: .word 9 arr3_size: .word 6 .text # a0 : save the return value # a1 : the base address of arr # a2 : the size of arr # t1 : count # t2 : 3 # s0 : arr[i] # s1 : the value of (*(arr + 1) & 1) initialize: la a1, arr1 lw a2, arr1_size addi t1, x0, 0 addi t2, x0, 3 jal ra, print_array la a1, arr1 #recover the addr of array1 lw a2, arr1_size #recover the array size of array1 jal ra, main #strat main program la a1, arr2 lw a2, arr2_size addi t1, x0, 0 addi t2, x0, 3 jal ra, print_array la a1, arr2 #recover the addr of array1 lw a2, arr2_size #recover the array size of array1 jal ra, main #strat main program la a1, arr3 lw a2, arr3_size addi t1, x0, 0 addi t2, x0, 3 jal ra, print_array la a1, arr3 #recover the addr of array1 lw a2, arr3_size #recover the array size of array1 jal ra, main #strat main program li a7, 10 ecall print_array: #print all array element lw s0, 0(a1) #load element addi a0, s0, 0 #a0 = s0 li a7, 1 #print string ecall la a0, str2 #print space li a7, 4 ecall addi a1, a1, 4 #next element addi a2, a2, -1 #array size -1 bne a2, x0, print_array #if still have element, go print ret main: blt a2, t2, False #if array size <3, go to False directly for_loop: addi a2, a2, -1 #array size -1 lw s0, 0(a1) #load element addi a1, a1, 4 #next element andi s1, s0, 1 #bitwise and 1, if an odd, s1 = 1 bne s1, x0, ADD_1 #branch to ADD1 if an odd addi t1, x0, 0 #count = 0 if even bne a2, x0, for_loop #if still have element, go to for_loop j False #if no consecutive odds, j False ADD_1: addi t1, t1, 1 #count = count + 1 beq t1, t2, True #if count = 3, True bne a2, x0, for_loop #if count != 3 but still have other elements in the array j False True: la a0, str1 li a7, 4 ecall ret False: la a0, str0 li a7, 4 ecall ret ``` ## **RV32emu** ### Process :::info 1.Compile ![](https://i.imgur.com/KOCju4x.jpg) ::: :::info 2.Size ![](https://i.imgur.com/4Eww12q.jpg) ::: :::info 3.ELF header ![](https://i.imgur.com/UwtVcym.jpg) ::: :::info 4.Assembly code ![](https://i.imgur.com/TogtC6w.jpg) ::: ### Size compare O0 ![](https://i.imgur.com/qk5c03R.jpg) O1 ![](https://i.imgur.com/dQgRYdQ.jpg) O2 ![](https://i.imgur.com/rIKu7ed.jpg) O3 ![](https://i.imgur.com/OrwXVlx.jpg) Os ![](https://i.imgur.com/8Neh3bG.jpg) Ofast ![](https://i.imgur.com/tD9jLa2.jpg) ### ELF compare O0 ![](https://i.imgur.com/8rlxgMR.jpg) O1 ![](https://i.imgur.com/oag2MVx.jpg) O2 ![](https://i.imgur.com/6oMDgeM.jpg) O3 ![](https://i.imgur.com/HygTcxa.jpg) Os ![](https://i.imgur.com/yTsMEQR.jpg) Ofast ![](https://i.imgur.com/NNDbUSA.jpg) ### **Observation** :::info O0->O1 : slightly change Os : best optimization O2 & O3 : almost the same ::: ### **Assembly code** O0 ```RISCV 00010184 <threeConsecutiveOdds>: 10184: fd010113 addi sp,sp,-48 10188: 02812623 sw s0,44(sp) 1018c: 03010413 addi s0,sp,48 10190: fca42e23 sw a0,-36(s0) 10194: fcb42c23 sw a1,-40(s0) 10198: fe042423 sw zero,-24(s0) 1019c: fd842703 lw a4,-40(s0) 101a0: 00200793 li a5,2 101a4: 00e7c663 blt a5,a4,101b0 <threeConsecutiveOdds+0x2c> 101a8: 00000793 li a5,0 101ac: 0680006f j 10214 <threeConsecutiveOdds+0x90> 101b0: fe042623 sw zero,-20(s0) 101b4: 0500006f j 10204 <threeConsecutiveOdds+0x80> 101b8: fec42783 lw a5,-20(s0) 101bc: 00279793 slli a5,a5,0x2 101c0: fdc42703 lw a4,-36(s0) 101c4: 00f707b3 add a5,a4,a5 101c8: 0007a783 lw a5,0(a5) 101cc: 0017f793 andi a5,a5,1 101d0: 02078263 beqz a5,101f4 <threeConsecutiveOdds+0x70> 101d4: fe842783 lw a5,-24(s0) 101d8: 00178793 addi a5,a5,1 101dc: fef42423 sw a5,-24(s0) 101e0: fe842703 lw a4,-24(s0) 101e4: 00300793 li a5,3 101e8: 00f71863 bne a4,a5,101f8 <threeConsecutiveOdds+0x74> 101ec: 00100793 li a5,1 101f0: 0240006f j 10214 <threeConsecutiveOdds+0x90> 101f4: fe042423 sw zero,-24(s0) 101f8: fec42783 lw a5,-20(s0) 101fc: 00178793 addi a5,a5,1 10200: fef42623 sw a5,-20(s0) 10204: fec42703 lw a4,-20(s0) 10208: fd842783 lw a5,-40(s0) 1020c: faf746e3 blt a4,a5,101b8 <threeConsecutiveOdds+0x34> 10210: 00000793 li a5,0 10214: 00078513 mv a0,a5 10218: 02c12403 lw s0,44(sp) 1021c: 03010113 addi sp,sp,48 10220: 00008067 ret 00010224 <main>: 10224: f9010113 addi sp,sp,-112 10228: 06112623 sw ra,108(sp) 1022c: 06812423 sw s0,104(sp) 10230: 07010413 addi s0,sp,112 10234: f8a42e23 sw a0,-100(s0) 10238: f8b42c23 sw a1,-104(s0) 1023c: 00200793 li a5,2 10240: fef42223 sw a5,-28(s0) 10244: 00600793 li a5,6 10248: fef42423 sw a5,-24(s0) 1024c: 000227b7 lui a5,0x22 10250: 81478793 addi a5,a5,-2028 # 21814 <__clzsi2+0xc8> 10254: 0007a303 lw t1,0(a5) 10258: 0047a883 lw a7,4(a5) 1025c: 0087a803 lw a6,8(a5) 10260: 00c7a503 lw a0,12(a5) 10264: 0107a583 lw a1,16(a5) 10268: 0147a603 lw a2,20(a5) 1026c: 0187a683 lw a3,24(a5) 10270: 01c7a703 lw a4,28(a5) 10274: 0207a783 lw a5,32(a5) 10278: fc642023 sw t1,-64(s0) 1027c: fd142223 sw a7,-60(s0) 10280: fd042423 sw a6,-56(s0) 10284: fca42623 sw a0,-52(s0) 10288: fcb42823 sw a1,-48(s0) 1028c: fcc42a23 sw a2,-44(s0) 10290: fcd42c23 sw a3,-40(s0) 10294: fce42e23 sw a4,-36(s0) 10298: fef42023 sw a5,-32(s0) 1029c: 000227b7 lui a5,0x22 102a0: 83878793 addi a5,a5,-1992 # 21838 <__clzsi2+0xec> 102a4: 0007a503 lw a0,0(a5) 102a8: 0047a583 lw a1,4(a5) 102ac: 0087a603 lw a2,8(a5) 102b0: 00c7a683 lw a3,12(a5) 102b4: 0107a703 lw a4,16(a5) 102b8: 0147a783 lw a5,20(a5) 102bc: faa42423 sw a0,-88(s0) 102c0: fab42623 sw a1,-84(s0) 102c4: fac42823 sw a2,-80(s0) 102c8: fad42a23 sw a3,-76(s0) 102cc: fae42c23 sw a4,-72(s0) 102d0: faf42e23 sw a5,-68(s0) 102d4: fe440793 addi a5,s0,-28 102d8: 00200593 li a1,2 102dc: 00078513 mv a0,a5 102e0: ea5ff0ef jal ra,10184 <threeConsecutiveOdds> 102e4: 00050793 mv a5,a0 102e8: fef407a3 sb a5,-17(s0) 102ec: fc040793 addi a5,s0,-64 102f0: 00900593 li a1,9 102f4: 00078513 mv a0,a5 102f8: e8dff0ef jal ra,10184 <threeConsecutiveOdds> 102fc: 00050793 mv a5,a0 10300: fef40723 sb a5,-18(s0) 10304: fa840793 addi a5,s0,-88 10308: 00600593 li a1,6 1030c: 00078513 mv a0,a5 10310: e75ff0ef jal ra,10184 <threeConsecutiveOdds> 10314: 00050793 mv a5,a0 10318: fef406a3 sb a5,-19(s0) 1031c: fef44783 lbu a5,-17(s0) 10320: 00078593 mv a1,a5 10324: 000217b7 lui a5,0x21 10328: 7d878513 addi a0,a5,2008 # 217d8 <__clzsi2+0x8c> 1032c: 264000ef jal ra,10590 <printf> 10330: fee44783 lbu a5,-18(s0) 10334: 00078593 mv a1,a5 10338: 000217b7 lui a5,0x21 1033c: 7ec78513 addi a0,a5,2028 # 217ec <__clzsi2+0xa0> 10340: 250000ef jal ra,10590 <printf> 10344: fed44783 lbu a5,-19(s0) 10348: 00078593 mv a1,a5 1034c: 000227b7 lui a5,0x22 10350: 80078513 addi a0,a5,-2048 # 21800 <__clzsi2+0xb4> 10354: 23c000ef jal ra,10590 <printf> 10358: 00000793 li a5,0 1035c: 00078513 mv a0,a5 10360: 06c12083 lw ra,108(sp) 10364: 06812403 lw s0,104(sp) 10368: 07010113 addi sp,sp,112 1036c: 00008067 ret ``` O1 ```RISCV 00010184 <threeConsecutiveOdds>: 10184: 00200793 li a5,2 10188: 04b7d463 bge a5,a1,101d0 <threeConsecutiveOdds+0x4c> 1018c: 00000693 li a3,0 10190: 00000713 li a4,0 10194: 00300613 li a2,3 10198: 0140006f j 101ac <threeConsecutiveOdds+0x28> 1019c: 00078693 mv a3,a5 101a0: 00170713 addi a4,a4,1 101a4: 00450513 addi a0,a0,4 101a8: 02e58063 beq a1,a4,101c8 <threeConsecutiveOdds+0x44> 101ac: 00052783 lw a5,0(a0) 101b0: 0017f793 andi a5,a5,1 101b4: fe0784e3 beqz a5,1019c <threeConsecutiveOdds+0x18> 101b8: 00168693 addi a3,a3,1 101bc: fec692e3 bne a3,a2,101a0 <threeConsecutiveOdds+0x1c> 101c0: 00100513 li a0,1 101c4: 00008067 ret 101c8: 00000513 li a0,0 101cc: 00008067 ret 101d0: 00000513 li a0,0 101d4: 00008067 ret 000101d8 <main>: 101d8: fa010113 addi sp,sp,-96 101dc: 04112e23 sw ra,92(sp) 101e0: 04812c23 sw s0,88(sp) 101e4: 04912a23 sw s1,84(sp) 101e8: 05212823 sw s2,80(sp) 101ec: 00200793 li a5,2 101f0: 04f12423 sw a5,72(sp) 101f4: 00600793 li a5,6 101f8: 04f12623 sw a5,76(sp) 101fc: 000217b7 lui a5,0x21 10200: 79c78793 addi a5,a5,1948 # 2179c <__clzsi2+0xc8> 10204: 0007ae03 lw t3,0(a5) 10208: 0047a303 lw t1,4(a5) 1020c: 0087a883 lw a7,8(a5) 10210: 00c7a803 lw a6,12(a5) 10214: 0107a503 lw a0,16(a5) 10218: 0147a583 lw a1,20(a5) 1021c: 0187a603 lw a2,24(a5) 10220: 01c7a683 lw a3,28(a5) 10224: 0207a703 lw a4,32(a5) 10228: 03c12223 sw t3,36(sp) 1022c: 02612423 sw t1,40(sp) 10230: 03112623 sw a7,44(sp) 10234: 03012823 sw a6,48(sp) 10238: 02a12a23 sw a0,52(sp) 1023c: 02b12c23 sw a1,56(sp) 10240: 02c12e23 sw a2,60(sp) 10244: 04d12023 sw a3,64(sp) 10248: 04e12223 sw a4,68(sp) 1024c: 0247a503 lw a0,36(a5) 10250: 0287a583 lw a1,40(a5) 10254: 02c7a603 lw a2,44(a5) 10258: 0307a683 lw a3,48(a5) 1025c: 0347a703 lw a4,52(a5) 10260: 0387a783 lw a5,56(a5) 10264: 00a12623 sw a0,12(sp) 10268: 00b12823 sw a1,16(sp) 1026c: 00c12a23 sw a2,20(sp) 10270: 00d12c23 sw a3,24(sp) 10274: 00e12e23 sw a4,28(sp) 10278: 02f12023 sw a5,32(sp) 1027c: 00200593 li a1,2 10280: 04810513 addi a0,sp,72 10284: f01ff0ef jal ra,10184 <threeConsecutiveOdds> 10288: 00050913 mv s2,a0 1028c: 00900593 li a1,9 10290: 02410513 addi a0,sp,36 10294: ef1ff0ef jal ra,10184 <threeConsecutiveOdds> 10298: 00050493 mv s1,a0 1029c: 00600593 li a1,6 102a0: 00c10513 addi a0,sp,12 102a4: ee1ff0ef jal ra,10184 <threeConsecutiveOdds> 102a8: 00050413 mv s0,a0 102ac: 00090593 mv a1,s2 102b0: 00021537 lui a0,0x21 102b4: 76050513 addi a0,a0,1888 # 21760 <__clzsi2+0x8c> 102b8: 260000ef jal ra,10518 <printf> 102bc: 00048593 mv a1,s1 102c0: 00021537 lui a0,0x21 102c4: 77450513 addi a0,a0,1908 # 21774 <__clzsi2+0xa0> 102c8: 250000ef jal ra,10518 <printf> 102cc: 00040593 mv a1,s0 102d0: 00021537 lui a0,0x21 102d4: 78850513 addi a0,a0,1928 # 21788 <__clzsi2+0xb4> 102d8: 240000ef jal ra,10518 <printf> 102dc: 00000513 li a0,0 102e0: 05c12083 lw ra,92(sp) 102e4: 05812403 lw s0,88(sp) 102e8: 05412483 lw s1,84(sp) 102ec: 05012903 lw s2,80(sp) 102f0: 06010113 addi sp,sp,96 102f4: 00008067 ret ``` O2 ```RISCV 000100c4 <main>: 100c4: 000217b7 lui a5,0x21 100c8: fb010113 addi sp,sp,-80 100cc: 7c478793 addi a5,a5,1988 # 217c4 <__clzsi2+0xc8> 100d0: 0087a383 lw t2,8(a5) 100d4: 00c7a283 lw t0,12(a5) 100d8: 0107af83 lw t6,16(a5) 100dc: 0147af03 lw t5,20(a5) 100e0: 0187ae83 lw t4,24(a5) 100e4: 01c7ae03 lw t3,28(a5) 100e8: 0207a303 lw t1,32(a5) 100ec: 0247a883 lw a7,36(a5) 100f0: 0287a803 lw a6,40(a5) 100f4: 02c7a603 lw a2,44(a5) 100f8: 0307a683 lw a3,48(a5) 100fc: 0347a703 lw a4,52(a5) 10100: 04812423 sw s0,72(sp) 10104: 04912223 sw s1,68(sp) 10108: 0047a403 lw s0,4(a5) 1010c: 0007a483 lw s1,0(a5) 10110: 0387a783 lw a5,56(a5) 10114: 04112623 sw ra,76(sp) 10118: 02712223 sw t2,36(sp) 1011c: 02512423 sw t0,40(sp) 10120: 03f12623 sw t6,44(sp) 10124: 03e12823 sw t5,48(sp) 10128: 03d12a23 sw t4,52(sp) 1012c: 03c12c23 sw t3,56(sp) 10130: 02612e23 sw t1,60(sp) 10134: 01112223 sw a7,4(sp) 10138: 01012423 sw a6,8(sp) 1013c: 00c12623 sw a2,12(sp) 10140: 00d12823 sw a3,16(sp) 10144: 00e12a23 sw a4,20(sp) 10148: 00912e23 sw s1,28(sp) 1014c: 02812023 sw s0,32(sp) 10150: 00900593 li a1,9 10154: 01c10513 addi a0,sp,28 10158: 00f12c23 sw a5,24(sp) 1015c: 120000ef jal ra,1027c <threeConsecutiveOdds.part.0> 10160: 00050493 mv s1,a0 10164: 00600593 li a1,6 10168: 00410513 addi a0,sp,4 1016c: 110000ef jal ra,1027c <threeConsecutiveOdds.part.0> 10170: 00050413 mv s0,a0 10174: 00021537 lui a0,0x21 10178: 00000593 li a1,0 1017c: 78850513 addi a0,a0,1928 # 21788 <__clzsi2+0x8c> 10180: 3c0000ef jal ra,10540 <printf> 10184: 00021537 lui a0,0x21 10188: 00048593 mv a1,s1 1018c: 79c50513 addi a0,a0,1948 # 2179c <__clzsi2+0xa0> 10190: 3b0000ef jal ra,10540 <printf> 10194: 00021537 lui a0,0x21 10198: 00040593 mv a1,s0 1019c: 7b050513 addi a0,a0,1968 # 217b0 <__clzsi2+0xb4> 101a0: 3a0000ef jal ra,10540 <printf> 101a4: 04c12083 lw ra,76(sp) 101a8: 04812403 lw s0,72(sp) 101ac: 04412483 lw s1,68(sp) 101b0: 00000513 li a0,0 101b4: 05010113 addi sp,sp,80 101b8: 00008067 ret 0001027c <threeConsecutiveOdds.part.0>: 1027c: 04b05063 blez a1,102bc <threeConsecutiveOdds.part.0+0x40> 10280: 00000713 li a4,0 10284: 00000693 li a3,0 10288: 00300613 li a2,3 1028c: 0100006f j 1029c <threeConsecutiveOdds.part.0+0x20> 10290: 02c70a63 beq a4,a2,102c4 <threeConsecutiveOdds.part.0+0x48> 10294: 00450513 addi a0,a0,4 10298: 02b68263 beq a3,a1,102bc <threeConsecutiveOdds.part.0+0x40> 1029c: 00052783 lw a5,0(a0) 102a0: 00168693 addi a3,a3,1 102a4: 00170713 addi a4,a4,1 102a8: 0017f793 andi a5,a5,1 102ac: fe0792e3 bnez a5,10290 <threeConsecutiveOdds.part.0+0x14> 102b0: 00000713 li a4,0 102b4: 00450513 addi a0,a0,4 102b8: feb692e3 bne a3,a1,1029c <threeConsecutiveOdds.part.0+0x20> 102bc: 00000513 li a0,0 102c0: 00008067 ret 102c4: 00100513 li a0,1 102c8: 00008067 ret 000102cc <threeConsecutiveOdds>: 102cc: 00200793 li a5,2 102d0: 04b7d063 bge a5,a1,10310 <threeConsecutiveOdds+0x44> 102d4: 00000713 li a4,0 102d8: 00000693 li a3,0 102dc: 00300613 li a2,3 102e0: 0100006f j 102f0 <threeConsecutiveOdds+0x24> 102e4: 02c70a63 beq a4,a2,10318 <threeConsecutiveOdds+0x4c> 102e8: 00450513 addi a0,a0,4 102ec: 02d58263 beq a1,a3,10310 <threeConsecutiveOdds+0x44> 102f0: 00052783 lw a5,0(a0) 102f4: 00168693 addi a3,a3,1 102f8: 00170713 addi a4,a4,1 102fc: 0017f793 andi a5,a5,1 10300: fe0792e3 bnez a5,102e4 <threeConsecutiveOdds+0x18> 10304: 00000713 li a4,0 10308: 00450513 addi a0,a0,4 1030c: fed592e3 bne a1,a3,102f0 <threeConsecutiveOdds+0x24> 10310: 00000513 li a0,0 10314: 00008067 ret 10318: 00100513 li a0,1 1031c: 00008067 ret ``` O3 ```RISCV 000100c4 <main>: 100c4: 000217b7 lui a5,0x21 100c8: fb010113 addi sp,sp,-80 100cc: 7c478793 addi a5,a5,1988 # 217c4 <__clzsi2+0xc8> 100d0: 0087a383 lw t2,8(a5) 100d4: 00c7a283 lw t0,12(a5) 100d8: 0107af83 lw t6,16(a5) 100dc: 0147af03 lw t5,20(a5) 100e0: 0187ae83 lw t4,24(a5) 100e4: 01c7ae03 lw t3,28(a5) 100e8: 0207a303 lw t1,32(a5) 100ec: 0247a883 lw a7,36(a5) 100f0: 0287a803 lw a6,40(a5) 100f4: 02c7a603 lw a2,44(a5) 100f8: 0307a683 lw a3,48(a5) 100fc: 0347a703 lw a4,52(a5) 10100: 04812423 sw s0,72(sp) 10104: 04912223 sw s1,68(sp) 10108: 0047a403 lw s0,4(a5) 1010c: 0007a483 lw s1,0(a5) 10110: 0387a783 lw a5,56(a5) 10114: 04112623 sw ra,76(sp) 10118: 02712223 sw t2,36(sp) 1011c: 02512423 sw t0,40(sp) 10120: 03f12623 sw t6,44(sp) 10124: 03e12823 sw t5,48(sp) 10128: 03d12a23 sw t4,52(sp) 1012c: 03c12c23 sw t3,56(sp) 10130: 02612e23 sw t1,60(sp) 10134: 01112223 sw a7,4(sp) 10138: 01012423 sw a6,8(sp) 1013c: 00c12623 sw a2,12(sp) 10140: 00d12823 sw a3,16(sp) 10144: 00e12a23 sw a4,20(sp) 10148: 00912e23 sw s1,28(sp) 1014c: 02812023 sw s0,32(sp) 10150: 00900593 li a1,9 10154: 01c10513 addi a0,sp,28 10158: 00f12c23 sw a5,24(sp) 1015c: 120000ef jal ra,1027c <threeConsecutiveOdds.part.0> 10160: 00050493 mv s1,a0 10164: 00600593 li a1,6 10168: 00410513 addi a0,sp,4 1016c: 110000ef jal ra,1027c <threeConsecutiveOdds.part.0> 10170: 00050413 mv s0,a0 10174: 00021537 lui a0,0x21 10178: 00000593 li a1,0 1017c: 78850513 addi a0,a0,1928 # 21788 <__clzsi2+0x8c> 10180: 3c0000ef jal ra,10540 <printf> 10184: 00021537 lui a0,0x21 10188: 00048593 mv a1,s1 1018c: 79c50513 addi a0,a0,1948 # 2179c <__clzsi2+0xa0> 10190: 3b0000ef jal ra,10540 <printf> 10194: 00021537 lui a0,0x21 10198: 00040593 mv a1,s0 1019c: 7b050513 addi a0,a0,1968 # 217b0 <__clzsi2+0xb4> 101a0: 3a0000ef jal ra,10540 <printf> 101a4: 04c12083 lw ra,76(sp) 101a8: 04812403 lw s0,72(sp) 101ac: 04412483 lw s1,68(sp) 101b0: 00000513 li a0,0 101b4: 05010113 addi sp,sp,80 101b8: 00008067 ret 0001027c <threeConsecutiveOdds.part.0>: 1027c: 04b05063 blez a1,102bc <threeConsecutiveOdds.part.0+0x40> 10280: 00000713 li a4,0 10284: 00000693 li a3,0 10288: 00300613 li a2,3 1028c: 0100006f j 1029c <threeConsecutiveOdds.part.0+0x20> 10290: 02c70a63 beq a4,a2,102c4 <threeConsecutiveOdds.part.0+0x48> 10294: 00450513 addi a0,a0,4 10298: 02b68263 beq a3,a1,102bc <threeConsecutiveOdds.part.0+0x40> 1029c: 00052783 lw a5,0(a0) 102a0: 00168693 addi a3,a3,1 102a4: 00170713 addi a4,a4,1 102a8: 0017f793 andi a5,a5,1 102ac: fe0792e3 bnez a5,10290 <threeConsecutiveOdds.part.0+0x14> 102b0: 00000713 li a4,0 102b4: 00450513 addi a0,a0,4 102b8: feb692e3 bne a3,a1,1029c <threeConsecutiveOdds.part.0+0x20> 102bc: 00000513 li a0,0 102c0: 00008067 ret 102c4: 00100513 li a0,1 102c8: 00008067 ret 000102cc <threeConsecutiveOdds>: 102cc: 00200793 li a5,2 102d0: 04b7d063 bge a5,a1,10310 <threeConsecutiveOdds+0x44> 102d4: 00000713 li a4,0 102d8: 00000693 li a3,0 102dc: 00300613 li a2,3 102e0: 0100006f j 102f0 <threeConsecutiveOdds+0x24> 102e4: 02c70a63 beq a4,a2,10318 <threeConsecutiveOdds+0x4c> 102e8: 00450513 addi a0,a0,4 102ec: 02d58263 beq a1,a3,10310 <threeConsecutiveOdds+0x44> 102f0: 00052783 lw a5,0(a0) 102f4: 00168693 addi a3,a3,1 102f8: 00170713 addi a4,a4,1 102fc: 0017f793 andi a5,a5,1 10300: fe0792e3 bnez a5,102e4 <threeConsecutiveOdds+0x18> 10304: 00000713 li a4,0 10308: 00450513 addi a0,a0,4 1030c: fed592e3 bne a1,a3,102f0 <threeConsecutiveOdds+0x24> 10310: 00000513 li a0,0 10314: 00008067 ret 10318: 00100513 li a0,1 1031c: 00008067 ret ``` Os ```RISCV= 000100c4 <main>: 100c4: fb010113 addi sp,sp,-80 100c8: 000215b7 lui a1,0x21 100cc: 04812423 sw s0,72(sp) 100d0: 02400613 li a2,36 100d4: 72458413 addi s0,a1,1828 # 21724 <__clzsi2+0xc8> 100d8: 01c10513 addi a0,sp,28 100dc: 72458593 addi a1,a1,1828 100e0: 04112623 sw ra,76(sp) 100e4: 04912223 sw s1,68(sp) 100e8: 29c000ef jal ra,10384 <memcpy> 100ec: 01800613 li a2,24 100f0: 02440593 addi a1,s0,36 100f4: 00410513 addi a0,sp,4 100f8: 28c000ef jal ra,10384 <memcpy> 100fc: 00900593 li a1,9 10100: 01c10513 addi a0,sp,28 10104: 120000ef jal ra,10224 <threeConsecutiveOdds.part.0> 10108: 00050493 mv s1,a0 1010c: 00600593 li a1,6 10110: 00410513 addi a0,sp,4 10114: 110000ef jal ra,10224 <threeConsecutiveOdds.part.0> 10118: 00050413 mv s0,a0 1011c: 00021537 lui a0,0x21 10120: 00000593 li a1,0 10124: 6e850513 addi a0,a0,1768 # 216e8 <__clzsi2+0x8c> 10128: 51c000ef jal ra,10644 <printf> 1012c: 00021537 lui a0,0x21 10130: 00048593 mv a1,s1 10134: 6fc50513 addi a0,a0,1788 # 216fc <__clzsi2+0xa0> 10138: 50c000ef jal ra,10644 <printf> 1013c: 00021537 lui a0,0x21 10140: 00040593 mv a1,s0 10144: 71050513 addi a0,a0,1808 # 21710 <__clzsi2+0xb4> 10148: 4fc000ef jal ra,10644 <printf> 1014c: 04c12083 lw ra,76(sp) 10150: 04812403 lw s0,72(sp) 10154: 04412483 lw s1,68(sp) 10158: 00000513 li a0,0 1015c: 05010113 addi sp,sp,80 10160: 00008067 ret 00010224 <threeConsecutiveOdds.part.0>: 10224: 00000693 li a3,0 10228: 00000713 li a4,0 1022c: 00300613 li a2,3 10230: 00b74663 blt a4,a1,1023c <threeConsecutiveOdds.part.0+0x18> 10234: 00000513 li a0,0 10238: 00008067 ret 1023c: 00271793 slli a5,a4,0x2 10240: 00f507b3 add a5,a0,a5 10244: 0007a783 lw a5,0(a5) 10248: 0017f793 andi a5,a5,1 1024c: 00078663 beqz a5,10258 <threeConsecutiveOdds.part.0+0x34> 10250: 00168793 addi a5,a3,1 10254: 00c78863 beq a5,a2,10264 <threeConsecutiveOdds.part.0+0x40> 10258: 00170713 addi a4,a4,1 1025c: 00078693 mv a3,a5 10260: fd1ff06f j 10230 <threeConsecutiveOdds.part.0+0xc> 10264: 00100513 li a0,1 10268: 00008067 ret 0001026c <threeConsecutiveOdds>: 1026c: 00200793 li a5,2 10270: 00b7d463 bge a5,a1,10278 <threeConsecutiveOdds+0xc> 10274: fb1ff06f j 10224 <threeConsecutiveOdds.part.0> 10278: 00000513 li a0,0 1027c: 00008067 ret ``` Ofast ```RISCV 000100c4 <main>: 100c4: 000217b7 lui a5,0x21 100c8: fb010113 addi sp,sp,-80 100cc: 7c478793 addi a5,a5,1988 # 217c4 <__clzsi2+0xc8> 100d0: 0087a383 lw t2,8(a5) 100d4: 00c7a283 lw t0,12(a5) 100d8: 0107af83 lw t6,16(a5) 100dc: 0147af03 lw t5,20(a5) 100e0: 0187ae83 lw t4,24(a5) 100e4: 01c7ae03 lw t3,28(a5) 100e8: 0207a303 lw t1,32(a5) 100ec: 0247a883 lw a7,36(a5) 100f0: 0287a803 lw a6,40(a5) 100f4: 02c7a603 lw a2,44(a5) 100f8: 0307a683 lw a3,48(a5) 100fc: 0347a703 lw a4,52(a5) 10100: 04812423 sw s0,72(sp) 10104: 04912223 sw s1,68(sp) 10108: 0047a403 lw s0,4(a5) 1010c: 0007a483 lw s1,0(a5) 10110: 0387a783 lw a5,56(a5) 10114: 04112623 sw ra,76(sp) 10118: 02712223 sw t2,36(sp) 1011c: 02512423 sw t0,40(sp) 10120: 03f12623 sw t6,44(sp) 10124: 03e12823 sw t5,48(sp) 10128: 03d12a23 sw t4,52(sp) 1012c: 03c12c23 sw t3,56(sp) 10130: 02612e23 sw t1,60(sp) 10134: 01112223 sw a7,4(sp) 10138: 01012423 sw a6,8(sp) 1013c: 00c12623 sw a2,12(sp) 10140: 00d12823 sw a3,16(sp) 10144: 00e12a23 sw a4,20(sp) 10148: 00912e23 sw s1,28(sp) 1014c: 02812023 sw s0,32(sp) 10150: 00900593 li a1,9 10154: 01c10513 addi a0,sp,28 10158: 00f12c23 sw a5,24(sp) 1015c: 120000ef jal ra,1027c <threeConsecutiveOdds.part.0> 10160: 00050493 mv s1,a0 10164: 00600593 li a1,6 10168: 00410513 addi a0,sp,4 1016c: 110000ef jal ra,1027c <threeConsecutiveOdds.part.0> 10170: 00050413 mv s0,a0 10174: 00021537 lui a0,0x21 10178: 00000593 li a1,0 1017c: 78850513 addi a0,a0,1928 # 21788 <__clzsi2+0x8c> 10180: 3c0000ef jal ra,10540 <printf> 10184: 00021537 lui a0,0x21 10188: 00048593 mv a1,s1 1018c: 79c50513 addi a0,a0,1948 # 2179c <__clzsi2+0xa0> 10190: 3b0000ef jal ra,10540 <printf> 10194: 00021537 lui a0,0x21 10198: 00040593 mv a1,s0 1019c: 7b050513 addi a0,a0,1968 # 217b0 <__clzsi2+0xb4> 101a0: 3a0000ef jal ra,10540 <printf> 101a4: 04c12083 lw ra,76(sp) 101a8: 04812403 lw s0,72(sp) 101ac: 04412483 lw s1,68(sp) 101b0: 00000513 li a0,0 101b4: 05010113 addi sp,sp,80 101b8: 00008067 ret 0001027c <threeConsecutiveOdds.part.0>: 1027c: 04b05063 blez a1,102bc <threeConsecutiveOdds.part.0+0x40> 10280: 00000713 li a4,0 10284: 00000693 li a3,0 10288: 00300613 li a2,3 1028c: 0100006f j 1029c <threeConsecutiveOdds.part.0+0x20> 10290: 02c70a63 beq a4,a2,102c4 <threeConsecutiveOdds.part.0+0x48> 10294: 00450513 addi a0,a0,4 10298: 02b68263 beq a3,a1,102bc <threeConsecutiveOdds.part.0+0x40> 1029c: 00052783 lw a5,0(a0) 102a0: 00168693 addi a3,a3,1 102a4: 00170713 addi a4,a4,1 102a8: 0017f793 andi a5,a5,1 102ac: fe0792e3 bnez a5,10290 <threeConsecutiveOdds.part.0+0x14> 102b0: 00000713 li a4,0 102b4: 00450513 addi a0,a0,4 102b8: feb692e3 bne a3,a1,1029c <threeConsecutiveOdds.part.0+0x20> 102bc: 00000513 li a0,0 102c0: 00008067 ret 102c4: 00100513 li a0,1 102c8: 00008067 ret 000102cc <threeConsecutiveOdds>: 102cc: 00200793 li a5,2 102d0: 04b7d063 bge a5,a1,10310 <threeConsecutiveOdds+0x44> 102d4: 00000713 li a4,0 102d8: 00000693 li a3,0 102dc: 00300613 li a2,3 102e0: 0100006f j 102f0 <threeConsecutiveOdds+0x24> 102e4: 02c70a63 beq a4,a2,10318 <threeConsecutiveOdds+0x4c> 102e8: 00450513 addi a0,a0,4 102ec: 02d58263 beq a1,a3,10310 <threeConsecutiveOdds+0x44> 102f0: 00052783 lw a5,0(a0) 102f4: 00168693 addi a3,a3,1 102f8: 00170713 addi a4,a4,1 102fc: 0017f793 andi a5,a5,1 10300: fe0792e3 bnez a5,102e4 <threeConsecutiveOdds+0x18> 10304: 00000713 li a4,0 10308: 00450513 addi a0,a0,4 1030c: fed592e3 bne a1,a3,102f0 <threeConsecutiveOdds+0x24> 10310: 00000513 li a0,0 10314: 00008067 ret 10318: 00100513 li a0,1 1031c: 00008067 ret ``` ## **Reference:** [Lab2: RISC-V RV32I[MACF] emulator with ELF support](https://hackmd.io/@sysprog/SJAR5XMmi) [Assignment2: RISC-V Toolchain](https://hackmd.io/@sysprog/2022-arch-homework2)