計算機概論Lab-5

HARDWARE ARCHITECTURE AND DATA MANIPULATION

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

https://hackmd.io/@IMOK/Lab5


Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

講師: 賴昱有


Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →


程式語言對於硬體的抽象程度

  • 機器語言(低)
    • 0010111010100100
  • 組合語言
    • MOVE R0, R5
  • 高級語言(高)
    • int a = 0;

組合語言

通常組成形式為

指令 [參數一][, 參數二][, 參數三]

e.g.
ADDI R2, R0, R1

簡單的加法

組合語言:

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 中 停止

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Program counter(PC)

address of next instruction

Instruction register(IR)

current instruction


指令表
僅限於今天會使用到的模擬軟體
實際上的組合語言會有多種不同指令集

R, S, T 皆代表 Register


指令 解釋
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 次

指令 解釋
JMPEQ R=R0, F 若 Register R 等於 Register R0 將程式跳到 Flag F
HALT 停止
LOAD R, [S] 將 Register S 中的值作為記憶體位址並將其中的值填入 Register R
e.g. S 中值為 65 就將記憶體 65 中的值填入 R
STORE R, [S] 將 Register S 中的值作為記憶體位址並將 Register R 的值填入其中
e.g. S 中值為 65 就將 R 中的值填入記憶體 65
JMPLE R<=R0, XY 若 Register R 小於等於 Register R0 將程式跳到 Flag F
JMP F 將程式跳到 Flag F
DB F Define Byte

三種不同的定址方法

  • Immediate Addressing
    • load R1, 0x64
  • Direct Addressing
    • load R1, [0x64]
  • Indirect Addressing
    • load R1, [R5]

BRANCHING

在 C 語言中:
if(...)
    do something
else
    do another thing

    
組合語言:
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


LOOPING

在 C 語言中:
for(int a = 0; a < 10; a++)
    do something

    
組合語言:
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


作業網站

作業做完請上傳到 http://140.121.197.13/tutorial
並且注意上傳時間限制、檔名、其他規範

上課時提前做完可以直接給助教 demo 登記
就不需要上傳 demo 過的部分


Question 1

Write a program to compute the
XOR
value of address
0x6C
,
0x6D
,
and store the results in address
0x6E

Question 2

Write a program to swap the values of memory location of

0xA0
and
0xB0
.


Question 3

Fill in the values

0x1
,
0x2
,
0x3
,
0x4
,
0x5
,
0x6
,
0x7
,
0x8
,
0x9
,
0xA
into the memory location starting from
0x50
.
Write a assembly program and add the values within the memory locations
0x50
to
0x59
and store it into memory location
0x40
.


Question 4

Write a program to add the values of 1 to x,
where 1 ≤ x ≤ 20. (x can be stored in some register)

Question 5

For the program below, instead of adding
the value of memory address
0x6C
and
0x6D
, (
0x6C
+
0x6D
),
change the program with the available instructions to do subtraction,
i.e.,
0x6C
-
0x6D
.
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)

Write a program to perform bubble sort
within the memory locations of
0xC0
to
0xCF
.
(Requires using
load R, [S]
and
store R, [S]
)

hint : bubble sort

Select a repo