?
Solved

MASM, copying one string (parameter) to another string variable

Posted on 2005-04-05
2
Medium Priority
?
737 Views
Last Modified: 2008-03-04
I would like to write program that store data entere from keypord in  a text file. The name of text file is gicen as a parameter in the command line.

When I want to copy the file name ( enetered as parameter) stored in PSP, something goes wrong and instead of storing inputed data in file I given, the data is stored in the file which name is "!!!!!!!"

What can be wrong,
Thanks for any help

The code is listed below( I marked section which doesn't function properly):

comment %
*********************************************
* copy characters from keyboard to the file *
*********************************************
%

.model small

.stack 512

.data
text_file BYTE 256 DUP(?)

character db ?
handle    dw ?

.code
      
      assume ds:nothing
beginning:
        mov bx, 80h             ; address of parameter in PSP block
        mov cl, ds:[bx]         ; parameter length
        xor ch, ch              ; cx - length
        or  cx, cx              ; is the parameter string empty?
        jz  ending


      mov ax, @data
      mov es, ax
      mov di, offset text_file

looping1:
         ; ****!!!!! here (below) is something wrong I suppose
       inc bx
       mov di,bx
      mov dl, ds:[bx]       ; load next character
             mov text_file[bx], dl

       loop looping1

        ;*****!!!!!! from this point everythink works fine

      assume ds:@data

        mov ax, @data           ; take address of data segment
        mov ds, ax              ; set the segment register
        mov ah, 3Dh             ; open file
        mov al, 1
        mov dx, offset text_file
        int 21h
        jnc opened              ; file opened
        mov ah, 3Ch             ; create file
        mov dx, offset text_file
        mov cx, 0               ; ordinary file
        int 21h
        jc  ending              ; jump if error
opened:
        mov handle, ax
        mov ah, 42h             ; go to the end of file
        mov bx, handle
        xor cx, cx              ; zero position
        xor dx, dx
        mov al, 2               ; from the end of the file
        int 21h
looping:
        mov ah, 08h
        int 21h                 ; read the character
        or  al, al              ; is character zero?
        jnz ok                  ; normal character
        mov ah, 08h             ; read second byte
        int 21h                 ; if special code
        jmp looping
ok:
        cmp al, 1Ah             ; Ctrl-Z - end of the text
        je  closing
        mov character, al
        mov ah, 02h
        mov dl, al
        int 21h                 ; display the character
        mov ah, 40h             ; write it to the file
        mov bx, handle
        mov dx, offset character
        mov cx, 1               ; one character
        int 21h
        jmp looping
closing:
        mov ah, 3Eh
        mov bx, handle
        int 21h
ending:
      mov ax, 4C00h           ; end of the program
      int 21h

end beginning
0
Comment
Question by:adamgasior
2 Comments
 
LVL 5

Accepted Solution

by:
mzvika earned 340 total points
ID: 13714683
in the line
      mov text_file[bx], dl
you access the variable which, by default, is taken as offset into DS.
but instaed, you initialized ES
     mov ax, @data
     mov es, ax
there are special instructions to store data into ES:DI --> stosb.
this instruction will put AL into ES:DI and increment DI.
so, your first loop should be

     mov ax, @data
     mov es, ax
     mov di, offset text_file

looping1:
      ; ****!!!!! here (below) is something wrong I suppose
      inc bx
      mov al, ds:[bx]      ; load next character
      stosb

      loop looping1

      ;*****!!!!!! from this point everythink works fine

of course, you could use
      rep movsb
which copies from DS:SI to ES:DI, assuming CX holds the length

        mov si, 80h             ; address of parameter in PSP block
        mov cl, ds:[si]         ; parameter length
        xor ch, ch              ; cx - length
        or  cx, cx              ; is the parameter string empty?
        jz  ending

       mov ax, @data
       mov es, ax
       mov di, offset text_file
       inc si
       rep movsb
0
 
LVL 22

Expert Comment

by:grg99
ID: 13735191
Remember to clear the direction flag with "CLD" or very funny things will happen!

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
If anyone asked you to network diagram of the internet, it was drawn in the form of a fluffy cloud which further became known as cloud computing. Popularly cloud computing is defined as workloads that run over the internet in a commercial provider’s…
Integration Management Part 2
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question