简体   繁体   English

无法编译内联汇编

[英]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. 我无法在Visual Studio 2017中编译程序集文件。我不确定这是由于Visual Studio还是由于我在代码中犯了一些错误,请帮助我。

I have a c++ main file in which I go for the call: 我有一个用于调用的c ++主文件:

#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 : 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. 我也尝试过使用ml.exe从powershell进行组装,但是在这里也没有运气。 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). 另外,如果有人可以指出一种简单的方法来捕获ml.exe的输出,那将是很好的选择,它在执行后立即消失了(一个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. 最后,我尝试将文件置于ANSI模式,但这根本不是问题,我只是通读了旧的stackoverflow答题器。

What am I missing? 我想念什么? Should I turn back to VS2010 maybe? 我应该回到VS2010吗? 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" ]. 原来这是第50行中的错别字[“ dwprd”而不是“ 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: 如果有人遇到像我这样的情况,他们只是找不到汇编器/ IDE提供的任何有用的提示,那么这就是我找到解决方案的方法:

[ in windows OS ] [在Windows OS中]

  1. Open explorer window and search for "ml.exe" (takes a long time) 打开资源管理器窗口,然后搜索“ ml.exe”(需要很长时间)
  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"] 使用ml.exe一次仅汇编一个文件,并使用标志“ c”,以便链接器保持平静和安静[因为c表示“完全不链接”]
  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 “ ml.exe -c”将提供一些错误输出(对我来说,类似“ unknown command dwprd”之类的东西),如果成功,也会创建一个.obj文件。
  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. 修复所有出现的错误和错误之后,请返回Visual Studio进行编译,如果仍然没有运气,请尝试将已组装的.obj文件移动到VS项目文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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