访问程序段前缀

时间:2017-08-17 19:43:34

标签: assembly x86 masm

我正在尝试访问x86 MASM汇编程序中的程序段前缀(PSP)。作为测试,我想在运行程序后打印给定的命令行参数。我尝试将PSP的地址放在dx寄存器中,偏移量为81h:命令行参数的位置。

但是,在运行程序后,我得到this作为回报。我可以看到给定的命令行参数,但它之前有很多乱码。知道为什么会这样吗?我猜我在81h没能正确访问PSP?

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

1 个答案:

答案 0 :(得分:3)

我怀疑它是因为INT 21h子功能9需要' $'终止字符串。

此外,我认为mov dx, ds:[81h]应为mov dx, 81h,因为DS已根据中断的要求加载。

考虑类似的事情:

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的有用资源:

http://spike.scu.edu.au/~barry/interrupts.html

相关问题