--- tags: sysprog2017 --- # 2017q3 Homework3 (simulator) contributed by <`Yuessiah`> <!-- ###### 本文件多處地方採用超連結來補完報告,建議點開來看 --> ## 開發環境 ``` eeeeeeeeeeeeeeeee eeeeeeeeeeeeeeeeeeeeeee eeeee eeeeeeeeeeee eeeee OS: elementary os elementary OS 0.3.2 freya eeee eeeee eee eeee Kernel: x86_64 Linux 4.4.0-93-generic eeee eeee eee eeee Shell: bash 4.3.11 eee eee eee eee Virtualization: VT-x eee eee eee eee CPU: Intel Core i7-7700 CPU @ 4.2GHz ee eee eeee eeee CPU(s): 8 ee eee eeeee eeeeee ee eee eeeee eeeee ee RAM: 1691MiB / 15934MiB eee eeee eeeeee eeeee eee L1d cache: 32K eee eeeeeeeeee eeeeee eee L1i cache: 32K eeeeeeeeeeeeeeeeeeeeeeee eeeee L2 cache: 256K eeeeeeee eeeeeeeeeeee eeee L3 cache: 8192K eeeee eeeee eeeeeee eeeeeee BogoMIPS: 7200.65 eeeeeeeeeeeeeeeee ``` ## 程式碼閱讀心得 ### `as.c` : `#define` 使用 `do { ... }while(0)` 是為了讓使用此 macro 行為像 function 用法[^1], 不過我覺得若 macro 只有一行指令時這樣用法是顯得有點冗。 function `make_oprend` 及 `make_result` 中多處在 `exit(-1)` 之前執行 `free(line)`, 這是不需要的[^2]。 而且 clang-format 會在 function `assemble_line` 中報 `(error) Deallocating a deallocated pointer: line_backup` ## 加入 label 功能 先將整份文件讀一遍,有出現 label 的地方將行數及 label 字串紀錄到一份表中 接著繼續照先前作法,但當 operand 遇到 label 字串,就從表中將對應的行數賦值給 operand。 詳細看 code。 ## Fibonacci 數列 此實做會印出 f(n=6), n 必須大於等於 1 - iterative ```= add $0 $1 #1 ; set f(1) = 1 add $0 $6 #3 ; for #3 = 6: add #0 #1 #2 ; store f(n-1) + f(n) to #2 add #1 $0 #0 add #2 $0 #1 sub #3 $1 #3 ; #3 -= 1 jnz #3 #2 ; end for if #3 == 0 print #0 ``` - recursive ```= jmp #14 ; if(n == 0): jnz #2 #3 ret ; if(n == 1): sub #2 $1 #3 jnz #3 #7 add #6 $1 #6 ret ; f(n-1) sub #2 $1 #2 call #1 add #2 $1 #2 ; f(n-2) sub #2 $2 #2 call #1 add #2 $2 #2 ret ; call f(n=6) add $0 $6 #2 call #1 print #6 ``` [^1]: [stackoverflow.com/ Why use do { } while \(0\) in macro definition?](https://stackoverflow.com/questions/9495962/why-use-do-while-0-in-macro-definition) [^2]: [blog.gtwang.org/ C 語言中關於記憶體釋放的議題](https://blog.gtwang.org/programming/memory-deallocation-issues-in-c)
×
Sign in
Email
Password
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