简体   繁体   中英

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 -.-

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 ]

  1. Open explorer window and search for "ml.exe" (takes a long time)
  2. Best practise is makeing a hardlink to this directory for ease of use
  3. assemble only one file at a time with ml.exe, use flag "c" so the linker will stay calm and quiet [because c is for "don't link at all"]
  4. "ml.exe -c" will provide some error output (for me something like "unknown command dwprd" or something like that), also there will be a .obj file created if successfull
  5. after you fixed all occuring bugs and errors, go back to Visual Studio and compile, if you still have no luck, try moving the assembled .obj files to the VS projects folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM