C++ 和汇编子例程
C++ and Assembly subroutines
我需要创建一个汇编子例程,它接受一个双字数组并将其乘以一个整数。下面是我的 main.cpp、multarray.h 和 multarray.asm 文件。我必须在汇编和 C++ 中执行该函数,然后比较两者的 运行 时间。我收到两个错误,粘贴在我的代码上方。请帮忙,因为我不确定为什么会收到此错误。
错误:
Error 2 error A1010: unmatched block nesting :
AsmMultArray F:\Assembly Project\Assembly
Project\lab8.asm 25 1 Assembly Project Error 3 error MSB3721: The
command "ml.exe /c /nologo /Zi /Fo"Debug\lab8.obj" /Fl".lst" /I
"c:\Irvine" /W3 /errorReport:prompt /Talab8.asm" exited with code 1.
代码:
**Main.cpp**
#include <iostream>
#include <time.h>
#include "multarray.h"
using namespace std;
int main() {
long arr1[10] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
const int multiplier = 7;
time_t startTime, endTime;
//Testing the C++ function
time(&startTime);
CMultArray(multiplier, arr1, 10);
time(&endTime);
cout<<"The time taken to run C++ function is: " <<long(endTime - startTime)<< " seconds.";
//Testing the assembly language procedure
time(&startTime);
AsmMultArray(multiplier, arr1, 10);
time(&endTime);
cout<<"The time taken to run Assembly language procedure is: "<<long(endTime - startTime)<< " seconds.";
return 0;
}
multarray.h
#include <stdio.h>
extern "C"{
//call to assembly language procedure
void AsmMultArray(long multiplier, long arr1[], long count);
//call to c++ language function
void CMultArray(long multiplier, long arr1[], long count);
}
.asm 文件
INCLUDE Irvine32.inc
TITLE MultArray Exmaple
; This program creates the procedure that multiplies the doubleword array by an
; Integer in both assembly and C++ languages and compares the execution times
.MODEL small
.data
AsmMultArray PROC USES edi eax ebx,
multiplier: DWORD, arrPtr: DWORD, count: DWORD
.code
mov edi, arrPtr
mov ebx, multiplier
mov ecx, count
L1:
mov eax,[edi]
mul ebx
mov [edi],eax
add edi,4
loop L1
ret
AsmMultArray ENDP
.
void CMultArray(long multiplier, long arr1[], long count)
{
for(int i=0;i<count;i++)
{
arr1[i]=arr1[i]*multiplier
}
}
当通过 INCLUDE Irvine32.inc
使用 Irvine32 库时,model
会自动设置为 .MODEL small
,因此可以安全地将其从您的代码(并且警告将消失)。
您的汇编程序文件没有 global/static 数据,因此可以删除 .data
部分。
PROCs(函数)需要放在 .code
部分,所以 .code
必须出现在 的定义之前过程。不这样做会使汇编程序感到困惑。
由于您将从 C/C++ 调用您的代码,您需要指定您的汇编代码将使用 C 在为 PROC 生成适当的序言和结尾代码时调用约定。这包括设置堆栈框架和 pushing/popping 参数,以及访问以正确顺序传递的参数。它还会自动为您的函数名称添加下划线前缀。为此,您将 PROC 的定义更改为 PROC C USES
而不是 PROC USES
(感谢 @rkhb)。
汇编程序文件的末尾需要 END
作为文件的最后一条语句。
根据上述建议,如果您的文件被修改为如下所示,应该可以使用:
INCLUDE Irvine32.inc
TITLE MultArray Example
; This program creates the procedure that multiplies the doubleword array by an
; Integer in both assembly and C++ languages and compares the execution times
.code
AsmMultArray PROC C USES edi eax ebx,
multiplier: DWORD, arrPtr: DWORD, count: DWORD
mov edi, arrPtr
mov ebx, multiplier
mov ecx, count
L1:
mov eax,[edi]
mul ebx
mov [edi],eax
add edi,4
loop L1
ret
AsmMultArray ENDP
END
我需要创建一个汇编子例程,它接受一个双字数组并将其乘以一个整数。下面是我的 main.cpp、multarray.h 和 multarray.asm 文件。我必须在汇编和 C++ 中执行该函数,然后比较两者的 运行 时间。我收到两个错误,粘贴在我的代码上方。请帮忙,因为我不确定为什么会收到此错误。
错误:
Error 2 error A1010: unmatched block nesting : AsmMultArray F:\Assembly Project\Assembly Project\lab8.asm 25 1 Assembly Project Error 3 error MSB3721: The command "ml.exe /c /nologo /Zi /Fo"Debug\lab8.obj" /Fl".lst" /I "c:\Irvine" /W3 /errorReport:prompt /Talab8.asm" exited with code 1.
代码:
**Main.cpp**
#include <iostream>
#include <time.h>
#include "multarray.h"
using namespace std;
int main() {
long arr1[10] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
const int multiplier = 7;
time_t startTime, endTime;
//Testing the C++ function
time(&startTime);
CMultArray(multiplier, arr1, 10);
time(&endTime);
cout<<"The time taken to run C++ function is: " <<long(endTime - startTime)<< " seconds.";
//Testing the assembly language procedure
time(&startTime);
AsmMultArray(multiplier, arr1, 10);
time(&endTime);
cout<<"The time taken to run Assembly language procedure is: "<<long(endTime - startTime)<< " seconds.";
return 0;
}
multarray.h
#include <stdio.h>
extern "C"{
//call to assembly language procedure
void AsmMultArray(long multiplier, long arr1[], long count);
//call to c++ language function
void CMultArray(long multiplier, long arr1[], long count);
}
.asm 文件
INCLUDE Irvine32.inc
TITLE MultArray Exmaple
; This program creates the procedure that multiplies the doubleword array by an
; Integer in both assembly and C++ languages and compares the execution times
.MODEL small
.data
AsmMultArray PROC USES edi eax ebx,
multiplier: DWORD, arrPtr: DWORD, count: DWORD
.code
mov edi, arrPtr
mov ebx, multiplier
mov ecx, count
L1:
mov eax,[edi]
mul ebx
mov [edi],eax
add edi,4
loop L1
ret
AsmMultArray ENDP
.
void CMultArray(long multiplier, long arr1[], long count)
{
for(int i=0;i<count;i++)
{
arr1[i]=arr1[i]*multiplier
}
}
当通过 INCLUDE Irvine32.inc
使用 Irvine32 库时,model
会自动设置为 .MODEL small
,因此可以安全地将其从您的代码(并且警告将消失)。
您的汇编程序文件没有 global/static 数据,因此可以删除 .data
部分。
PROCs(函数)需要放在 .code
部分,所以 .code
必须出现在 的定义之前过程。不这样做会使汇编程序感到困惑。
由于您将从 C/C++ 调用您的代码,您需要指定您的汇编代码将使用 C 在为 PROC 生成适当的序言和结尾代码时调用约定。这包括设置堆栈框架和 pushing/popping 参数,以及访问以正确顺序传递的参数。它还会自动为您的函数名称添加下划线前缀。为此,您将 PROC 的定义更改为 PROC C USES
而不是 PROC USES
(感谢 @rkhb)。
汇编程序文件的末尾需要 END
作为文件的最后一条语句。
根据上述建议,如果您的文件被修改为如下所示,应该可以使用:
INCLUDE Irvine32.inc
TITLE MultArray Example
; This program creates the procedure that multiplies the doubleword array by an
; Integer in both assembly and C++ languages and compares the execution times
.code
AsmMultArray PROC C USES edi eax ebx,
multiplier: DWORD, arrPtr: DWORD, count: DWORD
mov edi, arrPtr
mov ebx, multiplier
mov ecx, count
L1:
mov eax,[edi]
mul ebx
mov [edi],eax
add edi,4
loop L1
ret
AsmMultArray ENDP
END