flat assembler
Message board for the users of flat assembler.

flat assembler > DOS > FASMHX Sample

Author
Thread Post new topic Reply to topic
Japheth



Joined: 26 Oct 2004
Posts: 151
Hello,

I made a FASM sample of a 32bit DOS extended binary with a DPMI host included. Thus no external modules are needed when running in DOS.

http://www.japheth.de/Download/fasmhx.zip

The DPMI host which is included (HDPMI32) has full DOS API translation, that is, you can directly use the int 21h API with pointers.
Post 03 Sep 2007, 08:13
View user's profile Send private message Visit poster's website Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
Cute Smile
Post 03 Sep 2007, 10:38
View user's profile Send private message Visit poster's website Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1904
> I made a FASM sample of a 32bit DOS extended binary with a DPMI host included

Thanks. Smile
Post 04 Sep 2007, 20:37
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1904
Code:
    format MZ

    entry _TEXT:start

    segment _TEXT

    use16

inithdpmi: include "HDPMI32.INC"

start:

    push cs
    pop  ds

    mov ax, ss
    mov cx, es
    sub ax, cx

    mov bx, sp
    shr bx, 4
    inc bx
    add bx, ax         ; release unused DOS memory Confused
    mov ah, 4Ah        ; BX new size | ES segment
    int 21h

    mov ax, 1687h      ; DPMI host installed?
    int 2Fh
    and ax, ax
    jz havehost

    mov ah,51h         ; Pick PSP
    int 21h

    mov es,bx

    push bp            ; ??? Why ? Confused
    push cs            ; HDPMI init code returns with a RETF!
    call inithdpmi
    pop bp

    mov ax, 1687h      ; is DPMI host now installed?
    int 2Fh
    and ax, ax
    jnz nohost         ; Possible ??? Confused
     
havehost:    
    push es             ;save DPMI entry address
    push di
    and si, si          ;requires host client-specific DOS memory?
    jz nomemneeded

    mov bx, si
    mov ah, 48h         ;alloc DOS memory
    int 21h
    jc nomem            ; Sad
    mov es, ax

nomemneeded:
    mov bp, sp
    mov ax, 0001        ;start a 32-bit client
    call far [bp]       ;initial switch to protected-mode Smile
    jc initfailed
                        ;now in protected-mode
    mov cx,1            ;get a descriptor for the 32-bit code segment
    mov ax,0
    int 31h
    jc dpmierr
    mov bx,ax
    mov dx,_TEXT32
    mov cx,dx
    shl dx,4
    shr cx,12
    mov ax,7            ;set base
    int 31h
    or dx,-1            ; Save 1 byte ???
    xor cx,cx
    mov ax,8            ;set limit
    int 31h
    mov cx,cs
    lar cx,cx
    shr cx,8
    or  ch,40h          ;make a 32bit CS
    mov ax,9
    int 31h
    push bx
    push start32
    retf                ;jump to 32bit CS
nohost:
    mov dx, dErr1
error:    
    mov ah, 9
    int 21h
    mov ax, 4C00h
    int 21h
nomem:
    mov dx, dErr2
    jmp error
initfailed:
    mov dx, dErr3
    jmp error
dpmierr:
    mov dx, dErr4
    jmp error

;       segment _DATA

    use16

szWelcome db "welcome in protected-mode",'$'
dErr1 db "no DPMI host installed",'$'
dErr2 db "not enough DOS memory for client initialisation",'$'
dErr3 db "DPMI initialisation failed",'$'
dErr4 db "no LDT descriptors available",'$'

;--- the 32-bit code segment

        segment _TEXT32

    use32

start32:
    mov edx, szWelcome  ;print welcome message
    mov ah,9
    int 21h                 ; Smile
    mov ax, 4C00h       ;return to DOS
    int 21h
    


-----------------------------------------------------------------------------------

HDPMI size reduced by almost 2 KiB Smile

Code:
db 139, 65, 16,102,137, 71,  1,131,199, 16, 89, 31,195,249,235,250
db  30,106, 75, 31, 15,183,254,193,231,  4,102,199, 71,241,  0,  0
db  31,195,139,255, 87,102, 90,102, 89,139,216,102,184,  7,  0,205
db  49,114, 32, 35,246,116, 27, 78,247,198,  0,  0,240,255,116,  5
db 102,129,206,255, 15, 86,102, 90,102, 89,102,184,  8,  0,205, 49
db 114,  1,248,195, 96,139,116, 36, 40,139,124, 36, 36,102,185,  1
db   0, 51,192,205, 49,114, 10,102,137, 68, 36, 28,232,179,255,255
db 255, 97,194,  8,  0,141, 73,  0, 96,139,240, 15,183,250,139,223
    


8 from 2128 lines of HDPMI32.INC - funny to read Laughing

-----------------------------------------------------------------------------------

Code:
if STANDALONE=0
szHDPMI32 db "HDPMI32.EXE",0
end if ; Confused
execp dw 0
      dw dmycmd
execs dw 0
      dd 0,0
dmycmd db 0,13      
; end if ; Better here ? Wink
    


end if should move ?

Code:
    push bp    ; Why Confused
    push cs     ;HDPMI init code returns with a RETF!
    call inithdpmi
    pop bp
    


Why push'ing bp here ?

Also there is a magic with DOS memory release - what is exactly being done and is it required ?

_________________
Bug Nr.: 12345

Title: Hello World program compiles to 100 KB !!!

Status: Closed: NOT a Bug
Post 05 Sep 2007, 01:48
View user's profile Send private message Reply with quote
Japheth



Joined: 26 Oct 2004
Posts: 151
> Cute Smile

Cool

> Also there is a magic with DOS memory release - what is exactly being done and is it required ?

the DPMI host needs 8 kB for a translation buffer.

> end if should move ?
> Why push'ing bp here ?

code is not optimized (yet), just working.

Code:
    mov ax, 1687h      ; is DPMI host now installed?
    int 2Fh
    and ax, ax
    jnz nohost         ; Possible ??? Confused
    


Yes. Try to run the sample under (J)emm386 with NOVCPI.

> HDPMI size reduced by almost 2 KiB

command line parsing and help text are removed
Post 05 Sep 2007, 05:46
View user's profile Send private message Visit poster's website Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 688
Location: Adelaide
I've been looking for an example of using DPMI for ages...there doesn't seem to be any around (even in C :shudder:).
Your bit of code has shown me the way to go about it - thanks Japheth.

BTW I am using the DPMI host in XP and not your built-in one, but the theory is there. Once again, thanks mate Very Happy
Post 05 Sep 2007, 06:27
View user's profile Send private message Reply with quote
Japheth



Joined: 26 Oct 2004
Posts: 151
sinsi wrote:
I've been looking for an example of using DPMI for ages...there doesn't seem to be any around (even in C :shudder:).
Your bit of code has shown me the way to go about it - thanks Japheth.

Thanks for you kind words, sinsi! However, if you ever downloaded the FASM package in recent years you could have found the USEDPMI sample very easily. In fact, I took this sample and just modified it slightly.
Post 06 Sep 2007, 07:00
View user's profile Send private message Visit poster's website Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1904
Quote:
I've been looking for an example of using DPMI for ages...


Why didn't you ask ? Idea

Quote:
there doesn't seem to be any around


Wrong Sad There are USEDPMI (by Tomasz, since FASM 1.0 in 2000 Laughing ) and my "LE" / DOS/32A example Laughing

Quote:
(even in C :shudder:)


Use __any__ "Hello world" program ... and a 32-bit DOS compiler Laughing

Quote:
BTW I am using the DPMI host in XP and not your built-in one


Then you have 33 KiB of dead code Sad

Code:
    format MZ

    heap 0

    entry _TEXT:start

    segment _TEXT

    use16

inithdpmi: include "HDPMI32.INC"

start:

    push cs
    pop  ds

    mov  ax, 1687h      ; DPMI host installed?
    int  2Fh
    test ax, ax
    jz   havehost

    mov  es, PSP    ; Get it from somewhere Laughing

    push cs            ; HDPMI init code returns with a RETF!
    call inithdpmi

    mov ax, 1687h      ; is DPMI host now installed?
    int 2Fh
    test ax, ax
    jnz nohost         ; Possible if "V86" NOVCPI or similar silly reasons
     
havehost:    
    push  es           ; Save DPMI entry address
    push  di
    test  si, si       ; Requires host client-specific DOS memory?
    jz    @f

    mov  bx, si
    mov  ah, 48h         ; Alloc DOS memory
    int  21h
    jc   nomem           ; Sad
    mov  es, ax

@@:
    mov  bp, sp
    mov  ax, 1          ; start a 32-bit client
    call far [bp]       ; initial switch to protected-mode Smile
    jc   initfailed
                        ;now in protected-mode

    mov  cx,1            ;get a descriptor for the 32-bit code segment
    mov  ax,0
    int  31h
    jc   dpmierr
    mov  bx,ax
    mov  dx,_TEXT32
    mov  cx,dx
    shl  dx,4
    shr  cx,12
    mov  ax,7                ; set base
    int  31h
    or   dx,$FFFF            ; Save 1 byte ???
    xor  cx,cx
    mov  ax,8                ; set limit
    int  31h
    mov cx,cs
    lar cx,cx
    shr cx,8
    or  ch,40h          ;make a 32bit CS
    mov ax,9
    int 31h
    push bx
    push start32
    retf                ;jump to 32bit CS
;-------
nohost:
    mov dx, dErr1
error:    
    mov ah, 9
    int 21h
    mov ax, $4C00
    int 21h
nomem:
    mov dx, dErr2
    jmp error
initfailed:
    mov dx, dErr3
    jmp error
dpmierr:
    mov dx, dErr4
    jmp error

szWelcome db "welcome in protected-mode",'$'
dErr1 db "no DPMI host installed",'$'
dErr2 db "not enough DOS memory for client initialisation",'$'
dErr3 db "DPMI initialisation failed",'$'
dErr4 db "no LDT descriptors available",'$'

;--- the 32-bit code segment

        segment _TEXT32

    use32

start32:
    mov edx, szWelcome      ; Print welcome message
    mov ah,9
    int 21h                 ; Smile
    mov ax, 4C00h           ; Return to DOS
    int 21h
    


> the DPMI host needs 8 kB for a translation buffer.

Thanks ... but my point was something else Neutral Removing the "release" code results (surprisingly) in "out of memory", nevertheless, heap 0 fixes the bug again ... is it safe to use heap 0 or are there arguments to hog all the low memory first and release it then ?

> Try to run the sample under (J)emm386 with NOVCPI.

Obviously ... no need to test ... Sad

What are the requirements of this version of HDPMI ? Just sitting at begin of RM segment (seem to be no fixups) and far call to IP=0 ? PSP in ES is required or no ? Can I call inithdpmi and mode-switch with ES=0 or ES=CS ?

There seems to be a big magic about this PSP deal in DPMI ... but isn't it just a relict from 16-bit DPMI ?

Quote:

The environment pointer in the current program's
PSP will automatically be converted to a descriptor.


Confused

DPMI host will hack it ? And maybe change back on every switch to RM ? So I should pick PSP and envir address before switch to PM ?


Last edited by DOS386 on 07 Sep 2007, 22:47; edited 1 time in total
Post 06 Sep 2007, 22:45
View user's profile Send private message Reply with quote
Japheth



Joined: 26 Oct 2004
Posts: 151
Quote:
heap 0 fixes the bug again ... is it safe to use heap 0 or are there arguments to hog all the low memory first and release it then ?

yes, no.

Quote:
What are the requirements of this version of HDPMI ? Just sitting at begin of RM segment (seem to be no fixups) and far call to IP=0 ? PSP in ES is required or no ? Can I call inithdpmi and mode-switch with ES=0 or ES=CS ?


it expects to reside at the begin of a segment. It might be a good idea to put it in the very first segment of a binary. ES must contain the current PSP.

Quote:

Quote:

The environment pointer in the current program's
PSP will automatically be converted to a descriptor.


Confused

DPMI host will hack it ? And maybe change back on every switch to RM ? So I should pick PSP and envir address before switch to PM ?


the environment pointer in the PSP is never used by DOS, no need to change it once it has been converted to a selector.
Post 07 Sep 2007, 06:20
View user's profile Send private message Visit poster's website Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1904
Thanks.

> ES must contain the current PSP.

otherwise what disaster can happen ? Laughing

And ES=DS=CS=SS is good for mode switch call ? After switch to PM, will anyone except my application use my stack ? So I could create a new one in DPMI memory and [ab]use the RM one for something else (transfer buffer for ex.) ?
Post 07 Sep 2007, 22:45
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2019, Tomasz Grysztar.

Powered by rwasa.