News:

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

A question on Procedures in goasm

Started by wincry, August 28, 2008, 05:55:44 PM

Previous topic - Next topic

wincry


Hello Everybody,
    i am learning assembly language in goasm.
   i having a problem, i cant create a procedure which will find factorial of
   a given number .I cant understand goasm syntax for creating a procedure.
   I want to create a separate obj file for this and latter link it with main module,
       please also tell me if i made any mistake.
   
My code is :

;My first try to build a procedure

.code
     


  _findfact : push ebp
     
     mov ebp,esp  ;stack frame base pointer setup complete
     
     mov eax,[ebp+8] ; 4 bytes for return address and 4 bytes for old ebp
     
     cmp  eax,1
     
     je  endall

     dec eax

     push eax
     
     call _findfact
     
     pop edx
     
     mul edx     ;result is in eax.edx pair
     jmp endloop

endall:
     mov eax,1
endloop:
      mov esp,ebp
      pop ebp
      ret
             

jorgon

Hi wincry, welcome to the forum.

QuoteI cant understand goasm syntax for creating a procedure

So you want to know how to create a procedure in GoAsm?

Well, on the basis that a procedure is a code sequence, here is a simple procedure:-

MyProcedure:
RET


You would then call this procedure as follows:-

CALL MyProcedure

So here is a complete program which uses this procedure:-


CODE
MyProcedure:
RET
;
START:
CALL MyProcedure
RET


In this code, in fact you have two procedures.  One is called START (which is called by the Windows loader when the program is loaded) and the other is called MyProcedure.  Execution of the code starts at START and then is diverted to MyProcedure.  It then returns from MyProcedure and finally returns to the system, which terminates the program.

Of course this program does nothing.

I understand you want to create a procedure which calculates the factorial of a given number.  If you could post the mathematics to achieve that and explain it to members of the forum, this would help us to give a pointer or two.




Author of the "Go" tools (GoAsm, GoLink, GoRC, GoBug)

wincry



Hello jorgon
   
    thanks for the help,
       i want to ask you some questions,
       
        Q1.  can i put my procedure in a separate file ?
                 
                   I want to use the same procedure in different exe files
                    for example i want to put console mode reading, writing function
                     WriteFile and ReadFile and GetStdHandle in a separate
                     file so i don't have to write it every time.
                     
                        Please show me How this can be done....?
                       
                     
                           
        Q2 . For the problem of finding factorial of a given number
               i already included it with my post ( Procedure _findfact).
                please take some time to see if it is ok?
                 please also tell me if i made any mistake.   
           

jorgon

Q1 - Please clarify - do you want to make an exe file, which calls a procedure in another exe file?  Or do you want to make an exe file which calls a procedure in a dll?  Or maybe you want to make an object file which calls a procedure in another object file?

Q2 - Please provide the maths involved in this and explain how it works.

Author of the "Go" tools (GoAsm, GoLink, GoRC, GoBug)

wincry



Hello jorgon

  sorry, i am completely new to assembly programming(actually learning of my own).
  please let me be very clear with you so you can understand my question.
  because i don't know how to write a dll file in assembly language, so i guess
  i want to create an object file which calls a procedure in another object file.
  (please show me how this can be done). Another option you have given ie.
  to make an exe file, which calls a procedure in another exe file,how this can be done
  please explain it to me.   
 
 
                     I wanted to write an recursive factorial subprogram which will
                      calculate factorial of an given number .I have written a code also,
                      please check it for me.
                     
       
       My factorial subprogram starts below....
       
       
;   definition :         
;      n!=1 if n==0
;       n!=n * (n-1) * (n-2) * .... * 1 if n>0
;

.code
     


_findfact :
     
     push ebp
     
     mov ebp,esp  ;stack frame base pointer setup complete
     
     mov eax,[ebp+8] ; 4 bytes for return address and 4 bytes for old ebp
     
     cmp  eax,1 ; terminating condition
     
     je  >endall

     dec eax

     push eax
     
     call _findfact
     
     pop edx
     
     mul edx  ;result is in eax.edx pair
   
     jmp  >endloop

endall:
       mov eax,1
endloop:
       mov esp,ebp
       pop ebp
       ret
     
             
             I can't create a object file of the above code with goasm , it shows the following error
             
     
   f:\asmfiles>goasm findfact.asm       
      GoAsm.Exe Version 0.56.4k - Copyright Jeremy Gordon 2001/8 - JG@JGnet.co.uk

      Error!
      Line 8 of assembler source file (findfact.asm):-
      Unknown mnemonic, instruction, redefinition or directive:-
      _findfact : push ebp

       OBJ file not made

                                       

jorgon

Quotei guess i want to create an object file which calls a procedure in another object file.
  (please show me how this can be done).

When GoAsm assembles your source file it creates an object file.
When GoAsm assembles another source file it creates another object file.
Now you have two object files.
To enable a call in one of the object files to be made to a function (procedure) in the other object file you need to make sure they are both sent to the linker (instead of just one of them being sent to the linker).



But wincry do you really need to use two object files?  A simple program would usually have just one object file (that is, one source file).  And obviously you can call a function which is somewhere else in the same source file and therefore in the same object file.  It does not need to be in a separate file (see my MyProcedure example).




QuoteAnother option you have given ie. to make an exe file, which calls a procedure in another exe file,how this can be done please explain it to me.

Well you know how an exe can call a function (a procedure) in a dll?  This is because the dll has exported the function.  An ordinary exe can also export a function.  Therefore one exe can call a function in another exe.  How to export a function is explained in the GoAsm manual.



QuoteUnknown mnemonic, instruction, redefinition or directive:-
      _findfact : push ebp

_findfact is a code label.
All code labels must end with a colon.
You have a space between the end of the code label and the colon.
Remove the space and you will be away.



Author of the "Go" tools (GoAsm, GoLink, GoRC, GoBug)

ChrisLeslie

wincry

What you can do, for example, is:
1) Have all the console procs in one console.asm file, compile it with goasm to make an console.obj file.
2) Have all your disk procs in one disk.asm file, compile it with goasm to make an disk.obj file.
3) etc etc for other "packages".
4) Write you main program, which can include invokes or call (whichever your preferred calling method), to, say, main.asm and then compile with goasm to make an main.obj file.
5) Link your main.obj file to all or some of the other procedure .obj files, and to the system win .dll files to make a final executable main.exe file.

eg: enter 'golink /console main.obj console.obj disk.obj kernel32.dll user32.dll'

In this way you have the ability in you main file to invoke any of the win API, or your own procedures (which can also in turn invoke win APIs)

This method is fundamental to good old programming compiling/linking - a way of life that is these days becoming forgotten due to the common useage today of modern OOP programming paradigms.

Regards

Chris


donkey

wincry,

Yes, as Jeremy said GoAsm will assemble 2 separate source files into 2 separate OBJ files. However, it is important to distinguish between two separate sources and one source with multiple files. For example you can keep the code you posted in a file by itself and include it in any other source using #include <path\filename>, this will not assemble into separate obj files and you can call the procedure from anywhere in your code. For example if you created a file called findfact.asm to add that procedure to any of your other projects you would only add the line #include findfact.asm at the top of your source and it would be merged into your source before assembly. For small procedures this is much more practical than DLL's LIB's or other library schemes. Note that if the file contains source code that is to be assembled the file extension must begin with the letter A or goasm will cough.
"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