访问程序段前缀 [英] Accessing the Program Segment Prefix
问题描述
我正在尝试访问 x86 MASM 汇编器中的程序段前缀 (PSP).作为测试,我想在运行程序后打印给定的命令行参数.我尝试将PSP的地址放入 dx
寄存器中,其偏移量为 81h
:命令行参数的位置.
I'm trying to access the Program Segment Prefix (PSP) in x86 MASM Assembler. As a test, I'd like to print the given command line arguments after running my program. I tried putting the address of the PSP in the dx
register, with an offset of 81h
: the position of the command line arguments.
但是,运行该程序后,我得到此作为回报.我可以看到给定的命令行参数,但是它前面有很多乱码.知道为什么会这样吗?我想我在81h不能正确访问PSP吗?
However, after running the program, I get this in return. I can see the given command line argument, but it is preceded by a lot of gibberish. Any idea why this is happening? I guess I'm not correctly accessing the PSP at 81h?
IDEAL
P386
MODEL FLAT, C
ASSUME cs:_TEXT,ds:FLAT,es:FLAT,fs:FLAT,gs:FLAT
CODESEG
start:
sti ; Set The Interrupt Flag
cld ; Clear The Direction Flag
push ds ; Put value of DS register on the stack
pop es ; And write this value to ES
mov ah, 09h
mov dx, ds:[81h]
int 21h
mov eax, 4c00h ; AH = 4Ch - Exit To DOS
int 21h ; DOS INT 21h
DATASEG
STACK 1000h
END start
推荐答案
我怀疑是因为INT 21h子函数9要求使用'$'终止字符串.
I suspect it's because INT 21h subfunction 9 requires '$' to terminate string.
此外,我认为 mov dx,ds:[81h]
应该是 mov dx,81h
,因为DS已根据中断的要求进行了加载.
Also, I think mov dx, ds:[81h]
should be mov dx, 81h
since the DS is already loaded as required by the interrupt.
请考虑以下内容:
IDEAL
P386
MODEL FLAT, C
ASSUME cs:_TEXT,ds:FLAT,es:FLAT,fs:FLAT,gs:FLAT
CODESEG
start:
sti ; Set The Interrupt Flag
cld ; Clear The Direction Flag
push ds ; Put value of DS register on the stack
pop es ; And write this value to ES
; INT 21h subfunction 9 requires '$' to terminate string
xor bx, bx
mov bl, [80h]
cmp bl, 126
ja exit
mov byte [bx + 81h], '$'
; print the string
mov ah, 09h
mov dx, 81h
int 21h
exit:
mov eax, 4c00h ; AH = 4Ch - Exit To DOS
int 21h ; DOS INT 21h
DATASEG
STACK 1000h
END start
这是中断API的有用资源:
This is a useful resource for interrupt APIs:
http://spike.scu.edu.au/~barry/interrupts.html
这篇关于访问程序段前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!