Accessing the Program Segment Prefix

回眸只為那壹抹淺笑 提交于 2021-02-08 07:34:47

问题


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.

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

回答1:


I suspect it's because INT 21h subfunction 9 requires '$' to terminate string.

Also, I think mov dx, ds:[81h] should be mov dx, 81h since the DS is already loaded as required by the interrupt.

Consider something like:

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

This is a useful resource for interrupt APIs:

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



来源:https://stackoverflow.com/questions/45743751/accessing-the-program-segment-prefix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!