tags: sysprog2017
contributed by <workfunction
>
實作Fibonacci時需要使用者輸入要計算第幾項,因此需要一個可以輸入的介面,目前想到兩種方法實做:組譯器執行時加入引述或是新增一個輸入指令,我選擇後者實做。
in opcode.c
簡單新增一個 scanf
的功能
in opcode.def
定義 opcode
23為 scan
in vm.c
在 vm_run
中加入 scan
in driver.c
在程式執行期間操作如下:
assemble_from_fd
將檔案掃描過一遍並將指令儲存成 struct tablehook_opcodes
將 OP code 與實做連接vm_run
將指令 struct table 執行而要達成 label 的支援需要:
assemble_from_fd
中新增能夠判斷 label 的機制vm_run
前將 label 轉換成絕對位置,如此就不必修改分之類指令的行為in vm.h
新增一個結構儲存 label 的位置
in vm.c
在 env
中加入 label table 將所有 label 與位置紀錄在這裡,並且用 vm_operand 指標 table label_reference
將所有被 refrenced 的 label 的 operand 位置記錄在這裡
儲存指令參照過的 label
in as.c
判斷如果 operand 是以 :
為開頭,其後面為 label
in vm.c
如果指令後面的 operand 被標記成 label,將其儲存至 label_reference
,之後再轉換成絕對位置
儲存 label 位置
in as.c
in vm.c
如果該行指令並非系統預留指令並以 :
結尾將其定義為 label 並存至 label table
in driver.c
in vm.c
在執行前新增一個 vm_map_refrenced_label
function 將所有指令中出現的 label (label_reference
) 依據之前存的 label table 把位置一一放入 vm_operand.value.id
中,如此便能相容原本分之類指令使用 vm_operand.value.id
儲存絕對位置