1

I have this really simple code that give me this error when I try to execute it:

Unhandled division by zero at address 004015DD (thread 0024), starting debugger...

But I have set the divisor used on the div instruction to 5:

.686
.xmm
.model flat, C
OPTION CaseMap:None

include ../masm32/libs/windows.inc
include ../masm32/libs/kernel32.inc
include ../masm32/libs/user32.inc
include ../masm32/libs/msvcrt.inc
include ../masm32/libs/masm32.inc

EXTERN printf:PROC
EXTERN scanf:PROC

.code

main PROC
    push ebp
    mov ebp,esp

    mov eax,10
    mov edx,5
    div edx

    push eax
    push offset str_input_format
    call printf
    
    pop ebp
main ENDP
end

Why I'm getting this error? The divisor should be 5 not 0..

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Z3R0
  • 1,011
  • 10
  • 19
  • 1
    The division you are doing divides a 64-bit value (In EDX:EAX)divided by a 32 bit value, and in this case does EDX:EAX divided by a value (EDX in your case). The result is overflowing causing division by 0. You actually did 0x000000050000000A divided by 5. You will need to set EDX to 0 and use a register other than EDX (and EAX) as the divisor – Michael Petch Aug 07 '22 at 18:01
  • For 32-bit operand `div` instruction divides `edx:eax` pair by operand and stores two result (quotient and reminder) into `eax` and `edx`. You got division overflow, quotient can't fit into `eax` – dimich Aug 07 '22 at 18:04
  • @dimich Thank you for your answer! I though it was using eax only as dividend when given a 32bit divisor – Z3R0 Aug 08 '22 at 16:08

0 Answers0