c - 如何在汇编中调用函数?
问题描述
我将如何将我的汇编代码的一部分指定为函数并从 _asm {} 块中调用该函数?例如,我希望函数从 for 循环开始,“n”个元素填充了一个值。
#include <stdio.h>
#include <string.h>
void printBytes(char *data, int length)
{
int x;
for (x = 0; x < length; x++)
{
if ((x & 0xF) == 0) printf("\n");
printf("%02X ", (unsigned char)data[x]);
}
printf("\n\n");
return;
} // printBytes
void function(){
char string[] = "The end is near!";
void* dst = &string;
unsigned char value = 0xA5;
int nCount = 5;
printf("The message is: %s\n", string);
printBytes(string, strlen(string));
__asm {
//type cast the str from void* to char*
mov eax, DWORD PTR [dst]
mov DWORD PTR [ebp-88], eax
//fill "n" elements/blocks with value
mov DWORD PTR [ebp-76], 0
jmp label_3
label_2:
mov eax, DWORD PTR[ebp-76]
add eax, 1
mov DWORD PTR [ebp-76], eax
label_3:
mov eax, DWORD PTR [ebp-76]
cmp eax, DWORD PTR [nCount]
jge EXIT
mov eax, DWORD PTR [ebp-88]
add eax, DWORD PTR [ebp-76]
mov cl, BYTE PTR [value]
mov BYTE PTR[eax], cl
jmp label_2
EXIT:
}// asm_memset */
printf("Now the message is: %s\n", string);
printBytes(string, strlen(string));
return;
}
int main() {
function();
printf("Press ENTER key to continue...");
getchar();
return(0);
}
解决方案
请注意,您的问题的解决方案可能是不可移植的。
您需要为您的架构/系统/编译器找到ABI,并遵守其调用约定。
推荐阅读
- python-3.x - 使用 ib_insync 具有多个目标出口的括号定单
- html - 悬停在
我的网站有问题。我只使用 HTML 和 CSS。我正在使用几个“a href”,它们应该在悬停时稍微改变它们的背景颜色。问题是,每当我清除浏览器的缓存(或进入私密模式)时,我都需要刷新网页或单击一个链接,然后(所有这些)才开始产生悬停效果。只有再次单击我才能打开该链接。这发生在所有浏览器中。我在头部
- google-apps-script - 在独立脚本项目中调用 openById 方法时出现“服务电子表格失败”错误
- javascript - 如果条件通过单击不同的按钮返回不同的数据
- angularjs - 反应形式无法识别用户名、电子邮件、课程
- javascript - 为什么 Chrome 不创建自己的 ECMAScript 实现?
- javascript - HandleChange Redux
- pandas - 如何找到列的不同变量
- javascript - Javascript - 将照片中的图像显示为唯一的
- python - python中映射系统的加权无向图