Presentation is loading. Please wait.

Presentation is loading. Please wait.

Division instruction.

Similar presentations


Presentation on theme: "Division instruction."— Presentation transcript:

1 Division instruction

2 DIV Instruction The DIV (unsigned divide) instruction performs 8 bit, 16 bit , and 32 bit division on unsigned integers. Div r/m8 Div r/m16 Div r/m32 The following illustration shows EDX:EAX as the default dividend when a 32- bit divisor is used. EDX EAX = eax quotient r/m32 edx remainder Dividend Divisor Quotient Reminder AX r/m8 AL AH DX:AX r/m16 DX EDX:EAX r/m32 EAX EDX

3 Example 1: The following instruction perform 8-bit unsigned division (83h/2), producing a quotient of 41h and a remainder of 1. mov ax, 0083h mov bl, 2 div bl Example 2: The following instruction perform 16-bit unsigned division (8003h/100h), producing a quotient of 80h and a remainder of 3. DX contains the high pat of the dividend, so it must be cleared before the div instruction executes. mov dx, 0 mov ax, 8003h mov cx, 100h div cx

4 Signed Integer Division
CBW, CWD, CDQ Instructions Before discussing signed integer division, we need to look at three instructions that perform integer signed extension. The CBW instruction extends the sign bit of AL into the ah register. This preserves the number’s sign .data ByteVal sbyte ; 9Bh .code mov al, ByteVal ; AL = 9Bh cbw ; AX = FF9Bh In order words, 9Bh and FF9BH both equal -65. The only difference between the two is their storage size

5 The CWD (convert word to doubleword) instruction extends the sign bit of AX into the DX register
.data WordVal sword ; FF9Bh .code mov ax, WordVal ; AX = FF9Bh cwd ; DX:AX = FFFFFF9Bh The CDQ (convert doubleword to quadword) instruction extends the sign bit of EAX into the EDX register DwordVal sword ; FFFFF9Bh mov eax, DwordVal cdq ; EDX:EAX = FFFFFFFFFFFFF9Bh

6 The IDIV instruction The IDIV (signed divide instruction) performs signed integer division, using the same operands as the DIV instruction. When doing 8-bit division, you must sign-extend the dividend into AH before IDIV executes, (The CBW instruction can be used). Example: We divide -48 by 5. After IDIV executes, the quotient in AL is -9 and the remainder in AH is -3. .data ByteVal sbyte -48 .code mov al, ByteVal ; dividend cbw ; AL into AH mov bl, 5 ; Divisor idiv bl ; AL = -9, AH = -3

7 Example: Divide by 256 Similarly, 16-bit division requires that AX to be signed extended into DX .data WordVal sword -5000 .code mov ax, WordVal ; Dividend, low cwd ; extend AX into DX mov bx, ; divisor idiv bx ; quotient AX = -19 ; remainder DX = -136

8 mov eax, dwordVal ; dividend, low cdq ; extend EAX into EDX
Example: Divide by 256 Similarly, 32-bit division requires that EAX be signed extended into EDX. .data dwordVal SWORD .code mov eax, dwordVal ; dividend, low cdq ; extend EAX into EDX mov ebx, ; divisor idiv ebx ; quotient EAX = -195 ; remainder EDX = -80 For both DIV and IDIV, all of the arithmetic status flags are undefined after the operations


Download ppt "Division instruction."

Similar presentations


Ads by Google