external assembly file in visual studio - visual-studio

I searched and found I can not use __asm in x64 in visual studio. Instead I have to use an external assembly file.
How can I add external assembly file to my win32 console project?
How can compile them?
Can you explain step by step.

How to build a mixed-source x64-project with a x64 assembly file in Visual Studio:
1) Start Visual Studio (Community) 2015 and choose FILE - New - Project.
2) In the next window choose Win 32 Console Application.
3) You get a confirmation. Click on Next >.
4) In the next window you can accept the default settings. Click on Finish.
5) Make sure, that the project is highlighted in the Solution Explorer and and choose PROJECT - Build Customizations... from the menu.
6) In the next window tick masm(.targets,.props) and click on OK.
7) Choose Build - Configuration Manager...
8) Change the Active solution platform to x64
9) Create callee.asm: PROJECT - Add New Item.
10) In the next window choose C++File(.cpp) and - IMPORTANT! - give it a name with an .asm extension. Click on Add.
10) Now check if the .asm file has the right properties. In the Solution Explorer right-click on the file and choose Properties.
11) In the Property Page you should see at least:
Excluded From Build (empty) or No
Item Type Microsoft Macro Assembler
Under Command Line ensure that ml64.exe is chosen as the assembler.
Click on OK.
12) Now you can fill the files with content.
ConsoleApplication1.cpp:
#include <iostream>
using namespace std;
extern "C" void hello_from_asm();
int main()
{
cout << "Hello from CPP" << endl;
hello_from_asm();
return 0;
}
callee.asm:
PUBLIC hello_from_asm
EXTERN puts:PROC
.data
hello1 db "Hello from ASM.",0
.code
hello_from_asm PROC
push rbp
mov rbp, rsp
sub rsp, 32 ; Shadow Space
and spl, -16 ; Align stack at 16
lea rcx, hello1
call puts
leave ; Restore stack (rsp) & frame pointer (rbp)
ret
hello_from_asm ENDP
END
13) Build the .exe
and run it with CTRL-F5.
The application will be opened in a new window.

Related

I am having problems building my assembly code in visual studio

I am new to assembly and I am trying to build my code. But when ever I try, I get this error that I don't understand:
Severity Code Description Project File Line Suppression State
Error MSB3721 The command "ml.exe /c /nologo /Zi /Fo"Debug\Main.obj" /W3 /errorReport:prompt /TaMain.asm" exited with code 1. TestProject D:\VS\vs\MSBuild\Microsoft\VC\v170\BuildCustomizations\masm.targets 70
This file is not even in my project, I think it is the compiler but I am not sure why it is giving this error. Here is my code:
.386
.model flat
.code
start PROC
mov eax, 213
add eax, 432
ret
start endp
end start
I am using the VS masm and Local Windows Debuger. The version is 2022 comunity. If anyone knows the solution, or if I need to add more details please tell me.
Thank you and have a nice day!

`Error lnk1181: cannot open input file 'Debug\Source.obj` when trying to create listing file

I'm trying to get started with asm programming on Win 32 and, after searching the internet for instructions for a while and also using Irvine's online tutorial on setting this up, successfully got the following program to compile and run using VS 2015 community edition:
.386
.model flat,stdcall
option casemap:none
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD
.CODE
main PROC
mov eax,5 ; move 5 to the eax register
add eax,6 ; add 6 to the eax register
INVOKE ExitProcess,0
main ENDP
END main
(it's from Irvine's book).
Now I want to create a listing file. To this end I opened Properties -> Configuration Properties -> Microsoft Macro-Assembler and enabled 'Generate Preprocessed Source Listing' and 'List All Available Information' and added $(ProjectName).lst in the 'Assembled Code Listing File' entry. After that I get the error mentioned in the title when I try to build the project.
In fact, when I build the project (without these settings), no .obj file seems to be generated, just the .exe and .pdb. I can trigger building an .obj file by choosing the 'Enable Assembly Generated Code Listing' option (same path as above), but I still get the same error when I try to enable generation of the listing file as explained before. Any ideas?
(This is following instructions in Irvine's book, but without using his included files).
(I did disable my Anti Virus program before building).

Why am I getting a linker error when I use an Irvine32 function?

I'm trying to run an assembly program using MASM in Visual Studio. This is the code I have
include Irvine32.inc
.data
str1 BYTE "a test string",0
.code
main proc
mov edx,OFFSET str1
call WriteString
exit
main endp
End main
The error I'm getting is:
error LNK2019: unresolved external symbol _WriteString#0 referenced in function _main#0"
Why am I getting this error?
Press Shift + Alt + A
find Irvine32.inc and Irvine32.lib from your HDD
Add them in your project repository.
The linker error suggests that you aren't linking against the Irvine32 library. Although you have included Irvine32.inc at the top of your assembly program, you still need to add Irvine32.lib as a library to link against in your Solution, or add it to your command line (if compiling with at a command shell), or use includelib directive inside your assembly code to link with it.

User32.dll!NtUserWindowFromPoint getting corrupt when loaded by Mumble on Windows RT

I'm working on porting Mumble over to Windows RT (using the jailbreak), and I've hit an issue where this one function is getting corrupted when Mumble loads.
Mumble (Corrupt function):
0:000> dq user32.dll+0x023918
77a63918 47c3004244696841 4770df010c16f241
77a63928 4770df010c17f241 4770df010c18f241
77a63938 4770df010c19f241 4770df010c1af241
77a63948 4770df010c1bf241 4770df010c1cf241
77a63958 4770df010c1df241 4770df010c1ef241
77a63968 4770df010c1ff241 4770df015c81f44f
77a63978 4770df010c21f241 4770df010c22f241
77a63988 4770df010c23f241 4770df010c24f241
0:000> u user32.dll+0x023918
* ERROR: Symbol file could not be found. Defaulted to export symbols for
C:\windows\system32\user32.dll -
user32!WindowFromPoint:
77a63918 6841 ldr r1,[r0,#4]
77a6391a 4469 add r1,r1,sp
77a6391c 0042 lsls r2,r0,#1
77a6391e 47c3 ?blx r8
77a63920 f2410c16 mov r12,#0x1016
77a63924 df01 svc #1
TeXworks (Expected output):
0:000> dq user32.dll+0x23918
77a63918 4770df010c15f241 4770df010c16f241
77a63928 4770df010c17f241 4770df010c18f241
77a63938 4770df010c19f241 4770df010c1af241
77a63948 4770df010c1bf241 4770df010c1cf241
77a63958 4770df010c1df241 4770df010c1ef241
77a63968 4770df010c1ff241 4770df015c81f44f
77a63978 4770df010c21f241 4770df010c22f241
77a63988 4770df010c23f241 4770df010c24f241
0:000> u user32.dll+0x23918
* ERROR: Symbol file could not be found. Defaulted to export symbols for C:\windows\system32\USER32.dll -
USER32!WindowFromPoint:
77a63918 f2410c15 mov r12,#0x1015
77a6391c df01 svc #1
77a6391e 4770 bx lr
77a63920 f2410c16 mov r12,#0x1016
77a63924 df01 svc #1
77a63926 4770 bx lr
77a63928 f2410c17 mov r12,#0x1017
77a6392c df01 svc #1
(Apologies for the less than stellar formatting of the code, a screenshot of the windows can be found here: http://i.imgur.com/M6mLHN1.png )
Mumble uses Qt (customized by the Mumble team, to my understanding), Protobuf, Boost, and OpenSSL
TeXworks uses Qt
What I've tried so far:
Disabling the application compatibility engine
Unloading user32.dll at load, then reloading it (calling FreeLibrary 100 times, then calling LoadLibrary)
Removing anything that might look even remotely suspect from the manifests (from Qt and Mumble)
Removing the entire manifests (from Qt and Mumble)
If I patch this one function using cdb after Mumble launches it all works awesomely, but if I don't patch it the first action performed that calls that function ends in a crash. Opening/closing windows and dragging all call that function, so it's rather critical to the program that it's there.
Any help or pointers on this would be more than appreciated.
Edit: I've verified that it's something inside mainCRTStartup that's mucking around with it, trying to figure out what exactly it is now.
Edit 2: Found a platform-specific hook hidden in the Mumble code that was causing my troubles. Solved.
Since I can finally answer this now, Mumble had some hooks hidden away that I didn't know about. I defined a custom entrypoint that called mainCRTStartup so I could step through that and find exactly where that memory was getting changed, it led me straight to the hook.
Here's the code I used for that:
EXTERN_C int WINAPI mainCRTStartup();
void __stdcall EntryPoint()
{
MessageBox(HWND_DESKTOP,L"Pause(Before mainCRTStartup)",L"Pause(Before mainCRTStartup",MB_OK);
mainCRTStartup();
ExitProcess(0);
}
That allowed me to attach a debugger at the messagebox and step through mainCRTStartup until I found the static initializer that was getting called to load the hook.

Microsoft MPI doesn't run

I'm trying out Microsoft's implementation of MPI. I installed the CCP sdk from here:
http://www.microsoft.com/en-us/download/details.aspx?id=239
And then in my project settings I added the include folder, the lib folder and mentioned msmpi.lib.
With the remaining settings as-is, I build the program and then in the command prompt I proceed to run the program, but nothing happens after I start it up.
Here's the code (It's supposed to display the id numbers for each thread):
#include "stdafx.h"
#include "mpi.h"
#include <stdio.h>
//Commands in cmd prompt
//cd "C:\Program Files\Microsoft Compute Cluster Pack\Bin"
//mpiexec.exe -n 2 "C:\Users\MyNameHere\Documents\Visual Studio 2012\Projects\tspMpi\Debug\tspMpi.exe"
int main(int argc, char* argv[])
{
int nTasks = 0, rank = 0;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&nTasks);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
printf ("Number of threads = %d, My rank = %d\n", nTasks, rank);
return 0;
MPI_Finalize();
}
As soon as I run mpiexec.exe (the commands are in the comments) the program just does nothing, until I press Ctrl-C. Does anyone know what I'm doing wrong? There are no errors when I build the program, and if I run it from visual studio, it acts as if there was only one process started up.
I didn't find SDK useful at all, here are my steps to enable MPI cluster debugging in VS 2010 (VC10):
step 1. Install MS-MPI: http://www.microsoft.com/en-us/download/details.aspx?id=36045 (x64 only), this creates
C:\Program Files\Microsoft HPC Pack 2012\Inc
C:\Program Files\Microsoft HPC Pack 2012\Lib\amd64
C:\Program Files\Microsoft HPC Pack 2012\Lib\i386
step 2. Download example: http://msdn.microsoft.com/en-us/library/ee441265(v=vs.100).aspx#BKMK_debugMany
step 3. Debugging setting: Right click on the Startup Project > Properties > Debugging
Debugger to launch, change "Local Windows Debugger" to "MPI Cluster Debugger"
Run Environment, change "localhost/1" to "localhost/4"
Right click on Visudal Studio Toolbar area to check "Debug Location", now you can switch Process and its Threads in the Debug Location toolbar, have fun!

Resources