在汇编程序中调用C语言的库函数,h转inc

在汇编语言中如果能够调用C语言的库函数,有时会很方便

先给个示例程序:

.386

.model flat,stdcall

option casemap:none



;Include定义

include stdio.inc

includelib msvcrt.lib



.data

szMsg db "在汇编程序中调用C的puts函数!",0



.code

start:

invoke puts,offset szMsg

ret

end start


是不是有点像C中的Hello,world程序。


stdio.inc怎么来的?

stdio.inc为标准输入输出C函数的声明。

MASM32软件包中没有,Microsoft也没有提供,这个是我从VC中的stdio.h转换得来的。



怎么转换?

Microsoft提供了一个.H文件转换为.inc文件的工具 H2INC,在VC的目录中可以找到。

转换命令:

h2inc /WIN32 *.h


stdio.h直接转换时,会出错,要做一些修改。

1)在最前面加一句:

#define _WIN32

2)把文件中的#pragma pack(push,8)与//#pragma pack(pop)注释掉


转换成功之后,只要在程序开头加上

include stdio.inc

includelib msvcrt.lib

就可以调用C语言的输入输出函数了。

建议将msvcrt.lib所在的目录设置到lib环境变量中,并将stdio.inc所在的目录设置到include环境变量中

msvcrt.lib为msvcrt.dll或者msvcr71.dll(VC7.1)的库文件,VC的lib目录中有



使用h2inc转换头文件时需要注意一些事项:

1)/WIN32 参数一定要加上,不然就会按默认的16位进行转换

2)h文件最后一行要有个回车

3)函数类型声明会先转化为@proto_n的形式,然后再声明,如果转换了好几个关文件,那么几个文件中都是从@proto_0开始的,如果你同时包含这几文件时编译器就会提示:symbol type conflict(符号类型冲突)。

解决方法:

转换后,用文本编辑器的全部替换功能,将不同文件中的“proto_”全部替换为不同的字符串。

4)结构和联合也有与3)一样的问题。

5)如果h文件中的函数声名中没有加__cdecl修饰,就会被转换为默认的STDCALL类型

6)h2inc不会转换h文件中的程序,有很多C语法也不支持。 
下面是一个示例代码,用于将图像旋25度: ```c #include <stdio.h> #include <stdlib.h> #include <math.h> #define PI 3.1415926 int main() { FILE *fp_in, *fp_out; unsigned char *image_in, *image_out; char file_in_name[100], file_out_name[100]; int width, height, bpp, i, j, x, y, x1, y1; double angle = 25.0; // 读取输入图像 printf("Enter the input file name: "); scanf("%s", file_in_name); fp_in = fopen(file_in_name, "rb"); if (!fp_in) { printf("Error: failed to open the input file!\n"); return -1; } fseek(fp_in, 18, SEEK_SET); fread(&width, sizeof(int), 1, fp_in); // 读取图像宽度 fread(&height, sizeof(int), 1, fp_in); // 读取图像高度 bpp = fgetc(fp_in) / 8; // 读取每个像素所占的字节数 fseek(fp_in, 54, SEEK_SET); image_in = (unsigned char*)malloc(width * height * bpp); fread(image_in, sizeof(unsigned char), width * height * bpp, fp_in); // 读取图像数据 fclose(fp_in); // 分配输出图像内存 image_out = (unsigned char*)malloc(width * height * bpp); // 计算旋后的图像大小 int new_width = (int)(abs(width * cos(angle * PI / 180.0)) + abs(height * sin(angle * PI / 180.0))); int new_height = (int)(abs(height * cos(angle * PI / 180.0)) + abs(width * sin(angle * PI / 180.0))); // 计算旋后的图像心点 int center_x = width / 2; int center_y = height / 2; for (i = 0; i < new_height; i++) { for (j = 0; j < new_width; j++) { // 计算旋前的坐标 x1 = j - center_x; y1 = i - center_y; x = (int)(x1 * cos(angle * PI / 180.0) + y1 * sin(angle * PI / 180.0) + center_x); y = (int)(-x1 * sin(angle * PI / 180.0) + y1 * cos(angle * PI / 180.0) + center_y); // 检查坐标是否越界 if (x >= 0 && x < width && y >= 0 && y < height) { // 复制像素值 image_out[(i * new_width + j) * bpp + 0] = image_in[(y * width + x) * bpp + 0]; image_out[(i * new_width + j) * bpp + 1] = image_in[(y * width + x) * bpp + 1]; image_out[(i * new_width + j) * bpp + 2] = image_in[(y * width + x) * bpp + 2]; } else { // 越界的像素设为白色 image_out[(i * new_width + j) * bpp + 0] = 255; image_out[(i * new_width + j) * bpp + 1] = 255; image_out[(i * new_width + j) * bpp + 2] = 255; } } } // 保存输出图像 printf("Enter the output file name: "); scanf("%s", file_out_name); fp_out = fopen(file_out_name, "wb"); if (!fp_out) { printf("Error: failed to open the output file!\n"); return -1; } fseek(fp_out, 18, SEEK_SET); fwrite(&new_width, sizeof(int), 1, fp_out); fwrite(&new_height, sizeof(int), 1, fp_out); fputc(bpp * 8, fp_out); fseek(fp_out, 54, SEEK_SET); fwrite(image_out, sizeof(unsigned char), new_width * new_height * bpp, fp_out); fclose(fp_out); // 释放内存 free(image_in); free(image_out); return 0; } ``` 注意:这个代码假定输入图像为BMP格式,且每个像素的颜色通道顺序为BGR。如果你的输入图像格式或者颜色通道顺序不同,需要相应地修改代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值