News:

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

New code trouble

Started by etow, January 23, 2008, 02:25:56 PM

Previous topic - Next topic

etow

hi

I have trouble with the following code below:

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

.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

.Code

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

Main Proc Uses Ecx
   Local Number:DWord
   Local Remainder:DWord
   Local Quotient:DWord
   Local Factor:DWord
   Local PrimeFactor:SDWord
   Local WantContinue:DWord
   ; Number is the integer to be factored
   ; PrimeFactor is the factored primes of the user's integer
   ; WantContinue is the sentinel value of the first while loop   
                ;--------------------------------------------

    .While ((WantContinue >= 0) && (WantContinue < -1))
       print chr$(13, 10)
       Mov Number, sval(input("Enter an integer value for Number : "))
       Mov Factor, 2
       Mov Ecx, Number
       .While Factor <= Ecx
            Mov Eax, Number
            Mov Ebx, Factor
            Cdq
            IDiv Ebx
            Mov Quotient, Eax   ; Quotient = Number / Factor

            Mov Eax, Number
            Cdq
            Mov Ebx, Factor
            IDiv Ebx
            Mov Remainder, Edx  ; Quotient = Number mod Factor

          .If (Remainder == 0)
             Mov PrimeFactor, cat$(PrimeFactor, " * ", str$(Factor))
             Mov Quotient, Ecx
             Mov Eax, Number
               Mov Ebx, Factor
               Cdq
               IDiv Ebx
               Mov Quotient, Eax   ; Quotient = Number / Factor

               Mov Eax, Number
               Cdq
               Mov Ebx, Factor
               IDiv Ebx
               Mov Remainder, Edx  ; Quotient = Number mod Factor
          .ElseIf
              Inc Factor   ; Factor = Factor + 1
          .EndIf
       .EndW
       print str$(Number)
       print chr$(" = ")
       print str$(PrimeFactor)  ; output the prime factorization of the number
       Mov WantContinue, sval(input("Enter -1 to quit program : "))
    .EndW
    print chr$(13, 10)        ; a return key space will be outputted to the screen

  Ret
Main EndP

End start

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

The code above gives me a run time error.

I am not sure what is going on.

Please help

Thanks

ragdog

hi

try this


.686
.model flat, stdcall  ;32 bit memory model
option casemap :none
include windows.inc

ossama

i think it called "compile time" error, not run time error :bg

ToutEnMasm


There isn't many libraries in use.Masm32.lib for example,kernel32.lib..

RuiLoureiro

Quote from: etow on January 23, 2008, 02:25:56 PM

            Mov     Eax, Number
            Cdq
            Mov     Ebx, Factor
            IDiv     Ebx
            Mov     Remainder, Edx               ; Quotient = Number mod Factor

etow,
                you dont need to divide the Number again to get the Remainder. You need only

               Mov     Quotient, Eax                ; Quotient = Number / Factor
next
               Mov     Remainder, Edx              ; Quotient = Number mod Factor
one time.
                you need to give an initial value to the variable WantContinue before  .While.
RuiLoureiro


etow

.686 ; create 32 bit code

.Model flat, StdCall  ;32 bit memory model
Option CaseMap :none  ; case sensitive

Include   \masm32\include\windows.inc
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

.Code

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

Main Proc Uses Ecx
   Local Number:DWord
   Local Remainder:DWord
   Local Quotient:DWord
   Local Factor:DWord
   Local PrimeFactor:DWord
   Local WantContinue:DWord
   ; Number is the integer to be factored
   ; PrimeFactor is the string containing the factored primes of the user's integer
   ; WantContinue is the sentinel value of the first while loop
    ;-----------------------------------------------------------

    Mov WantContinue, 0
    Mov PrimeFactor, " "

    .While ((WantContinue >= 0) && (WantContinue < -1))
       print chr$(13, 10)
       Mov Number, sval(input("Enter an integer value for Number : "))
       Mov Factor, 2
       Mov Ecx, Number
       .If (Ecx >= 2)
         .While Factor <= Ecx  ; Factor <= (Number = Ecx)
             Mov Eax, Number
             Mov Ebx, Factor
             Cdq
             IDiv Ebx
             Mov Quotient, Eax   ; Quotient = Number / Factor

             Mov Eax, Number
             Cdq
             Mov Ebx, Factor
             IDiv Ebx
             Mov Remainder, Edx  ; Remainder = Number mod Factor

          .If (Remainder == 0)
             Mov PrimeFactor, cat$(str$(PrimeFactor), chr$(" * "), str$(Factor))
             Mov Quotient, Ecx   ; Quotient = Number = Ecx
             Mov Eax, Number
               Mov Ebx, Factor
               Cdq
               IDiv Ebx
               Mov Quotient, Eax   ; Quotient = Number / Factor

               Mov Eax, Number
               Cdq
               Mov Ebx, Factor
               IDiv Ebx
               Mov Remainder, Edx  ; Remainder = Number mod Factor
          .ElseIf
              Inc Factor   ; Factor = Factor + 1
          .EndIf
        .EndW
         print str$(Ecx)
         print chr$(" = ")
         print str$(PrimeFactor)  ; output the prime factorization of the number
         print chr$(13, 10)
         Mov WantContinue, sval(input("Enter -1 to quit program : "))
         print chr$(13, 10)
       .ElseIf (Number == 1)
                print str$(Number)
                print chr$(" = ")
                print str$(Number)
                print chr$(13, 10)
                Mov WantContinue, sval(input("Enter -1 to quit program : "))
                print chr$(13, 10)
       .ElseIf (Number <= 0)
           print chr$(13, 10)
           print chr$("Invalid integer entered!")
           print chr$(13, 10, 13, 10)
           Mov WantContinue, sval(input("Enter -1 to quit program : "))
           print chr$(13, 10)
       .EndIf
    .EndW
    print chr$(13, 10)        ; a return key space will be outputted to the screen

  Ret
Main EndP

End start

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

The above code does not work.

For example,
Here is how the code should work,
If Number = 30,  The PrimeFactor will display -->  30 = 2 * 3 * 5  on the screen

raymond

You did not mention what result your code was returning. I would guess that it is something such as:
2*3*5*6*10*15*30

Your code keeps on dividing Number by an ever increasing divisor starting at 2. And, whenever the remainder is 0, you keep on concatenating the value of the divisor, thus listing all the divisors of Number.

When developing an algo for any function, it's always a good idea to either run your code in a debugger and watch if your results are as expected at each loop, or do it by hand if the algo is not too complicated. If results don't come out as they should, you then modify the code as required.

After you modify your code such that it outputs what you expect with 30, try it with other input values such as 72.

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

etow

Hi Raymond and everyone else.

72 would be displayed as by PrimeFactor as  " 72 = 2* 2 * 2 * 3 * 3 "
PrimeFactor is a string value.  I am having trouble displaying the string value.

Where can I get a very good debugger for MASM32 version 9?
I have MASM32 Version 9 Assembler and I have Easy Code Version 1.0.1.0.0008 for IDE.

etow

I need a best free debugger for MASM32 version 9 that will also work with Easy Code that I mentioned above.


etow


raymond

Quote from: etow on January 24, 2008, 12:48:02 PM
I need a best free debugger for MASM32 version 9 that will also work with Easy Code that I mentioned above.

Get ollydbg. You will not get anything better for free. Get it from:

http://www.ollydbg.de/download.htm
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

etow

Main Proc
  Local FactorString:Dword
  Local Factor:Dword
  ; FactorString is a string to store the accumulated characters to build up the string
  ; Factor is a number

  Mov FactorString, "1"
  Mov Factor, 2

  .while (Factor <= 20)
      Mov FactorString, cat$(str$(FactorString), chr$(" * "), str$(Factor))                     
      add Factor, 1
  .endw

  print str$(FactorString)   ; print out the accumulated string from the while loop

ret
Main EndP
---------------------------------------------------------------------
For the code above, is this the correct way to accumulate a string by building it up in a while loop and later printing it out?  Also, did I declare the variable for a string to accumulate correctly in the above code?

Please help.

Thanks




jj2007

Quote from: etow on January 25, 2008, 12:47:10 PM
Main Proc
  Local FactorString:Dword
  Local Factor:Dword
..
Main EndP
---------------------------------------------------------------------
For the code above, is this the correct way to accumulate a string by building it up in a while loop and later printing it out?

You need to allocate a buffer, and use a pointer to that buffer, see below.

Main Proc
  Local FactorString:Dword
  Local Factor:Dword
  LOCAL FactorStringBuffer[1024]:BYTE ; max 32000 bytes (??)
  lea eax, FactorStringBuffer
  mov FactorString, eax

etow

.686 ; create 32 bit code

.Model flat, StdCall  ;32 bit memory model
Option CaseMap :none  ; case sensitive

Include   \masm32\include\windows.inc
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

.Code

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

Main Proc Uses Esi
   Local Number:DWord
   Local Remainder:DWord
   Local Quotient:DWord
   Local PrimeFactor:DWord
   Local Factor:DWord
   Local PrimeFactorBuffer[1024]:Byte
   Local primeCounter:DWord
   Local WantContinue:DWord
   ; Number is the integer to be factored
   ; PrimeFactor is the string containing the factored primes of the user's integer
   ; WantContinue is the sentinel value of the first while loop
    ;-----------------------------------------------------------
    Lea Eax, PrimeFactorBuffer
    Mov PrimeFactor, Eax
    Mov PrimeFactor, " "
    Mov WantContinue, 0

    .While ((WantContinue >= 0) && (WantContinue < -1))
       print chr$(13, 10)
       Mov Number, sval(input("Enter an integer value for Number : "))
       Mov Factor, 2
       Mov Esi, Number

       .If (Esi <= SDWord Ptr 1) ; if (Esi <= 1)
           print chr$(13, 10)
           print chr$("Invalid integer entered!", 13, 10)
           print chr$("An integer must be greater than 1!", 13, 10)
           print chr$(13, 10, 13, 10)
           Mov WantContinue, sval(input("Enter -1 to quit program : "))
           print chr$(13, 10)
       .ElseIf (Esi > 1)
           print str$(Esi)
           print chr$(" = ")
           Mov primeCounter, 1

         .While Factor <= Esi  ; Factor <= (Number = Esi)
             Mov Eax, Number
             Mov Ebx, Factor
             Cdq
             Div Ebx
             Mov Quotient, Eax   ; Quotient = Number / Factor

             Mov Eax, Number
             Cdq
             Mov Ebx, Factor
             Div Ebx
             Mov Remainder, Edx  ; Remainder = Number mod Factor

           .If (Remainder == 0)
               .If (primeCounter > 1)
                  print cat$(str$(PrimeFactor), chr$(" * "), str$(Factor))
               .ElseIf (primeCounter == 1)
                Mov Eax, Factor
                Mov PrimeFactor, Eax
                  print str$(PrimeFactor)
               .EndIf

Mov Quotient, Esi   ; Quotient = Number = Esi
             Mov Eax, Number
               Mov Ebx, Factor
               Cdq
               Div Ebx
               Mov Quotient, Eax   ; Quotient = Number / Factor

               Mov Eax, Number
               Cdq
               Mov Ebx, Factor
               Div Ebx
               Mov Remainder, Edx  ; Remainder = Number mod Factor
           .ElseIf (Remainder != 0)
              Inc Factor   ; Factor = Factor + 1
              Inc primeCounter
           .EndIf
           print chr$(13, 10)
         .EndW
         Mov WantContinue, sval(input("Enter -1 to quit program : "))
         print chr$(13, 10)
       .EndIf
    .EndW
    print chr$(13, 10)        ; a return key space will be outputted to the screen

  Ret
Main EndP

End start

--------------------------------------------------------------------------------------------
The code in red above I believe in giving me an infinite loop problem

I get an infinite display loop above
The following are examples of my input.

If I enter 2 for Number I get the following:

2
2
2
2
2
2
2
2
2
.
.
.
.
more 2's displayed to infinity

if I enter 3 for Number I get the following

32 * 3
32 * 3
32 * 3
32 * 3
32 * 3
32 * 3
32 * 3
32 * 3
32 * 3
.
.
.
.
.
.
more "32 * 3" 's displayed to infinity

if I enter 23 for Number
I get empty space displayed to infinity
<blank space>
.
.
.
.
.
.
.
.
<blank space>