在CPP文件运行ASM的程序 [英] Running asm procedure in cpp file
问题描述
我想从ASM文件中的cpp文件中运行的程序,但我得到这样的错误:
I am trying to run procedure from asm file in cpp file but i get such errors:
Error 1 error LNK2019: unresolved external symbol _calka referenced in function _main D:\Addem\main.obj Project
Error 2 error LNK1120: 1 unresolved externals D:\Addem\Debug\Project.exe 1 1 Project
下面是我的main.cpp:
Here is my main.cpp:
#include <iostream>
using namespace std;
extern "C" void calka();
int main()
{
calka();
system("Pause");
return 0;
}
和我calka.asm
And my calka.asm
; The calka Subroutine (calka.asm)
public _calka
INCLUDELIB kernel32.lib
INCLUDELIB user32.lib
INCLUDELIB Irvine32.lib
INCLUDE Irvine32.inc
INCLUDE macros.inc
.data
BUFFER_SIZE = 50
buffer BYTE BUFFER_SIZE DUP(?)
filename BYTE 80 DUP(0)
fileHandle HANDLE ?
Rozmiar dw ?
Znak db ?
Suma dw ?
Minus db 0
wynik dw ?
temp dd ?
.code
_calka proc near
; Let user input a filename.
mWrite "Podaj nazwe pliku (z rozszerzeniem): "
mov edx,OFFSET filename
mov ecx,SIZEOF filename
call ReadString
; Open the file for input.
mov edx,OFFSET filename
call OpenInputFile
mov fileHandle,eax
; Check for errors.
cmp eax,INVALID_HANDLE_VALUE ; error opening file?
jne file_ok ; no: skip
mWrite <"Cannot open file",0dh,0ah>
jmp quit ; and quit
file_ok:
; Read the file into a buffer.
mov edx,OFFSET buffer
mov ecx,BUFFER_SIZE
call ReadFromFile
jnc check_buffer_size ; error reading?
mWrite "Error reading file. " ; yes: show error message
call WriteWindowsMsg
jmp close_file
check_buffer_size:
cmp eax,BUFFER_SIZE ; buffer large enough?
jb buf_size_ok ; yes
mWrite <"Error: Buffer too small for the file",0dh,0ah>
jmp quit ; and quit
buf_size_ok:
mov buffer[eax],0 ; insert null terminator
;mWrite "File size: "
;call WriteDec ; display file size
;call Crlf
; Display the buffer.
; Pytanie o wielkosc przedzialu
mWrite "Podaj wielkosc przedzialu h na osi x: "
mov edx,OFFSET Rozmiar
mov ecx,SIZEOF Rozmiar
call ReadDec
mov Rozmiar, ax
;mWrite <"Buffer:",0dh,0ah,0dh,0ah>
;mov edx,OFFSET buffer ; display the buffer
;call WriteString
;call Crlf
mov ecx, 15
mov ebx, OFFSET buffer
mov si, 0
jmp odczyt1
odczyt:
mov ebx, temp
inc ebx
odczyt1:
mov cl, byte ptr [ebx]
mov Znak, cl
mov temp, ebx
cmp Znak, '$' ;koniec pliku
je koniec
cmp Znak, 3bh ;srednik - nastepna liczba
je nastepna
cmp Znak, 20h ;spacja - ponowne wczytanie
je odczyt
cmp Znak, '-'
je Zmien_znak
mov al, Znak
sub al, 30h
mov bl, al
mov bh, 0
mov ax, 10
mul si
add ax, bx
mov si, ax
jmp odczyt
Zmien_znak:
mov Minus, 1
jmp odczyt
nastepna:
cmp Minus, 0
je dodaj
jne odejm
dodaj:
add Suma, si
mov si, 0
jmp odczyt
odejm:
sub Suma, si
mov Minus, 0
mov si, 0
jmp odczyt
koniec:
cmp Minus, 0
je dod
jne minu
dod:
add Suma, si
mov si, 0
jmp kon
minu:
sub Suma, si
mov Minus, 0
mov si, 0
kon:
mov ax, Suma
mul Rozmiar
mWrite "Wynik calkowania: "
cmp ax,0
;mov ebx, eax
jg plus
mWrite "-"
mov bx, 0
sub bx, ax
mov ax, bx
plus:
call WriteDec
close_file:
mov eax,fileHandle
call CloseFile
; Zeby aplikacja sie nie zamknela, heheszki :D
mov edx,OFFSET Rozmiar
mov ecx,SIZEOF Rozmiar
call ReadString
quit:
exit
_calka ENDP
END
我已经试过各种我可以专注于 - 添加OBJ文件到链接等等。我只想类型的文件,从它的名字,读日期和计算。 Calka.asm效果很好,除非我设法得到它在我的cpp文件工作。
I have tried everything what I can focus on - adding obj files to linker and so on. I only want to type the name of file, read date from it and calculate it. Calka.asm works well unless I try to get it work in my cpp file.
我在做什么错了?
推荐答案
Irvine32.inc
包括 SmallWin.inc
而集 .MODEL平,STDCALL
。所以,每道工序都会被默认为,如果调用约定是 STDCALL 装饰。 _calka
变成 _calka @ 0
。对于 的需要的main.cpp $ CDECL调用约定C $ C>你要覆盖的程序与
PROCç
。现在,下划线会自动添加。 TLDR: calka PROCç
Irvine32.inc
includes SmallWin.inc
and that sets .MODEL flat, stdcall
. So, every procedure will be decorated by default as if the calling convention were STDCALL. _calka
becomes _calka@0
. For the CDECL calling convention needed for main.cpp
you have to override the default calling convention for the procedure with PROC C
. Now the underscore will be added automatically. TLDR: calka proc C
.
最好的是,你创建一个新项目。我会形容为Microsoft Visual Studio 2015 RC的程序在Windows 10的过程是其他版本的VS颇为相似。
The best is, you create a new project. I'll describe the procedure for Microsoft Visual Studio 2015 RC on Windows 10. The procedure is quite similar for other VS versions.
1)启动Visual Studio 2015年RC,然后选择文件 - 新建 - 项目
1) Start Visual Studio 2015 RC and choose FILE - New - Project
.
2)在接下来的窗口中选择 WIN 32控制台应用程序
。
2) In the next window choose Win 32 Console Application
.
3)你得到了确认。点击下一页方式&gt;
3) You get a confirmation. Click on Next >
.
4)在接下来的窗口中选择结果
[X]控制台应用 -
[X]空项目结果
点击完成
。
4) In the next window choose
[X] Console application
[X] Empty project
Click on Finish
.
5)现在选择项目 - 建立自定义...
6)在接下来的窗口剔 MASM(.targets,.props)
,然后点击确定
。
6) In the next window tick masm(.targets,.props)
and click on OK
.
7)创建的main.cpp:项目 - 添加新项
7) Create main.cpp: PROJECT - Add New Item
.
8)在接下来的窗口中选择 C ++文件(的.cpp)
,给它一个名称,然后单击添加
。
8) In the next window choose C++File(.cpp)
, give it a name and click on Add
.
9)创建calka.asm:项目 - 添加新项
。在接下来的窗口中选择 C ++文件(的.cpp)
和 - 重要! - 给它 .ASM
扩展名。点击添加
。
9) Create calka.asm: PROJECT - Add New Item
. In the next window choose C++File(.cpp)
and - IMPORTANT! - give it a name with .asm
extension. Click on Add
.
10)现在检查 .ASM
文件具有正确的属性。在解决方案资源管理器的右键单击文件,并选择属性
。
10) Now check if the .asm
file has the right properties. In the Solution Explorer right-click on the file and choose Properties
.
11)在属性页,你应该可以看到结果
排除从生成(空)或无结果
项目建设Microsoft宏汇编结果
点击确定
。
11) In the Property Page you should see
Excluded From Build (empty) or No
Item Build Microsoft Macro Assembler
Click on OK
.
12)现在,你可以填写这些文件与内容。
12) Now you can fill the files with content.
您不需要系统(暂停)
,因为 CTRL-F5 骨节病>构建,运行和暂停的应用程序。
You don't need system("Pause")
since CTRL-F5 builds, runs and pauses the application.
在 .ASM
文件中删除
INCLUDELIB kernel32.lib
INCLUDELIB user32.lib
和让Visual Studio中它们自动包括。加入
and let Visual Studio them automatically include. Add to
INCLUDELIB Irvine32.lib
INCLUDE Irvine32.inc
INCLUDE macros.inc
的完整路径的文件。这比改变VS的设置。
the full path to the files. That's better than to change the settings of VS.
修改
_calka proc near
...
_calka ENDP
到
calka PROC C
...
calka ENDP
13)建立并运行与<大骨节病> CTRL-F5 骨节病>。
该应用程序将在新窗口中打开。
The application will be opened in a new window.
这篇关于在CPP文件运行ASM的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!