I want to create a program that can calculate division problems. The problem is that my program crashed when I tried dividing by a negative number, even though I created a branch called "DivNeg" that was supposed to prevent it from crashing. Does anyone have ideas on how to fix this?
Here is my assembly code
.386
.model flat
public _Divide
.code
_Divide proc
mov eax, [esp + 4] ; First address ; this is the dividend
mov ebx, [esp + 8] ; Second address ; this is the divisor
cmp ebx, 0
je DivZero
cmp ebx, 0
jnae DivNeg
cdq
idiv ebx ; To divide by eax by ebx
mov ebx, [esp + 12] ; Third address; this is the remainder
jmp Done1
DivZero:
mov eax,-1 ; If user divides by zero, this will set the result to negative 1
mov edx, 0 ; If user divides by zero, this will set the remainder to 0
mov ebx,[esp +12] ; Needed for the remainder if divided by 0
cmp ebx, 0
je Done2
Done1:
mov [ebx], edx
je Done1
DivNeg:
cmp ebx, 0
jge Done2
mov eax, -1
neg eax
je DivNeg
Done2:
ret
_Divide endp
end