1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 05.b: Arithmetic Operations Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

Slides:



Advertisements
Similar presentations
MOV Instruction MOV destination, source ; copy source to dest. MOV A,#55H ;load value 55H into reg. A MOV R0,A ;copy contents of A into R0 ;(now A=R0=55H)
Advertisements

Introduction to Computer Engineering by Richard E. Haskell BCD Arithmetic Module M16.5 Section 10.4.
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 05.c: Logical Operations Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
1 x86’s instruction sets. 2 Instruction Set Classification  Transfer Move  Arithmetic Add / Subtract Mul / Div, etc.  Control Jump Call / Return, etc.
Department of Computer Science and Software Engineering
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
1 Multiplication, Division, and Numerical Conversions Chapter 6.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, MUL Instruction The MUL (unsigned multiply) instruction.
Gursharan Singh Tatla 21-Nov-20101www.eazynotes.com.
8086 : INSTRUCTION SET By, Pramod Sunagar Assistant Professor
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 5 Arithmetic and Logic Instructions.
9-1 ECE 424 Design of Microprocessor-Based Systems Haibo Wang ECE Department Southern Illinois University Carbondale, IL x86 Instructions Part.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may.
Shift and Rotate Instructions
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify.
Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify.
MUL Instruction (Unsigned Multiply) Multiplies an 8-, 16-, or 32-bit operand by either AL, AX or EAX. MUL r/m8 MUL r/m16 MUL r/m32.
The 8051 Microcontroller and Embedded Systems
Khaled A. Al-Utaibi  Introduction  Arithmetic Instructions  Basic Logical Instructions  Shift Instructions  Rotate Instructions.
Flag Control instructions CLC clear carry flag CF = 0 STC set carry flag CF= 1 CMC complement carry flag [CF] CF.
Sahar Mosleh California State University San MarcosPage 1 CPU Flags and Boolean Instructions.
ASCII and BCD Arithmetic Chapter 11 S. Dandamudi.
Chapter four – The 80x86 Instruction Set Principles of Microcomputers 2015年10月19日 2015年10月19日 2015年10月19日 2015年10月19日 2015年10月19日 2015年10月19日 1 Chapter.
Multiplication and Division Instructions & the 0Ah function.
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
Copyright 2000ELEC 242 Arithmetic Instructions1 Arithmetic Instructions Arithmetic and logical instructions modify the contents of the Flag (Status) register.
Arithmetic Flags and Instructions
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 4 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
ECE 353 Introduction to Microprocessor Systems Michael G. Morrow, P.E. Week 6.
Assembly Language for Intel-Based Computers, 4 th Edition Unpacked and Packed Integers (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for x86 Processors 6th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify and copy.
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 06: Control Structures Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
Arithmetic and Logic Instructions
3.4 Addressing modes Specify the operand to be used. To generate an address, a segment register is used also. Immediate addressing: the operand is a number.
Chapter 7: Integer Arithmetic. 2 Chapter Overview Shift and Rotate Instructions Shift and Rotate Applications Multiplication and Division Instructions.
Arithmetic Flags and Instructions Chapter 7 S. Dandamudi.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may.
ICS 312 SET 10 Multiplication & Division & input using function 0Ah.
The Assemble, Unassemble commands of the debugger: U Command for converting machine code language source Equivalent machine code instructions Equivalent.
Introduction Arithmetic instructions are used to perform arithmetic operation such as Addition Subtraction Multiplication Division These operations can.
Microprocessor & Assembly Language Arithmetic and logical Instructions.
ECE 353 Introduction to Microprocessor Systems Michael J. Schulte Week 6.
Multiplication and Division instructions Dr.Hadi AL Saadi.
Data Transfers, Addressing, and Arithmetic
Assembly Language for Intel-Based Computers, 5th Edition
Basic Assembly Language
Multiplication and Division Instructions
4.2 Arithmetic Instructions
INSTRUCTION SET OF 8086 PAWAN KUMAR SINGH.
CS 301 Fall 2002 Assembly Instructions
X86’s instruction sets.
Chapter 4: Instructions
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Microprocessor and Assembly Language
Multiplication and Division Instructions
Assembly Language for Intel-Based Computers, 4th Edition
Assembly Language for Intel-Based Computers, 4th Edition
Assembly Language for Intel-Based Computers, 5th Edition
EECE.3170 Microprocessor Systems Design I
Chapter 5 Arithmetic and Logic Instructions
Chapter 5: Arithmetic and Logic Instructions
Multiplication and Division Instructions
Multiplication and Division Instructions
Division instruction.
Computer Architecture and System Programming Laboratory
Part IV The FLAGS Register
Presentation transcript:

1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 05.b: Arithmetic Operations Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization, ed-5 3. Materi kuliah CS61C/2000 & CS152/1997, UCB 4. Intel Architecture Software Developer’s Manual 17 Maret 2004 L. Yohanes Stefanus Bobby Nazief bahan kuliah:

2 Arithmetic Instructions °The binary arithmetic instructions operate on 8-, 16-, and 32-bit numeric data encoded as signed or unsigned binary integers °Operations include add, subtract, multiply, divide increment, decrement, compare, and change sign (negate) °The are also BCD (binary coded decimal) arithmetic instructions

3 Arithmetic Instructions: Summary 1.ADD Integer add 2.ADC Add with carry 3.SUB Subtract 4.SBB Subtract with borrow 5.IMUL Signed multiply 6.MUL Unsigned multiply 7.IDIV Signed divide 8.DIV Unsigned divide 9.INC Increment 10.DEC Decrement 11.NEG Negate 12.CMP Compare 13.DAA Decimal adjust after addition 14.DAS Decimal adjust after subtraction 15.AAA ASCII adjust after addition 16.AAS ASCII adjust after subtraction 17.AAM ASCII adjust after multiplication 18.AAD ASCII adjust before division

4 Additions & Subtractions °ADDDEST,SRC; DEST  [DEST] + [SRC] OF, SF, ZF, AF, CF, and PF flags are set according to the result °ADCDEST,SRC; DEST  [DEST] + [SRC] + [CF] OF, SF, ZF, AF, CF, and PF flags are set according to the result °SUBDEST,SRC; DEST  [DEST] - [SRC] OF, SF, ZF, AF, CF, and PF flags are set according to the result °SBBDEST,SRC; DEST  [DEST] - [SRC] – [CF] OF, SF, ZF, AF, CF, and PF flags are set according to the result

5 Format Instruksi ADD, ADC, SUB, SBB ACC  [ACC] + IMM ADD AL,imm8 Add imm8 to AL ADD AX,imm16 Add imm16 to AX ADD EAX,imm32 Add imm32 to EAX REG/MEM  [REG/MEM] + IMM ADD r/m8,imm8 Add imm8 to r/m8 ADD r/m16,imm16 Add imm16 to r/m16 ADD r/m32,imm32 Add imm32 to r/m32 ADD r/m16,imm8 Add sign-extended imm8 to r/m16 ADD r/m32,imm8 Add sign-extended imm8 to r/m32 REG/MEM  [REG/MEM] + [REG] ADD r/m8,r8 Add r8 to r/m8 ADD r/m16,r16 Add r16 to r/m16 ADD r/m32,r32 Add r32 to r/m32 REG  [REG] + [REG/MEM] ADD r8,r/m8 Add r/m8 to r8 ADD r16,r/m16 Add r/m16 to r16 ADD r32,r/m32 Add r/m32 to r32

6 Multiplication °MUL SRC; DEST  [ACC] * [SRC] The MUL instruction multiplies two unsigned integer operands. SRCACCDEST r/m8ALAX r/m16AXDX:AX r/m32EAXEDX:EAX CF & OF flags are set to 0 if the upper half of the result is 0; otherwise, they are set to 1 °IMUL IMUL SRC ; lihat MUL SRC IMUL DEST,SRC; DEST  [DEST] * [SRC] The IMUL instruction multiplies two signed integer operands. CF & OF flags are set when: -significant bits are carried into the upper half of the result -the result must be truncated to fit in the destination operand size

7 Format Instruksi MUL °MUL r/m8 Unsigned multiply (AX ← [AL] * [REG/MEM]) °MUL r/m16 Unsigned multiply (DX:AX ← [AX] * [REG/MEM]) °MUL r/m32 Unsigned multiply (EDX:EAX ← [EAX] * [REG/MEM]) CONTOH: °MUL DL °MUL BYTE [DATA] °MUL DX °MUL WORD [DATA] °MUL EDX °MUL DWORD [DATA]

8 Format Instruksi IMUL 1-OPERAND IMUL r/m8AX← [AL] * [REG/MEM] IMUL r/m16DX:AX ← [AX] * [REG/MEM] IMUL r/m32EDX:EAX ← [EAX] * [REG/MEM] 2-OPERAND IMUL r16,r/m16REG ← [REG] * [REG/MEM] IMUL r16,imm8REG ← [REG] * sign-extended IMM IMUL r16,imm16REG ← r/m16 * IMM IMUL r32,r/m32REG ← [REG] * [REG/MEM] IMUL r32,imm8REG ← [REG] * sign-extended IMM IMUL r32,imm32REG ← [REG/MEM] * IMM 3-OPERAND IMUL r16,r/m16,imm8REG ← [REG/MEM] * sign-ext. IMM IMUL r16,r/m16,imm16REG ← [REG/MEM] * IMM IMUL r32,r/m32,imm8REG ← [REG/MEM] * sign-ext. IMM IMUL r32,r/m32,imm32REG ← [REG/MEM] * IMM

9 Division °DIV SRC; QUO. & REM.  [ACC] / [SRC] divides one unsigned integer operand (ACC) by another (SRC) SRCACCQUOTIENTREMAINDER r/m8AXALAH r/m16DX:AXAXDX r/m32EDX:EAXEAXEDX non-integral results are truncated (chopped) towards 0 remainder is always less than the divisor in magnitude overflow is indicated with the #DE (divide error) exception rather than with the OF flag °IDIV SRC; QUO. & REM.  [ACC] / [SRC] divides one signed integer operand (ACC) by another (SRC) non-integral results are truncated (chopped) towards 0 sign of the remainder is always the same as the sign of the dividend absolute value of the remainder is always less than the absolute value of the divisor overflow is indicated with the #DE (divide error) exception rather than with the OF (overflow) flag

10 Format Instruksi DIV & IDIV Unsigned Divide DIV r/m8 Q:AL, R:AH ← AX / [REG/MEM] DIV r/m16 Q:AX, R:DX ← DX:AX / [REG/MEM] DIV r/m32 Q:EAX, R:EDX ← EDX:EAX / [REG/MEM] Signed Divide IDIV r/m8 Q:AL, R:AH ← AX / [REG/MEM] IDIV r/m16 Q:AX, R:DX ← DX:AX / [REG/MEM] IDIV r/m32 Q:EAX, R:EDX ← EDX:EAX / [REG/MEM]

11 INC & DEC °INC DEST; DEST  [DEST] + 1 °DEC DEST; DEST  [DEST] – 1 Operand is assumed to be unsigned integer. CF flag is not affected. OF, SF, ZF, AF, and PF flags are set according to the result. °INC/DEC r/m8 Increment r/m byte by 1 INC BYTE [DATA] °INC/DEC r/m16 Increment r/m word by 1 INC WORD [DATA] °INC/DEC r/m32 Increment r/m doubleword by 1 INC DWORD [DATA] °INC/DEC r16 Increment word register by 1 °INC/DEC r32 Increment doubleword register by 1

12 CMP & NEG °CMP DEST,SRC; [DEST] – [SRC], update FLAGS The source operands are not modified, nor is the result saved. The CF, OF, SF, ZF, AF, and PF flags are set according to the result (in the same manner as the SUB instruction) °NEG DEST; DEST  0 – [DEST] Assume a signed integer operand CF flag set to 0 if the source operand is 0; otherwise it is set to 1 OF, SF, ZF, AF, and PF flags are set according to the result

13 Format Instruksi CMP [ACC] - IMM CMP AL, imm8 Compare imm8 with AL CMP AX, imm16 Compare imm16 with AX CMP EAX, imm32 Compare imm32 with EAX [REG/MEM] - IMM CMP r/m8, imm8 Compare imm8 with r/m8 CMP r/m16, imm16 Compare imm16 with r/m16 CMP r/m32,imm32 Compare imm32 with r/m32 CMP r/m16,imm8 Compare imm8 with r/m16 CMP r/m32,imm8 Compare imm8 with r/m32 [REG/MEM] – [REG] CMP r/m8,r8 Compare r8 with r/m8 CMP r/m16,r16 Compare r16 with r/m16 CMP r/m32,r32 Compare r32 with r/m32 [REG] – [REG/MEM] CMP r8,r/m8 Compare r/m8 with r8 CMP r16,r/m16 Compare r/m16 with r16 CMP r32,r/m32 Compare r/m32 with r32

14 Format Instruksi NEG °NEG r/m8 Two’s complement negate r/m8 °NEG r/m16 Two’s complement negate r/m16 °NEG r/m32 Two’s complement negate r/m32 CONTOH: °NEG DL °NEG BYTE [DATA] °NEG DX °NEG WORD [DATA] °NEG EDX °NEG DWORD [DATA]

15 DECIMAL ARITHMETIC

16 Packed & Unpacked BCD Integers °Binary-coded decimal integers (BCD integers) are unsigned 4-bit integers with valid values ranging from 0 to 9. °BCD integers can be unpacked (one BCD digit per byte) or packed (two BCD digits per byte). °The value of an unpacked BCD integer is the binary value of the low halfbyte (bits 0 through 3). The high half-byte (bits 4 through 7) can be any value during addition and subtraction, but must be zero during multiplication and division. °Packed BCD integers allow two BCD digits to be contained in one byte. Here, the digit in the high half-byte is more significant than the digit in the low half-byte.

17 Decimal Arithmetic °Decimal arithmetic can be performed by combining the binary arithmetic instructions ADD, SUB, MUL, and DIV with the decimal arithmetic instructions. °The decimal arithmetic instructions are provided to carry out the following operations: To adjust the results of a previous binary arithmetic operation to produce a valid BCD result. To adjust the operands of a subsequent binary arithmetic operation so that the operation will produce a valid BCD result. °Decimal arithmetic instructions operate only on both packed and unpacked BCD values.

18 Packed-BCD Instructions °The DAA (decimal adjust after addition) and DAS (decimal adjust after subtraction) instructions adjust the results of operations performed on packed BCD integers. °Adding two packed BCD values requires two instructions: an ADD instruction: adds (binary addition) the two values and stores the result in the AL register followed by a DAA instruction: adjusts the value in the AL register to obtain a valid, 2-digit, packed BCD value and sets the CF flag if a decimal carry occurred as the result of the addition. °Subtracting one packed BCD value from another requires: a SUB instruction: subtracts (binary subtraction) one BCD value from another and stores the result in the AL register followed by a DAS instruction: adjusts the value in the AL register to obtain a valid, 2-digit, packed BCD value and sets the CF flag if a decimal borrow occurred as the result of the subtraction.

19 Unpacked-BCD Instructions °AAA (ASCII adjust after addition) converts the binary value in the AL register into a decimal value and stores the result in the AL register in unpacked BCD format if a decimal carry occurred as a result of the addition, the CF flag is set and the contents of the AH register are incremented by 1 °AAS (ASCII adjust after subtraction) similar with AAA °AAM (ASCII adjust after multiplication) converts the binary value in the AL register into a decimal value and stores the least significant digit of the result in the AL register and the most significant digit, if there is one, in the AH register °AAD (ASCII adjust before division) converts the BCD value in registers AH (most significant digit) and AL (least significant digit) into a binary value and stores the result in register AL when the value in AL is divided by an unpacked BCD value, the quotient and remainder will be automatically encoded in unpacked BCD format

20 Contoh Program: math.asm (1/5) 1.%include "asm_io.inc" 2.segment.data 3.; 4.; Output strings 5.; 6.prompt db "Enter a number: ", 0 7.square_msgdb "Square of input is ", 0 8.cube_msg db "Cube of input is ", 0 9.cube25_msgdb "Cube of input times 25 is ", 0 10.quot_msg db "Quotient of cube/100 is ", 0 11.rem_msg db "Remainder of cube/100 is ", 0 12.neg_msg db "The negation of the remainder is ", 0 13.segment.bss 14.input resd 1

21 Contoh Program: math.asm (2/5) 15.segment.text 16. global _asm_main 17._asm_main: 18. enter 0,0 ; setup routine 19. pusha 20. mov eax, prompt 21. call print_string 22. call read_int 23. mov [input], eax 24. imul eax ; edx:eax = eax * eax 25. mov ebx, eax ; save answer in ebx 26. mov eax, square_msg 27. call print_string 28. mov eax, ebx 29. call print_int 30. call print_nl

22 Contoh Program: math.asm (3/5) 31. mov ebx, eax 32. imulebx, [input] ; ebx *= [input] 33. mov eax, cube_msg 34. call print_string 35. mov eax, ebx 36. call print_int 37. call print_nl 38. imul ecx, ebx, 25 ; ecx = ebx* mov eax, cube25_msg 40. call print_string 41. mov eax, ecx 42. call print_int 43. call print_nl

23 Contoh Program: math.asm (4/5) 44. moveax, ebx 45. cdq ; initialize edx by sign extension 46. mov ecx, 100 ; can't divide by immediate value 47. idiv ecx ; edx:eax / ecx 48. mov ecx, eax ; save quotient into ecx 49. mov eax, quot_msg 50. call print_string 51. mov eax, ecx 52. call print_int 53. call print_nl 54. mov eax, rem_msg 55. call print_string 56. mov eax, edx 57. call print_int 58. call print_nl

24 Contoh Program: math.asm (5/5) 59. negedx ; negate the remainder 60. mov eax, neg_msg 61. call print_string 62. mov eax, edx 63. call print_int 64. call print_nl 65. popa 66. mov eax, 0 ; return back to C 67. leave 68. ret

25 math.exe