masm division overflow
Asked Answered
M

3

11

I'm trying divide two numbers in assembly. I'm working out of the Irvine assembly for intel computers book and I can't make division work for the life of me.

Here's my code

.code
main PROC
    call division
    exit
main ENDP

division PROC
    mov eax, 4
    mov ebx, 2
    div ebx
    call WriteDec
    ret
divison ENDP

END main

Where WriteDec should write whatever number is in the eax register (should be set to the quotient after the division call). Instead everytime I run it visual studio crashes (the program does compile however).

Maiden answered 13/2, 2010 at 0:21 Comment(7)
Can you step through it in the Visual Studio debugger? How far do you get if you step into each instruction?Wideeyed
Unhandled exception at 0x00401075 in Project.exe: 0xC0000095: Integer overflow. Happens right after the call to div.Oracle
If you are dividing by 2 and you're interested in performance you might want to consider using SHR.Zayas
@GregHewgill How can you step through each assembly instruction using Visual Studio debugger? I still haven't figured this out after a couple of Google searches.Hornwort
@AndersonGreen: They keep changing the name of the menu item and the shortcut key to get there. Look for something like "View Disassembly". There is a view with a register window, a memory window, stack viewer, disassembly, etc. For VS 2012 here are the instructions: msdn.microsoft.com/en-us/library/a3cwf295.aspxWideeyed
@GregHewgill Are equivalent instructions available for VS 2010?Hornwort
@AndersonGreen: Certainly, see google.com/search?q=vs+2010+disassemblyWideeyed
C
17

You need to zero extend your EDX register before doing the division:

mov eax, 4
mov ebx, 2
xor edx, edx          ;set edx to zero
div ebx
call WriteDec

the ;set edx to zero is a comment in MASM. I don't know if it'll work if you are using inline assembly in C, so don't copy it if you are :)

Consols answered 13/2, 2010 at 0:28 Comment(2)
Instead of zeroing EDX explicity, one would typically use the CDQ instruction to sign extend EAX into EDX. If EAX is non-negative, EDX is filled with zeros; if EAX is negative, EDX is filled with one-bits.Clatter
Yes, you are right in that respect, but the OP is doing unsigned division, so i kept my answer simple.Consols
N
4

Yes, you need to set edx to zero.

The easiest way to do this is:

xor edx, edx
Nunes answered 22/5, 2010 at 1:20 Comment(0)
J
0

i think the above mentioned reason is correct because when u divide eax by ebx both are 32 bit numbers but the dividend needs to be 64 bit divisor is 32 bit and so it considers edx as the msb...u may make edx 0 or instead of using 3bx use only bx..in that way u will divide a 32bit number by a 16 bit number

Jolenejolenta answered 1/3, 2010 at 12:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.