News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

New code problem

Started by etow, January 22, 2008, 01:28:28 AM

Previous topic - Next topic

etow

Hi

I am having trouble with the following code because it causes a runtime error with no error message:
-----------------------------------------------------------------------------------------------------------------

.686 ; create 32 bit code
    Option CaseMap :none                    ; case sensitive

Include   \masm32\macros\macros.asm       ; MASM support macros

  ; -----------------------------------------------------------------
  ; include files that have MASM format prototypes for function calls
  ; -----------------------------------------------------------------
Include   \masm32\include\masm32rt.inc

  ; ------------------------------------------------
  ; Library files that have definitions for function
  ; exports and tested reliable prebuilt code.
  ; ------------------------------------------------
IncludeLib   \masm32\lib\msvcrt.lib

.Const
   Number = 3
   Divisor = 2

.Code

start:
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
   Call Main
   inkey        ; pause the screen while waiting for user to press any key
                ; to continue
    exit
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Main Proc
   Local Quotient:DWord
    ;--------------------------------------------
    Mov Eax, Number
    Mov Ebx, Divisor
    IDiv Ebx
    Mov Quotient, Eax         ; Quotient = Number / Divisor
    print str$(Quotient)
    print chr$(13, 10)        ; a return key space will be outputted to the screen

    Ret
Main EndP

End start

-----------------------------------------------------------------------------

Please I need much help

Thanks

NightWare

CDQ instruction missing...

raymond

In other words, you are dividing a 64-bit value when the stated divisor is a 32-bit value (EBX in this case). The 64-bit value is expected to be in the EDX:EAX registers, with the lower 32 bits in the EAX register and the higher 32 bits in the EDX register. The quotient will be returned in the EAX register while the remainder will be returned in the EDX register.

Furthermore, if the quotient exceeds 32 bits (and cannot fit into the EAX register), an internal fault is detected and your program gets terminated without any error message (the same as if you try to divide by 0)!!!! :(

When you use the IDIV instruction, you intend it as a signed division. With the DIV instruction, an unsigned division would be performed. When the number you want to divide does not exceed 32 bits, the EAX register should be sign extended into the EDX register for signed divisions (with the CDQ instruction), while the EDX register should be rezeroed before performing unsigned divisions.

If the number you intend to divide exceeds 32 bits (such as after multiplying two "large" integers), the content of EDX must be smaller than the divisor; otherwise the program crashes.

Have fun
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com