Have trouble with code. I compile it and I receive errors

Started by etow, January 21, 2008, 12:22:51 PM

Previous topic - Next topic

etow

Hi

I have trouble with the following MASM32 code:

----------------------------------------------------------------------------------------
.586 ; create 32 bit code
    .model flat, stdcall                    ; 32 bit memory model
    option casemap :none                    ; case sensitive

    include \masm32\include\windows.inc     ; always first
    include \masm32\macros\macros.asm       ; MASM support macros

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

  ; ------------------------------------------------
  ; Library files that have definitions for function
  ; exports and tested reliable prebuilt code.
  ; ------------------------------------------------
    includelib \masm32\lib\masm32.lib
    includelib \masm32\lib\gdi32.lib
    includelib \masm32\lib\user32.lib
    includelib \masm32\lib\kernel32.lib


.Code

start:
   Call Main
                inkey
                exit

Main Proc
   Local counter:DWord

    Mov counter, 0
   .While counter < 20
     Mul counter, 2
    .EndW

    print str$(counter), 13, 10
    Ret
Main EndP

End start


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

The debugger error messages are the following:

============== proj1 - Debug ==============

Assembling: Module1
Module1.asm(2) : warning A4011: multiple .MODEL directives found : .MODEL ignored
------------------------------------------
WARNING Duplicate include file windows.inc
------------------------------------------
-----------------------------------------
WARNING Duplicate include file user32.inc
-----------------------------------------
-----------------------------------------
WARNING Duplicate include file kernel32.inc
-----------------------------------------
Module1.asm(38) : error A2008: syntax error : ,

Errors ocurred.

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

How do I fix these errors so that I don't get any of these errors in MASM32 version 9 Assembler?

Please help.

Thanks

evlncrn8

you included the files somewhere else too?... code looks ok, cant see a reason for the errors...

etow

Hi evlncrn8,

Yes I am using EasyCode to setup to use to compile, debug and run it.

Tedd

Just comment out the lines it's whining about and it should be fine.
No snowflake in an avalanche feels responsible.

etow

There is one error message left which I don't understand

============== proj1 - Debug ==============

Assembling: Module1
Module1.asm(39) : error A2008: syntax error : ,

Errors ocurred.

etow

Hi

I updated my code below:

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

.586 ; create 32 bit code
; .Model flat, StdCall                    ; 32 bit memory model
    option casemap :none                    ; case sensitive

; Include   \masm32\include\windows.inc     ; always first
    include \masm32\macros\macros.asm       ; MASM support macros

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

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

.Code

start:
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
   Call Main
   inkey
    exit
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Main Proc
   Local counter:DWord
;---------------------------------------
    Mov counter, 2
    print str$(counter)

   .While counter < 20
     Mul counter, 2
     print chr$(" * ")
     print chr$("2")
    .EndW

    print chr$(" = ")
    print str$(counter), 13, 10
    Ret
Main EndP

End start

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

Only one error message left to solve below:

============== proj1 - Debug ==============

Assembling: Module1
Module1.asm(41) : error A2008: syntax error : ,

Errors ocurred.

etow

Almost everything is solved except for the following error messages:

============== proj1 - Debug ==============

Linking...
masm32.lib(wait_key.obj) : error LNK2001: unresolved external symbol __imp___getch
masm32.lib(wait_key.obj) : error LNK2001: unresolved external symbol __imp___kbhit
proj1.exe : fatal error LNK1120: 2 unresolved externals

Errors ocurred.

etow

Hi I finally got the errors solved.

Here is my updated code below:

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

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

; Include   \masm32\include\windows.inc     ; always first
    include \masm32\macros\macros.asm       ; MASM support macros

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

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

.Const
Two = 2

.Code

start:
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
   Call Main
                inkey
                exit
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Main Proc
   Local Number:DWord
   Local Counter:DWord
;---------------------------------------

    Mov Number, 2
    Mov Counter, 1

    print chr$(13, 10)
    print str$(Number)

   .While Counter <= 2
     Mul Number
     print chr$(" * ")
     print str$(Two)
     Add Counter, 1
                .EndW

    print chr$(" = ")
    print str$(Number), 13, 10
    print chr$(13, 10)

    Ret
Main EndP

End start

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

The only problem is that this program does not multiply correctly.

It outputs  2*2*2 = 2  instead of  2*2*2 = 8 .

I am not sure what is the problem?

Please help me.

Thanks


donkey

Well, not that I know much about (or even want to) the gibberish of the high level lib stuff but since you do have the occasional assembly language instruction in your "crippled C with registers" application you might think about setting the EAX register before you multiply it by "Number". Since the MUL instruction multiplies the contents of the EAX register by the operand and you have not defined it explicitly before the loop your output would stand a good chance of being incorrect. In the code you posted if EAX was 1 going into the routine 2*2*2 would end up with a result of 4. Since I have no idea what the return value of the "Print" function is I will just assume that it is either TRUE or FALSE for success or failure, in that case the value in EAX would be 1 (TRUE) going into the loop.

Donkey
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

jdoe

etow,

If you include masm32rt.inc, you can remove all the other includes.
In your case, I think adding "includelib \masm32\lib\msvcrt.lib" could be enough because you used a macro or a function from masm32 that require the C runtime.

About the multiplication, in addition to what donkey said, look at this quick fix...


    mov ebx, Two

    .While Counter <= 2
        mov eax, ebx
        Mul Number
        mov ebx, eax
        print chr$(" * ")
        print str$(Two)
        Add Counter, 1
    .EndW

    print chr$(" = ")
    print str$(ebx), 13, 10
    print chr$(13, 10)



etow


etow

Hi Jdoe,

I have a question for you.

In the code you modified for me to get it working for me.
I don't understand why Number is used between the Mov commands since you used registers to accomplish it.

   .While Counter <= 4
     Mov Eax, Ebx
     Mul Number             
     Mov Ebx, Eax
     print chr$(" * ")       
     print str$(Two)
     Add Counter, 1
               .EndW

jdoe


Look at the comments. I hope they will help you. Don't forget that some registers are free to use in procedures (EAX, ECX, EDX) and some must be preserved (EBX, EDI, ESI, ESP, EBP). In your loop, you call print and when it returns, you can't expect EAX, ECX, EDX to be the same. Your error was about EAX because MUL use it.

Quote
mov ebx, Two        ; this is the initial value that EAX gets in the loop

.While Counter <= 4
    Mov Eax, Ebx        ; put back the cumulative result in EAX
    Mul Number         ; EAX get multiplied by Number   ( EAX = EAX * Number )
    Mov Ebx, Eax       ; EBX is used to save the cumulative result
    print chr$(" * ")   ; after print returns EAX don't hold the cumulative result anymore, this is why EBX is used
    print str$(Two)
    Add Counter, 1
.EndW


etow

Hi Jdoe,

This makes sense now.  Thank you very much for your detailed explanation.