Arithmetic Flags and Instructions

Chapter 6 S. Dandamudi

Outline
• Status flags
∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗
1998

• Application examples
∗ PutInt8 ∗ GetInt8

Zero flag Carry flag Overflow flag Sign flag Auxiliary flag Parity flag Addition instructions Subtraction instructions Multiplication instructions Division instructions

• Multiword arithmetic
∗ ∗ ∗ ∗ Addition Subtraction Multiplication Division

• Arithmetic instructions

• Performance: Multiword multiplication

© S. Dandamudi

Arithmetic: Page 2

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Springer-Verlag, 1998.

1

Status Flags
• Six status flags monitor the outcome of arithmetic, logical, and related operations
Flags Register FLAGS
3 1 2 2 0 0 0 0 0 0 0 0 2 2 1 1 1 1 0 9 8 7 I V V A V 0 D I I C M P F 1 6 1 5 1 4 1 3 1 2 1 1 1 0

9

8

7 S F

6 Z F

5 0

4

3

2

1 1

0 C F

EFLAGS

0

R N F 0 T

IO PL

O D I T F F F F

A P F 0 F

Status Flags CF = Carry Flag PF = Parity Flag AF = Auxiliary Carry Flag ZF = Zero Flag SF = Sign Flag OF = Overflow Flag

Control Flags DF = Direction Flag

System Flags TF = Trap Flag IF = Interrupt Flag IOPL = I/O Privilege Level NT = Nested Task RF = Resume Flag VM = Virtual 8086 Mode AC = Alignment Check VIF = Virtual Interrupt Flag VIP = Virtual Interrupt Pending ID = ID Flag

31 EIP

Instruction Pointer 16 15 IP

0

1998

© S. Dandamudi

Arithmetic: Page 3

To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Springer-Verlag, 1998.

Status Flags (cont’d)
• Status flags are updated to indicate certain properties of the result
∗ Example: If the result is zero, zero flag is set

• Once a flag is set, it remains in that state until another instruction that affects the flags is executed • Not all instructions affect all status flags
∗ add and sub affect all six flags ∗ inc and dec affect all but the carry flag ∗ mov, push, and pop do not affect any flags
1998 © S. Dandamudi Arithmetic: Page 4
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Springer-Verlag, 1998.

2

Status Flags (cont’d) • Example .” Springer-Verlag. ZF remains 1 pop DX . ZF is still zero sub AL. ZF = 0 ∗ Zero can result in several ways (e. ZF remains 1 mov CX. ZF is set (ZF = 1) push BX . ZF is cleared (ZF = 0) 1998 © S. Dandamudi. Dandamudi. “Introduction to Assembly Language Programming. 1998.g.1 AX » All three examples result in zero result and set ZF ∗ Related instructions jump if zero (jump if ZF = 1) jz jnz jump if not zero (jump if ZF = 0) 1998 © S. 3 .0 . ZF remains 1 mov BX. initially.55H . assume ZF = 0 mov AL.” Springer-Verlag. Dandamudi Arithmetic: Page 5 To be used with S. 1998.0FH AL. overflow) mov add AL.55H . ZF remains 1 inc CX . result is 1 . Dandamudi Arithmetic: Page 6 To be used with S. “Introduction to Assembly Language Programming. Status Flags (cont’d) • Zero Flag ∗ Indicates zero result – If the result is zero. result is 0 .0FFFFH AX mov dec AX.AX . ZF = 1 – Otherwise.0F1H mov inc AX.

” Springer-Verlag.AX .” Springer-Verlag.’$’ .M outer_loop: mov CX. Dandamudi. Dandamudi To be used with S.AX Arithmetic: Page 8 1998 © S. ZF = 1 if char is $ cmp AX. Dandamudi Arithmetic: Page 7 To be used with S.N inner_loop: inc AX loop inner_loop dec DX jnz outer_loop exit_loops: mov sum. Status Flags (cont’d) • Consider the following code sum := 0 for (I = 1 to M) for (j = 1 to N) sum := sum + 1 end for end for • Assembly code sub AX. 4 . 1998.Status Flags (cont’d) • Uses of zero flag ∗ Two main uses of zero flag » Testing equality – Often used with cmp instruction cmp char. 1998. AX := 0 mov DX. “Introduction to Assembly Language Programming. Dandamudi. “Introduction to Assembly Language Programming.BX » Counting to a preset value – Initialize a register with the count value – Decrement it using dec instruction – Use jz/jnz to transfer control 1998 © S.

1998.294.0FH AL.Status Flags (cont’d) • Two observations ∗ loop instruction is equivalent to dec CX jnz inner_loop » This two instruction sequence is more efficient than the loop instruction (takes less time to execute) » loop instruction does not affect any flags! ∗ This two instruction sequence is better than initializing CX to zero and executing inc cmp jle 1998 CX CX.” Springer-Verlag.” Springer-Verlag. Dandamudi Arithmetic: Page 10 To be used with S. 16-.12AFH ∗ Range of 8-. Dandamudi.295 (232−1) © S. “Introduction to Assembly Language Programming. 5 . Status Flags (cont’d) • Carry Flag ∗ Records the fact that the result of an arithmetic operation on unsigned numbers is out of range ∗ The carry flag is set in the following examples mov add AL.0F1H mov sub AX.535 (216 − 1) 0 to 4. “Introduction to Assembly Language Programming.N inner_loop © S.12AEH AX. Dandamudi Arithmetic: Page 9 To be used with S. 1998.967. and 32-bit unsigned numbers size 8 bits 16 bits 32 bits 1998 range 0 to 255 (28 − 1) 0 to 65. Dandamudi.

Status Flags (cont’d) ∗ Carry flag is not set by inc and dec instructions » The carry flag is not set in the following examples mov AL. Dandamudi Arithmetic: Page 12 To be used with S. carry out of leftmost bit indicates overflow ∗ To test a bit using the shift/rotate instructions » Bit shifted/rotated out is captured in the carry flag » We can use jc/jnc to test whether this bit is 1 or 0 1998 © S. Dandamudi Arithmetic: Page 11 To be used with S. 1998. Dandamudi.0 inc AL dec AX ∗ Related instructions jc jnc jump if carry (jump if CF = 1) jump if no carry (jump if CF = 0) ∗ Carry flag can be manipulated directly using stc clc cmc 1998 set carry flag (set CF to 1) clear carry flag (clears CF to 0) complement carry flag (inverts CF value) © S. 1998. “Introduction to Assembly Language Programming. Dandamudi.” Springer-Verlag. 6 . Status Flags (cont’d) • Uses of carry flag ∗ To propagate carry/borrow in multiword addition/subtraction 1 x = 3710 26A8 y = 489B A321 7FAB C9CA ← carry from lower 32 bits 1257 9AE7H FE60 4213H 10B7 DCFAH ∗ To detect overflow/underflow condition » In the last example.” Springer-Verlag.0FFH mov AX. “Introduction to Assembly Language Programming.

Status Flags (cont’d) • Overflow flag ∗ Indicates out-of-range result on signed numbers – Signed number counterpart of the carry flag ∗ The following code sets the overflow flag but not the carry flag mov add AL. 7 . overflow: (overflow code here) . 72H = 114D .” Springer-Verlag.0EH jo overflow no_overflow: (no overflow code here) .147.647 1998 © S. 1998 © S.483.768 to +32..147...648 to +2.72H add AL.. overflow: (overflow code here) .. and 32-bit signed numbers size 8 bits range − 128 to +127 27 to (27 − 1) 215 to (215 − 1) 231 to (231 − 1) Arithmetic: Page 13 16 bits − 32...483..72H add AL.. 0EH = 14D ∗ Range of 8-. “Introduction to Assembly Language Programming.767 32 bits −2. Dandamudi. Dandamudi To be used with S. Arithmetic: Page 14 To be used with S. Dandamudi. Status Flags (cont’d) • Signed or unsigned: How does the system know? ∗ The processor does not know the interpretation ∗ It sets carry and overflow under each interpretation Unsigned interpretation mov AL.72H AL.0EH .. 16-. “Introduction to Assembly Language Programming..0EH jc overflow no_overflow: (no overflow code here) .” Springer-Verlag. 1998.. 1998. Dandamudi Signed interpretation mov AL.

Dandamudi Arithmetic: Page 15 To be used with S. “Introduction to Assembly Language Programming. “Introduction to Assembly Language Programming. 8 .15 sub AL.Status Flags (cont’d) ∗ Related instructions jo jno jump if overflow (jump if OF = 1) jump if no overflow (jump if OF = 0) interrupt on overflow ∗ There is a special software interrupt instruction into Details on this instruction in Chapter 12 • Uses of overflow flag ∗ Main use » To detect out-of-range result on signed numbers 1998 © S.97 clears the sign flag as the result is 112 (or 0111000 in binary) mov AL. Dandamudi.15 add AL. 1998.” Springer-Verlag. Dandamudi. Dandamudi Arithmetic: Page 16 To be used with S. Status Flags (cont’d) • Sign flag ∗ Indicates the sign of the result – Useful only when dealing with signed numbers – Simply a copy of the most significant bit of the result ∗ Examples mov AL.” Springer-Verlag. 1998.97 sets the sign flag as the result is −82 (or 10101110 in binary) ∗ Related instructions js jns 1998 jump if sign (jump if SF = 1) jump if no sign (jump if SF = 0) © S.

1998.Status Flags (cont’d) • Usage of sign flag ∗ To test the sign of the result ∗ Also useful to efficiently implement countdown loops • Consider the count down loop: for (i = M downto 0) <loop body> end for • If we don’t use the jns.M for_loop: <loop body> dec CX jns for_loop Arithmetic: Page 17 To be used with S. we need cmp as shown below: cmp CX.e. Status Flags (cont’d) • Auxiliary flag ∗ Indicates whether an operation produced a carry or borrow in the low-order 4 bits (nibble) of 8-. or 32bit operands (i. auxiliary flag is set 1998 © S. Dandamudi The count down loop can be implemented as mov CX. Dandamudi. “Introduction to Assembly Language Programming.” Springer-Verlag. 1998.” Springer-Verlag. Dandamudi. “Introduction to Assembly Language Programming. 9 .94 1 43D = 0010 94D = 0101 137D = 1000 ← carry from lower 4 bits 1011B 1110B 1001B » As there is a carry from the lower nibble.0 jl for_loop 1998 © S. 16-. operand size doesn’t matter) ∗ Example mov add AL.43 AL. Dandamudi Arithmetic: Page 18 To be used with S.

Dandamudi Arithmetic: Page 20 To be used with S. 10 .Status Flags (cont’d) ∗ Related instructions » No conditional jump instructions with this flag » Arithmetic operations on BCD numbers use this flag ASCII adjust for addition aaa ASCII adjust for subtraction aas ASCII adjust for multiplication aam ASCII adjust for division aad Decimal adjust for addition daa Decimal adjust for subtraction das – Chapter 11 has more details on these instructions ∗ Usage » Main use is in performing arithmetic operations on BCD numbers 1998 © S. Dandamudi. 1998.and 32-bit values.89 53D = 0011 0101B 89D = 0101 1001B 142D = 1000 1110B » As the result has even number of 1 bits. “Introduction to Assembly Language Programming.” Springer-Verlag. Status Flags (cont’d) • Parity flag ∗ Indicates even parity of the low 8 bits of the result – PF is set if the lower 8 bits contain even number 1 bits – For 16. Dandamudi Arithmetic: Page 19 To be used with S. 1998. “Introduction to Assembly Language Programming.53 AL. only the least significant 8 bits are considered for computing parity value ∗ Example mov add AL.” Springer-Verlag. parity flag is set ∗ Related instructions jp jnp 1998 jump on even parity (jump if PF = 1) jump on odd parity (jump if PF = 0) © S. Dandamudi.

sbb. idiv » Related instructions: cbw. Dandamudi Arithmetic: Page 21 To be used with S. cwd. cdq. movsx. that operate on decimal numbers » See Chapter 11 for details 1998 © S.” Springer-Verlag. aas. 1998. cmp » Multiplication: mul. inc » Subtraction: sub.Status Flags (cont’d) ∗ Usage of parity flag » Useful in writing data encoding programs » Example: Encodes the byte in AL (MSB is the parity bit) parity_encode PROC shl AL jp parity_zero stc jmp move_parity_bit parity_zero: clc move_parity_bit: rcr AL parity_encode ENDP 1998 © S. “Introduction to Assembly Language Programming. Dandamudi. 11 . Dandamudi. movzx ∗ There are few other instructions such as aaa. dec. etc. cwde. 1998. “Introduction to Assembly Language Programming. adc.and 32-bit operands » Addition: add.” Springer-Verlag. Dandamudi Arithmetic: Page 22 To be used with S. 16. Arithmetic Instructions • Pentium provides several arithmetic instructions that operate on 8-. neg. imul » Division: div.

1998.Arithmetic Instructions (cont’d) • Addition instructions ∗ Basic format add destination. Dandamudi Arithmetic: Page 24 To be used with S.source » Performs simple integer addition destination := destination + source ∗ Five operand combinations are possible add add add add add register. “Introduction to Assembly Language Programming.” Springer-Verlag. 12 . 1998.register © S. “Introduction to Assembly Language Programming.source » Performs integer addition with carry destination := destination + source + CF ∗ Useful in performing addition of long word arithmetic ∗ The three carry flag manipulating instructions are useful stc clc cmc set carry flag (set CF to 1) clear carry flag (clears CF to 0) complement carry flag (inverts CF value) 1998 © S. Dandamudi. Dandamudi. register register. Arithmetic Instructions (cont’d) ∗ Basic format adc destination.immediate memory.immediate register.” Springer-Verlag. Dandamudi 1998 Arithmetic: Page 23 To be used with S.memory memory.

source . Dandamudi.” Springer-Verlag.1 » Both take same time but inc version takes less space 1998 © S. Dandamudi Arithmetic: Page 25 To be used with S.” Springer-Verlag.source » Performs integer subtraction with borrow destination := destination . Dandamudi. “Introduction to Assembly Language Programming. 1998.source » Performs simple integer subtraction destination := destination − source sbb destination. 13 . Arithmetic Instructions (cont’d) • Subtraction instructions sub destination.Arithmetic Instructions (cont’d) ∗ The final instruction inc requires a single operand inc destination » Performs increment operation destination := destination + 1 » The operand is treated as an unsigned number ∗ Does not affect the carry flag » Other five status flags are updated ∗ In general inc BX is better than add BX. “Introduction to Assembly Language Programming. 1998.CF dec destination » Performs decrement operation destination := destination − 1 1998 © S. Dandamudi Arithmetic: Page 26 To be used with S.

” Springer-Verlag.source » Performs subtraction without updating destination destination . “Introduction to Assembly Language Programming. we need separate instructions for unsigned numbers mul imul for signed numbers 1998 © S. Dandamudi. Arithmetic Instructions (cont’d) • Multiplication ∗ More complicated than add/sub » Produces double-length results – E.source » Updates all six status flags to record the attributes of the result » The cmp instruction is typically followed by a conditional jump instruction 1998 © S. Dandamudi Arithmetic: Page 28 To be used with S. Dandamudi. Dandamudi Arithmetic: Page 27 To be used with S. 14 .g. 1998. Multiplying two 8 bit numbers produces a result that requires 16 bits » Cannot use a single multiply instruction for signed and unsigned numbers – add and sub instructions work both on signed and unsigned numbers – For multiplication. 1998.Arithmetic Instructions (cont’d) • Subtraction instructions (cont’d) neg destination » Performs sign reversal destination := 0 − destination » Useful in signed number manipulation cmp destination. “Introduction to Assembly Language Programming.” Springer-Verlag.

AL := -66 produces 66D in AX register (again.25 DL produces 250D in AX register (result fits in AL) • The imul instruction can use the same syntax » Also supports other formats ∗ Example mov mov mul DL. Arithmetic Instructions (cont’d) ∗ Example mov mov mul AL. result fits in AL) 1998 © S. Dandamudi.” Springer-Verlag. 1998. “Introduction to Assembly Language Programming. Dandamudi Arithmetic: Page 30 To be used with S.” Springer-Verlag. Dandamudi Arithmetic: Page 29 To be used with S. “Introduction to Assembly Language Programming. 1998. DL := -1 . the location of the other source operand and destination are selected High-order 8 bits AL 8-bit source AH Low-order 8 bits AL High-order 16 bits AX 16-bit source DX Low-order 16 bits AX Low-order 32 bits EAX High-order 32 bits EAX 32-bit source EDX 1998 © S.0FFH AL.Arithmetic Instructions (cont’d) • Unsigned multiplication mul source » Depending on the source operand size.10 DL. 15 .0BEH DL . Dandamudi.

Dandamudi Arithmetic: Page 32 To be used with S.” Springer-Verlag. 1998. divide overflow is possible Pentium provides a special software interrupt when a divide overflow occurs  ∗ Two instructions as in multiplication div idiv 1998 source source © S. Dandamudi. Dandamudi for unsigned numbers for signed numbers Arithmetic: Page 31 To be used with S. there will not be any overflow – In division.Arithmetic Instructions (cont’d) • Division instruction ∗ Even more complicated than multiplication » Produces two results – Quotient – Remainder » In multiplication. Dandamudi. using a double-length register. 16 . 1998. “Introduction to Assembly Language Programming. Arithmetic Instructions (cont’d) 16-bit dividend AX Quotient AL 8-bit source Divisor 32-bit dividend DX AX Quotient AX 16-bit source Divisor 64-bit dividend EDX EAX Quotient EAX 32-bit source Divisor and Remainder EDX and Remainder DX and Remainder AH 1998 © S. “Introduction to Assembly Language Programming.” Springer-Verlag.

Arithmetic Instructions (cont’d) • Signed division requires some help » We extended an unsigned 16 bit number to 32 bits by placing zeros in the upper 16 bits » This will not work for signed numbers – To extend signed numbers.00FBH CL.DX AX. Dandamudi Arithmetic: Page 33 To be used with S. Dandamudi. clear DX .” Springer-Verlag. you have to copy the sign bit into those upper bit positions ∗ Pentium provides three instructions in aiding sign extension » All three take no operands cbw converts byte to word (extends AL into AH) cwd converts word to doubleword (extends AX into DX) cdq converts doubleword to quadword (extends EAX into EDX) 1998 © S. CL := 12D produces 20D in AL and 11D as remainder in AH • Example sub mov mov div DX.141BH CX.Arithmetic Instructions (cont’d) • Example mov mov div AX. “Introduction to Assembly Language Programming. CX := 300D produces 17D in AX and 47D as remainder in DX 1998 © S.012CH CX . 1998. Dandamudi. Dandamudi Arithmetic: Page 34 To be used with S. AX := 5147D . 1998. AX := 251D . 17 .0CH CL .” Springer-Verlag. “Introduction to Assembly Language Programming.

Arithmetic Instructions (cont’d) • Example mov cbw mov idiv AL. dest has to be a 32 bit register 1998 © S.src (move sign-extended src to dest) movzx dest.” Springer-Verlag. “Introduction to Assembly Language Programming. DX := FFFFH .0A1H CL. CX := 300D produces −17D in AX and −47D as remainder in DX 1998 © S. Dandamudi. CL := 12D produces −7D in AL and −11D as remainder in AH • Example mov cwd mov idiv AX. Dandamudi.src (move zero-extended src to dest) » For both move instructions.012CH CX .0CH CL .” Springer-Verlag. 18 . dest has to be a register » The src operand can be in a register or memory – If src is 8-bits. AL := -95D . 1998. Dandamudi Arithmetic: Page 36 To be used with S. Dandamudi Arithmetic: Page 35 To be used with S. dest has to be either a 16 bit or 32 bit register – If src is 16-bits. “Introduction to Assembly Language Programming. AH = FFH .Arithmetic Instructions (cont’d) ∗ Some additional related instructions » Sign extension cwde converts word to doubleword (extends AX into EAX) » Two move instructions movsx dest. 1998. AX := -5147D .0EBE5 CX.

Application Examples • PutInt8 procedure ∗ To display a number. read each digit character » Convert to its numeric equivalent » Multiply the running total by 10 and add this digit Input digit Initial value ‘1’ ‘5’ ‘8’ 1998 Numeric value (N) -1 5 8 Number := Number*10 + N 0 0 * 10 + 1 = 1 1 * 10 + 5 = 15 15 * 10 + 8 = 158 Arithmetic: Page 38 © S. 1998. “Introduction to Assembly Language Programming. repeatedly divide it by 10 and display the remainders obtained 108/10 10/10 1/10 quotient 10 1 0 remainder 8 0 1 ∗ To display digits. 1998. Dandamudi To be used with S. Dandamudi Arithmetic: Page 37 To be used with S.” Springer-Verlag.’0’ 1998 © S. Dandamudi. Dandamudi. they must be converted to their character form » This means simply adding the ASCII code for zero (see line 24) line 24: add AH. “Introduction to Assembly Language Programming. Application Examples (cont’d) • GetInt8 procedure ∗ To read a number.” Springer-Verlag. 19 .

. 1998. and div) work on 8-.by setting the carry flag. sub. Dandamudi Arithmetic: Page 40 To be used with S. 1998. Dandamudi. 20 . . or 32-bit operands • Arithmetic on larger operands require multiword arithmetic software routines • Addition/subtraction ∗ These two operations are straightforward to extend to larger operand sizes ∗ Need to use adc/sbb versions to include the carry generated by the previous group of bits ∗ Example addition on the next slide 1998 © S.Adds two 64-bit numbers in EBX:EAX and EDX:ECX. Dandamudi Arithmetic: Page 39 To be used with S.ECX adc EBX.Overflow/underflow conditions are indicated .” Springer-Verlag. .” Springer-Verlag.-----------------------------------------------. .EDX ret add64 ENDP 1998 © S. “Introduction to Assembly Language Programming.Multiword Arithmetic • Arithmetic operations (add.Other registers are not disturbed.------------------------------------------------ add64 PROC add EAX. mul. “Introduction to Assembly Language Programming. Dandamudi.The result is returned in EBX:EAX. 16-. Multiword Arithmetic (cont’d) .

count := 64 A := multiplier.” Springer-Verlag. “Introduction to Assembly Language Programming. 1998. Multiword Arithmetic (cont’d) ∗ Longhand multiplication Final 128-bit result in P:A P := 0. Dandamudi. 21 . 1998.” Springer-Verlag. Dandamudi Arithmetic: Page 41 To be used with S. Dandamudi.Multiword Arithmetic (cont’d) • Multiplication ∗ We consider two algorithms » Longhand multiplication – Uses the method that we are familiar with – Needs addition operations only – Examines each bit in the multiplier and adds the multiplicand if the multiplier bit is 1 Appropriate shifting is required » Using the mul instruction – Chops the operand into 32-bit chunks and applies mul instruction – Similar to the addition example seen before  1998 © S. “Introduction to Assembly Language Programming. Dandamudi Arithmetic: Page 42 To be used with S. B := multiplicand while (count > 0) if (LSB of A = 1) then P := P+B CF := carry generated by P+B else CF := 0 end if shift right CF:P:A by one bit position count := count-1 end while 1998 © S.

“Introduction to Assembly Language Programming. 1998. 22 .” Springer-Verlag. Dandamudi. Dandamudi. “Introduction to Assembly Language Programming.Multiword Arithmetic (cont’d) ∗ Using the mul instruction » A 64-bit number is treated as two 32-bit numbers – A is considered as consisting of A1A0 (similarly B) – Left shift operation replaces zeros on the right temp := A0 × B0 result := temp temp := A1 × B0 temp := left shift temp by 32 bits result := result + temp temp := A0 × B1 temp := left shift temp by 32 bits result := result + temp temp := A1 × B1 temp := left shift temp by 32 bits result := result + temp Arithmetic: Page 43 1998 © S. we need an additional n+1 bit register P ∗ Core part of the algorithm » Test the sign of P » if P is negative – left shift P:A by one bit position – P := P+B » else – left shift P:A by one bit position – P := P−B 1998 © S. Dandamudi Arithmetic: Page 44 To be used with S.” Springer-Verlag. 1998. Multiword Arithmetic (cont’d) • Division ∗ To implement n-bit division (A by B). Dandamudi To be used with S.

“Introduction to Assembly Language Programming. Dandamudi.” Springer-Verlag. Dandamudi Arithmetic: Page 46 To be used with S.” Springer-Verlag. 1998. Dandamudi. “Introduction to Assembly Language Programming. Performance: Multiword Multiplication • Longhand version versus mul version ∗ To multiply 264−1 × 264−1 20 Execution time (seconds) 15 hm orit alg 10 5 n mul instructio 0 0 100 200 300 400 500 600 700 Number of calls (in thousands) 1998 © S. count := 64 A := dividend B := divisor while (count > 0) if (P is negative) then shift left P:A by 1 bit position P := P+B else shift left P:A by 1 bit position P := P-B end if 1998 A = quotient. 23 .Multiword Arithmetic (cont’d) Division Algorithm P := 0. 1998. Dandamudi To be used with S. P = remainder if (P is negative) then set low-order bit of A to 0 else set low-order bit of A to 1 end if count := count-1 end while if (P is negative) P := P+B end if Arithmetic: Page 45 © S.

AX AX.Performance: Multiword Multiply (cont’d) • Using add versus mul instruction ∗ Certain special cases of multiplication can be done by a series additions (e.AH BX.” Springer-Verlag.4 0.2 Execution time (seconds) m nst ul i ruc tion 0. Dandamudi. . “Introduction to Assembly Language Programming.0 0 100 200 300 400 on structi add in 500 600 700 Number of calls (in thousands) 1998 © S. power of 2 by shift operations) ∗ Example: Multiplication by 10 ∗ Instead of using mul instruction. .AX AX. . Performance: Multiword Multiply (cont’d) • Multiplication of 232−1 by 10 2.g. Dandamudi To be used with S. 24 . AH BX AX AX AX AX := := := := := := 0 x 2x 4x 5x 10x Arithmetic: Page 47 © S.AX . 1998.AX AX. we can multiply by 10 using add instructions as follows (performs AL × 10): sub mov add add add add 1998 AH.6 1. .BX AX. 1998.8 0. Dandamudi. . Dandamudi Arithmetic: Page 48 To be used with S. “Introduction to Assembly Language Programming.0 1.” Springer-Verlag.

Sign up to vote on this title
UsefulNot useful