0

On executing div command, it gives an error: integer overflow. first I want to know how this is integer overflow, then obviously how to correct it? Thank you in advance.

TITLE My First Program (Test.asm)
INCLUDE Irvine32.inc
.data
cr1grade WORD 5
cr2grade WORD 10
cr3grade WORD 4
cr4grade WORD 8
cr5grade WORD 5
totalcredit WORD 16

.code
main PROC
mov ax,cr1grade
add ax,cr2grade
add ax,cr3grade
add ax,cr4grade
add ax,cr5grade     ;total grade points => 32
mov bx,totalcredit  ;total credit hrs => 16
div bx              ;divide grade points by credit hrs =>2

call DumpRegs
exit
main ENDP
END main
tadman
  • 208,517
  • 23
  • 234
  • 262
kestrel
  • 126
  • 1
  • 16
  • Set DX to 0 before the division, e.g. using `xor dx,dx` (xor-ing *any* number with itself results in 0), or the slightly longer `mov dx,0`. – Rudy Velthuis Sep 07 '17 at 19:04
  • Am I getting it right (from the referenced answer ) that in my statement ax is being divided by bx so it stores the quotient in ax and remainder in bx? I'm not using dx here, so why i need to assign a 0 to dx? – kestrel Sep 07 '17 at 19:23
  • dx:ax is the quotient for `div r/m16` – Peter Cordes Sep 07 '17 at 19:24
  • Second thing, if we divide 32/16=>2, there will be no remainder, so what's the point of overflowing? – kestrel Sep 07 '17 at 19:25
  • If DX happens to be, say, 48 (0x0030), then you are dividing DX:AX, or 0x00300020 (3145760) by 0x0010 (16), which results in 0x00030002, which is 196610, and not 2 (and > 0xFFFF (65535), hence the **integer overflow**). Not what you want, right? So set DX to 0, then you are dividing 0x00000020 by 0x0010, and that is indeed 2. – Rudy Velthuis Sep 07 '17 at 19:34
  • @falco: No, DX:AX are considered as a 32 bit value that is divided by a 16 bit value (in this case by the 16 bit value in BX). In your case, the result of the division turns out to be be > 0xFFFF, so you get an integer overflow. If DX = 0, the result is not > 0xFFFF and you don't get an integer overflow. After a DIV, AX contains the quotient and DX the remainder, if there is no overflow. – Rudy Velthuis Sep 07 '17 at 19:40
  • @falco: if before the DIV, DX = 0, AX = 39 and BX = 16, then after the DIV, AX contains 2 (the quotient) and DX contains 7 (the remainder), because 16 * 2 + 7 = 39. – Rudy Velthuis Sep 07 '17 at 19:44
  • I get it, Thanks for the assistance. – kestrel Sep 08 '17 at 14:23
  • So shouldn't we assign values to dx and using it in expressions(as like ax or bx), because if we encounter a situation where we need to divide dx by some value then, in that case, we can't give it 0(b/c data in dx will be lost)? – kestrel Sep 08 '17 at 14:29
  • @falco97: The division destroys the value already in DX. How you use DX before the division is left to your imagination. And if you want to save the value in DX before the division, you can store it in memory, push it on the stack or move it to another register. That is also left to your imagination. – Rudy Velthuis Sep 08 '17 at 15:26

0 Answers0