Not so long ago, I purchased this course on Assembly programming (I am particularly interested in low-level languages and computer architecture). I'm new to assembly language, and I'm very unexperienced (I should mention though that I already have some basic familiarity with C).
Now, in this part of the tutorial, the author illustrates 4 versions of the same simple algorithm that determines the greatest common divisor of the two numbers given; now, it seems to me that the author is trying to include these .asm files and the code therein (specifically, functions and arguments) inside a C program that he uses to display the output of the assembly instructions (the C program merely takes in arguments in this case and displays them using the printf statements, if my understanding is correct)...The author however does not explain how to do this in the video, which can be found here, if someone is interested (the video was not uploaded by me, I just happened to come accross it as I was looking for some information on the topic) and instead includes a .bat file inside the folder with the work files (the  .bat file and the .asm\C instructions ); I might be wrong, but it seems to me that, when executed, the instructions inside that .bat file try to access the folowing path:  c:\program files\microsoft visual studio 9.0\vc\ , which, as I understand, will execute Microsoft Visual Studio 9.0, which I don't have, so it is not surprising if it does not do anything.(I do, however have MVS 10 Professional). Even if I did have MVS 9, this would be problematic for two main reasons: I want to know ho to do this on my own, without using instructions written by somebody else, and I also might want to use another compiler.
Could anybody explain to me how to include code written in assembly language in a C program (and, specifically, how to pass in arguments made by an assembly program inside a C string) and run it?
And what exactly do those .bat instructions do? (I am somewhat confused by the erase *.obj line, I've read somewhere else that the C compiler needs those .obj files)
Posted on 2012-07-25 08:30:22 by Kirill De Vries
If you are familiar with C programming you know that each source file is compiled to an object file ( in Windows: .obj ).  Within that batch file the author is simply forcing the deletion of object and executable files prior to re-creating them.  This is just another way of doing a "make clean" when using make files.

The author is apparently trying to explain how to go about integrating assembly code into C project executables and was using MSVS 9 at the time of writing.  He uses that product for both compiling the C source files and linking all necessary .obj files in order to create an executable ( for Windows: .exe ).

Since you have MSVS 10 simply edit the batch file and replace references to VS9 with the path to your VS10 install in order to compile and link the examples.

I do not have the time to verify whether the examples actually work but hopefully the explanation I gave helps.
Posted on 2012-07-25 10:59:01 by p1ranha
Yes I know I can edit the .bat file and include the directory I need, but is there another way to do what the author is trying to do, without using batch files, or is it necessary? For example, by including references to C\.asm source files inside another C or .asm source file, in order to avoid typing in external comands, so that the C compiler or the assembler itself does it for you?
Posted on 2012-07-25 12:41:41 by Kirill De Vries
And what exactly do those .bat instructions do? (I am somewhat confused by the erase *.obj line, I've read somewhere else that the C compiler needs those .obj files)


A .bat file is just a list of commands. You could just as easily type them into the command line yourself.


Yes I know I can edit the .bat file and include the directory I need, but is there another way to do what the author is trying to do, without using batch files, or is it necessary?


Sure!

  • You can manually type the contents of the .bat file into the command prompt.

  • You can create a Makefile and use Microsoft's NMake utility.

  • You can configure Visual Studios using the nasm.rules file available from the Netwide Assembler project.




For example, by including references to C\.asm source files inside another C or .asm source file, in order to avoid typing in external comands, so that the C compiler or the assembler itself does it for you?


Nope, you can't do that. There is the possibility of using inline assembly with C, but all compilers have their own way of handling that and some compilers just choose not to support it. You're really better off just doing it the way the tutorial shows (although I would have personally used Makefiles over .bat files).
Posted on 2012-07-25 13:32:05 by Synfire
There IS another way to do it... MSVC 10 has added something called 'MSBuild' which makes it very easy!
Recall that the latest version of MASM is *only* available as part of the MSVC devpack? Wondered why?

I've delivered instructions to Biterider for creating projects that mix c/c++ and masm/nasm and don't use any cheap tricks like 'build step batchfiles' to do it. He was going to write a proper tutorial based on my notes, you should ask him if he's done that :)

Here's how it basically works: a Solution can contain one or more Projects, each can have its own unique settings. You already knew that, what you didn't know is that we can create a Project which is 100% asm, compile it to an object or lib, and then link it against our (c/c++) other project(s) - and all in the one Solution, just by hitting the Build button like you normally do !!
MSVC lets you declare project dependencies - we can tell it that our main c++ project requires that the asm project is built before the c++ one, so it can be linked in at that time. In order to create an asm project in MSVC, there are a number of steps to perform, and screen shots of the menus involved would really help you out there.

What I typically do is build the asm code as a static lib, and then add that lib to the c/c++ project's linker settings. Then I create a c/c++ header file which describes the prototypes for the functions in the lib, so that c/c++ can call them by name.

The really nice thing about this technique, is that the MSVC Debugger has full access to your asm source, and it works as expected, you can debug your c/c++ and asm code in the same environment.

But back to your existing technique of using batchfiles driven by build steps .. this technique is still valid, I used it recently to build Android apps with MSVC :P It was a lot easier than learning the guts of MSBuild, which u must do in order for MSVC to support alien compilers (it involves hand-editing an XML file and having a lot of knowledge about what to put in there).

Have a nice day :)
Posted on 2012-07-26 00:26:52 by Homer