張中龍
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Assignment2: RISC-V Toolchain ## Problem Description The problem is LeetCode136 from [吳紀寬](https://hackmd.io/JqxPI8fpSP-_0JBFB2t-Tw?view). The following is the Description >Given a non-empty array of integers nums, every element appears twice except for one. Find that single one. >You must implement a solution with a linear runtime complexity and use only constant extra space. ## Motivation This problem seems simple at first. It can be implemented with multiple answer, but [吳紀寬](https://hackmd.io/JqxPI8fpSP-_0JBFB2t-Tw?view)'s answer looks interesting. I don't know that the XOR can use by this way. In my own answer, it need an extra array to record the number. But, this way will use more memory, and his answer looks more easy and efficient. ## C Code [吳紀寬](https://hackmd.io/JqxPI8fpSP-_0JBFB2t-Tw?view)'s C code is perfect already. I just move the ```singlenumber``` function to the head, making it can run in DevC++. Because rv32emu print a number under 10 is more easier, so I change the arr2 test data from```1, 2, 3, 1, 3, 2, *12* ```to```1, 2, 3, 1, 3, 2, *5* ```. Hence the answer will be changed from 12 to 5. ```clike= #include<stdio.h> int singlenumber(int* arr, int size) { int res = arr[0]; for (int i = 1; i < size; i++) res ^= arr[i]; return res; } int main() { int arr1[] = { 4, 1, 2, 1, 2 }; int size1 = 5; int res1 = singlenumber(arr1, size1); printf("result 1: %d \n", res1); int arr2[] = { 1, 2, 3, 1, 3, 2, 5 }; int size2 = 7; int res2 = singlenumber(arr2, size2); printf("result 2: %d \n", res2); int arr3[] = { 3, 6, 3, 6, 7, 8, 8, 5, 5 }; int size3 = 9; int res3 = singlenumber(arr3, size3); printf("result 3: %d \n", res3); } ``` ## Assembly(Origin) ``` assembly= .data arr1: .word 4, 1, 2, 1, 2 arr2: .word 1, 2, 3, 1, 3, 2, 5 arr3: .word 3, 6, 3, 6, 7, 8, 8, 5, 5 size1: .word 5 size2: .word 7 size3: .word 9 result1: .string "result 1: " result2: .string "result 2: " result3: .string "result 3: " space: .string " " enter: .string "\n" .text main: la a1, arr1 #load base address of arr1 into a1 lw a2, size1 #load size of arr1 into a2 jal ra, printarray #call for print example array function la a0, result1 #print string result1 li a7, 4 ecall jal ra, singlenumber #call for singlenumber function jal Enter la a1, arr2 #load base address of arr2 into a1 lw a2, size2 #load size of arr2 into a2 jal ra, printarray #call for print example array function la a0, result2 #print string result2 li a7, 4 ecall jal ra, singlenumber #call for singlenumber function jal Enter #print string enter la a1, arr3 #load base address of arr3 into a1 lw a2, size3 #load size of arr3 into a2 jal ra, printarray #call for print example array function la a0, result3 #print string result3 li a7, 4 ecall jal ra, singlenumber #call for singlenumber function jal Enter #print string enter li a7 10 ecall singlenumber: mv t0, x0 #t0 as temporary result mv t1, x0 #t1 as i of for loop mv t2, a1 #t2 as temporay address and initialize with address of example array loop1: beq t1, a2, exit1 #if i==size, exit1 lw t3, 0(t2) #load array element into t3 xor t0, t0, t3 #xor operation, and save result into t0 addi t2, t2, 4 #shift address by 1 word addi t1, t1, 1 #add i by 1 j loop1 #jump back to loop1 printarray: mv t1, x0 #t1 as i of for loop mv t2, a1 #t2 as temporay address and initialize with address of example array loop2: beq t1, a2, exit2 #if i == size, exit2 lw a0, 0(t2) #load array element into a0 li a7, 1 #print array element ecall la a0, space #print space li a7, 4 ecall addi t2, t2, 4 #shift address by 1 word addi t1, t1, 1 #add i by 1 j loop2 #jump back to loop2 exit1: mv a0, t0 #print result in a0 li a7, 1 ecall jr ra #jump back to main function exit2: la a0, enter #print enter li a7, 4 ecall jr ra #jump back to main function Enter: la a0, enter #print enter li a7, 4 ecall jr ra #jump back to main function ``` ![](https://i.imgur.com/KfXfsxv.png) ## After Because the three function exit, exit2 and Enter only print the ```\n``` string. The exit and Enter is the same. I combine the three functions into previous function. By this way, I can remove some jal and jr and make the code shortly . In loop1 and loop2, I change the beq to bne and remove the jump instruction and add print function(print answer and ```\n```) behind. Meanwhile, the main can remove the print function and jump instruction. Furthermore, because some ```li``` overlap, I can remove some unnecessary ```li```, too. ```assembly= .data arr1: .word 4, 1, 2, 1, 2 arr2: .word 1, 2, 3, 1, 3, 2, 5 arr3: .word 3, 6, 3, 6, 7, 8, 8, 5, 5 size1: .word 5 size2: .word 7 size3: .word 9 result1: .string "result 1: " result2: .string "result 2: " result3: .string "result 3: " space: .string " " enter: .string "\n" .text main: la a1, arr1 #load base address of arr1 into a1 lw a2, size1 #load size of arr1 into a2 jal ra, printarray #call for print example array function la a0, result1 #print string result1 ecall jal ra, singlenumber #call for singlenumber function la a1, arr2 #load base address of arr2 into a1 lw a2, size2 #load size of arr2 into a2 jal ra, printarray #call for print example array function la a0, result2 #print string result2 ecall jal ra, singlenumber #call for singlenumber function la a1, arr3 #load base address of arr3 into a1 lw a2, size3 #load size of arr3 into a2 jal ra, printarray #call for print example array function la a0, result3 #print string result3 ecall jal ra, singlenumber #call for singlenumber function #print string enter li a7 10 ecall singlenumber: mv t0, x0 #t0 as temporary result mv t1, x0 #t1 as i of for loop mv t2, a1 #t2 as temporay address and initialize with address of example array loop1: lw t3, 0(t2) #load array element into t3 xor t0, t0, t3 #xor operation, and save result into t0 addi t2, t2, 4 #shift address by 1 word addi t1, t1, 1 #add i by 1 bne t1, a2, loop1 #if t1<a2, jump back to loop1 mv a0, t0 #print result in a0 li a7, 1 ecall la a0, enter #print enter li a7, 4 ecall jr ra printarray: mv t1, x0 #t1 as i of for loop mv t2, a1 #t2 as temporay address and initialize with address of example array loop2: lw a0, 0(t2) #load array element into a0 li a7, 1 #print array element ecall la a0, space #print space li a7, 4 ecall addi t2, t2, 4 #shift address by 1 word addi t1, t1, 1 #add i by 1 bne t1, a2, loop2 #if i == size, exit2 la a0, enter #print enter ecall jr ra #jump back to main function ``` ![](https://i.imgur.com/G7jiFN3.png) From the picture above, the total cycle reduced. ## Convert to RV32I I change the print way to make rv32emu can run. ``` assembly= .org 0 .global _start /* newlib system calls */ .set SYSEXIT, 93 .set SYSWRITE, 64 .data arr1: .word 4, 1, 2, 1, 2 .set arr_size, .-arr1 arr2: .word 1, 2, 3, 1, 3, 2, 5 .set arr_size, .-arr2 arr3: .word 3, 6, 3, 6, 7, 8, 8, 5, 5 .set arr_size, .-arr3 size1: .word 5 size2: .word 7 size3: .word 9 result1: .ascii "result 1: " .set str_result1_len, .-result1 result2: .ascii "result 2: " .set str_result2_len, .-result2 result3: .ascii "result 3: " .set str_result3_len, .-result3 space: .ascii " " .set str_space_len, .-space enter: .ascii "\n" .set str_entere_len, .-enter .text _start: la s1, arr1 #load base address of arr1 into s1 lw s2, size1 #load size of arr1 into s2 jal ra, printarray la a1, result1 #print string result1 li a0 , 1 li a2, str_result1_len li a7, SYSWRITE ecall jal ra, singlenumber #call for singlenumber function la s1, arr2 #load base address of arr2 into s1 lw s2, size2 #load size of arr2 into s2 jal ra, printarray la a1, result2 #print string result2 li a0 , 1 li a2, str_result2_len li a7, SYSWRITE ecall jal ra, singlenumber #call for singlenumber function la s1, arr3 #load base address of arr3 into s1 lw s2, size3 #load size of arr3 into s2 jal ra, printarray la a1, result3 #print string result3 li a0 , 1 li a2, str_result3_len li a7, SYSWRITE ecall jal ra, singlenumber #call for singlenumber function li a0, 0 li a7, SYSEXIT ecall singlenumber: mv t0, x0 #t0 as temporary result mv t1, x0 #t1 as i of for loop mv t2, s1 #t2 as temporay address and initialize with address of example array loop1: lw t3, 0(t2) #load array element into t3 xor t0, t0, t3 #xor operation, and save result into t0 addi t2, t2, 4 #shift address by 1 word addi t1, t1, 1 #add i by 1 bne t1, s2, loop1 #if t1<s2, jump back to loop1 mv a1, t0 #print result in t0 addi a1,a1,48 #to ASCII addi sp, sp, -4 sw a1, 0(sp) addi a1, sp, 0 li a0 , 1 li a2, 4 li a7, SYSWRITE ecall addi sp,sp,4 la a1, enter #print enter li a0 , 1 li a2, str_entere_len li a7, SYSWRITE ecall jr ra printarray: mv t1, x0 #t1 as i of for loop mv t2, s1 #t2 as temporay address and initialize with address of example array loop2: lw a1, 0(t2) #load array element into a0 addi a1,a1,48 addi sp, sp, -4 sw a1, 0(sp) addi a1, sp, 0 li a0 , 1 li a2, 4 li a7, SYSWRITE ecall addi sp,sp,4 la a1, space #print space li a0 , 1 li a2, str_space_len li a7, SYSWRITE ecall addi t2, t2, 4 #shift address by 1 word addi t1, t1, 1 #add i by 1 bne t1, s2, loop2 #if i == size, exit2 la a1, enter #print enter li a0 , 1 li a2, str_entere_len li a7, SYSWRITE ecall jr ra ``` ``` 4 1 2 1 2 result 1: 4 1 2 3 1 3 2 5 result 2: 5 3 6 3 6 7 8 8 5 5 result 3: 7 inferior exit code 0 CSR cycle count: 576 ``` --- ## -O0 ### Size | text | data | bss |dec |hex |filename | | -------- | -------- | -------- | -------- | -------- | -------- | | 75104 | 2816 |812 |78732 |1338c |SingleNumber_o0 | ### Header ![](https://i.imgur.com/qkluPIZ.png) ### Assembly code after compiling ``` clike= 00010184 <singlenumber>: 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: fdc42783 lw a5,-36(s0) 1019c: 0007a783 lw a5,0(a5) 101a0: fef42623 sw a5,-20(s0) 101a4: 00100793 li a5,1 101a8: fef42423 sw a5,-24(s0) 101ac: 0300006f j 101dc <singlenumber+0x58> 101b0: fe842783 lw a5,-24(s0) 101b4: 00279793 slli a5,a5,0x2 101b8: fdc42703 lw a4,-36(s0) 101bc: 00f707b3 add a5,a4,a5 101c0: 0007a783 lw a5,0(a5) 101c4: fec42703 lw a4,-20(s0) 101c8: 00f747b3 xor a5,a4,a5 101cc: fef42623 sw a5,-20(s0) 101d0: fe842783 lw a5,-24(s0) 101d4: 00178793 addi a5,a5,1 101d8: fef42423 sw a5,-24(s0) 101dc: fe842703 lw a4,-24(s0) 101e0: fd842783 lw a5,-40(s0) 101e4: fcf746e3 blt a4,a5,101b0 <singlenumber+0x2c> 101e8: fec42783 lw a5,-20(s0) 101ec: 00078513 mv a0,a5 101f0: 02c12403 lw s0,44(sp) 101f4: 03010113 addi sp,sp,48 101f8: 00008067 ret 000101fc <main>: 101fc: f7010113 addi sp,sp,-144 10200: 08112623 sw ra,140(sp) 10204: 08812423 sw s0,136(sp) 10208: 09010413 addi s0,sp,144 1020c: f6a42e23 sw a0,-132(s0) 10210: f6b42c23 sw a1,-136(s0) 10214: 000227b7 lui a5,0x22 10218: 80878793 addi a5,a5,-2040 # 21808 <__clzsi2+0xbc> 1021c: 0007a583 lw a1,0(a5) 10220: 0047a603 lw a2,4(a5) 10224: 0087a683 lw a3,8(a5) 10228: 00c7a703 lw a4,12(a5) 1022c: 0107a783 lw a5,16(a5) 10230: fcb42223 sw a1,-60(s0) 10234: fcc42423 sw a2,-56(s0) 10238: fcd42623 sw a3,-52(s0) 1023c: fce42823 sw a4,-48(s0) 10240: fcf42a23 sw a5,-44(s0) 10244: 00500793 li a5,5 10248: fef42623 sw a5,-20(s0) 1024c: fc440793 addi a5,s0,-60 10250: fec42583 lw a1,-20(s0) 10254: 00078513 mv a0,a5 10258: f2dff0ef jal ra,10184 <singlenumber> 1025c: fea42423 sw a0,-24(s0) 10260: fe842583 lw a1,-24(s0) 10264: 000217b7 lui a5,0x21 10268: 7d878513 addi a0,a5,2008 # 217d8 <__clzsi2+0x8c> 1026c: 324000ef jal ra,10590 <printf> 10270: 000227b7 lui a5,0x22 10274: 81c78793 addi a5,a5,-2020 # 2181c <__clzsi2+0xd0> 10278: 0007a803 lw a6,0(a5) 1027c: 0047a503 lw a0,4(a5) 10280: 0087a583 lw a1,8(a5) 10284: 00c7a603 lw a2,12(a5) 10288: 0107a683 lw a3,16(a5) 1028c: 0147a703 lw a4,20(a5) 10290: 0187a783 lw a5,24(a5) 10294: fb042423 sw a6,-88(s0) 10298: faa42623 sw a0,-84(s0) 1029c: fab42823 sw a1,-80(s0) 102a0: fac42a23 sw a2,-76(s0) 102a4: fad42c23 sw a3,-72(s0) 102a8: fae42e23 sw a4,-68(s0) 102ac: fcf42023 sw a5,-64(s0) 102b0: 00700793 li a5,7 102b4: fef42223 sw a5,-28(s0) 102b8: fa840793 addi a5,s0,-88 102bc: fe442583 lw a1,-28(s0) 102c0: 00078513 mv a0,a5 102c4: ec1ff0ef jal ra,10184 <singlenumber> 102c8: fea42023 sw a0,-32(s0) 102cc: fe042583 lw a1,-32(s0) 102d0: 000217b7 lui a5,0x21 102d4: 7e878513 addi a0,a5,2024 # 217e8 <__clzsi2+0x9c> 102d8: 2b8000ef jal ra,10590 <printf> 102dc: 000227b7 lui a5,0x22 102e0: 83878793 addi a5,a5,-1992 # 21838 <__clzsi2+0xec> 102e4: 0007a303 lw t1,0(a5) 102e8: 0047a883 lw a7,4(a5) 102ec: 0087a803 lw a6,8(a5) 102f0: 00c7a503 lw a0,12(a5) 102f4: 0107a583 lw a1,16(a5) 102f8: 0147a603 lw a2,20(a5) 102fc: 0187a683 lw a3,24(a5) 10300: 01c7a703 lw a4,28(a5) 10304: 0207a783 lw a5,32(a5) 10308: f8642223 sw t1,-124(s0) 1030c: f9142423 sw a7,-120(s0) 10310: f9042623 sw a6,-116(s0) 10314: f8a42823 sw a0,-112(s0) 10318: f8b42a23 sw a1,-108(s0) 1031c: f8c42c23 sw a2,-104(s0) 10320: f8d42e23 sw a3,-100(s0) 10324: fae42023 sw a4,-96(s0) 10328: faf42223 sw a5,-92(s0) 1032c: 00900793 li a5,9 10330: fcf42e23 sw a5,-36(s0) 10334: f8440793 addi a5,s0,-124 10338: fdc42583 lw a1,-36(s0) 1033c: 00078513 mv a0,a5 10340: e45ff0ef jal ra,10184 <singlenumber> 10344: fca42c23 sw a0,-40(s0) 10348: fd842583 lw a1,-40(s0) 1034c: 000217b7 lui a5,0x21 10350: 7f878513 addi a0,a5,2040 # 217f8 <__clzsi2+0xac> 10354: 23c000ef jal ra,10590 <printf> 10358: 00000793 li a5,0 1035c: 00078513 mv a0,a5 10360: 08c12083 lw ra,140(sp) 10364: 08812403 lw s0,136(sp) 10368: 09010113 addi sp,sp,144 1036c: 00008067 ret ``` ### Observation * Line of code: ```126``` * Allocate ```144``` bytes on stack * Registers used: ```ra``` ```sp``` ```t1``` ```a0 ~ a7``` ```s0 ``` * Number of lw and sw : ```lw: 40``` ```sw: 38 ``` ### CSR cycle count and Execution Result ``` result 1: 4 result 2: 5 result 3: 7 inferior exit code 0 CSR cycle count: 4792 ``` ## -O1 ### Size | text | data | bss |dec |hex |filename | | -------- | -------- | -------- | -------- | -------- | -------- | | 74960 | 2816 |812 |78588 |132fc |SingleNumber_o1 | ### Header ![](https://i.imgur.com/iCoVVzE.png) ### Assembly code after compiling ``` clike= 00010184 <singlenumber>: 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: fdc42783 lw a5,-36(s0) 1019c: 0007a783 lw a5,0(a5) 101a0: fef42623 sw a5,-20(s0) 101a4: 00100793 li a5,1 101a8: fef42423 sw a5,-24(s0) 101ac: 0300006f j 101dc <singlenumber+0x58> 101b0: fe842783 lw a5,-24(s0) 101b4: 00279793 slli a5,a5,0x2 101b8: fdc42703 lw a4,-36(s0) 101bc: 00f707b3 add a5,a4,a5 101c0: 0007a783 lw a5,0(a5) 101c4: fec42703 lw a4,-20(s0) 101c8: 00f747b3 xor a5,a4,a5 101cc: fef42623 sw a5,-20(s0) 101d0: fe842783 lw a5,-24(s0) 101d4: 00178793 addi a5,a5,1 101d8: fef42423 sw a5,-24(s0) 101dc: fe842703 lw a4,-24(s0) 101e0: fd842783 lw a5,-40(s0) 101e4: fcf746e3 blt a4,a5,101b0 <singlenumber+0x2c> 101e8: fec42783 lw a5,-20(s0) 101ec: 00078513 mv a0,a5 101f0: 02c12403 lw s0,44(sp) 101f4: 03010113 addi sp,sp,48 101f8: 00008067 ret 000101fc <main>: 101fc: f7010113 addi sp,sp,-144 10200: 08112623 sw ra,140(sp) 10204: 08812423 sw s0,136(sp) 10208: 09010413 addi s0,sp,144 1020c: f6a42e23 sw a0,-132(s0) 10210: f6b42c23 sw a1,-136(s0) 10214: 000227b7 lui a5,0x22 10218: 80878793 addi a5,a5,-2040 # 21808 <__clzsi2+0xbc> 1021c: 0007a583 lw a1,0(a5) 10220: 0047a603 lw a2,4(a5) 10224: 0087a683 lw a3,8(a5) 10228: 00c7a703 lw a4,12(a5) 1022c: 0107a783 lw a5,16(a5) 10230: fcb42223 sw a1,-60(s0) 10234: fcc42423 sw a2,-56(s0) 10238: fcd42623 sw a3,-52(s0) 1023c: fce42823 sw a4,-48(s0) 10240: fcf42a23 sw a5,-44(s0) 10244: 00500793 li a5,5 10248: fef42623 sw a5,-20(s0) 1024c: fc440793 addi a5,s0,-60 10250: fec42583 lw a1,-20(s0) 10254: 00078513 mv a0,a5 10258: f2dff0ef jal ra,10184 <singlenumber> 1025c: fea42423 sw a0,-24(s0) 10260: fe842583 lw a1,-24(s0) 10264: 000217b7 lui a5,0x21 10268: 7d878513 addi a0,a5,2008 # 217d8 <__clzsi2+0x8c> 1026c: 324000ef jal ra,10590 <printf> 10270: 000227b7 lui a5,0x22 10274: 81c78793 addi a5,a5,-2020 # 2181c <__clzsi2+0xd0> 10278: 0007a803 lw a6,0(a5) 1027c: 0047a503 lw a0,4(a5) 10280: 0087a583 lw a1,8(a5) 10284: 00c7a603 lw a2,12(a5) 10288: 0107a683 lw a3,16(a5) 1028c: 0147a703 lw a4,20(a5) 10290: 0187a783 lw a5,24(a5) 10294: fb042423 sw a6,-88(s0) 10298: faa42623 sw a0,-84(s0) 1029c: fab42823 sw a1,-80(s0) 102a0: fac42a23 sw a2,-76(s0) 102a4: fad42c23 sw a3,-72(s0) 102a8: fae42e23 sw a4,-68(s0) 102ac: fcf42023 sw a5,-64(s0) 102b0: 00700793 li a5,7 102b4: fef42223 sw a5,-28(s0) 102b8: fa840793 addi a5,s0,-88 102bc: fe442583 lw a1,-28(s0) 102c0: 00078513 mv a0,a5 102c4: ec1ff0ef jal ra,10184 <singlenumber> 102c8: fea42023 sw a0,-32(s0) 102cc: fe042583 lw a1,-32(s0) 102d0: 000217b7 lui a5,0x21 102d4: 7e878513 addi a0,a5,2024 # 217e8 <__clzsi2+0x9c> 102d8: 2b8000ef jal ra,10590 <printf> 102dc: 000227b7 lui a5,0x22 102e0: 83878793 addi a5,a5,-1992 # 21838 <__clzsi2+0xec> 102e4: 0007a303 lw t1,0(a5) 102e8: 0047a883 lw a7,4(a5) 102ec: 0087a803 lw a6,8(a5) 102f0: 00c7a503 lw a0,12(a5) 102f4: 0107a583 lw a1,16(a5) 102f8: 0147a603 lw a2,20(a5) 102fc: 0187a683 lw a3,24(a5) 10300: 01c7a703 lw a4,28(a5) 10304: 0207a783 lw a5,32(a5) 10308: f8642223 sw t1,-124(s0) 1030c: f9142423 sw a7,-120(s0) 10310: f9042623 sw a6,-116(s0) 10314: f8a42823 sw a0,-112(s0) 10318: f8b42a23 sw a1,-108(s0) 1031c: f8c42c23 sw a2,-104(s0) 10320: f8d42e23 sw a3,-100(s0) 10324: fae42023 sw a4,-96(s0) 10328: faf42223 sw a5,-92(s0) 1032c: 00900793 li a5,9 10330: fcf42e23 sw a5,-36(s0) 10334: f8440793 addi a5,s0,-124 10338: fdc42583 lw a1,-36(s0) 1033c: 00078513 mv a0,a5 10340: e45ff0ef jal ra,10184 <singlenumber> 10344: fca42c23 sw a0,-40(s0) 10348: fd842583 lw a1,-40(s0) 1034c: 000217b7 lui a5,0x21 10350: 7f878513 addi a0,a5,2040 # 217f8 <__clzsi2+0xac> 10354: 23c000ef jal ra,10590 <printf> 10358: 00000793 li a5,0 1035c: 00078513 mv a0,a5 10360: 08c12083 lw ra,140(sp) 10364: 08812403 lw s0,136(sp) 10368: 09010113 addi sp,sp,144 1036c: 00008067 ret ``` ### Observation * Line of code: ```126``` * Allocate ```144``` bytes on stack * Registers used: ```ra``` ```sp``` ```a0 ~ a7``` ```s0 ``` * Number of lw and sw : ```lw: 40``` ```sw: 38 ``` ### CSR cycle count and Execution Result ``` result 1: 4 result 2: 5 result 3: 7 inferior exit code 0 CSR cycle count: 4562 ``` ## -O2 ### Size | text | data | bss |dec |hex |filename | | -------- | -------- | -------- | -------- | -------- | -------- | | 74908 | 2816 |812 |78536 |132c8 |SingleNumber_o2 | ### Header ![](https://i.imgur.com/P2x8W2r.png) ### Assembly code after compiling ``` clike= 000100c4 <main>: 100c4: fb010113 addi sp,sp,-80 100c8: 00021537 lui a0,0x21 100cc: 04812423 sw s0,72(sp) 100d0: 00400593 li a1,4 100d4: 72050513 addi a0,a0,1824 # 21720 <__clzsi2+0x88> 100d8: 00021437 lui s0,0x21 100dc: 75040413 addi s0,s0,1872 # 21750 <__clzsi2+0xb8> 100e0: 04112623 sw ra,76(sp) 100e4: 3f8000ef jal ra,104dc <printf> 100e8: 00842583 lw a1,8(s0) 100ec: 01042683 lw a3,16(s0) 100f0: 01842783 lw a5,24(s0) 100f4: 00042803 lw a6,0(s0) 100f8: 00442503 lw a0,4(s0) 100fc: 00c42603 lw a2,12(s0) 10100: 01442703 lw a4,20(s0) 10104: 00b12423 sw a1,8(sp) 10108: 00d12823 sw a3,16(sp) 1010c: 00f12c23 sw a5,24(sp) 10110: 01012023 sw a6,0(sp) 10114: 00a12223 sw a0,4(sp) 10118: 00c12623 sw a2,12(sp) 1011c: 00e12a23 sw a4,20(sp) 10120: 00010793 mv a5,sp 10124: 01810693 addi a3,sp,24 10128: 00100593 li a1,1 1012c: 0047a703 lw a4,4(a5) 10130: 00478793 addi a5,a5,4 10134: 00e5c5b3 xor a1,a1,a4 10138: fed79ae3 bne a5,a3,1012c <main+0x68> 1013c: 00021537 lui a0,0x21 10140: 73050513 addi a0,a0,1840 # 21730 <__clzsi2+0x98> 10144: 398000ef jal ra,104dc <printf> 10148: 02c42583 lw a1,44(s0) 1014c: 03442683 lw a3,52(s0) 10150: 03c42783 lw a5,60(s0) 10154: 01c42303 lw t1,28(s0) 10158: 02042883 lw a7,32(s0) 1015c: 02442803 lw a6,36(s0) 10160: 02842503 lw a0,40(s0) 10164: 03042603 lw a2,48(s0) 10168: 03842703 lw a4,56(s0) 1016c: 02b12623 sw a1,44(sp) 10170: 02d12a23 sw a3,52(sp) 10174: 02f12e23 sw a5,60(sp) 10178: 00612e23 sw t1,28(sp) 1017c: 03112023 sw a7,32(sp) 10180: 03012223 sw a6,36(sp) 10184: 02a12423 sw a0,40(sp) 10188: 02c12823 sw a2,48(sp) 1018c: 02e12c23 sw a4,56(sp) 10190: 01c10793 addi a5,sp,28 10194: 03c10693 addi a3,sp,60 10198: 00300593 li a1,3 1019c: 0047a703 lw a4,4(a5) 101a0: 00478793 addi a5,a5,4 101a4: 00e5c5b3 xor a1,a1,a4 101a8: fef69ae3 bne a3,a5,1019c <main+0xd8> 101ac: 00021537 lui a0,0x21 101b0: 74050513 addi a0,a0,1856 # 21740 <__clzsi2+0xa8> 101b4: 328000ef jal ra,104dc <printf> 101b8: 04c12083 lw ra,76(sp) 101bc: 04812403 lw s0,72(sp) 101c0: 00000513 li a0,0 101c4: 05010113 addi sp,sp,80 101c8: 00008067 ret 0001028c <singlenumber>: 1028c: 00100793 li a5,1 10290: 00050693 mv a3,a0 10294: 00052503 lw a0,0(a0) 10298: 02b7d063 bge a5,a1,102b8 <singlenumber+0x2c> 1029c: 00259593 slli a1,a1,0x2 102a0: 00468793 addi a5,a3,4 102a4: 00b686b3 add a3,a3,a1 102a8: 0007a703 lw a4,0(a5) 102ac: 00478793 addi a5,a5,4 102b0: 00e54533 xor a0,a0,a4 102b4: fed79ae3 bne a5,a3,102a8 <singlenumber+0x1c> 102b8: 00008067 ret ``` ### Observation * Line of code: ```80``` * Allocate ```80``` bytes on stack * Registers used: ```ra``` ```sp``` ```a0 ~ a7``` ```s0 ``` * Number of lw and sw : ```lw: 22``` ```sw: 18 ``` ### CSR cycle count and Execution Result ``` result 1: 4 result 2: 5 result 3: 7 inferior exit code 0 CSR cycle count: 4520 ``` ## -O3 ### Size | text | data | bss |dec |hex |filename | | -------- | -------- | -------- | -------- | -------- | -------- | | 74652 | 2816 |812 |78280 |131c8 |SingleNumber_o3 | ### Header ![](https://i.imgur.com/HtmPmyM.png) ### Assembly code after compiling ``` clike= 000100c4 <main>: 100c4: 00021537 lui a0,0x21 100c8: ff010113 addi sp,sp,-16 100cc: 00400593 li a1,4 100d0: 66050513 addi a0,a0,1632 # 21660 <__clzsi2+0x88> 100d4: 00112623 sw ra,12(sp) 100d8: 344000ef jal ra,1041c <printf> 100dc: 00021537 lui a0,0x21 100e0: 00c00593 li a1,12 100e4: 67050513 addi a0,a0,1648 # 21670 <__clzsi2+0x98> 100e8: 334000ef jal ra,1041c <printf> 100ec: 00021537 lui a0,0x21 100f0: 00700593 li a1,7 100f4: 68050513 addi a0,a0,1664 # 21680 <__clzsi2+0xa8> 100f8: 324000ef jal ra,1041c <printf> 100fc: 00c12083 lw ra,12(sp) 10100: 00000513 li a0,0 10104: 01010113 addi sp,sp,16 10108: 00008067 ret 000101cc <singlenumber>: 101cc: 00100793 li a5,1 101d0: 00050693 mv a3,a0 101d4: 00052503 lw a0,0(a0) 101d8: 02b7d063 bge a5,a1,101f8 <singlenumber+0x2c> 101dc: 00259593 slli a1,a1,0x2 101e0: 00468793 addi a5,a3,4 101e4: 00b686b3 add a3,a3,a1 101e8: 0007a703 lw a4,0(a5) 101ec: 00478793 addi a5,a5,4 101f0: 00e54533 xor a0,a0,a4 101f4: fef69ae3 bne a3,a5,101e8 <singlenumber+0x1c> 101f8: 00008067 ret ``` ### Observation * Line of code: ```32``` * Allocate ```16``` bytes on stack * Registers used: ```ra``` ```sp``` ```a0 ~ a5``` * Number of lw and sw : ```lw: 3``` ```sw: 1``` ### CSR cycle count and Execution Result ``` result 1: 4 result 2: 5 result 3: 7 inferior exit code 0 CSR cycle count: 4438 ``` ## -Ofast ### Size | text | data | bss |dec |hex |filename | | -------- | -------- | -------- | -------- | -------- | -------- | | 74652 | 2816 |812 |78280 |131c8 |SingleNumber_ofast | ### Header ![](https://i.imgur.com/Ek9iAYe.png) ### Assembly code after compiling ``` clike= 000100c4 <main>: 100c4: 00021537 lui a0,0x21 100c8: ff010113 addi sp,sp,-16 100cc: 00400593 li a1,4 100d0: 66050513 addi a0,a0,1632 # 21660 <__clzsi2+0x88> 100d4: 00112623 sw ra,12(sp) 100d8: 344000ef jal ra,1041c <printf> 100dc: 00021537 lui a0,0x21 100e0: 00c00593 li a1,12 100e4: 67050513 addi a0,a0,1648 # 21670 <__clzsi2+0x98> 100e8: 334000ef jal ra,1041c <printf> 100ec: 00021537 lui a0,0x21 100f0: 00700593 li a1,7 100f4: 68050513 addi a0,a0,1664 # 21680 <__clzsi2+0xa8> 100f8: 324000ef jal ra,1041c <printf> 100fc: 00c12083 lw ra,12(sp) 10100: 00000513 li a0,0 10104: 01010113 addi sp,sp,16 10108: 00008067 ret 000101cc <singlenumber>: 101cc: 00100793 li a5,1 101d0: 00050693 mv a3,a0 101d4: 00052503 lw a0,0(a0) 101d8: 02b7d063 bge a5,a1,101f8 <singlenumber+0x2c> 101dc: 00259593 slli a1,a1,0x2 101e0: 00468793 addi a5,a3,4 101e4: 00b686b3 add a3,a3,a1 101e8: 0007a703 lw a4,0(a5) 101ec: 00478793 addi a5,a5,4 101f0: 00e54533 xor a0,a0,a4 101f4: fef69ae3 bne a3,a5,101e8 <singlenumber+0x1c> 101f8: 00008067 ret ``` ### Observation * Line of code: ```32``` * Allocate ```16``` bytes on stack * Registers used: ```ra``` ```sp``` ```a0 ~ a5``` * Number of lw and sw : ```lw: 3``` ```sw: 1``` ### CSR cycle count and Execution Result ``` result 1: 4 result 2: 5 result 3: 7 inferior exit code 0 CSR cycle count: 4438 ``` ## -Os ### Size | text | data | bss |dec |hex |filename | | -------- | -------- | -------- | -------- | -------- | -------- | | 74836 | 2816 |812 |78464 |13280 |SingleNumber_os | ### Header ![](https://i.imgur.com/Bccy8lw.png) ### Assembly code after compiling ``` clike= 000100c4 <main>: 100c4: f9010113 addi sp,sp,-112 100c8: 000215b7 lui a1,0x21 100cc: 06812423 sw s0,104(sp) 100d0: 01400613 li a2,20 100d4: 6f058413 addi s0,a1,1776 # 216f0 <__clzsi2+0xb8> 100d8: 00c10513 addi a0,sp,12 100dc: 6f058593 addi a1,a1,1776 100e0: 06112623 sw ra,108(sp) 100e4: 27c000ef jal ra,10360 <memcpy> 100e8: 00500593 li a1,5 100ec: 00c10513 addi a0,sp,12 100f0: 140000ef jal ra,10230 <singlenumber> 100f4: 00050593 mv a1,a0 100f8: 00021537 lui a0,0x21 100fc: 6c050513 addi a0,a0,1728 # 216c0 <__clzsi2+0x88> 10100: 520000ef jal ra,10620 <printf> 10104: 01c00613 li a2,28 10108: 01440593 addi a1,s0,20 1010c: 02010513 addi a0,sp,32 10110: 250000ef jal ra,10360 <memcpy> 10114: 00700593 li a1,7 10118: 02010513 addi a0,sp,32 1011c: 114000ef jal ra,10230 <singlenumber> 10120: 00050593 mv a1,a0 10124: 00021537 lui a0,0x21 10128: 6d050513 addi a0,a0,1744 # 216d0 <__clzsi2+0x98> 1012c: 4f4000ef jal ra,10620 <printf> 10130: 02400613 li a2,36 10134: 03040593 addi a1,s0,48 10138: 03c10513 addi a0,sp,60 1013c: 224000ef jal ra,10360 <memcpy> 10140: 00900593 li a1,9 10144: 03c10513 addi a0,sp,60 10148: 0e8000ef jal ra,10230 <singlenumber> 1014c: 00050593 mv a1,a0 10150: 00021537 lui a0,0x21 10154: 6e050513 addi a0,a0,1760 # 216e0 <__clzsi2+0xa8> 10158: 4c8000ef jal ra,10620 <printf> 1015c: 06c12083 lw ra,108(sp) 10160: 06812403 lw s0,104(sp) 10164: 00000513 li a0,0 10168: 07010113 addi sp,sp,112 1016c: 00008067 ret 00010230 <singlenumber>: 10230: 00050713 mv a4,a0 10234: 00052503 lw a0,0(a0) 10238: 00100793 li a5,1 1023c: 00b7c463 blt a5,a1,10244 <singlenumber+0x14> 10240: 00008067 ret 10244: 00279693 slli a3,a5,0x2 10248: 00d706b3 add a3,a4,a3 1024c: 0006a683 lw a3,0(a3) 10250: 00178793 addi a5,a5,1 10254: 00d54533 xor a0,a0,a3 10258: fe5ff06f j 1023c <singlenumber+0xc> ``` ### Observation * Line of code: ```56``` * Allocate ```112``` bytes on stack * Registers used: ```ra``` ```sp``` ```a0 ~ a5``` ```s0``` * Number of lw and sw : ```lw: 4``` ```sw: 2``` ### CSR cycle count and Execution Result ``` result 1: 4 result 2: 5 result 3: 7 inferior exit code 0 CSR cycle count: 4675 ``` ## Summary ### Summary of results | | line of code | Allocate bytes on stack | Registers used |lw|sw|CSR| | -------- | -------- | -------- | -------- | -------- | -------- | -------- | | O0 |126 | 144 |```ra``` ```sp``` ```a0 ~ a7``` ```s0``` ```t1``` |40 |38 |4792| | O1 | 126 | 144 |```ra``` ```sp``` ```a0 ~ a7``` ```s0``` ```t1``` |40 |38 |4562 | | O2 | 80 | 80 |```ra``` ```sp``` ```a0 ~ a7``` ```s0``` |22 |18 |4520 | | O3 | 32 | 16 |```ra``` ```sp``` ```a0 ~ a7``` |3 |1 |4438 | | Ofast | 32 | 16 |```ra``` ```sp``` ```a0 ~ a7``` |3 |1 |4438 | | Os | 56 | 112 |```ra``` ```sp``` ```a0 ~ a7``` ``s0`` |4 |2 |4675 | ### anaylize * **O0** to **O1** : Almost the same, but fewer CSR. * **O1** to **O2** : Fewer lines of Code, stack usage amount, lw/sw count, CSR. The register ```t1```not used since O2. * **O2** to **O3** : More Fewer lines of Code, stack usage amount, lw/sw count, CSR. The register ```s0``` is not used in O3. Futhermore, O3's main funtion address is in front of the singlenumber fuction. * **Ofast** :As same as O3, not any differance. * **Os**: Lines of Code, stack usage amount, lw/sw count, register usage amount. is between O2 and O3, but CSR is between O0 and O1.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully