Can't compile inline assembly
Can't compile inline assembly
I am unable to compile an assembly file in Visual Studio 2017.
I am unsure weather this is due to visual studio or because i made some mistakes in the code, pls help me.
I have a c++ main file in which I go for the call:
#include "stdafx.h"
#include "myStruct.h"
extern "C" __int64 CalcStructSum_(const myStruct *kekStruct);
int _tmain(int argc, _TCHAR* argv)
myStruct kek;
kek.Val8 = 8;
kek.Val16 = 16;
kek.Val32 = 32;
kek.Val64 = 64;
__int64 summe = CalcStructSum_(&kek);
printf("summe betraegt: %d", summe);
getchar();
return 0;
mystruct.h :
#pragma once
typedef struct
__int8 Val8;
__int8 Pad8;
__int16 Val16;
__int32 Val32;
__int64 Val64;
myStruct;
here the assembly equivalent:
myStruct struct
Val8 byte ?
Pad8 byte ?
Val16 word ?
Val32 dword ?
Val64 qword ?
myStruct ends
and here is the assembly file:
.model flat,c
include myStruct_.inc
.code
;prolog and function start
CalcStructSum_ proc
push ebp
mov ebp,esp
push ebx
push esi
; das struct liegt auf dem stack
; alle felder wurden auf 32 bit sign-extended
; in esi kommt anfang des speicherbereichs rein
; später wird quasi [basereg + dispatch] daraus interpretiert
mov esi, [ebp+8] ; get a pointer to "mystruct"
; load 8- and 16bit sources into 32bit registers (movsx)
; this is necassery to load struct-fields from the stack
movsx eax, byte ptr [esi + myStruct.Val8] ; [baser. + disp]
movsx ecx, word ptr [esi + myStruct.Val16]
add eax, ecx
; now sign-extend eax register to 64bit
; high part: edx , low part: eax
cdq
;save result for later
mov ebx, eax
mov ecx, edx
; now load the int-part of the struct and add it
; (it must be sign-extended to edx:eax again)
mov eax, [esi + myStruct.Val32]
cdq
; (the high-part must be added with carry, if overflown)
add eax, ebx
adc edx, ecx
; now, get the long from stack
add eax, dword ptr [esi + myStruct.Val64] ; low part
adc edx, dwprd ptr [esi + myStruct.Val64 + 4] ; high part
; we now have the sum of all structure fields in the register pair edx:eax
pop esi
pop ebx
pop ebp
ret
CalcStructSum_ endp
end
Also, I included the files in my explorer window like this (and, of course, set build dependencies):
I also tried assembling from powershell with ml.exe, but no luck here either.
Also, it would be nice if someone could point out a simple method to capture the output of ml.exe, it is instantly away after execution (am a cmd-noob).
Finally, I tried putting the files in ANSI mode, but this should not be an issue at all, I just read through old stackoverflow answerers.
What am I missing? Should I turn back to VS2010 maybe? Or is it just an idiotic syntax error? This really freaks me out since such a simple proof-of-concept program should not be so hard to write -.-
I can't spot any inline assembly code in your example?
– πάντα ῥεῖ
Sep 17 '18 at 15:53
Just a shot in the dark, but make sure that you compile your code for 32 bit instead of 64 bit as your assembly code is not going to work in 64 bit mode.
– fuz
Sep 17 '18 at 16:02
@clockw0rk: x86-64 supports 32-bit addressing modes. Did you test with
mov eax, [rdi] or something to check that that wouldn't assemble, as a way of proving it was 32-bit? pop eax requires 32-bit mode (or 16-bit mode), but mov eax, [eax] assembles just fine in any of the 3 modes. (requires an address-size prefix in 16 and 64-bit modes, though, and an operand-size prefix in 16-bit mode.)– Peter Cordes
Sep 17 '18 at 16:22
mov eax, [rdi]
pop eax
mov eax, [eax]
@PeterCordes The only proof I have is that this is my default workspace, i set everything to 32bit mode, and assembled some x86 codes before just fine. If I try accessing rax register, it does not assemble, this should be proof
– clockw0rk
Sep 17 '18 at 20:13
1 Answer
1
Ok, problem is solved:
Turned out it was a by typo in line 50 [ "dwprd" instead of "dword" ].
Fixed and solved.
If anybody comes accross a situation like mine, where they just can't find any usefull hints provided by the assembler / IDE , then here is how i found the solution:
[ in windows OS ]
Must be a problem in your environment.I'd create a new project from scratch, and then first ensure build customizations have MASM target listed (right mouse click project, select build dependencies and build customizations and put a check by the MASM targets). Then add your files into the project. I took the files as is in VS2017, created a project added the MASM target and when building the project the errors for MASM appeared Line 50
dwprd mistake.– Michael Petch
Sep 18 '18 at 17:46
dwprd
thanks for the tipp , michael. closing this now
– clockw0rk
Sep 20 '18 at 10:22
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
What error diagnostics did you get.
– Cheers and hth. - Alf
Sep 17 '18 at 15:53