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).

  • 1
    Can you step through it in the Visual Studio debugger? How far do you get if you step into each instruction? – Greg Hewgill Feb 13 '10 at 00:24
  • Unhandled exception at 0x00401075 in Project.exe: 0xC0000095: Integer overflow. Happens right after the call to div. – Help I'm in college Feb 13 '10 at 00:26
  • If you are dividing by 2 and you're interested in performance you might want to consider using SHR. –  Feb 15 '10 at 05:52
  • @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. – Anderson Green Feb 15 '13 at 03:54
  • 1
    @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: http://msdn.microsoft.com/en-us/library/a3cwf295.aspx – Greg Hewgill Feb 15 '13 at 03:57
  • @GregHewgill Are equivalent instructions available for VS 2010? – Anderson Green Feb 15 '13 at 04:23
  • @AndersonGreen: Certainly, see https://www.google.com/search?q=vs+2010+disassembly – Greg Hewgill Feb 15 '13 at 07:25

3 Answers3

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 :)

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
slugster
  • 49,403
  • 14
  • 95
  • 145
  • 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. – I. J. Kennedy Feb 18 '10 at 18:25
  • Yes, you are right in that respect, but the OP is doing *un*signed division, so i kept my answer simple. – slugster Feb 18 '10 at 22:25
4

Yes, you need to set edx to zero.

The easiest way to do this is:

xor edx, edx
neersighted
  • 150
  • 13
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

ruchir patwa
  • 311
  • 1
  • 5
  • 13