Division Instructions
Division Instructions
Statement Purpose:
Objective of this lab is to introduce students with Integer division in x86 assembly language that can be performed as a 32-bit, 16-bit, or 8-bit operation. In many cases, it revolves around EDX and EAX or its subsets (AX, AL). The DIV and IDIV instructions perform unsigned and signed integer division, respectively. The DIV instruction performs unsigned integer division, and IDIV performs signed integer division.
Activity Outcomes:
This lab teaches you the following topics:
- Students will know that DIV and IDIV instructions are very helpful in performing division for unsigned and signed
- They will also learn the way these two differs from each other and the way we can use the suitable
- Students will be able to know that DIV and IDIV have the same operands so that major difference is the signed Unsigned inter
- Sign extension statements like CBW, CWD and CDQ are used and
- Student will be able to divide 64bit number and will get information about the remainder and quotients as well for different size of
Instructor Note:
DIV Instruction
- The DIV (unsigned divide) instruction performs 8-bit, 16-bit, and 32-bit division on unsigned integers. A single operand is supplied (register or memory operand), which is assumed to be the divisor
The IDIV Instruction
The IDIV (signed divide) instruction performs signed integer division, using the same operands as DIV.
- Before executing 8-bit division, the dividend (AX) must be completely sign-extended.
- The remainder always has the same sign as the dividend.
Introduction
Instruction formats for DIV:
- DIV reg/mem8
- DIV reg/mem16
- DIV reg/mem32
Instruction formats for IDIV are same as DIV:
- IDIV reg/mem8
- IDIV reg/mem16
- IDIV reg/mem32
To preserve the sign for 8bit numbers CBW instruction is used before IDIV while CWD for 16Bit and CDQ for 32Bit signed division.
Activities:
Activity 1:
8-bit unsigned division (83h / 2), producing a quotient of 41h and a remainder of 1.
Solution:
Activity 2:
16-bit unsigned division (8003h / 100h), producing a quotient of 80h and a remainder of 3.
Solution:
DX contains the high part of the dividend, so it must be cleared before the DIV instruction executes.
Activity 3:
32-bit unsigned division using a memory operand as the divisor.
Solution:
Activity 4:
8 bit division. Divide -48 by 5.
Solution:
Activity 5:
16-bit division requires AX to be sign-extended into DX. Divide -5000 by 256
Solution:
Activity 6:
32-bit division requires EAX to be sign-extended into EDX. Divide 50,000 by -256
Solution:
Home Activities:
Activity 1:
Solve this equation; var4 = (var1 + var2) * var3 Solution:
; Assume unsigned operands
TITLE Lab Multiplication and Division Instructions
; Author: Ashfaq Hussain Farooqi INCLUDE Irvine32.inc
.data
.code Main proc
mov eax,var1
add eax,var2 ; EAX = var1 + var2
mul var3 ; EAX = EAX * var3 jc TooBig ; check for carry mov var4,eax ; save product
Jump next:
TooBig:
exit
main ENDP END main
Activity 2:
Solve this equation; var4 = (var1 * 5) / (var2 – 3); Solution:
; Assume unsigned operands
TITLE Lab Multiplication and Division Instructions
; Author: Ashfaq Hussain Farooqi INCLUDE Irvine32.inc
.data
.code Main proc
mov eax,var1 ; left side mov ebx,5
mul ebx ; EDX:EAX = product mov ebx,var2 ; right side
sub ebx,3
div ebx ; final division mov var4,eax
exit
main ENDP END main
Activity 3:
Solve this equation; var4 = (var1 * 5) / (var2 – 3); Solution:
Solve this equation; var4 = (var1 * -5) / (-var2 % var3); Solution:
; Assume signed operands
TITLE Lab Multiplication and Division Instructions
; Author: Ashfaq Hussain Farooqi INCLUDE Irvine32.inc
.data
.code Main proc
mov eax,var2 ; begin right side neg eax
cdq; sign-extend dividend idiv var3 ; EDX = remainder
mov ebx, edx ; EBX = right side mov eax,-5 ; begin left side imul var1 ; EDX:EAX = left side idiv ebx ; final division
mov var4,eax ; quotient exit
main ENDP END main
Assignment:
Consider there are two arrays having six indexes. Populate them by taking inputs from the user. Now, populate the third array by dividing the same index values. If the numbers are positive use DIV and if the numbers are signed use IDIV instruction.
Note: Consider numbers as Signed/Unsigned and are 32Bit values.