首页 > 解决方案 > DOS可执行文件中self的完整路径

问题描述

在我的 16 位 DOS 程序中,我想使用 DOS 中断或其内部表获取程序实例的完整路径。换句话说,我正在寻找Windows API 函数GetModuleFileName(NULL)的 DOS 等效项

中断 21h/AH=60h似乎是一个正确的轨道,但当程序不在当前目录中时它会失败。我做了一个简单的测试程序:

MYTEST PROGRAM FORMAT=COM
    MOV AH,60h    ; TRUENAME - CANONICALIZE FILENAME OR PATH.
    MOV SI,MyName
    MOV DI,MyFullName
    INT 21h       ; Convert filename DS:SI to canonizalized name in ES:DI.
    MOV AH,09h    ; WRITE STRING$ TO STARNDARD OUTPUT.
    MOV DX,DI
    INT 21h       ; Display the canonizalized name.
    RET           ; Terminate program.
MyName     DB "MYTEST.COM",0 ; The ASCIIZ name of self (this executable program).
MyFullName DB 256 * BYTE '$' ; Room for the canonizalized name, $-terminated.
  ENDPROGRAM MYTEST

它被创建为“C:\WORK\MYTEST.COM”并在 Windows 10/64 位的 DOSBox 中运行:

C:\WORK>dir
MYTEST   COM     284 Bytes.
C:\WORK>mytest
C:\WORK\MYTEST.COM      REM this works as expected.
C:\WORK>d:
D:\>c:mytest
D:\MYTEST.COM           REM this is wrong, no such file exists.
D:\>

有人知道如何在 16 位汇编程序中获取 argv[0] 吗?

标签: assemblydosx86-16

解决方案


根据 DOS 版本,您可能会使用未记录的事实,即可以在环境变量之后找到文件名。如:

org 100h

    mov ax, [2Ch]    ; segment of environment from PSP
    mov ds, ax
    xor si, si
findloop:
    cmp word [si], 0 ; one zero for end of string, another for end of table
    lea si, [si+1]
    jne findloop
    lodsb            ; skip end of table
    lodsw            ; number of additional strings (?)
    cmp ax, 1
    jne error
    mov ah, 2
printloop:
    lodsb
    test al, al
    jz done
    mov dl, al
    int 21h
    jmp printloop
done:
error:
    mov ax, 4C00h
    int 21h

至少在 dosbox 中,这给出了完整路径。在不同的操作系统下,您可能需要结合当前目录甚至搜索PATH,如果它可以工作的话。


推荐阅读