100 likes | 260 Vues
The DIV instruction performs unsigned division on 8-bit, 16-bit, and 32-bit integers, utilizing the EDX:EAX registers for the dividend. Examples illustrate how to divide unsigned integers, showcasing results with specific instructions. The discussion also covers signed integer division with the IDIV instruction, which requires sign extension of the dividend. Key instructions such as CBW, CWD, and CDQ facilitate this process for respective sizes. Understanding flags and the importance of clearing registers before execution is critical for correct operations.
E N D
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
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
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 -101 ; 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
The CWD (convert word to doubleword) instruction extends the sign bit of AX into the DX register • .data • WordVal sword -101 ; 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 • .data • DwordVal sword -101 ; FFFFF9Bh • .code • mov eax, DwordVal • cdq ; EDX:EAX = FFFFFFFFFFFFF9Bh
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
Example: Divide -5000 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, 256 ; divisor • idiv bx ; quotient AX = -19 • ; remainder DX = -136
Example: Divide -50000 by 256 • Similarly, 32-bit division requires that EAX be signed extended into EDX. • .data • dwordVal SWORD -50000 • .code • mov eax, dwordVal ; dividend, low • cdq ; extend EAX into EDX • mov ebx, 256 ; 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