0

i wrote a code for this c++ expression in assembly language using 32 bit unsigned operands

If (val2 ==0) then

{val2 = 1}

val1 = (val3 / val2) * (val1 + val3)

but i got error "overflow Integer" the error is at div val2 when i enter the val1 = 600, val2= 0, val3= 1000 any higher number won't work just the smaller number... how to correct this?

https://i.imgur.com/ivcRkVI.png

    INCLUDE Irvine32.inc
.data
val1 BYTE ?
val2 BYTE ?
val3 BYTE ?
prompt BYTE "Please enter 3 numbers: ",0
out1 BYTE "val1 = (val3 / val2) * (val1 + val3) = ",0
.code
main PROC  
   ;;CLEAR ALL REGISTERS
   xor eax,eax
   xor ebx,ebx
   xor ecx,ecx
   xor edx,edx
   ;;DISPLAY PROMPT
   mov edx,offset prompt
   call writestring
   ;;NEWLINW
   mov al,0ah
   call writechar
   ;;READ VAL1 AND STORE
   call readint
   mov val1,al
   ;;READ VAL2 AND CHECK AND STORE
   call readint
   cmp al,0   ;;CHECK IF 0
   jnz skip
   mov val2,1   ;;IF 0, MAKE VAL1 AS 1
   jmp readnext
skip:
   mov val2,al   ;;ELSE STORE SAME VALUE
readnext:
   ;;READ VAL3 AND STORE
   call readint
   mov val3,al
   div val2   ;;CALCULATE VAL3/VAL2
   mov bl,al   ;;SAVE IN BL
   mov al,val3  
   add al,val1   ;;ADD VAL3+VAL1
   mul bl       ;;(VAL3/VAL2)*(VAL3+VAL1)
   ;;DISPLAY MESSAGE
   mov edx,offset out1
   call writestring
   ;;DISPLAY ANSWER
   call writedec
   ;;DUMP REGISTERS
   call dumpregs
call waitmsg       ;;WAIT FOR A KEY PRESS
INVOKE ExitProcess, 0
main ENDP
END main
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847

1 Answers1

1

You are using 32 bit unsigned operands but your variables val1 BYTE ? etc are declared as 8bit only, number above 255 will not fit in them. 32bit binary number obtained from readint should be stored with
mov val1,eax and not mov val1,al.

It is pointless to ;;CLEAR ALL REGISTERS before you move anything to them but be sure to clear EDX before div val2 ;;CALCULATE VAL3/VAL2 because the instruction DIV expects its dividend in 64bit register pair EDX:EAX.

vitsoft
  • 5,515
  • 1
  • 18
  • 31
  • I got your point but how I can make the division in 64bit because i tried but i think i did it wrong – Mohamad Alkousi Mar 28 '21 at 15:14
  • @MohamadAlkousi Actualy it is 32bit division. The bitness of division is controled by the divisor width, which is 32 if you declare your variable `val2` as DWORD. Intel says: Unsigned divide EDX:EAX by r/m32, with result stored in EAX ← Quotient, EDX ← Remainder. – vitsoft Mar 28 '21 at 16:24