<style> .command { display: inline-block; font-size: 24px; color: black; background-color: white; padding: 7px; border-radius: 10px; margin: 5px; } </style> ## 計算機概論Lab-5 ##### HARDWARE ARCHITECTURE AND DATA MANIPULATION ![image](https://hackmd.io/_uploads/HJPZqstkye.png) <p>https://hackmd.io/@IMOK/Lab5</p> --- <img src="https://hackmd.io/_uploads/ryUSIqnJT.jpg" width=400 style="border-radius:1000px;"/> 講師: 賴昱有 --- ![image](https://hackmd.io/_uploads/HJPF9jYkJl.png) --- #### 程式語言對於硬體的抽象程度 + <font size=6>機器語言(低) </font> * <div style="display:inline-block; font-size:24px; color:black; background-color:white;padding: 7px;border-radius: 10px;"> 0010111010100100 </div> + <font size=6> 組合語言</font> * <div style="display:inline-block; font-size:24px; color:black; background-color:white;padding: 7px;border-radius: 10px;"> MOVE R0, R5 </div> + <font size=6> 高級語言(高)</font> * <div style="display:inline-block; font-size:24px; color:black; background-color:white;padding: 7px;border-radius: 10px;"> int a = 0; </div> --- ## 組合語言 <font size =5>通常組成形式為</font> <div style="display:inline-block; font-size:24px; color:black; background-color:white;padding: 7px;border-radius: 10px;"> 指令 [參數一][, 參數二][, 參數三]</div> <br> <font size =5>e.g.</font> <div style="display:inline-block; font-size:24px; color:black; background-color:white;padding: 7px;border-radius: 10px;"> ADDI R2, R0, R1 </div> ---- ### 簡單的加法 #### 組合語言: ``` LOAD R0, [0x60] LOAD R1, [0x61] ADDI R2, R0, R1 STORE R2, [0x62] HALT ``` #### 對應解釋: ```= 將記憶體 60 中的值 load 到 Register R0 中 將記憶體 61 中的值 load 到 Register R1 中 將 Register R0 及 R1 整數相加後存到 Register R2 中 將 Register R2 的值存回記憶體 62 中 停止 ``` --- <img src="https://hackmd.io/_uploads/Hk2Gy-9y1x.png" width=700/> ---- <img src="https://hackmd.io/_uploads/Skdt1Z9JJx.png" width=300/> ---- <img src="https://hackmd.io/_uploads/HkXqCe51kl.png" width=200/> #### Program counter(PC) <font size=5>address of next instruction</font> <br> #### Instruction register(IR) <font size=5>current instruction</font> --- 指令表 僅限於今天會使用到的模擬軟體 實際上的組合語言會有多種不同指令集 R, S, T 皆代表 Register ---- <style> table { width: 85%; border-collapse: collapse; line-height: 2; /* 行距 */ } th, td { font-size: 24px; /* 字體大小 */ } </style> | 指令 | 解釋 | | --------------- | ------------------------------------------------- | | LOAD R, [0xXY] | 將記憶體 XY 中的值填入 Register R | | LOAD R, XY | 將值 XY 填入 Register R | | STORE R, [0xXY] | 將 Register R 的值存入記憶體 XY 中 | | MOVE S, R | 將 Register R 的值存入 Register S 中 | | ADDI R, S, T | 將 Register S 及 T 整數相加後存到 Register R 中 | | ADDF R, S, T | 將 Register S 及 T 浮點數相加後存到 Register R 中 | | OR R, S, T | 將 Register S 及 T 做 OR 後存到 Register R 中 | | AND R, S, T | 將 Register S 及 T 做 AND 後存到 Register R 中 | | XOR R, S, T | 將 Register S 及 T 做 XOR 後存到 Register R 中 | | ROR R, X | 將 Register R 中的值 Rotate X 次 | ---- <style> table { width: 90%; border-collapse: collapse; line-height: 2; /* 行距 */ } th, td { font-size: 16px; /* 字體大小 */ } </style> | 指令 | 解釋 | | --------------- | ------------------------------------------------- | | JMPEQ R=R0, F | 若 Register R 等於 Register R0 將程式跳到 Flag F | | HALT | 停止 | | LOAD R, [S] | 將 Register S 中的值作為記憶體位址並將其中的值填入 Register R<br>e.g. S 中值為 65 就將記憶體 65 中的值填入 R | | STORE R, [S] | 將 Register S 中的值作為記憶體位址並將 Register R 的值填入其中<br>e.g. S 中值為 65 就將 R 中的值填入記憶體 65 | | JMPLE R<=R0, XY | 若 Register R 小於等於 Register R0 將程式跳到 Flag F | | JMP F | 將程式跳到 Flag F | | DB F | Define Byte | ---- ### 三種不同的定址方法 - <font size="6">Immediate Addressing</font> - <font size="6">load R1, 0x64</font> - <font size="6">Direct Addressing</font> - <font size="6">load R1, [0x64]</font> - <font size="6">Indirect Addressing</font> - <font size="6">load R1, [R5]</font> --- ## BRANCHING <div style="width: 400px; float: left; margin:0px 20px 0px 50px;"> <font size="6">在 C 語言中:</font> <pre style="font-size: 20px;"> <code>if(...) do something else do another thing </code> </pre> </div> <div style="width: 400px; float: left; margin: 0px;"> <font size="6">組合語言:</font> <pre style="font-size: 20px;"> <code>main: LOAD R0, [0x60] LOAD R1, [0x61] JMPEQ R1=R0, equal JMP noteq equal: LOAD R2, [0x62] ADDI R3, R1, R2 STORE R3, [0x70] HALT noteq: LOAD R2, [0x63] ADDI R3, R1, R2 STORE R3, [0x70] HALT </code> </pre> </div> --- ## LOOPING <div style="width: 400px; float: left; margin:0px 20px 0px 50px;"> <font size="6">在 C 語言中:</font> <pre style="font-size: 20px;"> <code>for(int a = 0; a < 10; a++) do something </code> </pre> </div> <div style="width: 400px; float: left; margin: 0px;"> <font size="6">組合語言:</font> <pre style="font-size: 20px;"> <code>main: LOAD R0, [0x60] LOAD R1, 1 LOAD R2, 1 JMP loop loop: ADDI R1, R1, R2 JMPLE R1<=R0, loop JMP out out: HALT </code> </pre> </div> --- ## 作業網站 作業做完請上傳到 http://140.121.197.13/tutorial 並且注意上傳時間限制、檔名、其他規範 上課時提前做完可以直接給助教 demo 登記 就不需要上傳 demo 過的部分 ---- ## Question 1 <font size=5> Write a program to compute the <div class="command">XOR</div> value of address <div class="command">0x6C</div>, <div class="command">0x6D</div>, <br>and store the results in address <div class="command">0x6E</div> </font> ---- ## Question 2 <font size=5>Write a program to swap the values of memory location of <div class="command">0xA0</div> and <div class="command">0xB0</div>. </font> ---- ## Question 3 <font size=5>Fill in the values <div class="command">0x1</div>, <div class="command">0x2</div>, <div class="command">0x3</div>, <div class="command">0x4</div>, <div class="command">0x5</div>, <div class="command">0x6</div>, <div class="command">0x7</div>, <div class="command">0x8</div>, <div class="command">0x9</div>, <div class="command">0xA</div> into the memory location starting from <div class="command">0x50</div>. Write a assembly program and add the values within the memory locations <div class="command">0x50</div> to <div class="command">0x59</div> and store it into memory location <div class="command">0x40</div>. </font> ---- ## Question 4 <font size=5> Write a program to add the values of 1 to x, <br>where 1 ≤ x ≤ 20. (x can be stored in some register) </font> ---- ## Question 5 <font size=5> For the program below, instead of adding <br>the value of memory address <div class="command">0x6C</div> and <div class="command">0x6D</div>, (<div class="command">0x6C</div> + <div class="command">0x6D</div>),<br> change the program with the available instructions to do subtraction,<br> i.e., <div class="command">0x6C</div> - <div class="command">0x6D</div>. </font> ``` load R5, [0x6C]; Load the value of memory address 0x6C into R5 load R6, [0x6D]; Load the value of memory address 0x6D into R6 addi R0, R5, R6; Adds the value of R5 and R6 and store i t in R0 store R0, [0x6E]; Stores the value of R0 to memory address 0x6E halt ``` ---- ## Question 6(Hard) <font size=5> Write a program to perform bubble sort<br> within the memory locations of <div class="command">0xC0</div> to <div class="command">0xCF</div>.<br> (Requires using <div class="command">load R, [S]</div> and <div class="command">store R, [S]</div>) </font> <font size = 5>hint : [bubble sort](https://www.youtube.com/watch?v=Dv4qLJcxus8)</font>
{"title":"計算機概論-Lab5","contributors":"[{\"id\":\"738dd674-cd6a-462c-87e2-b67e68f12ac0\",\"add\":19851,\"del\":11363}]","description":"image"}
    629 views