News:

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

Have trouble with this code

Started by etow, January 23, 2008, 02:49:12 AM

Previous topic - Next topic

etow

Hi

I am having 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

.Const
Two = 2

.Code

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

Main Proc
   Local Number:DWord
   Local numAmt:DWord
   ; Number is the number being mulitipled
                ; numAmt is the number of times it is multiplied
                ;--------------------------------------------

    Mov numAmt, sval(input("Enter a positive integer for the number of times 2 to be multiplied by itself : "))

    Mov Ebx, Two
    Mov Ecx, 1      ; while loop counter

    print chr$(13, 10)        ; a return key space will be outputted to the screen
    print chr$(" ")           ; a space will be outputted to the screen
    print str$(Two)           ; 2 will be outputted to the screen

   .While Ecx <= numAmt
     Mov Eax, Ebx
     IMul Number
     Mov Ebx, Eax
     print chr$(" * ")       ; * 2 will be outputted to the screen as many times according to user
     print str$(Two)
     Add Ecx, 1
    .EndW

    print chr$(" = ")         ; =  will be outputted to the screen
    print str$(Ebx), 13, 10   ; the result of the multiplication will be
                              ; outputted to the screen
    print chr$(13, 10)        ; a return key space will be outputted to the screen

    Ret
Main EndP

End start

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

I can not get the while loop to work above because
if I enter 1 it displays "2 = 2" and other numbers the same result is displayed.
It supposed to say 2 * 2 = 4 when 1 is entered for input by user
and 2 * 2 * 2 * 2 = 16 when 3 is entered for input by user

I need much help.

Thanks

jdoe

etow,

Don't give up   ::)

1) You forgot to set the initial value of Number
2) ECX is a register that needs to be preserved and in the loop ECX get lost after the "print" call

For registers preservation...

Main Proc uses ebx esi


Initial value of Number and ECX replacement by ESI

   Mov Number, Two
   Mov Ebx, Two
   Mov Esi, 1      ; while loop counter


The loop

.While Esi <= numAmt
    ...
    Add Esi, 1
.EndW



etow

Hi here is my final code in zip file

For product of 2's by mulitplication:



[attachment deleted by admin]

etow

Here is the the algorithm for product of twos:



     maxNumberAmount = (positive integer >= 1)  ; number of times the while loop executes
     numberMultiply = 2  ; the accumulation of the product of twos
     counter = 1     ; number of times to multiply numberMultiply by

     print(" 2 ")             
     Do While (counter <= maxNumberAmount)
        numberMultiply = numberMultiply * counter
        print(" * 2 ")
        counter = counter + 1           
     Loop

     print (" = ")
     print(" 2^")
     print(counter)
     print(" = ")
     print(numberMultiply)


etow

Hi

I modify my final code to do base two to powers from 0 to 29 in the zip file:

[attachment deleted by admin]