IDEAL
MODEL small
STACK 100h
DATASEG
;filename   db 'Hamster.bmp',0
filename   db 'sample.bmp',0   ;chng
filehandle dw ?
header     db 54 dup(0)
palette    db 1024 dup(0)
scrline    db 48 dup(0)        ;chng  
;scrLine db 320 dup (0)
errormsg   db 'Error',13,10,'$'  
bmphdrl    equ 54

;imglines   equ 200
;imgcolumns equ 320
imglines   equ 48              ;chng
imgcolumns equ 48              ;chng
; --------------------------
; Your variables here
; --------------------------
CODESEG

proc      openfile
          mov ah,3Dh
          xor al,al
          mov dx, offset filename
          int 21h
          jc  openerror
          mov [filehandle], ax
          ret
openerror:
          mov dx, offset errormsg
          mov ah, 9h
          int 21h
endp      openfile

proc      readheader   
; Read BMP file header, 54 bytes
          mov ah, 3fh
          mov bx, [filehandle]  
          mov cx, bmphdrl  
          mov dx, offset header 
          int 21h
          ret
endp      readheader 

proc      readpalette 
          ; bmp color palette has 256 colors, each is represented by 4 bytes (400 hex)
          mov ah, 3fh
          mov cx, 400h
          mov dx, offset palette 
          int 21h
          ret
endp      readpalette

proc      copypal
          ; copying color palette to te video memory, the number of te first color
          ; goes to port 3C8h and the palette to port 3C9h.
           
          mov si, offset palette
          mov cx, 256
          mov dx, 3C8h
          mov al, 0
          out dx, al  ; port 3C8h gets the starting color
          inc dx
palloop:              
         ; copy the palette in a loop of 256
         ; bmp is saved as color scheme of BGR, so we change the order
          mov al, [si+2]   ; get R (red) from BGR
          shr al, 2        ; divide by 4 (shift right 2 bits) max palette value is: 63
          out dx, al       ; send to port
          mov al,[si+1]    ; get G (green) value
          shr al, 2
          out dx, al
          mov al, [si]     ; get B (blue) value
          shr al, 2
          out dx, al
          add si, 4  
          loop palloop
          ret
endp      copypal

proc      copybitmap
          mov ax, 0A000h
          mov es, ax
          mov cx, imglines
printbmploop:
          push cx
          mov di, cx
          shl cx, 6
          shl di, 8
          add di, cx
          mov ah, 3fh
          mov cx, imgcolumns
          mov dx, offset scrline
          int 21h
          cld
          mov cx, imgcolumns
          mov si, offset scrline
          rep movsb
          pop cx
          loop printbmploop
          ret
endp      copybitmap                                                      


                      
start:
	      mov ax, @data
	      mov ds, ax
	      ; Graphics mode 
	      mov ax, 13h
	      int 10h
	      ; process bmp
	      call openfile
	      call readheader
	      call readpalette
	      call copypal
	      call copybitmap
	      ; wait for key press
	      mov  ah, 1
	      int 21h
	      mov ah, 0
	      mov al, 2
	      int 10h	
exit:
	      mov ax, 4c00h
	      int 21h
END start