; LASTDRV2.ASM -- uses undocumented DOS
; this version fixes a misprint in the first and second printings of
; UNDOCUMENTED DOS, p. 45

        assume cs:_TEXT, ds:_DATA, ss:_STACK

_STACK  segment para stack 'STACK'
_STACK  ends

_DATA   segment word public 'DATA'
msg     db      'LASTDRIVE='
dletter db      (?)
        db      0dh, 0ah, '$'
_DATA   ends

_TEXT   segment word public 'CODE'

        public  _lstdrv

_lstdrv proc    far
        push    si
        push    bx
        push    cx

        mov     si, 1Bh         ; assume DOS 3.0

        mov     ax, 3000h       ; Get MS-DOS version number
        int     21h             ; major=AL, minor=AH
        cmp     al, 2
        jl      fail            ; Requires DOS 2+

        jne     dos3up          ; DOS 3+
        mov     si, 10h         ; DOS 2.x
        jmp     short get
dos3up: cmp     al, 3
        jne     ofs21
        and     ah, ah          ; DOS 3.0
        jz      get
ofs21:  mov     si, 21h         ; DOS 3.1+, DOS 4.x
        
get:    mov     ah, 52h         ; Get List of Lists
        xor     bx, bx          ; Zero out ES:BX so we can check
        mov     es, bx          ;   for NULL after INT 21h
        int     21h             ; list=ES:BX
        mov     cx, es
        or      cx, bx          ; Is ES:BX NULL?
        jz      fail            ; Function 52h not supported

        mov     al, byte ptr es:[bx+si]
        xor     ah, ah          ; return LASTDRIVE in AX
        jmp     short leave

fail:   xor     ax, ax          ; return 0 in AX
leave:  pop     cx
        pop     bx
        pop     si
        ret
_lstdrv endp

main    proc    near
        mov     ax, _DATA
        mov     ds, ax

        call    _lstdrv
        and     ax, ax          ; test for failure
        jz      done

        mov     bl, al          ; save LASTDRIVE in BL
        add     al, ('A' - 1)   ; convert LASTDRIVE to drive letter
        mov     dletter, al     ; insert into string

        mov     ah, 9           ; Display String
        mov     dx, offset msg
        int     21h

done:   mov     ah, 4Ch         ; Return to DOS
        mov     al, bl          ; exit code
        int     21h
main    endp

_TEXT   ends

        END     main
