1 / 41

BIT & STRING OPERATIONS

BIT & STRING OPERATIONS. Suthida Chaichomchuen std@kmitnb.ac.th. BOOLEAN OPERATIONS.

carlow
Download Presentation

BIT & STRING OPERATIONS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. BIT & STRING OPERATIONS Suthida Chaichomchuen std@kmitnb.ac.th

  2. BOOLEAN OPERATIONS คำสั่งประเภทตรรก เป็นคำสั่งที่มีการปฏิบัติการตามกฎทางตรรก โดยที่ค่าในแต่ละบิตของ operand จะมีความหมายได้ 2 อย่างคือ จริง [1] และเท็จ [0]ผลจากการปฏิบัติคำสั่งจะส่งไปยัง Flag ต่อไปนี้ CF, OF, PF,SF และ ZF

  3. รูปแบบคำสั่งคือ [label:] operation destination,source destination: register/memory source: register/memory/immediate Boolean Logic Instructions

  4. Boolean Logic Instructions • AND: ถ้าค่าของคู่บิตเป็น 1 เหมือนกัน ผลลัพธ์จะถูก SET เป็น 1 แต่ถ้าไม่ใช่ผลลัพธ์จะเป็น 0 • OR: ถ้าค่าใดค่าหนึ่งของคู่บิตเป็น 1 ผลลัพธ์จะถูก SET เป็น 1 แต่ถ้าไม่ใช่ผลลัพธ์จะเป็น 0

  5. Boolean Logic Instructions • XOR: ถ้าค่าของคู่บิตมีค่าที่แตกต่างกัน ผลลัพธ์จะถูก SET เป็น 1 แต่ถ้าไม่ใช่ผลลัพธ์จะเป็น 0 • TEST: ปฏิบัติการเสมือนเป็นคำสั่ง AND แต่จะเปลี่ยนเฉพาะค่าของ Flags เท่านั้น ค่าของ operand ยังเหมือนเดิม

  6. Examples of Boolean Logic AND OR XOR Source 0011 0011 0011 Dest 010101010101 Result: Dest 0001 0111 0110

  7. Examples of Boolean Instructions • สมมติให้ BL=0011 1010 และ CH=1010 0011 • AND BL,0FH • AND BL,00H • AND BL,CH • OR CH,BL • OR CH,CH • XOR BL,0FH • XOR BL,BL

  8. Examples of Boolean Operations • คำสั่ง TEST • TEST CX,0FFH • TEST BL,00000001B • TEST CL,11110000B

  9. Boolean Logic Instructions: NOT เป็นคำสั่งที่ใช้ในการเปลี่ยนค่าบิตของ operand ให้เป็นค่าตรงกันข้าม คือ จาก 1 เป็น 0 และจาก 0 กลายเป็น 1 หรือเป็นการหาค่า 1 s-complement นั่นเอง

  10. รูปแบบคำสั่งคือ [label:] NOT register/memory Boolean Logic Instructions: NOT

  11. Boolean Logic Instructions: NOT • กำหนด BL=0011 1010 • NOT BL • ผลลัพธ์คือ BL=1100 0101

  12. SHIFTING BITS • เป็นคำสั่งในการเลื่อนบิต • ใช้ได้ทั้ง Register และ Memory • เลื่อนไปทางด้านซ้ายหรือขวาก็ได้ • เลื่อนได้ทั้ง byte และword • ใช้ได้ทั้งข้อมูลที่มีเครื่องหมายและไม่มีเครื่องหมาย

  13. รูปแบบคำสั่งคือ [label:] shift destination,count destination: register/memory count: CL/immediate Shifting Bits Instructions

  14. Shifting Bits Right Instructions • SHR (Shift Logical Right) : unsigned • ดำเนินการเลื่อนบิตไปทางขวาโดยบิตที่ถูกเลื่อนออกไปจะบรรจุอยู่ใน Carry flag • SAR (Shift Arithmetic Right) : signed • คัดลอกค่า Sign bit ใส่ในบิตถัดไปและนำค่าบิตที่ถูกเลื่อนไปบรรจุใน Carry flag

  15. 0 C S C Shifting Bits Right ลักษณะการเลื่อนบิต SHR: SAR:

  16. Shifting Bits Left Instructions • SHL (Shift Logical Right) • SAL (Shift Arithmetic Right) • ทั้ง 2 คำสั่งจะปฏิบัติการเหมือนกัน โดยจะทำการเลื่อนบิตไปทางด้านซ้าย และบิตที่เลื่อนไปจะนำไปใส่ไว้ใน Carry flag

  17. C 0 Shifting Bits Left ลักษณะการเลื่อนบิต SHL/SAL:

  18. Examples of Shifting Bits สมมติให้ AL=10110100B และ CF เป็น 1 SAL AL,1 AL: 01101000 CF: 1 SHL AL,1 AL: 11010000 CF: 0 SAR AL,1 AL: 11101000 CF: 0 SHR AL,1 AL: 01110100 CF: 0

  19. ROTATING BITS • เป็นคำสั่งในการหมุนบิต • ใช้ได้ทั้ง Register และ Memory • หมุนบิตไปทางด้านซ้ายหรือขวาก็ได้ • หมุนได้ทั้ง byte และ word • ใช้ได้ทั้งข้อมูลที่มีเครื่องหมายและไม่มีเครื่องหมาย

  20. รูปแบบคำสั่งคือ [label:] rotate destination,count destination: register/memory count: CL/immediate Rotating Bits Instructions

  21. Rotating Bits Right Instructions • R0R (Rotate Logical Right) • ดำเนินการหมุนบิตไปทางขวาโดยบิตที่ถูกเลื่อนออกจะถูกคัดลอกไปไว้ใน Carry flag ด้วย • RCR (Rotate with Carry Right) • ดำเนินการหมุนบิตร่วมกับค่าใน Carry flag โดยค่าของบิตที่เลื่อนจะนำไปไว้ใน Carry flag และค่าใน Carry flag จะถูกส่งมายังบิตซ้ายสุด

  22. C C Rotating Bits Right ลักษณะการหมุนบิต ROR: RCR:

  23. Rotating Bits Left Instructions • R0L (Rotate Logical Left) • ดำเนินการหมุนบิตไปทางซ้ายโดยบิตที่ถูกเลื่อนออกจะถูกคัดลอกไปไว้ใน Carry flag ด้วย • RCL (Rotate with Carry Left) • ดำเนินการหมุนบิตร่วมกับค่าใน Carry flag โดยค่าของบิตที่เลื่อนจะนำไปไว้ใน Carry flag และค่าใน Carry flag จะถูกส่งมายังบิตขวาสุด

  24. C C Rotating Bits Left ลักษณะการหมุนบิต ROL: RCL:

  25. Examples of Rotating Bits สมมติให้ AL=10110100B และ CF เป็น 1 ROL AL,1 AL: 01101001 CF: 1 ROR AL,2 AL: 01011010 CF: 0 RCL AL,1 AL: 10110100 CF: 0 RCR AL,2 AL: 00101101 CF: 0

  26. STRING OPERATIONS • เป็นปฏิบัติการเกี่ยวกับชุดของตัวอักษร โดยอาจเป็นได้ทั้งตัวเลข ตัวอักษรหรือสัญลักษณ์พิเศษ • จะปฏิบัติการทีละไบต์หรือเวิร์ด

  27. STRING OPERATIONS • กำหนด source-string อยู่ใน DS : Data Segment • destination-string อยู่ใน ES : Extra Segment • ใช้ SI : Source Index ในการอ้างถึงไบต์หรือเวิร์ดของ source-string • ใช้ DI : Destination Index ในการอ้างถึงไบต์หรือเวิร์ดของ destination-string

  28. STRING OPERATIONS • SI และ DI จะเพิ่มค่าเมื่อการปฏิบัติการกระทำจากตัวอักษรตัวแรกไปยังตัวสุดท้าย • SI และ DI จะลดค่าเมื่อการปฏิบัติการกระทำจากตัวอักษรตัวสุดท้ายไปยังตัวแรก

  29. STRING OPERATIONS • ใช้ DF : Direction Flag เป็นตัวกำหนดการเพิ่มขึ้นและลดลงของ SI และ DI • DF เป็น 0 : SI และ DI จะถูกเพิ่มค่า • DF เป็น 1 : SI และ DI จะถูกลดค่า

  30. STRING OPERATIONS • ตัวอย่างคำสั่งในการกำหนด Segment ของ string • MOV AX,@data • MOV DS,AX • MOV ES,AX

  31. STRING OPERATIONS • ตัวอย่างคำสั่งในการกำหนด Address ของ string • LEA DI,BYTE2 • LEA SI,BYTE1

  32. STRING OPERATIONS • คำสั่งในการกำหนดค่าของ DF • CLD : Clear Direction Flag เป็น 0 • STD : Set Direction Flag เป็น 1

  33. REP: Repeat String Prefix • เป็นคำสั่งที่วางไว้หน้าคำสั่งปฏิบัติการ string อื่น ๆ เพื่อใช้ในการทำงานซ้ำหลาย ๆ ครั้ง • จำนวนรอบของการทำซ้ำจะกำหนดอยู่ใน CX

  34. Variations of REP • REP : ปฏิบัติการจนกระทั่ง CX=0 • REPE/REPZ : ปฏิบัติการเมื่อ ZF set หรือจะหยุดการทำงานเมื่อ ZF clear หรือ CX=0 • REPNE/REPNZ : ปฏิบัติการเมื่อ ZF clear หรือจะหยุดการทำงานเมื่อ ZF set หรือ CX=0

  35. String Instructions • Move: MOVS • Load: LODS • Store: STOS • Compare: CMPS • Scan: SCAS

  36. Example of String Instructions DATASTR1 DB 25 DUP (‘*’) DATASTR2 DB 25 DUP (‘ ’) . . . CLD MOV CX,25 LEA DI,DATASTR2 LEA SI,DATASTR1 REP MOVSB

  37. MOVS : Move String Instruction • คำสั่งเกี่ยวกับการเคลื่อนย้ายไบต์หรือเวิร์ดจาก source ไปยัง destination • มี 3 คำสั่ง • MOVS destination-string,source-string • MOVSB • MOVSW

  38. LODS : Load String Instruction • คำสั่งเกี่ยวกับการโหลด string จาก source ณ ตำแหน่งที่ SI ชี้อยู่ไปไว้ที่ AL หรือ AX • มี 3 คำสั่ง • LODS source-string • LODSB • LODSW

  39. STOS : Store String Instruction • คำสั่งเกี่ยวกับการเก็บค่าของ string ใน AL หรือ AX ไปไว้ที่ destination-string ณ ตำแหน่งที่ DI ชี้อยู่ • มี 3 คำสั่ง • STOS destination-string • STOSB • STOSW

  40. CMPS : Compare String Instruction • คำสั่งเกี่ยวกับการเปรียบเทียบตัวอักษรระหว่าง source-string กับ destination-string • มี 3 คำสั่ง • CMPS destination-string,source-string • CMPSB • CMPSW

  41. SCAS : Scan String Instruction • คำสั่งที่ตรวจดูว่าตัวอักษรใน string ที่อยู่ใน ES ณ ตำแหน่งที่ DI ชี้อยู่เหมือนกับค่าใน AL หรือ AX หรือไม่ • มี 3 คำสั่ง • SCAS destination-string • SCASB • SCASW

More Related