1 / 54

Basic I nstructions

Basic I nstructions. SCC : Suthida Chaichomchuen std@kmitnb.ac.th. Basic Instructions. MOV XCHG LEA ADD/ADC SUB/SBB MUL/IMUL DIV/IDIV CMP INC/DEC NEG. MOV ( move ).

admon
Download Presentation

Basic I nstructions

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. Basic Instructions SCC : Suthida Chaichomchuen std@kmitnb.ac.th

  2. Basic Instructions • MOV • XCHG • LEA • ADD/ADC • SUB/SBB • MUL/IMUL • DIV/IDIV • CMP • INC/DEC • NEG

  3. MOV (move) • เป็นการคัดลอกข้อมูลขนาดไบต์หรือเวิร์ดจากต้นทางไปยังปลายทางโดยที่ต้นทางและปลายทางอาจจะเป็น รีจิสเตอร์กับรีจิสเตอร์ หรือรีจิสเตอร์กับตำแหน่งของหน่วยความจำ หรือค่าคงที่ไปยังรีจิสเตอร์หรือหน่วยความจำ

  4. MOV : Format • [label:] MOV destination, source • [label:]MOV register, register • [label:]MOV register, memory • [label:]MOV register, immediate • [label:]MOV memory, register • [label:]MOV memory, immediate

  5. MOV : Example • MOV AX, Table • MOV Table, AX • MOV ES:[BX], AX • MOV DS, AX • MOV BL, AL • MOV CL, -30 • MOV Dest, 25H

  6. MOV : Constraint • ไม่สามารถย้ายข้อมูลระหว่างหน่วยความจำได้ • ต้องใช้รีจิสเตอร์เป็นตัวกลางในการย้ายข้อมูลระหว่างหน่วยความจำ • เช่น ต้องการย้ายข้อมูลจาก Pounds ไปยัง Weight โดยผ่านรีจิสเตอร์ AX MOV AX, Pounds MOV Weight, AX

  7. MOV : Constraint • ไม่สามารถนำค่า immediate เช่นค่า offset ใส่ให้ segment register ได้โดยตรง • ต้องใช้รีจิสเตอร์ AX เป็นตัวกลางผ่านค่า offset ไปยัง segment register MOV AX, @DATA MOV DS, AX • รีจิสเตอร์ CS ไม่สามารถใช้เป็น destination ของคำสั่ง MOV ได้

  8. XCHG (exchange) • คำสั่ง XCHG จะสลับค่าเป็นไบต์หรือเวิร์ดระหว่าง operand ที่เป็น source และ destination • สามารถสลับค่าระหว่างรีจิสเตอร์ 2 ตัว หรือระหว่างรีจิสเตอร์กับค่าในหน่วยความจำ • Segment Register ไม่สามารถนำมาใช้เป็น operand ได้

  9. XCHG : Format • [label:] XCHG destination, source • [label:] XCHG register, register • [label:] XCHG register, memory • [label:] XCHG memory, register

  10. XCHG : Example • XCHG AX, BX • XCHG AL, BH • XCHG DL, Byte • XCHG Word, DX

  11. LEA (load effective address) • คำสั่ง LEA จะนำค่าออฟเซตของ Operand ในหน่วยความจำไปใส่ไว้ในรีจิสเตอร์ 16 บิต • รูปแบบคำสั่ง [label:] LEA register16, memory16

  12. LEA : Example • นำค่าออฟเซตของ Table ไปใส่ใน BX Table DB 10,20,30,40,50,60,70 LEA BX, Table

  13. Addition Instruction • บวก operand 8/16 บิต • ADD • destination = destination+source • ADC : ADD with Carry • destination = destination+source+carry

  14. ADD & ADC : Format • [Label:] ADD/ADC register, register • [Label:] ADD/ADC memory, register • [Label:] ADD/ADC register, memory • [Label:] ADD/ADC register, immediate • [Label:] ADD/ADC memory, immediate

  15. ADD : Example • ADD AX, BX • ADD AL, Byte • ADD Word, AX • ADD BL, 10H • ADD Byte, 25H

  16. ADC : Example • ADC AX, BX • ADC AL, Byte • ADCWord, AX • ADC BL, 10H • ADC Byte, 25H

  17. Subtraction Instruction • ลบค่า operand 8/16 บิต • CF ทำหน้าที่เหมือนตัวยืม • SUB • destination = destination-source • SBB : Subtract with Borrow • destination = destination-source-carry

  18. SUB & SBB: Format • [Label:] SUB/SBB register, register • [Label:] SUB/SBB memory, register • [Label:] SUB/SBB register, memory • [Label:] SUB/SBB register, immediate • [Label:] SUB/SBB memory, immediate

  19. SUB : Example • SUB AX, BX • SUB AX, Word • SUB Word, AX • SUB BX, 1000H • SUB Word, 256H

  20. SBB : Example • SBB AX, BX • SBB AX, Word • SBB Word, AX • SBB BX, 1000H • SBB Word, 256H

  21. SUB & SBB : 32 bit • ถ้าต้องการลบค่า 32 บิต ใช้คำสั่งดังนี้ • กำหนดตัวตั้งใน BX, AX • ตัวลบอยู่ใน DX, CX • SUB AX, CX • SBB BX, DX

  22. Multiplication Instruction • เป็นคำสั่งที่ใช้ในการคูณเลข 8 บิตและ 16 บิต • ผลลัพธ์ที่ได้จากการคูณจะมีขนาดเพิ่มขึ้นเป็น 2 เท่า • มี 2 แบบคือ • MUL : Unsigned Multiplication • IMUL : Signed Multiplication

  23. Multiplication : 8 bit • การคูณเลข 8 บิต • ตัวตั้งเก็บไว้ที่รีจิสเตอร์ AL • ตัวคูณเก็บไว้ที่รีจิสเตอร์หรือหน่วยความจำขนาด 8 บิต • ผลลัพธ์จะมีขนาด 16 บิต เก็บอยู่ในรีจิสเตอร์ AX

  24. Multiplication : 16 bit • การคูณเลข 16 บิต • ตัวตั้งเก็บไว้ที่รีจิสเตอร์ AX • ตัวคูณเก็บไว้ที่รีจิสเตอร์หรือหน่วยความจำขนาด 16 บิต • ผลลัพธ์จะมีขนาด 32 บิต เก็บอยู่ในรีจิสเตอร์ DX และ AX • DX เก็บผลลัพธ์ 16 บิตบน • AX เก็บผลลัพธ์ 16 บิตล่าง

  25. MUL : Format • [label:] MUL register/memory • เป็นการคูณแบบไม่คิดเครื่องหมาย • ถ้าครึ่งบนของผลลัพธ์การคูณเป็น 0 • CF และ OF จะมีค่าเป็น 0

  26. IMUL : Format • [label:] IMUL register/memory • เป็นการคูณแบบคิดเครื่องหมาย • ถ้าครึ่งบนของผลลัพธ์การคูณเป็น sign-extension • CF และ OF จะมีค่าเป็น 0

  27. MUL & IMUL : Example • MUL BX • MUL Byte • IMUL DL • IMUL Word

  28. MUL : Example • คูณเลขที่ไม่มีเครื่องหมาย ระหว่างเลข 8 บิต กับ 16 บิต MOV AX, Multiplicand ; AX = ตัวตั้ง MOV CL, Multiplier ;CL = ตัวคูณ MOV CH, 0 MUL CX; AX = AX * CX

  29. IMUL : Example • คูณเลขที่มีเครื่องหมาย ระหว่างเลข 8 บิต กับ 16 บิต MOV AL, Multiplicand ; AL = เลข 8 บิต CBW ; ขยายเป็น 16 บิต MOV CX, Multiplier ;CX = เลข 16 บิต IMUL CX; AX = AX * CX

  30. Division Instruction • เป็นคำสั่งที่ใช้ในการหารค่าตัวเลขด้วยเลข 8 บิตและ 16 บิต • มี 2 แบบคือ • DIV : Unsigned Division • IDIV : Signed Division • รูปแบบ • [label:] DIV/IDIV register/memory

  31. Division : by 8 bit • การหารเลข 16 บิต ด้วยเลข 8 บิต • ตัวตั้งขนาด 16 บิตเก็บไว้ที่รีจิสเตอร์ AX • ตัวหารขนาด 8 บิตเก็บไว้ที่รีจิสเตอร์/หน่วยความจำ • ผลลัพธ์ • จำนวนเต็มเก็บไว้ที่ AL • เศษเก็บไว้ที่ AH

  32. Division : by 16 bit • การหารเลข 32 บิตด้วย 16 บิต • ตัวตั้งขนาด 32 บิตเก็บในรีจิสเตอร์ DX และ AX • DX เก็บไบต์สูง • AX เก็บไบต์ต่ำ • ตัวหารขนาด 16 บิตเก็บในรีจิสเตอร์/หน่วยความจำ • ผลลัพธ์ • จำนวนเต็มเก็บไว้ที่ AX • เศษเก็บไว้ที่ DX

  33. Division :Overflow • ถ้าผลลัพธ์จากการหารได้ค่าจำนวนเต็มที่ใหญ่กว่าที่ AL หรือ AX จะเก็บได้ จะเกิดการ Interrupt ชนิด 0 โดย DOS จะแสดงข้อความว่า “Divide Overflow”

  34. DIV & IDIV : Example • DIV CL • DIV BX • DIV byte • DIV word • IDIV DL • IDIV CX • IDIV byte • IDIV word

  35. DIV : Example • การหารเลขที่ไม่มีเครื่องหมายขนาด 16 บิตด้วยเลข 8 บิต • MOV AX, 037D7H ; 14295 • MOV BL, 097H ; 151 • DIV BL ; 14295/151

  36. IDIV : Example • การหารเลขที่มีเครื่องหมายขนาด 16 บิตด้วยเลข 8 บิต • MOV AX, 03ABH ; 939 • MOV BL, 0D3H ; -45 • IDIV BL ; 939/-45

  37. CMP (compare) • เป็นคำสั่งที่ใช้เปรียบเทียบค่า 2 ค่า • ใช้หลักการลบค่า source ออกจาก destination แต่ไม่ได้เก็บค่าผลลัพธ์ไว้ • มีผลต่อแฟลก AF, CF, OF, PF, SF และ ZF • ใช้ในการตรวจสอบค่าแฟลก เพื่อตัดสินใจในการย้ายคำสั่งการทำงานไปยังจุดต่าง ๆ

  38. CMP: Format • [label:] CMP destination, source • [label:] CMP register, register • [label:] CMP register, memory • [label:] CMP register, immediate • [label:] CMP memory, register • [label:] CMP memory,immediate

  39. CMP: Example • CMP AX, BX • CMP CL, count • CMP CX, 0 • CMP number,AL • CMP empty,0

  40. Unsigned Operands Condition OF SF ZF CF Source < Destination x x 0 0 Source = Destination x x 1 0 Source > Destination x x 0 1 CMP : Unsigned

  41. Signed Operands Condition OF SF ZF CF Source < Destination 0/1 0 0 x Source = Destination 0 0 1 x Source > Destination 0/1 1 0 x CMP : Signed

  42. INC (increment) • เพิ่มค่าให้กับรีจิสเตอร์/หน่วยความจำ 1 ค่า • รูปแบบ • [label:] INC register/memory • Example • INC CX • INC AL • INC byte • INC word

  43. DEC (decrement) • ลดค่าในรีจิสเตอร์/หน่วยความจำ 1 ค่า • รูปแบบ • [label:] DEC register/memory • Example • DEC CX • DEC AL • DEC byte • DEC word

  44. NEG (negate) • เป็นคำสั่งในการหาค่า 2’s Complement • รูปแบบ • [label:] NEG register/memory • Example • NEG CL • NEG BX • NEG byte • NEG word

  45. การแปลง operator = • B = A • MOV AX, A • MOV B, AX • var1 = var2 • MOV AX, var2 • MOV var1, AX

  46. การแปลง operator + • X = Y + Z • MOV AX, Y • ADD AX, Z • MOV X, AX

  47. การแปลง operator - • X = Y - Z • MOV AX, Y • SUB AX, Z • MOV X, AX

  48. การแปลง operator * • X = Y * Z • MOV AL, Y • MUL Z • MOV X, AX

  49. การแปลง operator div (unsigned) • X = Y div Z • MOV AX, Y • MOV DX, 0 • DIV Z • MOV X, AX

  50. การแปลง operator div (signed) • X = Y div Z • MOV AX, Y • CWD • IDIV Z • MOV X, AX

More Related