	FORMAT MZ
	; DOS 16-bit EXE format
	STACK 200h
	HEAP 0
	ENTRY CODE16:ShowMsg

	
SEGMENT CODE16 USE16
ShowMsg:
	call CODE4:ftest
	mov	ax,4c00h	 ; Send exit code to dos
	int	21h		; Send command to DOS

	
SEGMENT DATA16 USE16
	Msg db "Hello World!$"

SEGMENT DATA162 USE16
	Msg2 db "Hello World!$"
	


SEGMENT CODE4 USE16
ftest:
	mov ax,DATA2
	mov ds,ax

	mov ah,48h
	mov bx,4000 ; Allocate 128K
	int 21h
	jc nomemm
	mov [off],ax
	mov ds,ax
	mov cx,60000
	xor si,si

loopp:
	mov byte [ds:si],44
	inc si
	loop	loopp

	mov ax,DATA2
	mov ds,ax
	

	mov ax,0b800h
	mov es,ax
	mov byte [es:1040],65

	pushf
	pop ax
	test ax,ax
	js error

	mov cx,4000
	mov si,buffer
loop1:
	mov byte [ds:si],66
	inc si
	loop loop1	

	xor ax,ax
	mov si,buffer
	add si,5
	mov al,[ds:si]
	call printAscifiedInt

	push es
	mov ax,4300h
	int 2fh
	cmp al,80h
	jne noxms
	mov ax,4310h
	int 2fh
	mov word [xmoDriverAddr],bx
	mov word [xmoDriverAddr+2],es
	mov ah,08h
	call [xmoDriverAddr]	; Query free memory
	call printAscifiedInt
	pop es

	mov ah,09h
	mov dx,4		; Allocate 4K
	call [xmoDriverAddr]
	cmp ax,1
	jne noxms
	mov [xmmsHandle],dx

	mov ah,09h
	mov dx,40		; Allocate 40K
	call [xmoDriverAddr]
	cmp ax,1
	jne noxms
	mov [xmmsHandle2],dx

	mov ax,DATA2
	mov ds,ax

	mov ah,0bh		; Move XMS Block
	mov dx, [xmmsHandle]
	mov dword [msize], 4000	; Move 4000 bytes
	mov word [source_handle],0 ; From conventional memory
	mov word [source_offset],0 ; Offset 0
	mov word [source_offset+2],es ; Segment ES
	mov word [destination_handle],dx
	mov dword [destination_offset], 0
	mov si,movestruc
	call [xmoDriverAddr]

	cmp ax,1
	jne noxms

	mov ah,0bh
	mov dx, [xmmsHandle2]
	mov dword [msize], 40960
	mov word [source_handle],0
	mov word [source_offset],buffer
	mov word [source_offset+2],ds
	mov word [destination_handle],dx
	mov dword [destination_offset], 0
	mov si,movestruc
	call [xmoDriverAddr]

	cmp ax,1
	jne noxms

	mov cx,2000
	mov bx,0
loopa1:
	mov word [es:bx],0357h
	add bx,2
	loop loopa1	

	mov ah,10h
	int 16h

	mov cx,4000
	mov si,buffer
loop2:
	mov byte [ds:si],99
	inc si
	loop loop2


	mov ah,0bh
	mov dx, [xmmsHandle]
	mov dword [msize], 4000
	mov word [source_handle],dx
	mov dword [source_offset],0
	mov word [destination_handle],0
	mov word [destination_offset], 0
	mov word [destination_offset+2], es

	mov si,movestruc
	call [xmoDriverAddr]

	cmp ax,1
	jne noxms

	xor ax,ax
	mov al, byte [ds:buffer]
	call printAscifiedInt


	mov ah,0bh
	mov dx, [xmmsHandle2]
	mov dword [msize], 40960
	mov word [source_handle],dx
	mov dword [source_offset],0
	mov word [destination_handle],0
	mov word [destination_offset], buffer
	mov word [destination_offset+2], ds

	mov si,movestruc
	call [xmoDriverAddr]

	cmp ax,1
	jne noxms

	xor ax,ax
	mov al, byte [ds:buffer]
	call printAscifiedInt

	mov ah,0ah
	mov dx,[xmmsHandle]
	call [xmoDriverAddr]
	cmp ax,1
	jne noxms

	mov ah,0ah
	mov dx,[xmmsHandle2]
	call [xmoDriverAddr]
	cmp ax,1
	jne noxms


	;	jmp end3
	retf

end3:
	mov	ax,4c00h	 ; Send exit code to dos
	int	21h		; Send command to DOS
fail:
	mov ax,0900h
	mov dx,noxmsm
	int 21h
	jmp end3

freefail:
	mov ax,0900h
	mov dx,freefailm
	int 21h
	jmp end3
error:
	mov ax,0900h
	mov dx,no286
	int 21h
	jmp end3

noxms:
	mov ax,0900h
	mov dx,noxmsm
	int 21h
	jmp end3

nomemm:
	mov ax,0900h
	mov dx,dood
	int 21h
	jmp end3


	

printAscifiedInt:
	;initilize count
	pusha
	push ds
	push ax
	mov ax,DATA2
	mov ds,ax
	pop ax
	mov cx,0 
	mov dx,0 
label1: 
        ; if ax is zero 
        cmp ax,0 
        je print1       
        
        ;initilize bx to 10 
        mov bx,10         
        
        ; extract the last digit 
        div bx                   
        
        ;push it in the stack 
        push dx               
        
        ;increment the count 
        inc cx               
        
        ;set dx to 0  
        xor dx,dx 
        jmp label1 
print1: 
        ;check if count  
        ;is greater than zero 
        cmp cx,0 
        je exit
        
        ;pop the top of stack 
        pop dx 
        
        ;add 48 so that it  
        ;represents the ASCII 
        ;value of digits 
        add dx,48 
        
        ;interuppt to print a 
        ;character 
        mov ah,02h 
        int 21h 
        
        ;decrease the count 
        dec cx 
        jmp print1 
exit:
	mov ah,09h
	mov dx,entt
	int 21h
	pop ds
	popa
	ret 


SEGMENT DATA2 USE16
	entt db 10,13,24h
	testam db "Helllloooooo!$"
	no286 db "You need at least a 286",10,13,24h
	noxmsm db "No XMS Support",10,13,24h
	dood db "DOOOD",10,13,24h
	freefailm db "Could not free memory",10,13,24h
	xmoDriverAddr dd 0
	ascifiedint db 32
	aaaa db 24h
	ascifiedint_length dd 4
	xmmsHandle dw 0
	xmmsHandle2 dw 0
	off dw 4
movestruc:
	msize dd 0
	source_handle dw 0
	source_offset dd 0
	destination_handle  dw 0
	destination_offset dd 0
buffer2:
	buffer rb 40960

align 64
SEGMENT ENDSEG
	
