MASM32 Assembly does not work in Visual Studio - visual-studio

I wanted to start learn Assembly but it did not run any way. First I tried with py il with python but it did not work. Now I have a Visual Studio 2022 and I installed the MASM32. The problem is Visual Studio give a A1000 error so it does not find the file, and the second error:
MSB3721 The command "ml.exe /c /nologo /Zi /Fo"Debug\Main.obj" /W3 /errorReport:prompt /TaMain.asm" exited with code 1.TemplateC:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\BuildCustomizations\masm.targets
So I checked the template file's Property > Linker:
Output file: D:Assembly
Additional Library Directories: C:\Users\User\source\repos\Template\Template
After I checked the Main.asm file's Property > General > Ithem type: Microsoft Macro Assembler
And my code:
INCLUDE Irvine32.inc
.386
.model flat, stdcall
.stack 4049
ExitProcess PROTO, dwExitCode:DWORD
.data
; define your variables here
.code
main PROC
; write your assembly code here
mov eax, 3
mov ebx, 5
add eax, ebx
INVOKE ExitProcess, 0
main ENDP
END main
What could be the problem?
Thanks for the answers!

Visual Studio 2022 and prior versions include 32 bit masm (ml.exe) and 64 bit masm (ml64.exe). Rather than using project defaults, I start off with empty projects and add custom build info. These are the steps I use:
create empty console project, use same directory for ..., creates a directory. Shell out and copy source file(s) into directory. Add existing item to add source file(s) to project. Double click on file name for it to show in main window.
project properties I use:
linker
General
Enable Incremental Linking: No (/INCREMENTAL:NO)
Advanced
Randomized Base Address: No (/DYNAMICBASE:NO)
Image Has Safe Exception Handlers: No (/SAFESEH:NO)
source file properties I use:
Excluded From Build: No
Item Type: Custom Build Tool
Custom Build Tool for debug
command line: ml /c /Zi /Fo$(OutDir)\x.obj x.asm
output file: $(OutDir)\x.obj
Custom Build Tool for release (/Zi is not needed)
command line: ml /c /Fo$(OutDir)\x.obj x.asm
output file: $(OutDir)\x.obj
For 64 bit builds, use ml64 (64 bit assembler) instead of ml.
When doing a build, ignore Link warning about /LTCG (whole code optimization), it isn't an issue to leave it on. Randomized Base Address can affect benchmarks on some processors, which is why I turn it off.
Link to a prior answer that includes a source code example:
https://stackoverflow.com/a/64676049/3282056
There is a possibility that Irvine32.inc will need to be modified to work with Visual Studio (I've never used it). The example masm code in that prior answer uses Visual Studio include files (for the .lib references). As noted in that prior answer, the legacy lib is needed for printf, scanf, since VS 2015.
When in doubt about using some library or naming conventions (like C++ mangled function names), I code it in C | C++, and have Visual Studio produce assembly code to get the calling sequence code.

Related

Visual Studio debugger not working for a DLL build from assembly source files?

I'm currently using masm in a C++ wrapper to develop a .dll so it can be used by other applications, notably a c# one in Visual Studio 2022
However developing this way has some big drawbacks. Visual Studio has all sorts of little niggles about getting MASM working such as building properly if a file is 'included' in the asm source. Such files also cannot have break points. This means I have to resort to one large file, which is getting very unwieldy.
Its like Microsoft have given up supporting MASM, unless there's some way to build a DLL that allows debugging?
Or is there some better way to develop x64 .dlls on Windows, if there's no easy fix for the Visual Studio debugging problem?
Working example. I'm able to step through the c# code and into the assembly code. Directory for assembly based DLL is c:\xcadll.
xa.asm:
includelib msvcrtd
includelib oldnames ;optional
.data
.data?
.code
public DllMain
DllMain proc ;return true
mov rax, 1
ret 0
DllMain endp
include xb.asm
end
xb.asm:
public Example
Example proc ;[rcx] = 0123456789abcdefh
mov rax, 0123456789abcdefh
mov [rcx],rax
ret 0
Example endp
end
Program.cs - used to call the debug version of the dll:
using System;
using System.Runtime.InteropServices;
namespace Program
{
class Program
{
[DllImport("c:\\xcadll\\x64\\debug\\xcadll.dll")]
static extern void Example(ulong[] data);
static void Main(string[] args)
{
ulong[] data = new ulong[4] {0,0,0,0};
Console.WriteLine("{0:X16}", data[0]);
Example(data);
Console.WriteLine("{0:X16}", data[0]);
return;
}
}
}
Project settings for DLL build:
Create a .dll project for the assembly (or C or C++) code:
project properties: Debug | x64
properties for xa.asm:
General / Excluded From Build: No
Custom Build Tool / Command Line: ml64 /c /Zi /Fo$(OutDir)\xa.obj xa.asm
Custom Build Tool / Outputs: $(OutDir)\xa.obj
Project settings for C# build:
Create a C# project
project properties: Debug | x64
As shown in the example code above, the C# program needs to import the debug version of the DLL.

I'm trying to build my first assembly project in VS2019 to no avail

This is the assembly code I'm trying to run in VS2019:
; AddTwo.asm - adds two 32 bits integer.
; Chapter 3 example
.386
.model flat, stdcall
.stack 4096
ExitProcess proto, dwExitCode:dword
.code
main PROC
mov eax, 5
add eax, 6
invoke ExitProcess, 0
main endp
end main
When I press the F5 key I get the error message shown in the print screen below:
The code doesn't build. Note that I have selected within the project's Build Dependencies > Build Customizations, the option masm. What am I missing?
Edit
I found out what the problem was. I simply didn't update the file main.asm Properties > Microsoft Macro Assembler > Item type to Microsoft Macro Assembler. The immediate question that came to my mind was: why doesn't this Item type come with this (correct) setting, given that I had already selected in the project's Build Dependencies > Build Customizations, the option masm?
Most likely the problem stems from missing include files and libraries. You are advertising the external function ExitProcess with
ExitProcess proto, dwExitCode:dword
but do not include the corresponding header file (maybe "windows.inc" from hutch/MASM32) and are probably not linking the associated library file (some .lib).
Therefore the output of the assembler and linker is not present...
because the files are not produced due to errors...
giving you the error you are experiencing...
File not found
Without knowing more of the development environment you are using, it's hard to tell what you should do in this case. The MASM32 SDK by Hutch in combination with Iczelion's ASM tutorials(Download) were a good start for Win32 ASM development.

Can't run assembly language program in Visual Studio 2019- Unable to start program, system cant find file specified

im trying to run this program in VS2019 but the it keep pop out "Unable to start program, system cant find file specified". i see my lecturer run this smoothly but when on me it just doesnt work. can it be because of the setup?
'''
INCLUDE Irvine32.inc
.code
main PROC
mov eax,10000h ; EAX=10000h
add eax,10000h; EAX=50000h
sub eax,20000h; EAX=30000h
call DumpRegs
exit
main ENDP
END main
It's possible that you don't have the assembly source file included in the build. Right click on the source file name, then properties, then set "excluded from build" to no. Then set up custom build step. For example, if source file name is x.asm, for debug build:
command line: ml /c /Zi /Fo$(OutDir)\x.obj x.asm
output: $(OutDir)\x.obj
For release, the same, except for the Zi:
command line: ml /c Fo$(OutDir)\x.obj x.asm
output: $(OutDir)\x.obj
I usually create an empty project, and once the directory is created, copy the source file(s) into the directory, then "add existing item" to the project, then do the above steps to get the assembler commands to work.
In some cases, VS2019 will default to building assembly source files when first installed, but if it doesn't I haven't found a way to get it to happen without manually setting up a custom build step.

Build error in Visual Studio with assembly

I have the Visual Studio Express 2015 and I try to learn Assembly with the Kip Irvine package. When I try to build the Project I always got an error message. It's code is LNK1104 and the message is cannot open file 'C:\Irvine\Examples\Project32\Debug\Project.exe'.
The code opened is the AddTwo.asm file from the package.
; AddTwo.asm - adds two 32-bit integers.
; Chapter 3 example
.386
.model flat,stdcall
.stack 4096
ExitProcess proto,dwExitCode:dword
.code
main proc
mov eax,5
add eax,6
invoke ExitProcess,0
main endp
end main
I tried build it with listing file and without it, tried to copy the asm file to the Project32 folder and Build that one but the error is always the same.

How to use CMake to generate vs project, which link to some .dll files

I use Cmake to generate VS project, based on some dll file by
if(WIN32)
MESSAGE(WINDOWS)
LINK_LIBRARIES(${***_Test_SOURCE_DIR}/../../Build/Win32/Release/***.dll)
else(WIN32)
MESSAGE(POSIX)
LINK_LIBRARIES(${***_Test_SOURCE_DIR}/../../Build/POSIX/lib***.so)
endif(WIN32)
But when I open the generated project, build and it throw out
\***.dll : fatal error LNK1107: invalid or corrupt file: cannot read at 0x2B0
Is anyone has ideas?
If do not use CMake, how to add external dll(not reference to some project) file in VS project. Is there any strict steps I can follow?
Thanks
On Windows, you need to link against .lib and not against a .dll, that's stupid, but Microsoft uses the same .lib extension for static and dynamic libraries. There should be ***.lib somewhere in ${***_Test_SOURCE_DIR}/../../Build/Win32/Release/.
LINK_LIBRARIES(${***_Test_SOURCE_DIR}/../../Build/Win32/Release/***.lib)
If it is not the case, you can follow this article to build a .lib from a .dll: How To Create 32-bit Import Libraries Without .OBJs or Source. Briefly (taken from Adrian Henke’s Blog), within a VS command prompt:
dumpbin /exports C:\yourpath\yourlib.dll
This will print quite a bit of text to the console. However we are only interested in the functions:
ordinal hint RVA name
1 0 00017770 jcopy_block_row
2 1 00017710 jcopy_sample_rows
3 2 000176C0 jdiv_round_up
4 3 000156D0 jinit_1pass_quantizer
5 4 00016D90 jinit_2pass_quantizer
6 5 00005750 jinit_c_coef_controller
...etc
Now copy all those function names (only the names!) and paste them into a new textfile. Name the nextfile yourlib.def and put the line “EXPORTS” at its top. My yourlib.def file looks like this:
EXPORTS
jcopy_block_row
jcopy_sample_rows
jdiv_round_up
jinit_1pass_quantizer
jinit_2pass_quantizer
jinit_c_coef_controller
...
Now from that definition file, we can finally create the .lib file. We use the “lib” tool for this, so run this command in your Visual Studio Command Prompt:
lib /def:C:\mypath\mylib.def /OUT:C:\mypath\mylib.lib

Resources