1 / 25

IKI10230 Pengantar Organisasi Komputer Kuliah no. 05.b: Arithmetic Operations

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

livvy
Télécharger la présentation

IKI10230 Pengantar Organisasi Komputer Kuliah no. 05.b: Arithmetic 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. IKI10230Pengantar Organisasi KomputerKuliah no. 05.b: Arithmetic Operations Sumber:1. Paul Carter, PC Assembly Language2. Hamacher. Computer Organization, ed-53. Materi kuliah CS61C/2000 & CS152/1997, UCB4. Intel Architecture Software Developer’s Manual 17 Maret 2004 L. Yohanes Stefanus (yohanes@cs.ui.ac.id)Bobby Nazief (nazief@cs.ui.ac.id) bahan kuliah: http://www.cs.ui.ac.id/kuliah/POK/

  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 • ADD Integer add • ADC Add with carry • SUB Subtract • SBB Subtract with borrow • IMUL Signed multiply • MUL Unsigned multiply • IDIV Signed divide • DIV Unsigned divide • INC Increment • DEC Decrement • NEG Negate • CMP Compare • DAA Decimal adjust after addition • DAS Decimal adjust after subtraction • AAA ASCII adjust after addition • AAS ASCII adjust after subtraction • AAM ASCII adjust after multiplication • AAD ASCII adjust before division

  4. Additions & Subtractions • ADD DEST,SRC ; DEST  [DEST] + [SRC] • OF, SF, ZF, AF, CF, and PF flags are set according to the result • ADC DEST,SRC ; DEST  [DEST] + [SRC] + [CF] • OF, SF, ZF, AF, CF, and PF flags are set according to the result • SUB DEST,SRC ; DEST  [DEST] - [SRC] • OF, SF, ZF, AF, CF, and PF flags are set according to the result • SBB DEST,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,imm8Add imm8 to AL • ADD AX,imm16Add imm16 to AX • ADD EAX,imm32Add imm32 to EAX REG/MEM  [REG/MEM] + IMM • ADD r/m8,imm8Add imm8 to r/m8 • ADD r/m16,imm16Add imm16 to r/m16 • ADD r/m32,imm32Add imm32 to r/m32 • ADD r/m16,imm8Add sign-extended imm8 to r/m16 • ADD r/m32,imm8Add sign-extended imm8 to r/m32 REG/MEM  [REG/MEM] + [REG] • ADD r/m8,r8Add r8 to r/m8 • ADD r/m16,r16Add r16 to r/m16 • ADD r/m32,r32Add r32 to r/m32 REG  [REG] + [REG/MEM] • ADD r8,r/m8 Add r/m8 to r8 • ADD r16,r/m16Add r/m16 to r16 • ADD r32,r/m32Add r/m32 to r32

  6. Multiplication • MUL SRC ; DEST  [ACC] * [SRC] • The MUL instruction multiplies two unsigned integer operands. • SRC ACC DEST r/m8 AL AX r/m16 AX DX:AX r/m32 EAX EDX: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 bitsare 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/m8Unsigned multiply (AX ← [AL]*[REG/MEM]) • MUL r/m16Unsigned multiply (DX:AX ← [AX]*[REG/MEM]) • MUL r/m32Unsigned 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-extendedIMM • 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] • dividesoneunsigned integer operand (ACC) by another (SRC) • SRC ACC QUOTIENT REMAINDER r/m8 AX AL AH r/m16 DX:AX AX DX r/m32 EDX:EAX EAX EDX • non-integral results are truncated (chopped) towards 0 • remainder is always less than thedivisor in magnitude • overflow is indicated with the #DE (divide error) exception rather thanwith the OF flag • IDIV SRC ; QUO. & REM.  [ACC] / [SRC] • dividesonesigned integer operand (ACC) by another (SRC) • non-integral results are truncated (chopped) towards 0 • sign of the remainder is always thesame as the sign of the dividend • absolute value of the remainder is always less than theabsolute value of the divisor • overflow is indicated with the #DE (divide error) exception ratherthan with the OF (overflow) flag

  10. Format Instruksi DIV & IDIV Unsigned Divide • DIV r/m8Q:AL, R:AH ← AX /[REG/MEM] • DIV r/m16Q:AX, R:DX ← DX:AX /[REG/MEM] • DIV r/m32Q:EAX, R:EDX ← EDX:EAX /[REG/MEM] Signed Divide • IDIV r/m8Q:AL, R:AH ← AX /[REG/MEM] • IDIV r/m16Q:AX, R:DX ← DX:AX /[REG/MEM] • IDIV r/m32Q: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/DECr/m8 Increment r/m byte by 1 • INC BYTE [DATA] • INC/DECr/m16 Increment r/m word by 1 • INC WORD [DATA] • INC/DECr/m32 Increment r/m doubleword by 1 • INC DWORD [DATA] • INC/DECr16 Increment word register by 1 • INC/DECr32 Increment doubleword register by 1

  12. CMP & NEG • CMP DEST,SRC ; [DEST] – [SRC], update FLAGS • The source operands arenot 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, imm8Compare imm8 with AL • CMP AX, imm16Compare imm16 with AX • CMP EAX, imm32Compare imm32 with EAX [REG/MEM] - IMM • CMP r/m8, imm8Compare imm8 with r/m8 • CMP r/m16, imm16Compare imm16 with r/m16 • CMP r/m32,imm32Compare imm32 with r/m32 • CMP r/m16,imm8Compare imm8 with r/m16 • CMP r/m32,imm8Compare imm8 with r/m32 [REG/MEM] – [REG] • CMP r/m8,r8Compare r8 with r/m8 • CMP r/m16,r16Compare r16 with r/m16 • CMP r/m32,r32Compare r32 with r/m32 [REG] – [REG/MEM] • CMP r8,r/m8Compare r/m8 with r8 • CMP r16,r/m16Compare r/m16 with r16 • CMP r32,r/m32Compare r/m32 with r32

  14. Format Instruksi NEG • NEG r/m8Two’s complement negate r/m8 • NEG r/m16Two’s complement negate r/m16 • NEG r/m32Two’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 valuesranging from 0 to 9. • BCD integers can be unpacked (one BCD digit per byte) or packed (twoBCD 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 additionand 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 thehigh 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 thedecimal arithmetic instructions. • The decimal arithmetic instructions are provided to carry outthe following operations: • To adjust the results of a previous binary arithmetic operation to produce a valid BCDresult. • To adjust the operands of a subsequent binary arithmetic operation so that the operationwill 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) instructionsadjust the results of operations performed on packed BCD integers. • Adding two packed BCDvalues 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 BCDvalue 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 fromanother and stores the result in the AL register • followedby a DAS instruction: adjusts the value inthe AL register to obtain a valid, 2-digit, packed BCD value and sets the CF flag if a decimalborrow 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 andstores the result in the AL register in unpacked BCD format • if a decimal carry occurred as a resultof 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 (ASCIIadjust after multiplication) • converts the binary value in the AL register into a decimal value andstores the least significant digit of the result in the AL register andthe most significant digit, if there is one, in the AH register • AAD (ASCII adjust before division) • converts the BCDvalue in registers AH (most significant digit) and AL (least significant digit) into a binary valueand 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) • %include "asm_io.inc" • segment .data • ; • ; Output strings • ; • prompt db "Enter a number: ", 0 • square_msgdb "Square of input is ", 0 • cube_msg db "Cube of input is ", 0 • cube25_msgdb "Cube of input times 25 is ", 0 • quot_msg db "Quotient of cube/100 is ", 0 • rem_msg db "Remainder of cube/100 is ", 0 • neg_msg db "The negation of the remainder is ", 0 • segment .bss • input resd 1

  21. Contoh Program: math.asm (2/5) • segment .text • global _asm_main • _asm_main: • enter 0,0 ; setup routine • pusha • mov eax, prompt • call print_string • call read_int • mov [input], eax • imul eax; edx:eax = eax * eax • mov ebx, eax ; save answer in ebx • mov eax, square_msg • call print_string • mov eax, ebx • call print_int • call print_nl

  22. Contoh Program: math.asm (3/5) • mov ebx, eax • imulebx, [input]; ebx *= [input] • mov eax, cube_msg • call print_string • mov eax, ebx • call print_int • call print_nl • imul ecx, ebx, 25; ecx = ebx*25 • mov eax, cube25_msg • call print_string • mov eax, ecx • call print_int • call print_nl

  23. Contoh Program: math.asm (4/5) • moveax, ebx • cdq; initialize edx by sign extension • mov ecx, 100 ; can't divide by immediate value • idiv ecx; edx:eax / ecx • mov ecx, eax ; save quotient into ecx • mov eax, quot_msg • call print_string • mov eax, ecx • call print_int • call print_nl • mov eax, rem_msg • call print_string • mov eax, edx • call print_int • call print_nl

  24. Contoh Program: math.asm (5/5) • negedx; negate the remainder • mov eax, neg_msg • call print_string • mov eax, edx • call print_int • call print_nl • popa • mov eax, 0 ; return back to C • leave • ret

  25. math.exe

More Related