N26112128吳紀寬
    • 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
    • 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 Versions and GitHub Sync Note Insights 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Assignment 2: Lemonade Change ## Problem description The problem is from [張中龍](https://hackmd.io/@ncxDr8I_QguW3tkdtqgZ-Q/Sk0rRIvbi), based on [leetcode 860](https://leetcode.com/problems/lemonade-change/). The description is as follow: > At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you and order one at a time (in the order specified by bills). Each customer will only buy one lemonade and pay with either a $5, $10, or $20 bill. You must provide the correct change to each customer so that the net transaction is that the customer pays $5. > >**Note that you do not have any change in hand at first.** > > Given an integer array bills where bills[i] is the bill the ith customer pays, return true if you can provide every customer with the correct change, or false otherwise My motivation is that it looks quite complex at first sight, so I choosed his topic for my assignment. ## C code implementation His original code seems logically fine to me, but it looks a bit odd, so I refined it as follows: ```clike= #include <stdio.h> #include <stdbool.h> bool LemonadeChange(int* bills, int billsSize){ int bill_5 = 0; int bill_10 = 0; int bill_20 = 0; for(int i = 0; i < billsSize; i++){ if(bills[i] == 5){ bill_5 ++; } else if(bills[i] == 10){ if(bill_5 < 1) return false; bill_5 --; bill_10 ++; } else{ if(bill_10 > 0 && bill_5 > 0){ bill_5 --; bill_10 --; bill_20 ++; } else if(bill_5 > 2){ bill_5 = bill_5 - 3; bill_20 ++; } else{ return false; } } } return true; } int main(int argc, char*argv[]){ int test1[5] = {5,5,5,10,20}; int test2[5] = {5,5,10,10,20}; int test3[10] = {5,5,5,10,5,5,10,20,20,20}; int size1 = 5; int size2 = 5; int size3 = 10; int j = 0; printf("Test 1: "); for(j = 0; j < size1; j++) printf("%d ",test1[j]); if(LemonadeChange(test1, size1) == true) printf(" Ans : true \n"); else printf(" Ans : false \n"); printf("Test 2: "); for(j = 0; j < size2; j++) printf("%d ",test2[j]); if(LemonadeChange(test2, size2) == true) printf(" Ans : true \n"); else printf(" Ans : false \n"); printf("Test 3: "); for(j = 0; j < size3; j++) printf("%d ",test3[j]); if(LemonadeChange(test3, size3) == true) printf(" Ans : true \n"); else printf(" Ans : false \n"); } ``` rv32emu execute result: ``` troy@troy-H610M-S2-DDR4:~/rv32emu$ build/rv32emu tests/hw2/hw2 Test 1: 5 5 5 10 20 Ans : true Test 2: 5 5 10 10 20 Ans : false Test 3: 5 5 5 10 5 5 10 20 20 20 Ans : false inferior exit code 0 ``` ## Comparing Assembly Code Original Assembly Code from [張中龍](https://hackmd.io/@ncxDr8I_QguW3tkdtqgZ-Q/Sk0rRIvbi) ```clike= .data arr1: .word 5,5,5,10,20 size1: .word 5 arr2: .word 5,5,10,10,20 size2: .word 5 arr3: .word 5,5,5,10,5,5,10,20,20,20 size3: .word 10 str0: .string "Ans: false\n" str1: .string "Ans: true\n" space: .string " " tes1str: .string "Test1: " tes2str: .string "Test2: " tes3str: .string "Test3: " .text main: la s0, arr1 #load test1 array base address to s0 lw s1, size1 #load test1 array size to s1 la a0, tes1str #load teststr1 and print li a7, 4 ecall jal ra, Print_test #print test1 array jal ra, LemonadeChange #caculate the result la s0 arr2 #load test2 array base address to s0 lw s1 size2 #load test2 array size to s1 la a0,tes2str #load teststr2 and print ecall jal ra , Print_test #print test2 array jal ra , LemonadeChange #caculate the result la s0 arr3 #load test3 array base address to s0 lw s1 size3 #load test3 array size to s1 la a0 ,tes3str #load teststr3 and print ecall jal ra Print_test #print test3 array jal ra , LemonadeChange #caculate the result li a7 10 ecall LemonadeChange: #s0:bills base s1:size t0:bill_5 t1:bill_10 t2:bill_20 t3:i t4:return address to main #s2=answer addi t0, x0, 0 #clear reg t0 addi t1, x0, 0 #clear reg t1 addi t2, x0, 0 #clear reg t2 mv a0, x0 #clear reg a0 addi t3, x0, 0 #clear reg t2 addi t4, ra, 0 #save return addrees to t4 addi t5, x0, 0 #clear reg t5 jal ra, LOOP #jump to LOOP and save return address in ra j TRUE #jump to true LOOP: addi t3, t3, 1 #i = i + 1 beq s1, t3, EXIT #for loop end, jump to exit slli a0, t3, 2 #a0 = i *4 add a0, a0, s0 #a0= test array base address + i*4 lw a0, 0(a0) #a0= test array[i] addi a1, x0, 5 #set a1 reg to 5 beq a1, a0, bill5 #current bill = 5? addi a1, x0, 10 #set a1 reg to 10 beq a1, a0, bill10 #current bill = 10? addi a1, x0, 20 #set a1 reg to 20 beq a1, a0, bill20 #current bill = 20? j LOOP #continue for loop bill5: addi t0, t0, 1 #t0 =t0 + 1 j LOOP #continue for loop bill10: beq t0, x0, FALSE #if don't have any bill_5 : false addi t1, t1, 1 #else: bill_10 = bill_10 + 1 addi t0, t0, -1 #bill_5 = bill_5 -1 j LOOP #continue for loop bill20: slti a2, t0, 1 slti a3, t1, 1 add a4, a2, a3 beq a4, x0, bill20if1 #if have bill_5 and bill_10, jump to bill20if1 slti a2, t0, 3 beq a2, x0, bill20if2 #if have 3 or more bill_5, jump to bill20if2 j FALSE bill20if1: addi t0, t0, -1 #bill_5 = bill_5 - 1 addi t1, t1, -1 #bill_10 = bill_10 - 1 addi t2, t2, 1 #bill_20 = bill_20 - 1 j LOOP #continue for loop bill20if2: addi t0, t0, -3 #bill_5 = bill_5 - 3 addi t2, t2, 1 #bill_20 = bill_20 + 1 j LOOP #continue for loop FALSE: la a0, str0 li a7, 4 ecall jr t4 TRUE: la a0, str1 li a7,4 ecall jr t4 Print_test: # t5: j t1: j*4 slli t1, t5, 2 #t1 = t5 * 4 add t0, s0, t1 #t1 = t1 + s0 lw a0, 0(t0) #a0 = arr[j] li a7, 1 ecall la a0, space li a7, 4 ecall addi t5, t5, 1 #t5 = t5 +1 bne t5, s1, Print_test # if not scan over, then continue Print_test loop jr ra EXIT: jr ra ``` #### Modified it to fit rv32emu Oringinal assembly code has problem with registers abusing(eg: Take ```a``` registers as ```t``` registers), so I tried to modify it. ```clike= ..org 0 # Provide program starting address to linker .global main /* newlib system calls */ .set SYSEXIT, 93 .set SYSWRITE, 64 .data arr1: .word 5,5,5,10,20 size1: .word 5 arr2: .word 5,5,10,10,20 size2: .word 5 arr3: .word 5,5,5,10,5,5,10,20,20,20 size3: .word 10 str0: .string "Ans: false\n" .set str0size, .-str0 str1: .string "Ans: true\n" .set str1size, .-str1 space: .string " " .set spacesize, .-space tes1str: .string "Test1: " .set tes1strsize, .-tes1str tes2str: .string "Test2: " .set tes2strsize, .-tes2str tes3str: .string "Test3: " .set tes3strsize, .-tes3str .text _start: la a3, arr1 #load test1 array base address to a3 lw a4, size1 #load test1 array size to s1 li a7, SYSWRITE # "write" syscall li a0, 1 # 1 = standard output (stdout) la a1, tes1str # load address li a2, tes1strsize # length of string ecall jal ra, LemonadeChange #caculate the result la a3, arr2 #load test2 array base address to a3 lw a4, size2 #load test2 array size to a4 li a7, SYSWRITE # "write" syscall li a0, 1 # 1 = standard output (stdout) la a1, tes2str # load address li a2, tes2strsize # length of string ecall jal ra , LemonadeChange #caculate the result la a3, arr3 #load test3 array base address to a3 lw a4, size3 #load test3 array size to a4 li a7, SYSWRITE # "write" syscall li a0, 1 # 1 = standard output (stdout) la a1, tes2str # load address li a2, tes2strsize # length of string ecall jal ra , LemonadeChange #caculate the result j End LemonadeChange: addi t0, x0, 0 #clear reg t0 addi t1, x0, 0 #clear reg t1 addi t2, x0, 0 #clear reg t2 mv t6, x0 #clear reg a0 addi t3, x0, 0 #clear reg t2 addi t4, ra, 0 #save return addrees to t4 addi t5, x0, 0 #clear reg t5 jal ra, LOOP #jump to LOOP and save return address in ra j TRUE #jump to true LOOP: addi t3, t3, 1 #i = i + 1 beq a4, t3, EXIT #for loop end, jump to exit slli t6, t3, 2 #t6 = i *4 add t6, t6, a3 #t6 = test array base address + i*4 lw t6, 0(t6) #t6 = test array[i] addi s3, x0, 5 #set s3 reg to 5 beq s3, t6, bill5 #current bill = 5? addi s3, x0, 10 #set s3 reg to 10 beq s3, t6, bill10 #current bill = 10? addi s3, x0, 20 #set s3 reg to 20 beq s3, t6, bill20 #current bill = 20? j LOOP #continue for loop bill5: addi t0, t0, 1 #t0 =t0 + 1 j LOOP #continue for loop bill10: beq t0, x0, FALSE #if don't have any bill_5 : false addi t1, t1, 1 #else: bill_10 = bill_10 + 1 addi t0, t0, -1 #bill_5 = bill_5 -1 j LOOP #continue for loop bill20: slti a5, t0, 1 slti t1, t1, 1 add t2, a5, t1 beq t2, x0, bill20if1 #if have bill_5 and bill_10, jump to bill20if1 slti t3, t0, 3 beq t3, x0, bill20if2 #if have 3 or more bill_5, jump to bill20if2 j FALSE bill20if1: addi t0, t0, -1 #bill_5 = bill_5 - 1 addi t1, t1, -1 #bill_10 = bill_10 - 1 addi t2, t2, 1 #bill_20 = bill_20 - 1 j LOOP #continue for loop bill20if2: addi t0, t0, -3 #bill_5 = bill_5 - 3 addi t2, t2, 1 #bill_20 = bill_20 + 1 j LOOP #continue for loop FALSE: li a7, SYSWRITE # "write" syscall li a0, 1 # 1 = standard output (stdout) la a1, str0 # load address li a2, str0size # length of string ecall jr t4 TRUE: li a7, SYSWRITE # "write" syscall li a0, 1 # 1 = standard output (stdout) la a1, str1 # load address li a2, str1size # length of string ecall jr t4 EXIT: jr ra End: li a0, 0 li a7, SYSEXIT ecall ``` #### Results ``` troy@troy-H610M-S2-DDR4:~/rv32emu/tests/hw2test2$ make && ../../build/rv32emu hw2.elf riscv-none-elf-as -R -march=rv32i -mabi=ilp32 -o hw2.o hw2.S riscv-none-elf-ld -o hw2.elf -T hw2.ld --oformat=elf32-littleriscv hw2.o Test1: Ans: true Test2: Ans: false Test2: Ans: false inferior exit code 0 ``` ### O0 #### Size after compiling with O0 option ``` troy@troy-H610M-S2-DDR4:~/rv32emu/tests/hw2$ riscv-none-elf-size hw2_o0 text data bss dec hex filename 75916 2816 812 79544 136b8 hw2_o0 ``` #### Header ``` troy@troy-H610M-S2-DDR4:~/rv32emu/tests/hw2$ riscv-none-elf-readelf -h hw2_o0 ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: EXEC (Executable file) Machine: RISC-V Version: 0x1 Entry point address: 0x100dc Start of program headers: 52 (bytes into file) Start of section headers: 95000 (bytes into file) Flags: 0x0 Size of this header: 52 (bytes) Size of program headers: 32 (bytes) Number of program headers: 3 Size of section headers: 40 (bytes) Number of section headers: 15 Section header string table index: 14 ``` #### Assembly code ```clike= 00010184 <LemonadeChange>: 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: fe042623 sw zero,-20(s0) 1019c: fe042423 sw zero,-24(s0) 101a0: fe042223 sw zero,-28(s0) 101a4: fe042023 sw zero,-32(s0) 101a8: 0ec0006f j 10294 <LemonadeChange+0x110> 101ac: fe042783 lw a5,-32(s0) 101b0: 00279793 slli a5,a5,0x2 101b4: fdc42703 lw a4,-36(s0) 101b8: 00f707b3 add a5,a4,a5 101bc: 0007a703 lw a4,0(a5) 101c0: 00500793 li a5,5 101c4: 00f71a63 bne a4,a5,101d8 <LemonadeChange+0x54> 101c8: fec42783 lw a5,-20(s0) 101cc: 00178793 addi a5,a5,1 101d0: fef42623 sw a5,-20(s0) 101d4: 0b40006f j 10288 <LemonadeChange+0x104> 101d8: fe042783 lw a5,-32(s0) 101dc: 00279793 slli a5,a5,0x2 101e0: fdc42703 lw a4,-36(s0) 101e4: 00f707b3 add a5,a4,a5 101e8: 0007a703 lw a4,0(a5) 101ec: 00a00793 li a5,10 101f0: 02f71863 bne a4,a5,10220 <LemonadeChange+0x9c> 101f4: fec42783 lw a5,-20(s0) 101f8: 00f04663 bgtz a5,10204 <LemonadeChange+0x80> 101fc: 00000793 li a5,0 10200: 0a40006f j 102a4 <LemonadeChange+0x120> 10204: fec42783 lw a5,-20(s0) 10208: fff78793 addi a5,a5,-1 1020c: fef42623 sw a5,-20(s0) 10210: fe842783 lw a5,-24(s0) 10214: 00178793 addi a5,a5,1 10218: fef42423 sw a5,-24(s0) 1021c: 06c0006f j 10288 <LemonadeChange+0x104> 10220: fe842783 lw a5,-24(s0) 10224: 02f05a63 blez a5,10258 <LemonadeChange+0xd4> 10228: fec42783 lw a5,-20(s0) 1022c: 02f05663 blez a5,10258 <LemonadeChange+0xd4> 10230: fec42783 lw a5,-20(s0) 10234: fff78793 addi a5,a5,-1 10238: fef42623 sw a5,-20(s0) 1023c: fe842783 lw a5,-24(s0) 10240: fff78793 addi a5,a5,-1 10244: fef42423 sw a5,-24(s0) 10248: fe442783 lw a5,-28(s0) 1024c: 00178793 addi a5,a5,1 10250: fef42223 sw a5,-28(s0) 10254: 0340006f j 10288 <LemonadeChange+0x104> 10258: fec42703 lw a4,-20(s0) 1025c: 00200793 li a5,2 10260: 02e7d063 bge a5,a4,10280 <LemonadeChange+0xfc> 10264: fec42783 lw a5,-20(s0) 10268: ffd78793 addi a5,a5,-3 1026c: fef42623 sw a5,-20(s0) 10270: fe442783 lw a5,-28(s0) 10274: 00178793 addi a5,a5,1 10278: fef42223 sw a5,-28(s0) 1027c: 00c0006f j 10288 <LemonadeChange+0x104> 10280: 00000793 li a5,0 10284: 0200006f j 102a4 <LemonadeChange+0x120> 10288: fe042783 lw a5,-32(s0) 1028c: 00178793 addi a5,a5,1 10290: fef42023 sw a5,-32(s0) 10294: fe042703 lw a4,-32(s0) 10298: fd842783 lw a5,-40(s0) 1029c: f0f748e3 blt a4,a5,101ac <LemonadeChange+0x28> 102a0: 00100793 li a5,1 102a4: 00078513 mv a0,a5 102a8: 02c12403 lw s0,44(sp) 102ac: 03010113 addi sp,sp,48 102b0: 00008067 ret 000102b4 <main>: 102b4: f8010113 addi sp,sp,-128 102b8: 06112e23 sw ra,124(sp) 102bc: 06812c23 sw s0,120(sp) 102c0: 08010413 addi s0,sp,128 102c4: f8a42623 sw a0,-116(s0) 102c8: f8b42423 sw a1,-120(s0) 102cc: 000227b7 lui a5,0x22 102d0: b2878793 addi a5,a5,-1240 # 21b28 <__clzsi2+0xd0> 102d4: 0007a583 lw a1,0(a5) 102d8: 0047a603 lw a2,4(a5) 102dc: 0087a683 lw a3,8(a5) 102e0: 00c7a703 lw a4,12(a5) 102e4: 0107a783 lw a5,16(a5) 102e8: fcb42623 sw a1,-52(s0) 102ec: fcc42823 sw a2,-48(s0) 102f0: fcd42a23 sw a3,-44(s0) 102f4: fce42c23 sw a4,-40(s0) 102f8: fcf42e23 sw a5,-36(s0) 102fc: 000227b7 lui a5,0x22 10300: b3c78793 addi a5,a5,-1220 # 21b3c <__clzsi2+0xe4> 10304: 0007a583 lw a1,0(a5) 10308: 0047a603 lw a2,4(a5) 1030c: 0087a683 lw a3,8(a5) 10310: 00c7a703 lw a4,12(a5) 10314: 0107a783 lw a5,16(a5) 10318: fab42c23 sw a1,-72(s0) 1031c: fac42e23 sw a2,-68(s0) 10320: fcd42023 sw a3,-64(s0) 10324: fce42223 sw a4,-60(s0) 10328: fcf42423 sw a5,-56(s0) 1032c: 000227b7 lui a5,0x22 10330: b5078793 addi a5,a5,-1200 # 21b50 <__clzsi2+0xf8> 10334: 0007ae03 lw t3,0(a5) 10338: 0047a303 lw t1,4(a5) 1033c: 0087a883 lw a7,8(a5) 10340: 00c7a803 lw a6,12(a5) 10344: 0107a503 lw a0,16(a5) 10348: 0147a583 lw a1,20(a5) 1034c: 0187a603 lw a2,24(a5) 10350: 01c7a683 lw a3,28(a5) 10354: 0207a703 lw a4,32(a5) 10358: 0247a783 lw a5,36(a5) 1035c: f9c42823 sw t3,-112(s0) 10360: f8642a23 sw t1,-108(s0) 10364: f9142c23 sw a7,-104(s0) 10368: f9042e23 sw a6,-100(s0) 1036c: faa42023 sw a0,-96(s0) 10370: fab42223 sw a1,-92(s0) 10374: fac42423 sw a2,-88(s0) 10378: fad42623 sw a3,-84(s0) 1037c: fae42823 sw a4,-80(s0) 10380: faf42a23 sw a5,-76(s0) 10384: 00500793 li a5,5 10388: fef42423 sw a5,-24(s0) 1038c: 00500793 li a5,5 10390: fef42223 sw a5,-28(s0) 10394: 00a00793 li a5,10 10398: fef42023 sw a5,-32(s0) 1039c: fe042623 sw zero,-20(s0) 103a0: 000227b7 lui a5,0x22 103a4: ae078513 addi a0,a5,-1312 # 21ae0 <__clzsi2+0x88> 103a8: 3bc000ef jal ra,10764 <printf> 103ac: fe042623 sw zero,-20(s0) 103b0: 0340006f j 103e4 <main+0x130> 103b4: fec42783 lw a5,-20(s0) 103b8: 00279793 slli a5,a5,0x2 103bc: ff078793 addi a5,a5,-16 103c0: 008787b3 add a5,a5,s0 103c4: fdc7a783 lw a5,-36(a5) 103c8: 00078593 mv a1,a5 103cc: 000227b7 lui a5,0x22 103d0: aec78513 addi a0,a5,-1300 # 21aec <__clzsi2+0x94> 103d4: 390000ef jal ra,10764 <printf> 103d8: fec42783 lw a5,-20(s0) 103dc: 00178793 addi a5,a5,1 103e0: fef42623 sw a5,-20(s0) 103e4: fec42703 lw a4,-20(s0) 103e8: fe842783 lw a5,-24(s0) 103ec: fcf744e3 blt a4,a5,103b4 <main+0x100> 103f0: fcc40793 addi a5,s0,-52 103f4: fe842583 lw a1,-24(s0) 103f8: 00078513 mv a0,a5 103fc: d89ff0ef jal ra,10184 <LemonadeChange> 10400: 00050793 mv a5,a0 10404: 00078a63 beqz a5,10418 <main+0x164> 10408: 000227b7 lui a5,0x22 1040c: af078513 addi a0,a5,-1296 # 21af0 <__clzsi2+0x98> 10410: 4cc000ef jal ra,108dc <puts> 10414: 0100006f j 10424 <main+0x170> 10418: 000227b7 lui a5,0x22 1041c: b0078513 addi a0,a5,-1280 # 21b00 <__clzsi2+0xa8> 10420: 4bc000ef jal ra,108dc <puts> 10424: 000227b7 lui a5,0x22 10428: b1078513 addi a0,a5,-1264 # 21b10 <__clzsi2+0xb8> 1042c: 338000ef jal ra,10764 <printf> 10430: fe042623 sw zero,-20(s0) 10434: 0340006f j 10468 <main+0x1b4> 10438: fec42783 lw a5,-20(s0) 1043c: 00279793 slli a5,a5,0x2 10440: ff078793 addi a5,a5,-16 10444: 008787b3 add a5,a5,s0 10448: fc87a783 lw a5,-56(a5) 1044c: 00078593 mv a1,a5 10450: 000227b7 lui a5,0x22 10454: aec78513 addi a0,a5,-1300 # 21aec <__clzsi2+0x94> 10458: 30c000ef jal ra,10764 <printf> 1045c: fec42783 lw a5,-20(s0) 10460: 00178793 addi a5,a5,1 10464: fef42623 sw a5,-20(s0) 10468: fec42703 lw a4,-20(s0) 1046c: fe442783 lw a5,-28(s0) 10470: fcf744e3 blt a4,a5,10438 <main+0x184> 10474: fb840793 addi a5,s0,-72 10478: fe442583 lw a1,-28(s0) 1047c: 00078513 mv a0,a5 10480: d05ff0ef jal ra,10184 <LemonadeChange> 10484: 00050793 mv a5,a0 10488: 00078a63 beqz a5,1049c <main+0x1e8> 1048c: 000227b7 lui a5,0x22 10490: af078513 addi a0,a5,-1296 # 21af0 <__clzsi2+0x98> 10494: 448000ef jal ra,108dc <puts> 10498: 0100006f j 104a8 <main+0x1f4> 1049c: 000227b7 lui a5,0x22 104a0: b0078513 addi a0,a5,-1280 # 21b00 <__clzsi2+0xa8> 104a4: 438000ef jal ra,108dc <puts> 104a8: 000227b7 lui a5,0x22 104ac: b1c78513 addi a0,a5,-1252 # 21b1c <__clzsi2+0xc4> 104b0: 2b4000ef jal ra,10764 <printf> 104b4: fe042623 sw zero,-20(s0) 104b8: 0340006f j 104ec <main+0x238> 104bc: fec42783 lw a5,-20(s0) 104c0: 00279793 slli a5,a5,0x2 104c4: ff078793 addi a5,a5,-16 104c8: 008787b3 add a5,a5,s0 104cc: fa07a783 lw a5,-96(a5) 104d0: 00078593 mv a1,a5 104d4: 000227b7 lui a5,0x22 104d8: aec78513 addi a0,a5,-1300 # 21aec <__clzsi2+0x94> 104dc: 288000ef jal ra,10764 <printf> 104e0: fec42783 lw a5,-20(s0) 104e4: 00178793 addi a5,a5,1 104e8: fef42623 sw a5,-20(s0) 104ec: fec42703 lw a4,-20(s0) 104f0: fe042783 lw a5,-32(s0) 104f4: fcf744e3 blt a4,a5,104bc <main+0x208> 104f8: f9040793 addi a5,s0,-112 104fc: fe042583 lw a1,-32(s0) 10500: 00078513 mv a0,a5 10504: c81ff0ef jal ra,10184 <LemonadeChange> 10508: 00050793 mv a5,a0 1050c: 00078a63 beqz a5,10520 <main+0x26c> 10510: 000227b7 lui a5,0x22 10514: af078513 addi a0,a5,-1296 # 21af0 <__clzsi2+0x98> 10518: 3c4000ef jal ra,108dc <puts> 1051c: 0100006f j 1052c <main+0x278> 10520: 000227b7 lui a5,0x22 10524: b0078513 addi a0,a5,-1280 # 21b00 <__clzsi2+0xa8> 10528: 3b4000ef jal ra,108dc <puts> 1052c: 00000793 li a5,0 10530: 00078513 mv a0,a5 10534: 07c12083 lw ra,124(sp) 10538: 07812403 lw s0,120(sp) 1053c: 08010113 addi sp,sp,128 10540: 00008067 ret ``` #### Observation * Line of code: ```242``` * Allocate ```128``` bytes on stack * Registers used: ```ra``` ```sp``` ```a0 ~ a6``` ```s0``` ```t1 t3``` * Number of lw and sw instructions: 62 ```lw``` 50 ```sw``` #### Execution and CSR count ``` troy@troy-H610M-S2-DDR4:~/rv32emu$ build/rv32emu --stats tests/hw2/hw2_o0 Test 1: 5 5 5 10 20 Ans : true Test 2: 5 5 10 10 20 Ans : false Test 3: 5 5 5 10 5 5 10 20 20 20 Ans : false inferior exit code 0 CSR cycle count: 20016 ``` ### O1 optimized #### Size after compiling with O1 option ``` troy@troy-H610M-S2-DDR4:~/rv32emu/tests/hw2$ riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -O1 -o hw2_o1 hw2.c troy@troy-H610M-S2-DDR4:~/rv32emu/tests/hw2$ riscv-none-elf-size hw2_o1 text data bss dec hex filename 75584 2816 812 79212 1356c hw2_o1 ``` #### Header ``` troy@troy-H610M-S2-DDR4:~/rv32emu/tests/hw2$ riscv-none-elf-readelf -h hw2_o1 ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: EXEC (Executable file) Machine: RISC-V Version: 0x1 Entry point address: 0x100dc Start of program headers: 52 (bytes into file) Start of section headers: 95000 (bytes into file) Flags: 0x0 Size of this header: 52 (bytes) Size of program headers: 32 (bytes) Number of program headers: 3 Size of section headers: 40 (bytes) Number of section headers: 15 Section header string table index: 14 ``` #### Assembly code after compiling ```clike= 00010184 <LemonadeChange>: 10184: 06b05a63 blez a1,101f8 <LemonadeChange+0x74> 10188: 00000793 li a5,0 1018c: 00000613 li a2,0 10190: 00000713 li a4,0 10194: 00500813 li a6,5 10198: 00a00893 li a7,10 1019c: 00200313 li t1,2 101a0: 0140006f j 101b4 <LemonadeChange+0x30> 101a4: 00170713 addi a4,a4,1 101a8: 00178793 addi a5,a5,1 101ac: 00450513 addi a0,a0,4 101b0: 04f58063 beq a1,a5,101f0 <LemonadeChange+0x6c> 101b4: 00052683 lw a3,0(a0) 101b8: ff0686e3 beq a3,a6,101a4 <LemonadeChange+0x20> 101bc: 01168c63 beq a3,a7,101d4 <LemonadeChange+0x50> 101c0: 02c05263 blez a2,101e4 <LemonadeChange+0x60> 101c4: 04e05263 blez a4,10208 <LemonadeChange+0x84> 101c8: fff70713 addi a4,a4,-1 101cc: fff60613 addi a2,a2,-1 101d0: fd9ff06f j 101a8 <LemonadeChange+0x24> 101d4: 02e05663 blez a4,10200 <LemonadeChange+0x7c> 101d8: fff70713 addi a4,a4,-1 101dc: 00160613 addi a2,a2,1 101e0: fc9ff06f j 101a8 <LemonadeChange+0x24> 101e4: 02e35663 bge t1,a4,10210 <LemonadeChange+0x8c> 101e8: ffd70713 addi a4,a4,-3 101ec: fbdff06f j 101a8 <LemonadeChange+0x24> 101f0: 00100513 li a0,1 101f4: 00008067 ret 101f8: 00100513 li a0,1 101fc: 00008067 ret 10200: 00000513 li a0,0 10204: 00008067 ret 10208: 00000513 li a0,0 1020c: 00008067 ret 10210: 00000513 li a0,0 10214: 00008067 ret 00010218 <main>: 10218: fa010113 addi sp,sp,-96 1021c: 04112e23 sw ra,92(sp) 10220: 04812c23 sw s0,88(sp) 10224: 04912a23 sw s1,84(sp) 10228: 05212823 sw s2,80(sp) 1022c: 000227b7 lui a5,0x22 10230: 9e078793 addi a5,a5,-1568 # 219e0 <__clzsi2+0xd4> 10234: 0007a503 lw a0,0(a5) 10238: 0047a583 lw a1,4(a5) 1023c: 0087a603 lw a2,8(a5) 10240: 00c7a683 lw a3,12(a5) 10244: 0107a703 lw a4,16(a5) 10248: 02a12e23 sw a0,60(sp) 1024c: 04b12023 sw a1,64(sp) 10250: 04c12223 sw a2,68(sp) 10254: 04d12423 sw a3,72(sp) 10258: 04e12623 sw a4,76(sp) 1025c: 0147a503 lw a0,20(a5) 10260: 0187a583 lw a1,24(a5) 10264: 01c7a603 lw a2,28(a5) 10268: 0207a683 lw a3,32(a5) 1026c: 0247a703 lw a4,36(a5) 10270: 02a12423 sw a0,40(sp) 10274: 02b12623 sw a1,44(sp) 10278: 02c12823 sw a2,48(sp) 1027c: 02d12a23 sw a3,52(sp) 10280: 02e12c23 sw a4,56(sp) 10284: 0287ae03 lw t3,40(a5) 10288: 02c7a303 lw t1,44(a5) 1028c: 0307a883 lw a7,48(a5) 10290: 0347a803 lw a6,52(a5) 10294: 0387a503 lw a0,56(a5) 10298: 03c7a583 lw a1,60(a5) 1029c: 0407a603 lw a2,64(a5) 102a0: 0447a683 lw a3,68(a5) 102a4: 0487a703 lw a4,72(a5) 102a8: 04c7a783 lw a5,76(a5) 102ac: 01c12023 sw t3,0(sp) 102b0: 00612223 sw t1,4(sp) 102b4: 01112423 sw a7,8(sp) 102b8: 01012623 sw a6,12(sp) 102bc: 00a12823 sw a0,16(sp) 102c0: 00b12a23 sw a1,20(sp) 102c4: 00c12c23 sw a2,24(sp) 102c8: 00d12e23 sw a3,28(sp) 102cc: 02e12023 sw a4,32(sp) 102d0: 02f12223 sw a5,36(sp) 102d4: 00022537 lui a0,0x22 102d8: 99850513 addi a0,a0,-1640 # 21998 <__clzsi2+0x8c> 102dc: 33c000ef jal ra,10618 <printf> 102e0: 03c10413 addi s0,sp,60 102e4: 05010913 addi s2,sp,80 102e8: 000224b7 lui s1,0x22 102ec: 00042583 lw a1,0(s0) 102f0: 9a448513 addi a0,s1,-1628 # 219a4 <__clzsi2+0x98> 102f4: 324000ef jal ra,10618 <printf> 102f8: 00440413 addi s0,s0,4 102fc: ff2418e3 bne s0,s2,102ec <main+0xd4> 10300: 00500593 li a1,5 10304: 03c10513 addi a0,sp,60 10308: e7dff0ef jal ra,10184 <LemonadeChange> 1030c: 0a050e63 beqz a0,103c8 <main+0x1b0> 10310: 00022537 lui a0,0x22 10314: 9a850513 addi a0,a0,-1624 # 219a8 <__clzsi2+0x9c> 10318: 478000ef jal ra,10790 <puts> 1031c: 00022537 lui a0,0x22 10320: 9c850513 addi a0,a0,-1592 # 219c8 <__clzsi2+0xbc> 10324: 2f4000ef jal ra,10618 <printf> 10328: 02810413 addi s0,sp,40 1032c: 03c10913 addi s2,sp,60 10330: 000224b7 lui s1,0x22 10334: 00042583 lw a1,0(s0) 10338: 9a448513 addi a0,s1,-1628 # 219a4 <__clzsi2+0x98> 1033c: 2dc000ef jal ra,10618 <printf> 10340: 00440413 addi s0,s0,4 10344: ff2418e3 bne s0,s2,10334 <main+0x11c> 10348: 00500593 li a1,5 1034c: 02810513 addi a0,sp,40 10350: e35ff0ef jal ra,10184 <LemonadeChange> 10354: 08050263 beqz a0,103d8 <main+0x1c0> 10358: 00022537 lui a0,0x22 1035c: 9a850513 addi a0,a0,-1624 # 219a8 <__clzsi2+0x9c> 10360: 430000ef jal ra,10790 <puts> 10364: 00022537 lui a0,0x22 10368: 9d450513 addi a0,a0,-1580 # 219d4 <__clzsi2+0xc8> 1036c: 2ac000ef jal ra,10618 <printf> 10370: 00010413 mv s0,sp 10374: 02810913 addi s2,sp,40 10378: 000224b7 lui s1,0x22 1037c: 00042583 lw a1,0(s0) 10380: 9a448513 addi a0,s1,-1628 # 219a4 <__clzsi2+0x98> 10384: 294000ef jal ra,10618 <printf> 10388: 00440413 addi s0,s0,4 1038c: ff2418e3 bne s0,s2,1037c <main+0x164> 10390: 00a00593 li a1,10 10394: 00010513 mv a0,sp 10398: dedff0ef jal ra,10184 <LemonadeChange> 1039c: 04050663 beqz a0,103e8 <main+0x1d0> 103a0: 00022537 lui a0,0x22 103a4: 9a850513 addi a0,a0,-1624 # 219a8 <__clzsi2+0x9c> 103a8: 3e8000ef jal ra,10790 <puts> 103ac: 00000513 li a0,0 103b0: 05c12083 lw ra,92(sp) 103b4: 05812403 lw s0,88(sp) 103b8: 05412483 lw s1,84(sp) 103bc: 05012903 lw s2,80(sp) 103c0: 06010113 addi sp,sp,96 103c4: 00008067 ret 103c8: 00022537 lui a0,0x22 103cc: 9b850513 addi a0,a0,-1608 # 219b8 <__clzsi2+0xac> 103d0: 3c0000ef jal ra,10790 <puts> 103d4: f49ff06f j 1031c <main+0x104> 103d8: 00022537 lui a0,0x22 103dc: 9b850513 addi a0,a0,-1608 # 219b8 <__clzsi2+0xac> 103e0: 3b0000ef jal ra,10790 <puts> 103e4: f81ff06f j 10364 <main+0x14c> 103e8: 00022537 lui a0,0x22 103ec: 9b850513 addi a0,a0,-1608 # 219b8 <__clzsi2+0xac> 103f0: 3a0000ef jal ra,10790 <puts> 103f4: fb9ff06f j 103ac <main+0x194> ``` #### Observation * Line of code: ```159``` * Allocate ```96``` bytes on stack * Registers used: ```ra``` ```sp``` ```a0~a7``` ```s0~s2``` ```t1 t3``` * Number of lw and sw instructions: 28 ```lw``` 24 ```sw``` #### Execution and CSR count ``` troy@troy-H610M-S2-DDR4:~/rv32emu$ build/rv32emu --stats tests/hw2/hw2_o1 Test 1: 5 5 5 10 20 Ans : true Test 2: 5 5 10 10 20 Ans : false Test 3: 5 5 5 10 5 5 10 20 20 20 Ans : false inferior exit code 0 CSR cycle count: 19482 ``` ### O2 optimized #### Size after compiling with O2 option ``` troy@troy-H610M-S2-DDR4:~/rv32emu/tests/hw2$ riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -O2 -o hw2_o2 hw2.c troy@troy-H610M-S2-DDR4:~/rv32emu/tests/hw2$ riscv-none-elf-size hw2_o2 text data bss dec hex filename 75568 2816 812 79196 1355c hw2_o2 ``` #### Header ``` troy@troy-H610M-S2-DDR4:~/rv32emu/tests/hw2$ riscv-none-elf-readelf -h ./hw2_o2 ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: EXEC (Executable file) Machine: RISC-V Version: 0x1 Entry point address: 0x102bc Start of program headers: 52 (bytes into file) Start of section headers: 95016 (bytes into file) Flags: 0x0 Size of this header: 52 (bytes) Size of program headers: 32 (bytes) Number of program headers: 3 Size of section headers: 40 (bytes) Number of section headers: 15 Section header string table index: 14 ``` #### The code after compiling ```clike= 00010364 <LemonadeChange>: 10364: 04b05c63 blez a1,103bc <LemonadeChange+0x58> 10368: 00000793 li a5,0 1036c: 00000613 li a2,0 10370: 00000713 li a4,0 10374: 00500813 li a6,5 10378: 00a00893 li a7,10 1037c: 00200313 li t1,2 10380: 0240006f j 103a4 <LemonadeChange+0x40> 10384: 05168063 beq a3,a7,103c4 <LemonadeChange+0x60> 10388: 04060663 beqz a2,103d4 <LemonadeChange+0x70> 1038c: 04070a63 beqz a4,103e0 <LemonadeChange+0x7c> 10390: fff70713 addi a4,a4,-1 10394: fff60613 addi a2,a2,-1 10398: 00178793 addi a5,a5,1 1039c: 00450513 addi a0,a0,4 103a0: 00f58e63 beq a1,a5,103bc <LemonadeChange+0x58> 103a4: 00052683 lw a3,0(a0) 103a8: fd069ee3 bne a3,a6,10384 <LemonadeChange+0x20> 103ac: 00178793 addi a5,a5,1 103b0: 00170713 addi a4,a4,1 103b4: 00450513 addi a0,a0,4 103b8: fef596e3 bne a1,a5,103a4 <LemonadeChange+0x40> 103bc: 00100513 li a0,1 103c0: 00008067 ret 103c4: 00070e63 beqz a4,103e0 <LemonadeChange+0x7c> 103c8: fff70713 addi a4,a4,-1 103cc: 00160613 addi a2,a2,1 103d0: fc9ff06f j 10398 <LemonadeChange+0x34> 103d4: 00e35663 bge t1,a4,103e0 <LemonadeChange+0x7c> 103d8: ffd70713 addi a4,a4,-3 103dc: fbdff06f j 10398 <LemonadeChange+0x34> 103e0: 00000513 li a0,0 103e4: 00008067 ret 000100c4 <main>: 100c4: 000227b7 lui a5,0x22 100c8: f9010113 addi sp,sp,-112 100cc: 9d078793 addi a5,a5,-1584 # 219d0 <__clzsi2+0xd4> 100d0: 0147a383 lw t2,20(a5) 100d4: 0187a283 lw t0,24(a5) 100d8: 06112623 sw ra,108(sp) 100dc: 06812423 sw s0,104(sp) 100e0: 0007a083 lw ra,0(a5) 100e4: 0107a403 lw s0,16(a5) 100e8: 06912223 sw s1,100(sp) 100ec: 07212023 sw s2,96(sp) 100f0: 00c7a483 lw s1,12(a5) 100f4: 0087a903 lw s2,8(a5) 100f8: 05312e23 sw s3,92(sp) 100fc: 0047a983 lw s3,4(a5) 10100: 02c7a503 lw a0,44(a5) 10104: 01c7af83 lw t6,28(a5) 10108: 0207af03 lw t5,32(a5) 1010c: 0247ae83 lw t4,36(a5) 10110: 0287ae03 lw t3,40(a5) 10114: 0347a303 lw t1,52(a5) 10118: 0387a883 lw a7,56(a5) 1011c: 03c7a803 lw a6,60(a5) 10120: 0407a583 lw a1,64(a5) 10124: 0447a603 lw a2,68(a5) 10128: 0487a683 lw a3,72(a5) 1012c: 04c7a703 lw a4,76(a5) 10130: 00112023 sw ra,0(sp) 10134: 0307a783 lw a5,48(a5) 10138: 01212423 sw s2,8(sp) 1013c: 00912623 sw s1,12(sp) 10140: 00812823 sw s0,16(sp) 10144: 00712a23 sw t2,20(sp) 10148: 00512c23 sw t0,24(sp) 1014c: 01312223 sw s3,4(sp) 10150: 01f12e23 sw t6,28(sp) 10154: 02a12623 sw a0,44(sp) 10158: 00022537 lui a0,0x22 1015c: 98850513 addi a0,a0,-1656 # 21988 <__clzsi2+0x8c> 10160: 03e12023 sw t5,32(sp) 10164: 03d12223 sw t4,36(sp) 10168: 03c12423 sw t3,40(sp) 1016c: 02f12823 sw a5,48(sp) 10170: 02612a23 sw t1,52(sp) 10174: 03112c23 sw a7,56(sp) 10178: 03012e23 sw a6,60(sp) 1017c: 04b12023 sw a1,64(sp) 10180: 04c12223 sw a2,68(sp) 10184: 04d12423 sw a3,72(sp) 10188: 04e12623 sw a4,76(sp) 1018c: 00010413 mv s0,sp 10190: 01410913 addi s2,sp,20 10194: 474000ef jal ra,10608 <printf> 10198: 000224b7 lui s1,0x22 1019c: 00042583 lw a1,0(s0) 101a0: 99448513 addi a0,s1,-1644 # 21994 <__clzsi2+0x98> 101a4: 00440413 addi s0,s0,4 101a8: 460000ef jal ra,10608 <printf> 101ac: fe8918e3 bne s2,s0,1019c <main+0xd8> 101b0: 00500593 li a1,5 101b4: 00010513 mv a0,sp 101b8: 1ac000ef jal ra,10364 <LemonadeChange> 101bc: 0c050463 beqz a0,10284 <main+0x1c0> 101c0: 00022537 lui a0,0x22 101c4: 99850513 addi a0,a0,-1640 # 21998 <__clzsi2+0x9c> 101c8: 5b8000ef jal ra,10780 <puts> 101cc: 00022537 lui a0,0x22 101d0: 9b850513 addi a0,a0,-1608 # 219b8 <__clzsi2+0xbc> 101d4: 434000ef jal ra,10608 <printf> 101d8: 00090413 mv s0,s2 101dc: 02810993 addi s3,sp,40 101e0: 00042583 lw a1,0(s0) 101e4: 99448513 addi a0,s1,-1644 101e8: 00440413 addi s0,s0,4 101ec: 41c000ef jal ra,10608 <printf> 101f0: fe8998e3 bne s3,s0,101e0 <main+0x11c> 101f4: 00500593 li a1,5 101f8: 00090513 mv a0,s2 101fc: 168000ef jal ra,10364 <LemonadeChange> 10200: 06050a63 beqz a0,10274 <main+0x1b0> 10204: 00022537 lui a0,0x22 10208: 99850513 addi a0,a0,-1640 # 21998 <__clzsi2+0x9c> 1020c: 574000ef jal ra,10780 <puts> 10210: 00022537 lui a0,0x22 10214: 9c450513 addi a0,a0,-1596 # 219c4 <__clzsi2+0xc8> 10218: 3f0000ef jal ra,10608 <printf> 1021c: 00098413 mv s0,s3 10220: 05010913 addi s2,sp,80 10224: 00042583 lw a1,0(s0) 10228: 99448513 addi a0,s1,-1644 1022c: 00440413 addi s0,s0,4 10230: 3d8000ef jal ra,10608 <printf> 10234: ff2418e3 bne s0,s2,10224 <main+0x160> 10238: 00a00593 li a1,10 1023c: 00098513 mv a0,s3 10240: 124000ef jal ra,10364 <LemonadeChange> 10244: 04050863 beqz a0,10294 <main+0x1d0> 10248: 00022537 lui a0,0x22 1024c: 99850513 addi a0,a0,-1640 # 21998 <__clzsi2+0x9c> 10250: 530000ef jal ra,10780 <puts> 10254: 06c12083 lw ra,108(sp) 10258: 06812403 lw s0,104(sp) 1025c: 06412483 lw s1,100(sp) 10260: 06012903 lw s2,96(sp) 10264: 05c12983 lw s3,92(sp) 10268: 00000513 li a0,0 1026c: 07010113 addi sp,sp,112 10270: 00008067 ret 10274: 00022537 lui a0,0x22 10278: 9a850513 addi a0,a0,-1624 # 219a8 <__clzsi2+0xac> 1027c: 504000ef jal ra,10780 <puts> 10280: f91ff06f j 10210 <main+0x14c> 10284: 00022537 lui a0,0x22 10288: 9a850513 addi a0,a0,-1624 # 219a8 <__clzsi2+0xac> 1028c: 4f4000ef jal ra,10780 <puts> 10290: f3dff06f j 101cc <main+0x108> 10294: 00022537 lui a0,0x22 10298: 9a850513 addi a0,a0,-1624 # 219a8 <__clzsi2+0xac> 1029c: 4e4000ef jal ra,10780 <puts> 102a0: fb5ff06f j 10254 <main+0x190> ``` #### Observation * Line of code: ```155``` * Allocate ```112``` bytes on stack * Registers used: ```ra``` ```sp``` ```a0~a7``` ```s0~s3``` ```t0~t7``` * Number of lw and sw instructions: 29 ```lw``` 25 ```sw``` #### Execution and CSR count ``` troy@troy-H610M-S2-DDR4:~/rv32emu$ build/rv32emu --stats tests/hw2/hw2_o2 Test 1: 5 5 5 10 20 Ans : true Test 2: 5 5 10 10 20 Ans : false Test 3: 5 5 5 10 5 5 10 20 20 20 Ans : false inferior exit code 0 CSR cycle count: 19482 ``` ### O3 optimized #### Size after compiling with O3 option ``` troy@troy-H610M-S2-DDR4:~/rv32emu/tests/hw2$ riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -O3 -o hw2_o3 hw2.c troy@troy-H610M-S2-DDR4:~/rv32emu/tests/hw2$ riscv-none-elf-size hw2_o3 text data bss dec hex filename 75568 2816 812 79196 1355c hw2_o3 ``` #### Header ``` troy@troy-H610M-S2-DDR4:~/rv32emu/tests/hw2$ riscv-none-elf-readelf -h ./hw2_o3 ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: EXEC (Executable file) Machine: RISC-V Version: 0x1 Entry point address: 0x102bc Start of program headers: 52 (bytes into file) Start of section headers: 95016 (bytes into file) Flags: 0x0 Size of this header: 52 (bytes) Size of program headers: 32 (bytes) Number of program headers: 3 Size of section headers: 40 (bytes) Number of section headers: 15 Section header string table index: 14 ``` #### The code after compiling ```clike= 00010364 <LemonadeChange>: 10364: 04b05c63 blez a1,103bc <LemonadeChange+0x58> 10368: 00000793 li a5,0 1036c: 00000613 li a2,0 10370: 00000713 li a4,0 10374: 00500813 li a6,5 10378: 00a00893 li a7,10 1037c: 00200313 li t1,2 10380: 0240006f j 103a4 <LemonadeChange+0x40> 10384: 05168063 beq a3,a7,103c4 <LemonadeChange+0x60> 10388: 04060663 beqz a2,103d4 <LemonadeChange+0x70> 1038c: 04070a63 beqz a4,103e0 <LemonadeChange+0x7c> 10390: fff70713 addi a4,a4,-1 10394: fff60613 addi a2,a2,-1 10398: 00178793 addi a5,a5,1 1039c: 00450513 addi a0,a0,4 103a0: 00f58e63 beq a1,a5,103bc <LemonadeChange+0x58> 103a4: 00052683 lw a3,0(a0) 103a8: fd069ee3 bne a3,a6,10384 <LemonadeChange+0x20> 103ac: 00178793 addi a5,a5,1 103b0: 00170713 addi a4,a4,1 103b4: 00450513 addi a0,a0,4 103b8: fef596e3 bne a1,a5,103a4 <LemonadeChange+0x40> 103bc: 00100513 li a0,1 103c0: 00008067 ret 103c4: 00070e63 beqz a4,103e0 <LemonadeChange+0x7c> 103c8: fff70713 addi a4,a4,-1 103cc: 00160613 addi a2,a2,1 103d0: fc9ff06f j 10398 <LemonadeChange+0x34> 103d4: 00e35663 bge t1,a4,103e0 <LemonadeChange+0x7c> 103d8: ffd70713 addi a4,a4,-3 103dc: fbdff06f j 10398 <LemonadeChange+0x34> 103e0: 00000513 li a0,0 103e4: 00008067 ret 000100c4 <main>: 100c4: 000227b7 lui a5,0x22 100c8: f9010113 addi sp,sp,-112 100cc: 9d078793 addi a5,a5,-1584 # 219d0 <__clzsi2+0xd4> 100d0: 0147a383 lw t2,20(a5) 100d4: 0187a283 lw t0,24(a5) 100d8: 06112623 sw ra,108(sp) 100dc: 06812423 sw s0,104(sp) 100e0: 0007a083 lw ra,0(a5) 100e4: 0107a403 lw s0,16(a5) 100e8: 06912223 sw s1,100(sp) 100ec: 07212023 sw s2,96(sp) 100f0: 00c7a483 lw s1,12(a5) 100f4: 0087a903 lw s2,8(a5) 100f8: 05312e23 sw s3,92(sp) 100fc: 0047a983 lw s3,4(a5) 10100: 02c7a503 lw a0,44(a5) 10104: 01c7af83 lw t6,28(a5) 10108: 0207af03 lw t5,32(a5) 1010c: 0247ae83 lw t4,36(a5) 10110: 0287ae03 lw t3,40(a5) 10114: 0347a303 lw t1,52(a5) 10118: 0387a883 lw a7,56(a5) 1011c: 03c7a803 lw a6,60(a5) 10120: 0407a583 lw a1,64(a5) 10124: 0447a603 lw a2,68(a5) 10128: 0487a683 lw a3,72(a5) 1012c: 04c7a703 lw a4,76(a5) 10130: 00112023 sw ra,0(sp) 10134: 0307a783 lw a5,48(a5) 10138: 01212423 sw s2,8(sp) 1013c: 00912623 sw s1,12(sp) 10140: 00812823 sw s0,16(sp) 10144: 00712a23 sw t2,20(sp) 10148: 00512c23 sw t0,24(sp) 1014c: 01312223 sw s3,4(sp) 10150: 01f12e23 sw t6,28(sp) 10154: 02a12623 sw a0,44(sp) 10158: 00022537 lui a0,0x22 1015c: 98850513 addi a0,a0,-1656 # 21988 <__clzsi2+0x8c> 10160: 03e12023 sw t5,32(sp) 10164: 03d12223 sw t4,36(sp) 10168: 03c12423 sw t3,40(sp) 1016c: 02f12823 sw a5,48(sp) 10170: 02612a23 sw t1,52(sp) 10174: 03112c23 sw a7,56(sp) 10178: 03012e23 sw a6,60(sp) 1017c: 04b12023 sw a1,64(sp) 10180: 04c12223 sw a2,68(sp) 10184: 04d12423 sw a3,72(sp) 10188: 04e12623 sw a4,76(sp) 1018c: 00010413 mv s0,sp 10190: 01410913 addi s2,sp,20 10194: 474000ef jal ra,10608 <printf> 10198: 000224b7 lui s1,0x22 1019c: 00042583 lw a1,0(s0) 101a0: 99448513 addi a0,s1,-1644 # 21994 <__clzsi2+0x98> 101a4: 00440413 addi s0,s0,4 101a8: 460000ef jal ra,10608 <printf> 101ac: fe8918e3 bne s2,s0,1019c <main+0xd8> 101b0: 00500593 li a1,5 101b4: 00010513 mv a0,sp 101b8: 1ac000ef jal ra,10364 <LemonadeChange> 101bc: 0c050463 beqz a0,10284 <main+0x1c0> 101c0: 00022537 lui a0,0x22 101c4: 99850513 addi a0,a0,-1640 # 21998 <__clzsi2+0x9c> 101c8: 5b8000ef jal ra,10780 <puts> 101cc: 00022537 lui a0,0x22 101d0: 9b850513 addi a0,a0,-1608 # 219b8 <__clzsi2+0xbc> 101d4: 434000ef jal ra,10608 <printf> 101d8: 00090413 mv s0,s2 101dc: 02810993 addi s3,sp,40 101e0: 00042583 lw a1,0(s0) 101e4: 99448513 addi a0,s1,-1644 101e8: 00440413 addi s0,s0,4 101ec: 41c000ef jal ra,10608 <printf> 101f0: fe8998e3 bne s3,s0,101e0 <main+0x11c> 101f4: 00500593 li a1,5 101f8: 00090513 mv a0,s2 101fc: 168000ef jal ra,10364 <LemonadeChange> 10200: 06050a63 beqz a0,10274 <main+0x1b0> 10204: 00022537 lui a0,0x22 10208: 99850513 addi a0,a0,-1640 # 21998 <__clzsi2+0x9c> 1020c: 574000ef jal ra,10780 <puts> 10210: 00022537 lui a0,0x22 10214: 9c450513 addi a0,a0,-1596 # 219c4 <__clzsi2+0xc8> 10218: 3f0000ef jal ra,10608 <printf> 1021c: 00098413 mv s0,s3 10220: 05010913 addi s2,sp,80 10224: 00042583 lw a1,0(s0) 10228: 99448513 addi a0,s1,-1644 1022c: 00440413 addi s0,s0,4 10230: 3d8000ef jal ra,10608 <printf> 10234: ff2418e3 bne s0,s2,10224 <main+0x160> 10238: 00a00593 li a1,10 1023c: 00098513 mv a0,s3 10240: 124000ef jal ra,10364 <LemonadeChange> 10244: 04050863 beqz a0,10294 <main+0x1d0> 10248: 00022537 lui a0,0x22 1024c: 99850513 addi a0,a0,-1640 # 21998 <__clzsi2+0x9c> 10250: 530000ef jal ra,10780 <puts> 10254: 06c12083 lw ra,108(sp) 10258: 06812403 lw s0,104(sp) 1025c: 06412483 lw s1,100(sp) 10260: 06012903 lw s2,96(sp) 10264: 05c12983 lw s3,92(sp) 10268: 00000513 li a0,0 1026c: 07010113 addi sp,sp,112 10270: 00008067 ret 10274: 00022537 lui a0,0x22 10278: 9a850513 addi a0,a0,-1624 # 219a8 <__clzsi2+0xac> 1027c: 504000ef jal ra,10780 <puts> 10280: f91ff06f j 10210 <main+0x14c> 10284: 00022537 lui a0,0x22 10288: 9a850513 addi a0,a0,-1624 # 219a8 <__clzsi2+0xac> 1028c: 4f4000ef jal ra,10780 <puts> 10290: f3dff06f j 101cc <main+0x108> 10294: 00022537 lui a0,0x22 10298: 9a850513 addi a0,a0,-1624 # 219a8 <__clzsi2+0xac> 1029c: 4e4000ef jal ra,10780 <puts> 102a0: fb5ff06f j 10254 <main+0x190> ``` #### Observation * Line of code: ```155``` * Allocate ```112``` bytes on stack * Registers used: ```ra``` ```sp``` ```a0~a7``` ```s0~s3``` ```t0~t6``` * Number of lw and sw instructions: 29 ```lw``` 25 ```sw``` #### Execution and CSR count ``` troy@troy-H610M-S2-DDR4:~/rv32emu$ build/rv32emu --stats tests/hw2/hw2_o3 Test 1: 5 5 5 10 20 Ans : true Test 2: 5 5 10 10 20 Ans : false Test 3: 5 5 5 10 5 5 10 20 20 20 Ans : false inferior exit code 0 CSR cycle count: 19482 ``` ### O-fast optimized #### Size after compiling with O-fast option ``` troy@troy-H610M-S2-DDR4:~/rv32emu/tests/hw2$ riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -Ofast -o hw2_ofast hw2.c troy@troy-H610M-S2-DDR4:~/rv32emu/tests/hw2$ riscv-none-elf-size hw2_ofast text data bss dec hex filename 75568 2816 812 79196 1355c hw2_ofast ``` #### Header ``` troy@troy-H610M-S2-DDR4:~/rv32emu/tests/hw2$ riscv-none-elf-readelf -h hw2_ofast ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: EXEC (Executable file) Machine: RISC-V Version: 0x1 Entry point address: 0x102bc Start of program headers: 52 (bytes into file) Start of section headers: 95016 (bytes into file) Flags: 0x0 Size of this header: 52 (bytes) Size of program headers: 32 (bytes) Number of program headers: 3 Size of section headers: 40 (bytes) Number of section headers: 15 Section header string table index: 14 ``` #### Assembly code after compiling ```clike= 00010364 <LemonadeChange>: 10364: 04b05c63 blez a1,103bc <LemonadeChange+0x58> 10368: 00000793 li a5,0 1036c: 00000613 li a2,0 10370: 00000713 li a4,0 10374: 00500813 li a6,5 10378: 00a00893 li a7,10 1037c: 00200313 li t1,2 10380: 0240006f j 103a4 <LemonadeChange+0x40> 10384: 05168063 beq a3,a7,103c4 <LemonadeChange+0x60> 10388: 04060663 beqz a2,103d4 <LemonadeChange+0x70> 1038c: 04070a63 beqz a4,103e0 <LemonadeChange+0x7c> 10390: fff70713 addi a4,a4,-1 10394: fff60613 addi a2,a2,-1 10398: 00178793 addi a5,a5,1 1039c: 00450513 addi a0,a0,4 103a0: 00f58e63 beq a1,a5,103bc <LemonadeChange+0x58> 103a4: 00052683 lw a3,0(a0) 103a8: fd069ee3 bne a3,a6,10384 <LemonadeChange+0x20> 103ac: 00178793 addi a5,a5,1 103b0: 00170713 addi a4,a4,1 103b4: 00450513 addi a0,a0,4 103b8: fef596e3 bne a1,a5,103a4 <LemonadeChange+0x40> 103bc: 00100513 li a0,1 103c0: 00008067 ret 103c4: 00070e63 beqz a4,103e0 <LemonadeChange+0x7c> 103c8: fff70713 addi a4,a4,-1 103cc: 00160613 addi a2,a2,1 103d0: fc9ff06f j 10398 <LemonadeChange+0x34> 103d4: 00e35663 bge t1,a4,103e0 <LemonadeChange+0x7c> 103d8: ffd70713 addi a4,a4,-3 103dc: fbdff06f j 10398 <LemonadeChange+0x34> 103e0: 00000513 li a0,0 103e4: 00008067 ret 000100c4 <main>: 100c4: 000227b7 lui a5,0x22 100c8: f9010113 addi sp,sp,-112 100cc: 9d078793 addi a5,a5,-1584 # 219d0 <__clzsi2+0xd4> 100d0: 0147a383 lw t2,20(a5) 100d4: 0187a283 lw t0,24(a5) 100d8: 06112623 sw ra,108(sp) 100dc: 06812423 sw s0,104(sp) 100e0: 0007a083 lw ra,0(a5) 100e4: 0107a403 lw s0,16(a5) 100e8: 06912223 sw s1,100(sp) 100ec: 07212023 sw s2,96(sp) 100f0: 00c7a483 lw s1,12(a5) 100f4: 0087a903 lw s2,8(a5) 100f8: 05312e23 sw s3,92(sp) 100fc: 0047a983 lw s3,4(a5) 10100: 02c7a503 lw a0,44(a5) 10104: 01c7af83 lw t6,28(a5) 10108: 0207af03 lw t5,32(a5) 1010c: 0247ae83 lw t4,36(a5) 10110: 0287ae03 lw t3,40(a5) 10114: 0347a303 lw t1,52(a5) 10118: 0387a883 lw a7,56(a5) 1011c: 03c7a803 lw a6,60(a5) 10120: 0407a583 lw a1,64(a5) 10124: 0447a603 lw a2,68(a5) 10128: 0487a683 lw a3,72(a5) 1012c: 04c7a703 lw a4,76(a5) 10130: 00112023 sw ra,0(sp) 10134: 0307a783 lw a5,48(a5) 10138: 01212423 sw s2,8(sp) 1013c: 00912623 sw s1,12(sp) 10140: 00812823 sw s0,16(sp) 10144: 00712a23 sw t2,20(sp) 10148: 00512c23 sw t0,24(sp) 1014c: 01312223 sw s3,4(sp) 10150: 01f12e23 sw t6,28(sp) 10154: 02a12623 sw a0,44(sp) 10158: 00022537 lui a0,0x22 1015c: 98850513 addi a0,a0,-1656 # 21988 <__clzsi2+0x8c> 10160: 03e12023 sw t5,32(sp) 10164: 03d12223 sw t4,36(sp) 10168: 03c12423 sw t3,40(sp) 1016c: 02f12823 sw a5,48(sp) 10170: 02612a23 sw t1,52(sp) 10174: 03112c23 sw a7,56(sp) 10178: 03012e23 sw a6,60(sp) 1017c: 04b12023 sw a1,64(sp) 10180: 04c12223 sw a2,68(sp) 10184: 04d12423 sw a3,72(sp) 10188: 04e12623 sw a4,76(sp) 1018c: 00010413 mv s0,sp 10190: 01410913 addi s2,sp,20 10194: 474000ef jal ra,10608 <printf> 10198: 000224b7 lui s1,0x22 1019c: 00042583 lw a1,0(s0) 101a0: 99448513 addi a0,s1,-1644 # 21994 <__clzsi2+0x98> 101a4: 00440413 addi s0,s0,4 101a8: 460000ef jal ra,10608 <printf> 101ac: fe8918e3 bne s2,s0,1019c <main+0xd8> 101b0: 00500593 li a1,5 101b4: 00010513 mv a0,sp 101b8: 1ac000ef jal ra,10364 <LemonadeChange> 101bc: 0c050463 beqz a0,10284 <main+0x1c0> 101c0: 00022537 lui a0,0x22 101c4: 99850513 addi a0,a0,-1640 # 21998 <__clzsi2+0x9c> 101c8: 5b8000ef jal ra,10780 <puts> 101cc: 00022537 lui a0,0x22 101d0: 9b850513 addi a0,a0,-1608 # 219b8 <__clzsi2+0xbc> 101d4: 434000ef jal ra,10608 <printf> 101d8: 00090413 mv s0,s2 101dc: 02810993 addi s3,sp,40 101e0: 00042583 lw a1,0(s0) 101e4: 99448513 addi a0,s1,-1644 101e8: 00440413 addi s0,s0,4 101ec: 41c000ef jal ra,10608 <printf> 101f0: fe8998e3 bne s3,s0,101e0 <main+0x11c> 101f4: 00500593 li a1,5 101f8: 00090513 mv a0,s2 101fc: 168000ef jal ra,10364 <LemonadeChange> 10200: 06050a63 beqz a0,10274 <main+0x1b0> 10204: 00022537 lui a0,0x22 10208: 99850513 addi a0,a0,-1640 # 21998 <__clzsi2+0x9c> 1020c: 574000ef jal ra,10780 <puts> 10210: 00022537 lui a0,0x22 10214: 9c450513 addi a0,a0,-1596 # 219c4 <__clzsi2+0xc8> 10218: 3f0000ef jal ra,10608 <printf> 1021c: 00098413 mv s0,s3 10220: 05010913 addi s2,sp,80 10224: 00042583 lw a1,0(s0) 10228: 99448513 addi a0,s1,-1644 1022c: 00440413 addi s0,s0,4 10230: 3d8000ef jal ra,10608 <printf> 10234: ff2418e3 bne s0,s2,10224 <main+0x160> 10238: 00a00593 li a1,10 1023c: 00098513 mv a0,s3 10240: 124000ef jal ra,10364 <LemonadeChange> 10244: 04050863 beqz a0,10294 <main+0x1d0> 10248: 00022537 lui a0,0x22 1024c: 99850513 addi a0,a0,-1640 # 21998 <__clzsi2+0x9c> 10250: 530000ef jal ra,10780 <puts> 10254: 06c12083 lw ra,108(sp) 10258: 06812403 lw s0,104(sp) 1025c: 06412483 lw s1,100(sp) 10260: 06012903 lw s2,96(sp) 10264: 05c12983 lw s3,92(sp) 10268: 00000513 li a0,0 1026c: 07010113 addi sp,sp,112 10270: 00008067 ret 10274: 00022537 lui a0,0x22 10278: 9a850513 addi a0,a0,-1624 # 219a8 <__clzsi2+0xac> 1027c: 504000ef jal ra,10780 <puts> 10280: f91ff06f j 10210 <main+0x14c> 10284: 00022537 lui a0,0x22 10288: 9a850513 addi a0,a0,-1624 # 219a8 <__clzsi2+0xac> 1028c: 4f4000ef jal ra,10780 <puts> 10290: f3dff06f j 101cc <main+0x108> 10294: 00022537 lui a0,0x22 10298: 9a850513 addi a0,a0,-1624 # 219a8 <__clzsi2+0xac> 1029c: 4e4000ef jal ra,10780 <puts> 102a0: fb5ff06f j 10254 <main+0x190> ``` #### Observation * Line of code: ```155``` * Allocate ```112``` bytes on stack * Registers used: ```ra``` ```sp``` ```a0~a7``` ```s0~s3``` ```t0~t6``` * Number of lw and sw instructions: 29 ```lw``` 25 ```sw``` #### Execution and CSR count ``` troy@troy-H610M-S2-DDR4:~/rv32emu$ build/rv32emu --stats tests/hw2/hw2_ofast Test 1: 5 5 5 10 20 Ans : true Test 2: 5 5 10 10 20 Ans : false Test 3: 5 5 5 10 5 5 10 20 20 20 Ans : false inferior exit code 0 CSR cycle count: 19482 ``` ### Os optimized #### Size after compiling with Os option ``` troy@troy-H610M-S2-DDR4:~/rv32emu/tests/hw2$ riscv-none-elf-size hw2_os text data bss dec hex filename 75436 2816 812 79064 134d8 hw2_os ``` #### Header ``` troy@troy-H610M-S2-DDR4:~/rv32emu/tests/hw2$ riscv-none-elf-readelf -h hw2_os ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: EXEC (Executable file) Machine: RISC-V Version: 0x1 Entry point address: 0x10240 Start of program headers: 52 (bytes into file) Start of section headers: 95016 (bytes into file) Flags: 0x0 Size of this header: 52 (bytes) Size of program headers: 32 (bytes) Number of program headers: 3 Size of section headers: 40 (bytes) Number of section headers: 15 Section header string table index: 14 ``` #### Assembly code after compiling ```clike= 000102e8 <LemonadeChange>: 102e8: 00000693 li a3,0 102ec: 00000713 li a4,0 102f0: 00000793 li a5,0 102f4: 00500813 li a6,5 102f8: 00a00893 li a7,10 102fc: 00200313 li t1,2 10300: 00b6c663 blt a3,a1,1030c <LemonadeChange+0x24> 10304: 00100513 li a0,1 10308: 00008067 ret 1030c: 00269613 slli a2,a3,0x2 10310: 00c50633 add a2,a0,a2 10314: 00062603 lw a2,0(a2) 10318: 01061863 bne a2,a6,10328 <LemonadeChange+0x40> 1031c: 00178793 addi a5,a5,1 10320: 00168693 addi a3,a3,1 10324: fddff06f j 10300 <LemonadeChange+0x18> 10328: 01161a63 bne a2,a7,1033c <LemonadeChange+0x54> 1032c: 02078863 beqz a5,1035c <LemonadeChange+0x74> 10330: fff78793 addi a5,a5,-1 10334: 00170713 addi a4,a4,1 10338: fe9ff06f j 10320 <LemonadeChange+0x38> 1033c: 00070a63 beqz a4,10350 <LemonadeChange+0x68> 10340: 00078e63 beqz a5,1035c <LemonadeChange+0x74> 10344: fff78793 addi a5,a5,-1 10348: fff70713 addi a4,a4,-1 1034c: fd5ff06f j 10320 <LemonadeChange+0x38> 10350: 00f35663 bge t1,a5,1035c <LemonadeChange+0x74> 10354: ffd78793 addi a5,a5,-3 10358: fc9ff06f j 10320 <LemonadeChange+0x38> 1035c: 00000513 li a0,0 10360: 00008067 ret 000100c4 <main>: 100c4: f9010113 addi sp,sp,-112 100c8: 000225b7 lui a1,0x22 100cc: 07212023 sw s2,96(sp) 100d0: 01400613 li a2,20 100d4: 94858913 addi s2,a1,-1720 # 21948 <__clzsi2+0xd0> 100d8: 00010513 mv a0,sp 100dc: 94858593 addi a1,a1,-1720 100e0: 06112623 sw ra,108(sp) 100e4: 06812423 sw s0,104(sp) 100e8: 06912223 sw s1,100(sp) 100ec: 05312e23 sw s3,92(sp) 100f0: 01410493 addi s1,sp,20 100f4: 374000ef jal ra,10468 <memcpy> 100f8: 01490593 addi a1,s2,20 100fc: 01400613 li a2,20 10100: 00048513 mv a0,s1 10104: 364000ef jal ra,10468 <memcpy> 10108: 02810413 addi s0,sp,40 1010c: 02890593 addi a1,s2,40 10110: 02800613 li a2,40 10114: 00040513 mv a0,s0 10118: 350000ef jal ra,10468 <memcpy> 1011c: 00022537 lui a0,0x22 10120: 90050513 addi a0,a0,-1792 # 21900 <__clzsi2+0x88> 10124: 604000ef jal ra,10728 <printf> 10128: 00010913 mv s2,sp 1012c: 000229b7 lui s3,0x22 10130: 00092583 lw a1,0(s2) 10134: 90c98513 addi a0,s3,-1780 # 2190c <__clzsi2+0x94> 10138: 00490913 addi s2,s2,4 1013c: 5ec000ef jal ra,10728 <printf> 10140: ff2498e3 bne s1,s2,10130 <main+0x6c> 10144: 00500593 li a1,5 10148: 00010513 mv a0,sp 1014c: 19c000ef jal ra,102e8 <LemonadeChange> 10150: 0a050a63 beqz a0,10204 <main+0x140> 10154: 00022537 lui a0,0x22 10158: 91050513 addi a0,a0,-1776 # 21910 <__clzsi2+0x98> 1015c: 744000ef jal ra,108a0 <puts> 10160: 00022537 lui a0,0x22 10164: 93050513 addi a0,a0,-1744 # 21930 <__clzsi2+0xb8> 10168: 5c0000ef jal ra,10728 <printf> 1016c: 00048913 mv s2,s1 10170: 00092583 lw a1,0(s2) 10174: 90c98513 addi a0,s3,-1780 10178: 00490913 addi s2,s2,4 1017c: 5ac000ef jal ra,10728 <printf> 10180: ff2418e3 bne s0,s2,10170 <main+0xac> 10184: 00500593 li a1,5 10188: 00048513 mv a0,s1 1018c: 15c000ef jal ra,102e8 <LemonadeChange> 10190: 08050063 beqz a0,10210 <main+0x14c> 10194: 00022537 lui a0,0x22 10198: 91050513 addi a0,a0,-1776 # 21910 <__clzsi2+0x98> 1019c: 704000ef jal ra,108a0 <puts> 101a0: 00022537 lui a0,0x22 101a4: 93c50513 addi a0,a0,-1732 # 2193c <__clzsi2+0xc4> 101a8: 580000ef jal ra,10728 <printf> 101ac: 00040493 mv s1,s0 101b0: 0004a583 lw a1,0(s1) 101b4: 90c98513 addi a0,s3,-1780 101b8: 00448493 addi s1,s1,4 101bc: 56c000ef jal ra,10728 <printf> 101c0: 05010793 addi a5,sp,80 101c4: fef496e3 bne s1,a5,101b0 <main+0xec> 101c8: 00a00593 li a1,10 101cc: 00040513 mv a0,s0 101d0: 118000ef jal ra,102e8 <LemonadeChange> 101d4: 04050463 beqz a0,1021c <main+0x158> 101d8: 00022537 lui a0,0x22 101dc: 91050513 addi a0,a0,-1776 # 21910 <__clzsi2+0x98> 101e0: 6c0000ef jal ra,108a0 <puts> 101e4: 06c12083 lw ra,108(sp) 101e8: 06812403 lw s0,104(sp) 101ec: 06412483 lw s1,100(sp) 101f0: 06012903 lw s2,96(sp) 101f4: 05c12983 lw s3,92(sp) 101f8: 00000513 li a0,0 101fc: 07010113 addi sp,sp,112 10200: 00008067 ret 10204: 00022537 lui a0,0x22 10208: 92050513 addi a0,a0,-1760 # 21920 <__clzsi2+0xa8> 1020c: f51ff06f j 1015c <main+0x98> 10210: 00022537 lui a0,0x22 10214: 92050513 addi a0,a0,-1760 # 21920 <__clzsi2+0xa8> 10218: f85ff06f j 1019c <main+0xd8> 1021c: 00022537 lui a0,0x22 10220: 92050513 addi a0,a0,-1760 # 21920 <__clzsi2+0xa8> 10224: fbdff06f j 101e0 <main+0x11c> ``` #### Observation * Line of code: ```122``` * Allocate ```112``` bytes on stack * Registers used: ```ra``` ```sp``` ```a0~a7``` ```s0~s3``` ```t1``` * Number of lw and sw instructions: 9 ```lw``` 5 ```sw``` #### Execution and CSR count ``` troy@troy-H610M-S2-DDR4:~/rv32emu$ build/rv32emu --stats tests/hw2/hw2_os Test 1: 5 5 5 10 20 Ans : true Test 2: 5 5 10 10 20 Ans : false Test 3: 5 5 5 10 5 5 10 20 20 20 Ans : false inferior exit code 0 CSR cycle count: 19603 ``` ## Summary :::info * `O0` -> `O1` -> `O2` -> `O3` ->`O-fast` -> `Os` * Line of code: `242` -> `159` -> `155` -> `155` ->`155` -> `122` * Stack usage (Bytes): `128` -> `96` -> `122` -> `112` -> `112` -> `112` * Amount of registers used: `12` -> `15` -> `22` -> `21` -> `21` -> `15` * `lw` and `sw` instructions in total: `112` -> `52` -> `54` -> `54` -> `54` -> `14` * CSR : ```20016``` -> ```19482``` -> ```19482``` -> ```19482``` -> ```19482``` ->```19603``` ::: :::info * From `O0` to `O1` Smaller code size, downgraded on stack usage and far less `lw` `sw` instructions was used, but more tolerated in register usage. * From `O1` to `O2` More ambitious on stack and register scheduling . * From `O2` to `O3` Almost the same. * `O-fast` Similar with option `O2` and `O3` on most of the aspects. * `Os` Comparing with `O2` and `O3`, the assembly code has smaller size, fewer register was used and downgraded on amount of `lw` and `sw` instructions. :::

    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