显示640x480的BMP图像与内联ASM c++
Show 640x480 BMP image with inline ASM c++
你好,我试图在c++中显示一个640x480 BMP图像(16色位图)与内联ASM,它必须与内联ASM,因为它是一个作业。我用汇编代码编写了这段代码:
cad db 'Error, file not found, press a key to finish.$'
filename db "C:image.bmp"
handle dw ?
col dw 0
ren dw 479
col1 dw ?
ren1 dw ?
col2 dw ?
ren2 dw ?
buffer db ?
colo db ?
eti0:
mov ah,3dh
mov al,0
mov dx,offset filename
int 21h
jc err
mov handle,ax
mov cx,118d
eti1:
push cx
mov ah,3fh
mov bx,handle
mov dx,offset buffer
mov cx,1
int 21h
pop cx
loop eti1
mov ah,00h
mov al,18d
int 10h
eti2:
mov ah,3fh
mov bx,handle
mov dx,offset buffer
mov cx,1
int 21h
mov al,buffer
and al,11110000b
ror al,4
mov colo,al
mov ah,0ch
mov al,colo
mov cx,col
mov dx,ren
int 10h
mov al,buffer
and al,00001111b
mov colo,al
inc col
mov ah,0ch
mov al,colo
mov cx,col
mov dx,ren
int 10h
inc col
mov ah,0ch
mov al,colo
mov cx,col
mov dx,ren
int 10h
cmp col,639d
jbe eti2
mov col,0
dec ren
cmp ren,-1
jne eti2
现在把它放在内联ASM我正在尝试下一个代码:
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<dos.h>
#include<stdlib.h>
void main(void)
{
clrscr();
unsigned char buffer,colo;
unsigned int handle,col=0,ren=479,col1,col2,ren2;
int filename=675892105109971031011104698109112;
asm{
mov ah,3dh
mov al,0
mov dx,filename
int 21h
mov handle,ax
mov cx,118d
}
cout<<"si mino1";
for(int i=118;i>0;i++){
asm{
mov ah,3fh
mov bx,handle
mov dx,offset buffer
mov cx,1
int 21h
}
}
asm{
mov ah,00h
mov al,18d
int 10h
}
cout<<"si mino2";
eti2:
asm{
mov ah,3fh
mov bx,handle
mov dx,offset buffer
mov cx,1
int 21h
mov al,buffer
and al,11110000b
ror al,4
mov colo,al
mov ah,0ch
mov al,colo
mov cx,col
mov dx,ren
int 10h
mov al,buffer
and al,00001111b
mov colo,al
inc col
mov ah,0ch
mov al,colo
mov cx,col
mov dx,ren
int 10h
inc col
mov ah,0ch
mov al,colo
mov cx,col
mov dx,ren
int 10h
cmp col,639d
jbe eti2
mov col,0
dec ren
cmp ren,-1
jne eti2
}
cout<<"si mino3";
getch();
}
代码到达第一个cout,然后进入一个无限循环。
你真的是指下面这行代码吗?
for(int i=118;i>0;i++)
将i
初始化为118,并且每次迭代都加1。它只会越来越大(直到i
溢出)。判断循环是否应该继续的测试是i > 0
,它总是为真(直到i
溢出)。
你确定你在无限循环中吗?也许很多很多的int 21h
只是花了很长很长的时间。
我想出了如何显示一个640x200 16色BMP与下一个代码:
//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// << DISPLAY 4-Bit BMP (16 colors) >>
// This program shows how to display a 16 color bitmap.
// I am not using palettes for bmp,hence all default 16 colors are used.
// If you know BMP structure you can try to add palettes.
//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#include <alloc.h>
#include <conio.h>
#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#define UL unsigned long
#define UI unsigned int
#define UC unsigned char
//+-+-+-+-+-+-+-+-+-+-+-+-+-+< BMP Structures >+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
typedef struct
{
char Type[2];
UL Size;
UI R1;
UI R2;
UL OffSet;
}BMP1;
//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
typedef struct
{
UL headsize;
UL Hlen;
UL Vlen;
UI planes;
UI BPP;
UL Method;
UL BmpSize;
UL HRes;
UL VRes;
UL Colors;
UL IColors;
}BMP2;
//+-+-+-+-+-+-+-+-+-+-+-+-+-< Display BMP >+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
int ShowBMP(int x, int y, char* FileName)
{
int b,a;
BMP1 Obj1;
BMP2 Obj2;
UC * Holder;
int in=0;
UC c=0;
FILE * fp;
fp = fopen(FileName,"rb");
if(fp==NULL)
return 0;
fread(&Obj1, sizeof(Obj1), 1, fp);
fread(&Obj2, sizeof(Obj2), 1, fp);
if(Obj2.BPP!=4) // This isn't a 16 color bmp we can read;
{
fclose(fp);
return 0;
};
fseek(fp,Obj1.OffSet,SEEK_SET);
Holder=(UC *) calloc(Obj2.Hlen/2+1, sizeof(UC));
for(b=Obj2.Vlen;b>=0;b--)
{
fread(Holder, sizeof(UC), Obj2.Hlen/2, fp);
c=0;
in=0;
for(a=0;a<=Obj2.Hlen;a+=2)
{
c = (Holder[in] | 0x00) >>4;
putpixel(a+x,b+y,c);
c = (Holder[in] | 0xF0) & 0x0F;
putpixel(a+1+x,b+y,c);
in++;
}
}
free (Holder);
fclose(fp);
return 1;
}
//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-< **** >+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// Two bmp demo.bmp & demo1.bmp are provided.
// open these bmp in windows paint & change.(do not change size of bmp)
//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-< Main >+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
void main()
{
int color,D=3,E=0;
//registerfarbgidriver(EGAVGA_driver_far);
initgraph(&D,&E,"C:\BC31\BGI");
E=0;
if(!ShowBMP(0,0,"C:\imagen.bmp")) E=1;
getch();
closegraph();
if(E) printf("nError.");
else printf("Sucess !");
}
//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
相关文章:
- 使用C++获取程序的 ASM
- 在 c++ 中使用 x86 DIV 的这个 asm 块有什么用?
- g++ -O3 为 loop 创建了奇怪的指令 - 两个具有相同 asm 的版本
- Extern "C"错误在'int'之前'asm'或'__attribute__'
- 了解 C/C++ 中 Windows / MSVC 的一些反调试内联 asm
- 尝试使用 x86 asm SSSE3 将大端转换为小端序
- 如何使用"asm volatile"编写 btr 指令
- objdump 不显示机器代码,但显示 ASM
- 未使用的 asm() 在不受支持的体系结构上的行为
- 内联asm编译器屏障(内存阻塞器)是算作外部函数,还是算作静态函数调用
- std::mutex::lock() 产生奇怪(和不必要的)ASM 代码
- 为什么发出空asm命令会交换变量
- C++-更改另一个文件的ASM指令
- 尝试用纯 c 编程 arduino,得到以下错误:预期的"="、""、";",'asm'或"打印"之前的'__attribute__'|
- C++/ASM:按值传递使用 xmm0,但按引用使用 rdi
- 有人知道可以使用 c++ 以字节为单位编码 asm 字符串的任何东西吗?
- 优化了 VC++ 和 ASM 中的代码
- 如何在 Linux asm 中正确返回双精度值
- 使用读取[EBP 4]的MSVC内联ASM移植到64位
- 从 ASM 调用C++代码中标准库的链接