Patryk
Patryk

Reputation: 3152

Writing .com program's environment (NASM)

I'm having problem with writing .com program's environment. I know, it's placed at es:2ch when es points at the beginning of the psp. Here's my code:

    org 100h
    mov cx,256
    mov ah,2


    xor si, si  ; si = 0
    mov si,[es:si]  ; si = begining of the psp
    sub si, 2ch ; adding 2ch offset to si
    mov si, [si]    ; making si point to com environement



loop1:   ; output chars until '0'
    mov dl, [si]
    inc si      
    cmp dl, '0' 
    je end_of_program           
    int 21h

loop loop1



end_of_program:
    mov ah, 0
    int 16h 

    mov ah, 4ch
    int 21h

output:

R♥˙{  T♥ |      `♦­☺Ç☻  ▼ ţ☺  IT
♦☻  NLł☻  PO┬☺  NO×☻  CFÓ☻  SV┤☻  SU╩☻  LA▲♥  DV4♥  RU÷☻  BRJ♥
HUĘ♥  ISż♥  PLď♥  ROŕ♥  SL ♦  YU▬♦  TRB♦  ETn♦  JPX♦  USä♦

Upvotes: 0

Views: 241

Answers (2)

Frank Kotler
Frank Kotler

Reputation: 3119

As I recall, the "environment" at PSP:2Ch is a segment address. While we can't load a segreg with an immediate or register, we can load one from memory...

mov ds, [2Ch]

Then do your loop starting from offset 0. This will only print the first of your environment variables. When that loop ends, check for another 0. If not, run your loop again (might want to throw in a CR/LF). When you get to the double zero, there's another word(?), and then the program name. Of course, at this point we've lost ds... but cs and es both still point to our original PSP so it can be easily restored, if needed...

Upvotes: 2

Michael
Michael

Reputation: 58507

See the Program_Segment_Prefix entry at Wikipedia.

The segment address of the PSP is passed in the DS register when the program is executed.
...
Alternatively, in .COM programs loaded at offset 100h, one can address the PSP directly just by using the offsets listed above. Offset 000h points to the beginning of the PSP, 0FFh points to the end, etc.

So you could e.g. read the command that was used to start your program from offset 81h and onwards.

Upvotes: 0

Related Questions