2.2 ภาษาเครื่อง (Machine language)

Download Report

Transcript 2.2 ภาษาเครื่อง (Machine language)

2.2 ภาษาเครื่อง (Machine language)
จากรู ป แสดงลาดับของเลข 0 และ 1 นาไปใช้งานโดย CPU เป็ นคาสั่ง Load
ข้อมูล และ Mnemonic form ที่ใช้แทนคาสั่งดังกล่าวนี้
13/06/47
Machine Structure, Machine
Language, and Assembly
1
ตัวอย่าง
จงเขียนโปรแกรม ที่บวกตัวเลข 49 กับข้อมูลแบบ Fullword ที่เก็บ
เรี ยงแถวติดกัน 10 ตัว โดยอยูภ่ ายใต้ขอ้ สมมุติฐานต่อไปนี้
• ข้อสมมุติฐานที่ 1: เลข 10 จานวน ที่จะนามาบวก เป็ นแบบ Fullword เรี ยง
ติดกัน มีแอดเดรสเริ่ มต้นแบบ Absolute ที่ 952
• ข้อสมมุติฐานที่ 2: โปรแกรมอยูใ่ นหน่วยความจา เริ่ มต้นที่แอดเดรส 48
• ข้อสมมุติฐานที่ 3: เลข 49 เป็ นข้อมูลแบบ Fullword อยูท่ ี่ Absolute
แอดเดรส 948
• ข้อสมมุติฐานที่ 4: รี จิสเตอร์ 1 บรรจุขอ้ มูล 48
13/06/47
Machine Structure, Machine
Language, and Assembly
2
13/06/47
Machine Structure, Machine
Language, and Assembly
3
13/06/47
Machine Structure, Machine
Language, and Assembly
4
Absolute
address
Relative
Address
Hexa
Decimal
Instructions
:
:
:
:
:
:
:
:
:
:
:
:
948
952
956
960
964
968
972
976
980
984
988
:
:
:
:
:
:
:
:
:
:
:
:
900
904
908
912
916
920
924
928
932
936
940
:
:
:
:
:
:
:
:
:
:
:
:
00000031
00000005
00000007
00000009
0000000C
00000008
0000000A
0000000F
00000100
00000003
00000004
:
:
:
:
:
:
:
:
:
:
:
:
49
5
7
9
12
8
10
15
16
3
4
13/06/47
Machine Structure, Machine
Language, and Assembly
5
Absolute
address
Relative
Address
Hexa
Decimal
Instructions
:
:
:
:
:
:
:
:
:
:
:
:
948
952
956
960
964
968
972
976
980
984
988
:
:
:
:
:
:
:
:
:
:
:
:
900
904
908
912
916
920
924
928
932
936
940
:
:
:
:
:
:
:
:
:
:
:
:
00000031
00000036
00000007
00000009
0000000C
00000008
0000000A
0000000F
00000100
00000003
00000004
:
:
:
:
:
:
:
:
:
:
:
:
49
54
7
9
12
8
10
15
16
3
4
13/06/47
Machine Structure, Machine
Language, and Assembly
6
Absolute
address
Relative
Address
Hexa
Decimal
Instructions
:
:
:
:
:
:
:
:
:
:
:
:
948
952
956
960
964
968
972
976
980
984
988
:
:
:
:
:
:
:
:
:
:
:
:
900
904
908
912
916
920
924
928
932
936
940
:
:
:
:
:
:
:
:
:
:
:
:
00000031
00000036
00000038
00000009
0000000C
00000008
0000000A
0000000F
00000100
00000003
00000004
:
:
:
:
:
:
:
:
:
:
:
:
49
54
56
9
12
8
10
15
16
3
4
13/06/47
Machine Structure, Machine
Language, and Assembly
7
Absolute
address
Relative
Address
Hexa
Decimal
48
52
56
:
:
:
:
:
:
:
:
:
948
952
956
960
964
968
972
976
980
984
988
0
4
8
:
:
:
:
:
:
:
:
:
900
904
908
912
916
920
924
928
932
936
940
58201388
5A201384
5201388
:
:
:
:
:
:
:
:
:
00000031
00000005
00000007
00000009
0000000C
00000008
0000000A
0000000F
00000100
00000003
00000004
13/06/47
Machine Structure, Machine
Language, and Assembly
Instructions
L
A
ST
:
:
:
:
:
:
:
:
:
49
5
7
9
12
8
10
15
16
3
4
2,904(0,1)
2,900(0,1)
2,904(0,1)
8
Absolute
address
Relative
Address
Hexa
Decimal
48
52
56
:
:
:
:
:
:
:
:
:
948
952
956
960
964
968
972
976
980
984
988
0
4
8
:
:
:
:
:
:
:
:
:
900
904
908
912
916
920
924
928
932
936
940
58201388
5A201384
5201388
:
:
:
:
:
:
:
:
:
00000031
00000036
00000007
00000009
0000000C
00000008
0000000A
0000000F
00000100
00000003
00000004
13/06/47
Machine Structure, Machine
Language, and Assembly
Instructions
L
A
ST
:
:
:
:
:
:
:
:
:
49
54
7
9
12
8
10
15
16
3
4
2,904(0,1)
2,900(0,1)
2,904(0,1)
9
Absolute
address
Relative
Address
Hexa
Decimal
48
52
56
60
64
68
:
:
:
:
:
:
948
952
956
960
964
968
972
976
980
984
988
0
4
8
12
16
20
:
:
:
:
:
:
900
904
908
912
916
920
924
928
932
936
940
58201388
5A201384
5201388
5820138C
5A201384
5020138C
:
:
:
:
:
:
00000031
00000036
00000007
00000009
0000000C
00000008
0000000A
0000000F
00000100
00000003
00000004
13/06/47
Machine Structure, Machine
Language, and Assembly
Instructions
L
A
ST
L
A
ST
:
:
:
:
:
:
49
54
7
9
12
8
10
15
16
3
4
2,904(0,1)
2,900(0,1)
2,904(0,1)
2,908(0,1)
2,900(0,1)
2,908(0,1)
10
Absolute
address
Relative
Address
Hexa
Decimal
48
52
56
60
64
68
:
:
:
:
:
:
948
952
956
960
964
968
972
976
980
984
988
0
4
8
12
16
20
:
:
:
:
:
:
900
904
908
912
916
920
924
928
932
936
940
58201388
5A201384
5201388
5820138C
5A201384
5020138C
:
:
:
:
:
:
00000031
00000036
00000038
00000009
0000000C
00000008
0000000A
0000000F
00000100
00000003
00000004
13/06/47
Machine Structure, Machine
Language, and Assembly
Instructions
L
A
ST
L
A
ST
:
:
:
:
:
:
49
54
56
9
12
8
10
15
16
3
4
2,904(0,1)
2,900(0,1)
2,904(0,1)
2,908(0,1)
2,900(0,1)
2,908(0,1)
11
2.2.1 หนทางยาวไกล ไม่ มลี ูปวนซ้า
(Long Way, No Looping)
ปัญหาของโปรแกรมทีแ่ ล้ ว
1) ถ้าเราต้องการโปรเซสข้อมูล 300 ตัว (แทนที่จะเป็ น 10 ตัว เหมือนครั้ง
แรก)
– ที่เก็บสาหรับคาสั่ง จะเป็ น
(3 คาสัง่ ) x (ความยาวของแต่ละคาสัง่ ) x (จานวนข้อมูลทั้งหมด)
= 3,600 ไบท์
– ดังนั้น คาสั่งก็จะ ทับข้อมูลของเราในหน่วยความจา
13/06/47
Machine Structure, Machine
Language, and Assembly
12
ปัญหาของโปรแกรมทีแ่ ล้ ว (ต่ อ)
2) ระยะทางจาก คาสัง่ แรก จนถึงข้อมูลตัวสุ ดท้าย ควรจะเป็ น 4,800 ไบท์
แต่เนื่องจาก ข้อมูลมันเอง กินพื้นที่ 4 x 300 = 1,200 ไบท์ การใช้รีจิสเตอร์
เป็ น Base รี จิสเตอร์ จึงเป็ นไปไม่ได้ที่จะ Access ข้อมูล ตั้งแต่
หัวถึงท้ายพร้อมกัน
ถ้าเขียนภาษาเครื่ อง และจะต้องนึกอยูเ่ สมอว่าตัวเลขที่มากที่สุด ที่เป็ นไปได้
คือ 212-1 หรื อ 4,095 ซึ่งอาจไม่สามารถ Access ข้อมูลได้ทวั่ ถึง
ทั้งหมดพร้อมกัน
13/06/47
Machine Structure, Machine
Language, and Assembly
13
Relocation
ถ้าโปรแกรมก่อนหน้านี้ ถูก Load ไปไว้ที่แอดเดรส 336 (แทนที่จะเป็ น
แอดเดรส 48 เหมือนก่อนหน้า) มันยังคงทางานได้อย่างถูกต้อง ถ้าค่าใน
รี จิสเตอร์ 1 ถูกเปลี่ยนให้เป็ น 336 การย้ายโปรแกรมไปยังตาแหน่งต่างๆ
ในหน่วยความจา เรี ยกว่า การ Relocation โดยปกติ Base
รี จิสเตอร์ จะถูกใช้ในการ Relocation นี้
13/06/47
Machine Structure, Machine
Language, and Assembly
14
2.2.2 การเปลีย่ นค่ าแอดเดรส
โดยการใช้ คาสั่ งเสมือนเป็ นข้ อมูล
สมมติวา่ มีนกั ศึกษาของ MIT คนหนึ่งต้องการออกเดทกับหญิงสาว ขั้นตอนการออก
เดทของเขา อาจจะเขียนเป็ นโปรแกรมได้ดงั นี้
1
2
เหน็บ
เช่ าสู ท
13/06/47
Slide
rule
3
4
5
โทรหา
Nancy
ถ้ าถูกปฏิเสธ
เปลีย่ น 3 เป็ น
Marry
กลับไปทา 1
Machine Structure, Machine
Language, and Assembly
15
2.2.2 การเปลีย่ นค่ าแอดเดรส
โดยการใช้ คาสั่ งเสมือนเป็ นข้ อมูล (ต่อ)
สมมติวา่ มีนกั ศึกษาของ MIT คนหนึ่งต้องการออกเดทกับหญิงสาว ขั้นตอนการออก
เดทของเขา อาจจะเขียนเป็ นโปรแกรมได้ดงั นี้
1
2
เหน็บ
เช่ าสู ท
13/06/47
Slide
rule
3
4
5
โทรหา
Marry
ถ้ าถูกปฏิเสธ
เปลีย่ น 3 เป็ น
Jane
กลับไปทา 1
Machine Structure, Machine
Language, and Assembly
16
Absolute
address
13/06/47
Instructions
Relative
Address
48
52
56
60
64
68
72
76
80
0
4
8
12
16
20
24
28
32
:
:
944
948
952
956
960
964
968
972
976
980
984
988
:
:
896
900
904
908
912
916
920
924
928
932
936
940
L
2,904(0,1)
A
2,900(0,1)
ST 2,904(0,1)
L
2,0 (0,1)
A
2,896(0,1)
ST 2,0 (0,1)
L
2,8 (0,1)
A
2,896(0,1)
ST 2,8 (0,1)
Branch to 0
Comments
บวก 49 ให้ กบั 904
เพิม่ Load อีก 4
เพิม่ Store อีก
4
:
:
4
49
5
7
9
12
8
10
15
16
3
4
Machine Structure, Machine
Language, and Assembly
17
Absolute
address
13/06/47
Instructions
Relative
Address
48
52
56
60
64
68
72
76
80
0
4
8
12
16
20
24
28
32
:
:
944
948
952
956
960
964
968
972
976
980
984
988
:
:
896
900
904
908
912
916
920
924
928
932
936
940
L
2,904(0,1)
A
2,900(0,1)
ST 2,904(0,1)
L
2,0 (0,1)
A
2,896(0,1)
ST 2,0 (0,1)
L
2,8 (0,1)
A
2,896(0,1)
ST 2,8 (0,1)
Branch to 0
Comments
บวก 49 ให้ กบั 904
เพิม่ Load อีก 4
เพิม่ Store อีก
4
:
:
4
49
5
7
9
12
8
10
15
16
3
4
Machine Structure, Machine
Language, and Assembly
18
Absolute
address
13/06/47
Instructions
Relative
Address
48
52
56
60
64
68
72
76
80
0
4
8
12
16
20
24
28
32
:
:
944
948
952
956
960
964
968
972
976
980
984
988
:
:
896
900
904
908
912
916
920
924
928
932
936
940
L
2,904(0,1)
A
2,900(0,1)
ST 2,904(0,1)
L
2,0 (0,1)
A
2,896(0,1)
ST 2,0 (0,1)
L
2,8 (0,1)
A
2,896(0,1)
ST 2,8 (0,1)
Branch to 0
Comments
บวก 49 ให้ กบั 904
เพิม่ Load อีก 4
เพิม่ Store อีก
4
:
:
4
49
54
7
9
12
8
10
15
16
3
4
Machine Structure, Machine
Language, and Assembly
19
Absolute
address
13/06/47
Instructions
Relative
Address
48
52
56
60
64
68
72
76
80
0
4
8
12
16
20
24
28
32
:
:
944
948
952
956
960
964
968
972
976
980
984
988
:
:
896
900
904
908
912
916
920
924
928
932
936
940
L
2,904(0,1)
A
2,900(0,1)
ST 2,904(0,1)
L
2,0 (0,1)
A
2,896(0,1)
ST 2,0 (0,1)
L
2,8 (0,1)
A
2,896(0,1)
ST 2,8 (0,1)
Branch to 0
Comments
บวก 49 ให้ กบั 904
เพิม่ Load อีก 4
เพิม่ Store อีก
4
:
:
4
49
54
7
9
12
8
10
15
16
3
4
Machine Structure, Machine
Language, and Assembly
20
Absolute
address
13/06/47
Instructions
Relative
Address
48
52
56
60
64
68
72
76
80
0
4
8
12
16
20
24
28
32
:
:
944
948
952
956
960
964
968
972
976
980
984
988
:
:
896
900
904
908
912
916
920
924
928
932
936
940
L
2,908(0,1)
A
2,900(0,1)
ST 2,904(0,1)
L
2,0 (0,1)
A
2,896(0,1)
ST 2,0 (0,1)
L
2,8 (0,1)
A
2,896(0,1)
ST 2,8 (0,1)
Branch to 0
Comments
บวก 49 ให้ กบั 904
เพิม่ Load อีก 4
เพิม่ Store อีก
4
:
:
4
49
54
7
9
12
8
10
15
16
3
4
Machine Structure, Machine
Language, and Assembly
21
Absolute
address
13/06/47
Instructions
Relative
Address
48
52
56
60
64
68
72
76
80
0
4
8
12
16
20
24
28
32
:
:
944
948
952
956
960
964
968
972
976
980
984
988
:
:
896
900
904
908
912
916
920
924
928
932
936
940
L
2,908(0,1)
A
2,900(0,1)
ST 2,904(0,1)
L
2,0 (0,1)
A
2,896(0,1)
ST 2,0 (0,1)
L
2,8 (0,1)
A
2,896(0,1)
ST 2,8 (0,1)
Branch to 0
Comments
บวก 49 ให้ กบั 904
เพิม่ Load อีก 4
เพิม่ Store อีก
4
:
:
4
49
54
7
9
12
8
10
15
16
3
4
Machine Structure, Machine
Language, and Assembly
22
Absolute
address
13/06/47
Instructions
Relative
Address
48
52
56
60
64
68
72
76
80
0
4
8
12
16
20
24
28
32
:
:
944
948
952
956
960
964
968
972
976
980
984
988
:
:
896
900
904
908
912
916
920
924
928
932
936
940
L
2,908(0,1)
A
2,900(0,1)
ST 2,908(0,1)
L
2,0 (0,1)
A
2,896(0,1)
ST 2,0 (0,1)
L
2,8 (0,1)
A
2,896(0,1)
ST 2,8 (0,1)
Branch to 0
Comments
บวก 49 ให้ กบั 904
เพิม่ Load อีก 4
เพิม่ Store อีก
4
:
:
4
49
54
7
9
12
8
10
15
16
3
4
Machine Structure, Machine
Language, and Assembly
23
Absolute
address
13/06/47
Instructions
Relative
Address
48
52
56
60
64
68
72
76
80
0
4
8
12
16
20
24
28
32
:
:
944
948
952
956
960
964
968
972
976
980
984
988
:
:
896
900
904
908
912
916
920
924
928
932
936
940
L
2,908(0,1)
A
2,900(0,1)
ST 2,908(0,1)
L
2,0 (0,1)
A
2,896(0,1)
ST 2,0 (0,1)
L
2,8 (0,1)
A
2,896(0,1)
ST 2,8 (0,1)
Branch to 0
Comments
บวก 49 ให้ กบั 904
เพิม่ Load อีก 4
เพิม่ Store อีก
4
:
:
4
49
54
56
9
12
8
10
15
16
3
4
Machine Structure, Machine
Language, and Assembly
24
แอดเดรส เปลี่ยนได้อย่างไร
เพื่อที่จะดูการทางานของโปรแกรม เราต้องนึกอยูเ่ สมอว่า ค่าที่บรรจุอยูใ่ นตาแหน่ง 48
(ดังรู ปที่ 2.1 ไม่ใช่ L 2, 904(0,1) แต่มนั เป็ น
13/06/47
Machine Structure, Machine
Language, and Assembly
25
ปัญหา
• การใช้คาสัง่ เป็ นเสมือนข้อมูลไม่ใช่เทคนิคการเขียนโปแกรมที่ดี
เพราะว่าในระยะยาว การแก้ไขบารุ งรักษา (Maintenance)
โปรแกรมจะลาบาก มันจะยากต่อการทาความเข้าใจว่าเริ่ มแรกเลย
ผูเ้ ขียนต้องการจะทาอะไร
• ในกรณี ของระบบ Multiprocessing (หลายงาน บนระบบที่มี
โปรเซสเซอร์มากกว่า 1 ตัว ทางานพร้อมกัน) มันจะขัดแย้งกับกับกฎ
ของ โค้ดบริ สุทธิ์ (Pure code) หรื อที่เรี ยกว่า เข้าใหม่ได้หลาย
ครั้ง (Re-entrant code) เป็ นโค้ดที่ ไม่มีการแก้ไขตัวมันเอง
13/06/47
Machine Structure, Machine
Language, and Assembly
26