The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: victorvmpm on April 27, 2010, 09:51:51 PM

Title: Help Generating an Executable w/out visual
Post by: victorvmpm on April 27, 2010, 09:51:51 PM
Hi everyone, I have a question about the project. You'll see i'm building a compiler for my own invented language for my college class and I decided to make my intermediate code masm32 assembler code. My question is:

How can i link the assembler generated code to its compiller without intermediary programs to generate the executable?

regards,
Victor Pereira
Title: Re: Help Generating an Executable w/out visual
Post by: dedndave on April 27, 2010, 10:13:13 PM
 :dazzled: ???

could you re-phrase the question ?

ahhhh - i think i understand
we use MASM - microsoft macro-assembler
that program accepts assembly language text files and generates object modules
we then use LINK to convert object modules into executables
Title: Re: Help Generating an Executable w/out visual
Post by: jj2007 on April 27, 2010, 10:14:31 PM
Quote from: victorvmpm on April 27, 2010, 09:51:51 PM
How can i link the assembler generated code to its compiller without intermediary programs to generate the executable?

Hi Victor,
Welcome to the forum. Your question is not very clear: Do you mean you have an own syntax and want to use Masm to implement it?
Example:

Open_a_file_for_input "MyFile.txt" ; your syntax
v v v v v v
invoke CreateFile, chr$("MyFile.txt"), GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0

Such conversions can be done with Masm's built in macro engine. It is powerful but old and very tricky. You might check MasmBasic (http://www.masm32.com/board/index.php?topic=12460), with over 80 "Basic" commands, all realised as macros that call runtime routines.

Please post some examples of your "invented language", so that we can judge better where you are heading.
Regards,
Jochen
Title: Re: Help Generating an Executable w/out visual
Post by: victorvmpm on April 27, 2010, 10:27:46 PM
My program is kind of:


start

function add(x:integer, y:integer){
return x + y;
}

function main() returns integer{
print(add(1,2));
return 0;
}

end


the code generated is kind of like this:

.
.
.

jmp main
add:
pop ax
pop bx
add ax, bx
push ax
ret
main:
push 1
push 2
call add
pop ax
;printing interruptions and exit interruption


;Sorry but my assembly is kind of rusted and i'm in unrusting process as well as learnning a new knowledge.

usually the linking is done in microsoft redistributable c++ or something like that. My question is how can create it without it.
System calls would do it.

kind of
asm-C addressSource -options directoroutput
Title: Re: Help Generating an Executable w/out visual
Post by: jj2007 on April 27, 2010, 10:46:54 PM
Do you have a good reason to stick with 16-bit assembly?

Here (http://www.webalice.it/jj2006/Masm32_Tips_Tricks_and_Traps.htm) are some basic steps to install the Masm32 package and work with it. It's a two-pager that certainly is no substitute for reading the manuals.
Title: Re: Help Generating an Executable w/out visual
Post by: victorvmpm on April 28, 2010, 12:22:17 AM
no i don't have to stick with it, but it become kind of an vice when i study assembly language in college. Since it was rare to use the extended registries.

actually the distribution is full 32bits signed.

In the project it would be the extended kind even the float data.

Title: Re: Help Generating an Executable w/out visual
Post by: hutch-- on April 29, 2010, 02:54:20 AM
Victor,

A 32 bit EXE only has an interfface is you write one for it. You can easily write an app that just runs and does what its supposed to do without sending anything to the screen at all. It would be written more like and old DOS app in that it would not have a message loop or other components designed to maintain a working Window but its simple enouugh to do.

You can also write a console mode app in win32 which writes any output you want to the win32 console CMD.EXE.
Title: Re: Help Generating an Executable w/out visual
Post by: donkey on April 29, 2010, 03:51:12 AM
MASM32 uses Microsoft link.exe that is not redistributable under the current EULA, if you plan on distributing your language you will either have to get permission to distribute ML.EXE and LINK.EXE from Microsoft or choose an assembler/linker combination with a more flexible license. One that comes to mind is JWASM which is apparently compatible with MASM so it should not require any rewriting of your code generation engine. For a linker there are many of them out there, PoLink has a fairly liberal license as far as I remember.

Japheth's JWASM site (http://www.japheth.de/)
Title: Re: Help Generating an Executable w/out visual
Post by: Slugsnack on April 29, 2010, 11:05:31 AM
okay, am i correct in saying, you are making a compiler and have done the code generation part. and have generated assembler code and that you have to assemble and link this with external programs such as ml and link and you want to know how to do it so you don't need to use these 2 programs and can have it all done within your compiler ?
Title: Re: Help Generating an Executable w/out visual
Post by: redskull on April 29, 2010, 11:55:33 AM
It's been a long while since my compiler class, but IIRC the "intermediate" code is better off being several levels higher than assembly, in a sort of psuedo-code (e.g., still contains IF statments, all "registers" are still virtual, but still one instruction per line), and then translate that into the program bytes.  After all, if you are going straight from the tree to assembly, you might as well just go straight to hex bytes; it's just a table lookup.

-r
Title: Re: Help Generating an Executable w/out visual
Post by: victorvmpm on May 07, 2010, 09:27:57 PM
Is that im using the intermediate code to be a aciclic graph, and what Slugsnack says its correct. If possible i would want to know how to do that part? If not possible I would proportion the links for them to download ML and Link by themselves and by that avoiding problems of redistribution.

And yes it's an table lookup. I'm using stack movement with the lookup. Just by address, is kind of easy when it's implemented.
Title: Re: Help Generating an Executable w/out visual
Post by: Slugsnack on May 08, 2010, 12:52:20 AM
If you want to use ML and Link, you can bundle those with your final executable and it'll still end up as one big program. I'm not sure if that violates any copyright agreements though. Otherwise, you could code your own assembler and linker. That's probably a lot more work than you're looking for though.
Title: Re: Help Generating an Executable w/out visual
Post by: joemc on May 08, 2010, 02:21:14 AM
I would just CreateProcess (http://msdn.microsoft.com/en-us/library/ms682425(VS.85).aspx) JWASM's assembler and PoLink's linker. that way you can pipe the output from it to wherever you want inside of your application.  Maybe an Output Window,  or you could even parse it and make your own output.
Title: Re: Help Generating an Executable w/out visual
Post by: dedndave on May 08, 2010, 02:25:11 AM
as long as you don't modify them - i don't see the harm
afterall, it is no different than a batch
this is a great idea - you could make simplified command line parms that cover the types of apps you usually build
with a little tinkering, you could even add ultra-modern 16-bit support - lol
Title: Re: Help Generating an Executable w/out visual
Post by: joemc on May 08, 2010, 02:28:35 AM
Quote from: dedndave on May 08, 2010, 02:25:11 AM
as long as you don't modify them - i don't see the harm
afterall, it is no different than a batch
this is a great idea - you could make simplified command line parms that cover the types of apps you usually build
with a little tinkering, you could even add ultra-modern 16-bit support - lol

instead of makeit.bat i use makeit.exe that i wrote.  I suppose i could achieve the same thing with a batch file, but it assembles all *.asm files into seperate /obj/*.obj files than links them into /exe/output.exe.  I plan on adding code to check the date of the asm file vs the obj file and if it has not been touched not to rebuild it, but i got lazy.  If this is what you are looking for victor, I can post the code.
Title: Re: Help Generating an Executable w/out visual
Post by: victorvmpm on May 08, 2010, 06:48:27 AM
Please, that sounds great! it would really help me!

Thanks everyone and thanks in advance joemc!
Title: Re: Help Generating an Executable w/out visual
Post by: joemc on May 08, 2010, 07:15:39 PM
here is a short sample of how you would run ml.exe and link.exe
it does not do all the features mentioned earlier. i cannot find source. may rewrite it, but i cannot find it.  Would not be hard to write from here, just need a few FindFirstFile and a little logic.


include \masm32\include\masm32rt.inc

.data?
value dd ?

.data
item db "Test",13,10,0
szMLEXE db "C:\masm32\bin\ml.exe",0
szMLCMD db "/coff /Fo .\obj\jmake.obj /c jmake.asm",0
szLINKEXE db "C:\Program Files\Microsoft Visual Studio 10.0\VC\bin\link.exe",0
szLINKCMD db "/subsystem:CONSOLE -LIBPATH:c:\masm32\lib .\obj\jmake.obj",0
sinf STARTUPINFO <>
pinf PROCESS_INFORMATION <>

.code

start:
cls


invoke GetStartupInfo, OFFSET sinf
invoke CreateProcess,OFFSET szMLEXE,OFFSET szMLCMD,NULL,NULL,FALSE, NORMAL_PRIORITY_CLASS,NULL,NULL,OFFSET sinf,OFFSET pinf
invoke CloseHandle,pinf.hThread

invoke WaitForSingleObject, pinf.hProcess, INFINITE

invoke CloseHandle,pinf.hProcess

invoke CreateProcess,OFFSET szLINKEXE,OFFSET szLINKCMD,NULL,NULL,FALSE, NORMAL_PRIORITY_CLASS,NULL,NULL,OFFSET sinf,OFFSET pinf
invoke CloseHandle,pinf.hThread

invoke WaitForSingleObject, pinf.hProcess, INFINITE

invoke CloseHandle,pinf.hProcess


inkey
exit
end start


edit:

just tested it and for some reason it is not accepting the /subsystem flag... idk why yet... it still assembles and links though.  just gives  a warning saying defaulting to console. Not sure how i did it before. i may disassemble it and find out what i did last time.   ML also has a linker built in if you would rather just call it. The only reason you would not want to use it is if your compiler wanted to output object files.
Title: Re: Help Generating an Executable w/out visual
Post by: joemc on May 08, 2010, 07:21:48 PM
[delete]
Title: Re: Help Generating an Executable w/out visual
Post by: hutch-- on May 09, 2010, 12:05:37 AM
If you are going to do this stuff, write the build info as a batch file then run the batch file and you don't have any inheritance or process ownership problems.