assembly - 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] 吗?
解决方案
根据 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
,如果它可以工作的话。
推荐阅读
- python - 比较两个数据框的日期列并保持具有相同日期的行
- c# - 在 ASP.NET MVC 中将变量整数转换为百分比
- java - 如何使用包名为 java 类定义 Dockerfile?
- html - Div 没有出现在 Html 和 Css 中
- compiler-errors - 当我们有 2 个带有 NVLink 硬件组件的 NVIDIA/CUDA GPU 卡时如何正确使用 -Xnvlink 编译器选项
- javascript - 如何在错误请求时显示警报,但在成功时重定向?
- networking - 基于信用的流量控制机制
- javascript - 同时应用淡入淡出效果
- java - 找到单个数字的最大完美平方的最快函数?
- objective-c - Mac 应用程序中的编程 NSLayoutConstraint 调整窗口内容视图的大小