Show 640x480 BMP image with inline ASM c++

753 views Asked by At

Hello i'm trying to show a 640x480 BMP image (16 color bitmap) with inline ASM in c++, it has to be with inline asm because it's a homework. I have this code in assembly code to do that:

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

Now to put it in inline ASM i'm trying with the next code:

#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();
}

the code reaches to the first cout and then enters in an infinite loop.

2

There are 2 answers

1
Steve On

Do you really mean the following line of code:

for(int i=118;i>0;i++)

This initializes i to 118, and every iteration, adds 1. It will only ever get larger (until i overflows). The test for whether the loop should continue is i > 0, which will always be true (until i overflows).

Are you sure you're in an infinite loop? Maybe the many, many int 21h just take a very, very long time.

1
AudioBubble On

I figured out how to show an 640x200 16 colors BMP with the next code:

//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//                << 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 !");
}
//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+