Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

masm division overflow

Tags:

x86

assembly

masm

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

like image 425
Help I'm in college Avatar asked Feb 13 '10 00:02

Help I'm in college


1 Answers

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

like image 74
slugster Avatar answered Sep 18 '22 12:09

slugster