Transcript บทที่ 2
บทที่ 2 Machine Structure, Machine Language, and Assembly Language 07/06/47 Machine Structure, Machine Language, and Assembly 1 จุดมุ่งหมาย 2. โครงสร้างเครื่ อง ภาษาเครื่ อง และภาษาแอสเซมบลี้ 21 รู ้โครงสร้างเครื่ องทัว่ ไป 211 อธิ บายหน่วยประมวลผลกลาง 212 อธิ บายหน่วยความจา 213 อธิบายรี จิสเตอร์ 214 อธิ บายชุดคาสั่ง 22 รู้โครงสร้างของภาษาเครื่ อง 221 เขียนโปรแกรมด้วยภาษาเครื่ องกรณี ไม่มีลปู วน 222 อธิบายการแอดเดรสโดยตรง 07/06/47 Machine Structure, Machine Language, and Assembly 2 จุดมุ่งหมาย (ต่อ) 223 อธิบายการแอดเดรส โดยใช้อินเด็กส์รีจิสเตอร์ 224 เขียนโปรแกรมด้วยภาษาเครื่ องกรณี มีลูปวน 23 รู ้ภาษาแอสเซมบลี้ 232 เขียนโปรแกรม ด้วยภาษาแอสเซมบลี้ 07/06/47 Machine Structure, Machine Language, and Assembly 3 โครงสร้างของเครื่ อง 07/06/47 Machine Structure, Machine Language, and Assembly 4 Instruction Format ตัวอย่าง Instruction Format ของเครื่ อง SC-6251 ซึ่ งมี 4 Registers Operation Code (Op) Register Number (Reg) Memory Location (Address) Add 2, 176 07/06/47 Machine Structure, Machine Language, and Assembly 5 Instruction Format ตัวอย่าง Instruction Format ของเครื่ อง SC-6251 ซึ่ งมี 4 Registers Operation Code (Op) Add Register Number (Reg) Memory Location (Address) 2, 176 นาค่าในแอดเดรสที่ 176 + ค่าใน Reg 2 ไปเก็บไว้ใน Reg 2 07/06/47 Machine Structure, Machine Language, and Assembly 6 07/06/47 Machine Structure, Machine Language, and Assembly 7 2.1.1 เมื่อเปลีย่ นไปใช้ เครื่องใหม่ สิ่ งที่เราจะต้องรู ้เพื่อเป็ นข้อมูลสาสาหรับเครื่ องใหม่ ที่ใช้ CPU แตกต่างกันไป 1.หน่วยความจา (Memory) 2.รี จิสเตอร์ (Registers) 3.ข้อมูล (Data) 4.คาสั่ง (Instructions) 5.องค์ประกอบอื่นๆ 07/06/47 Machine Structure, Machine Language, and Assembly 8 1. หน่ วยความจา (Memory) • หน่วยของหน่วยความจาคืออะไร? • ขนาดของหน่วยความจาเป็ นอย่างไร? และ • รู ปแบบการอ้างแอดเดรสของหน่วยความจาเป็ นอย่างไร? 07/06/47 Machine Structure, Machine Language, and Assembly 9 2. รีจิสเตอร์ (Registers) • มีรีจิสเตอร์กี่ตวั ? • ขนาดและ หน้าที่ของรี จิสเตอร์แต่ละตัว • ความสัมพันธ์ระหว่างกัน 07/06/47 Machine Structure, Machine Language, and Assembly 10 3. ข้ อมูล (Data) • ข้อมูลชนิดอะไรบ้าง ที่คอมพิวเตอร์ สามารถใช้งานได้? • สามารถเก็บตัวเลข ตัวอักษร หรื อข้อมูลทางตรรกศาสตร์ได้หรื อไม่? • ข้อมูลเหล่านี้ มีวิธีการเก็บอย่างไร? 07/06/47 Machine Structure, Machine Language, and Assembly 11 4. คาสั่ ง (Instructions) • ชั้น (Classes) ของกลุ่มคาสัง่ มีอะไรบ้าง? • มีคาสัง่ คานวณทางคณิ ตศาสตร์ และตรรกศาสตร์หรื อไม่? • คาสัง่ เหล่านี้ มีวิธีการเก็บในหน่วยความจาอย่างไร? 07/06/47 Machine Structure, Machine Language, and Assembly 12 5. องค์ ประกอบอืน่ ๆ • โครงสร้างของ Interrupt เป็ นอย่างไร? • มีระบบป้ องกัน รักษาความปลอดภัย ของข้อมูล ให้กบั ผูใ้ ช้หรื อไม่? 07/06/47 Machine Structure, Machine Language, and Assembly 13 2.1.2 โครงสร้ างของเครื่อง IBM 360/370 1. 2. 3. 4. 5. 07/06/47 หน่วยความจา Registers ข้อมูล (Data) คาสัง่ (Instructions) ส่ วนพิเศษอื่นๆ Machine Structure, Machine Language, and Assembly 14 1. หน่ วยความจา หน่ วยของหน่ วยความจา ไบท์ (Unit of Memory) Bits) Byte Halfword Word Doubleword 07/06/47 (Bytes) 1 2 4 8 Machine Structure, Machine Language, and Assembly ความยาวเป็ นบิท (Lengt in 8 16 32 64 15 1. หน่ วยความจา (ต่ อ) • หน่วยของหน่วยความจา ประกอบไปด้วย 4 บิท เรี ยกว่า นิบเบิ้ล (Nibble) • ขนาด (Size) หน่วยความจาของ IBM 360 คือ 224 ไบท์ (ประมาณ 16 ล้าน) 07/06/47 Machine Structure, Machine Language, and Assembly 16 1. หน่ วยความจา (ต่ อ) การอ้างแอดเดรสในหน่วยความจาของ 360 ประกอบด้วย 3 องค์ประกอบ คือ อ๊อฟเซท (Offset) รวมกับค่าของ อินเด็กรี จิสเตอร์ (Index Register) และค่าของเบสรี จิสเตอร์ (Base Register) แอดเดรสรวม = ค่าของเบสรี จิสเตอร์ + ค่าของอ๊อฟเซท + ค่าของอินเด็กรี จิสเตอร์ (เราจะนาเสนอตัวอย่าง ในหัวข้อการแอดเดรสถัดไป) 07/06/47 Machine Structure, Machine Language, and Assembly 17 2. Registers • 360 มี รี จิสเตอร์วตั ถุประสงค์ทวั่ ไป (General-Purpose Register) ขนาด 32 บิท จานวน 16 ตัว • มีรีจิสเตอร์ที่คานวณทางทศนิยม (Floating-Point Registers) จานวน 4 ตัว • มี Program Status Word (PSW) ขนาด 64 บิท ที่ บรรจุ ค่าของ Location Counter (LC) ข้อมูลที่ถูกป้ องกัน และ Interrupt 07/06/47 Machine Structure, Machine Language, and Assembly 18 รี จิสเตอร์วตั ถุประสงค์ทวั่ ไป (General-Purpose Register) • สามารถทาการคานวณทางคณิ ตศาสตร์ ตรรกศาสตร์ และ • ใช้เป็ น Base Register 07/06/47 Machine Structure, Machine Language, and Assembly 19 Base Register Index Register A Base Register 1, 901 (2, 15) Offset แอดเดรสรวม 07/06/47 = ค่าของเบสรี จิสเตอร์ + ค่าของอ๊อฟเซท + ค่าของอินเด็กรี จิสเตอร์ Machine Structure, Machine Language, and Assembly 20 ข้อดี Base Register 1) ช่วยในการ โปรเซสในเรื่ องของ Relative address (ย้าย โปรแกรมไปมาในหน่วยความจาได้ โดยไม่มีผลกระทบ ต่อการอ้าง แอดเดรส ในหน่วยความจา เพียงแต่ เปลี่ยนค่าของ Base register เท่านั้น) Base Reg. = 1000 Base Reg. = 2000 07/06/47 Machine Structure, Machine Language, and Assembly 0 49 โปรแกรม 1000 1049 1725 2000 2049 21 ข้อดี Base Register (ต่อ) 2) ประการสาคัญก็คือ เราสามารถอ้างแอดเดรสได้ตลอดทั้งหน่วยความจา ยกตัวอย่าง ถ้าต้องการอ้างแอดเดรส ในหน่วยความจาตลอดทั้งย่าน 224 (ประมาณ 16 ล้านที่) ถ้าไม่ใช้ Base register เราต้องใช้ที่สาหรับอ้างแอดเดรส 24 บิท ดังนั้น คาสั่งบวกจากตัวอย่างที่แล้ว ก็จะใช้ที่เก็บชุดคาสั่ง 40 บิท 07/06/47 Machine Structure, Machine Language, and Assembly 22 ข้อดี Base Register (ต่อ) แต่ถา้ ใช้ Base register เราจะใช้ที่เก็บชุดคาสั่งเพียง 32 บิท 07/06/47 Machine Structure, Machine Language, and Assembly 23 ข้อเสี ย Base Register • การโปรเซสของเครื่ องจะช้าลง เพราะต้องคานวณแอดเดรส • การอ้างแอดเดรส จะทาได้แคบลง เพราะอ๊อฟเซท เหลือเพียงแค่ 12 บิท ทาให้อา้ งแอดเดรสได้เพียง 0 – 4095 ดังนั้น ถ้าเราไม่ใช้อินเด็กรี จิสเตอร์ เราจะไม่สามารถอ้างแอดเดรส ได้เกินกว่า 4095 จากค่าของแอดเดรส ที่ เก็บใน Base register 07/06/47 Machine Structure, Machine Language, and Assembly 24 3. ข้ อมูล (Data) S D Z C F Ch 07/06/47 หมายถึง หมายถึง หมายถึง หมายถึง หมายถึง หมายถึง Sign Bit => 1 bit หรื อ 4 bit Binary Coded Decimal => 4 bits Zone Code => 4 bits Characteristic of Exponent => 7 bits Fraction of Mantissa => 24 bits or 56 bits 1 character symbol => 8 bits Machine Structure, Machine Language, and Assembly 25 3. ข้ อมูล (Data) (ต่ อ) S 0 1 Integer S Fixed-point (Short Form) 15 Integer Fixed-point (Long Form) 0 1 1 to 16 bytes D 0 31 D 4 S 0 1 C S C 8 07/06/47 D Packed Decimal Unpacked Decimal Floating-point (Short Form) F 31 (Long Form) 8 8 S S F Ch ch 0 D 8 Z D Z D 0 4 8 12 0 1 D 63 ch 16 Machine Structure, Machine Language, and Assembly 26 4. คาสัง่ (Instructions) Op-code 0 8 R1 Op-code R1 0 8 Op-code R1 0 Op-code 0 Op-code 0 Op-code 0 07/06/47 8 R3 B2 L1 12 16 16 L 8 15 B2 12 16 12 RR-format X2 I2 8 8 12 R2 16 L2 16 B1 B1 B1 20 20 20 20 20 D2 D2 D1 D1 D1 RX-format 31 RS-format 31 SI-format 31 32 32 B2 B2 36 36 D2 D2 Machine Structure, Machine Language, and Assembly 47 47 SS-format 1 27 ตัวอย่าง ของคาสัง่ RR-format • คาสั่ง 07/06/47 AR 3,4 Machine Structure, Machine Language, and Assembly 28 Storage operand เลข Fixed-point แบบ Fullword ขนาด 32 บิท ค่า +267 ( 10B ในฐานสิ บหก) จะเก็บที่แอดเดรส 1016 ดังรู ป 07/06/47 Machine Structure, Machine Language, and Assembly 29 แอดเดรสของ Storage operand Address = หรื อ c(Bi) + c(Xi) + Di c(Bi) + Di (RX format) (RS, SI, SS format) • โดยที่ c(Bi) และ c(Xi) แสดงข้อมูลที่บรรจุ อยูใ่ นรี จิสเตอร์ Bi และ Xi ตามลาดับ • ยกเว้นแต่วา่ ถ้า Xi = 0 ดังนั้น c(Xi) จะมีค่าเป็ น 0 และ Bi ก็เช่นเดียวกัน 07/06/47 Machine Structure, Machine Language, and Assembly 30 ตัวอย่าง ของคาสัง่ RX-format คาสัง่ A 3, 16(0,5) สมมติวา่ รี จิสเตอร์ 5 เก็บค่าของ 1000 จะทาให้ค่าของข้อมูลชนิด Word ที่อยูท่ ี่ แอดเดรส = c(B2) + c(X2) + D2 = c(5) + c(0) + 16 = 1000 + c(0) + 16 = 1016 07/06/47 Machine Structure, Machine Language, and Assembly 31 ตัวอย่าง ของคาสัง่ SS-format คาสั่ง MVC 32(79,5),300(5) สมมติวา่ รี จิสเตอร์ 5 เก็บค่าของ 1000 Storage operand 1 address = c(B1) + D1 = c(5) + 32 = 1000 + 32 = 1032 Storage operand 2 address = c(B1) + D1 = c(5) + 32 = 1000 + 300 = 1300 07/06/47 Machine Structure, Machine Language, and Assembly 32 คาสั่ งของเครื่อง 360/370 (ดู Appendix A) กลุ่มที่ 1 Load Instruction และ store Instructions Hexa code op-code 58 48 98 18 12 50 40 90 07/06/47 Mnemonic Format of Instruction L LH LM LR LTR ST STH STM Machine Structure, Machine Language, and Assembly RX RX RS RR RR RX RX RS 33 กลุ่มที่ 2 คานวณ Hexa code op-code 5A 4A 1A 59 49 19 5D 1D 5C 1C 5B 1B 07/06/47 Mnemonic A AH AR C CH CR D DR M MR S SR Format of Instruction RX RX RR RX RX RR RX RR RX RR RX RR Machine Structure, Machine Language, and Assembly 34 กลุ่มที่ 3 Logical Hexa code Mnemonic Format of Instruction op-code 55 15 D2 92 8D 07/06/47 CL CLR MVC MVI SLDL Machine Structure, Machine Language, and Assembly RX RR SS SI RX 35 กลุ่มที่ 4 Branch and Link Hexa code Mnemonic Format of Instruction op-code 47 07 45 07/06/47 BC BCR BAL Machine Structure, Machine Language, and Assembly RX RR RX 36 5. ส่ วนพิเศษอืน่ ๆ • เครื่ อง 360 ยังมีส่วน Hardware ที่ช่วยป้ องกันข้อมูลเป็ น Block ขนาด 2,048 ไบท์ และยังมีระบบ Interrupt อีกด้วย จะกล่าวระ เอียด ในบทที่ 9 07/06/47 Machine Structure, Machine Language, and Assembly 37