0

My program asks for a filename to be read, then it should produce a clone of that file with the filename "Clone_originalfilename". This is my block of code:

.data
filename db 100 
         db ?
         db 100 dup (0)
copyfile db "Clone_", 0

.code
mov dx, offset filename ; reads the filename entered by user
mov ah, 0Ah
int 21h

mov si, offset filename + 1 ; replaces the last character (Enter) to '$'
mov cl, [ si ] 
mov ch, 0      
inc cx 
add si, cx 
mov al, '$'
mov [ si ], al 

; concatenate "Clone_" to the filename 
lea si, filename
lea di, copyfile

L0: 
    cmp byte ptr [di], '_' 
    jz exL0
    inc di
    jmp L0
exL0:
    inc di 
    add si, 2 
    xor bx, bx 
L1:
    cmp byte ptr [si], 0    
    jz exL1
    mov bl, byte ptr [si]   
    mov byte ptr [di], bl

    inc si
    inc di
    jmp L1
exL1:
    inc di
    mov bl, byte ptr [si]       
    mov byte ptr [di], bl


mov dx, offset filename 
mov al, 2
mov ah, 3Dh
int 21h

mov handle, ax
jc erroropening
....

but whenever I execute it, the program proceeds to erroropening which displays that the file cannot be read, I think the problem's in the concatenation of the strings but I have no idea how to fix it. Sorry I'm a newbie here.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
rjk
  • 61
  • 5
  • 1
    I would recommend stepping through the code with a debugger (even debug.exe or get a copy of turbo debugger td.exe) to observe what happens. Learning to use a debugger is a valuable skill. – Michael Petch Oct 31 '15 at 17:58
  • 1
    If you are creating an EXE (not COM) program then you need to initialise the Data Segment (DS) when your program starts. Usually with `mov ax,@data` followed by `mov ds,ax` – Michael Petch Oct 31 '15 at 18:04
  • 1
    Why title and tag <[tag:masm32]>? – rkhb Oct 31 '15 at 18:37

1 Answers1

3

There are a lot of mistakes in the short snippet. Let's assume you've forgotten to copy

.model small
.stack 1000h

and

.code
start:

END start

then remains:

  1. There is no initialization of DS:

    mov ax, @data
    mov ds, ax
    

    So at least lea di, copyfile won't load the correct address. filename was stored at the wrong place.

  2. You don't "concatenate "Clone_" to the filename" but vice versa. For that purpose you don't have enough space after copyfile db "Clone_", 0.

  3. You replace the last character of the input with '$' but test the string afterwards for null (cmp byte ptr [si], 0).

  4. In a DOS environment you can handle only with 8.3 filenames. I'm betting your filename Clone_... is too long.

  5. On offset filename you won't find a filename but the begin of the input structure of int 21h/ah=0Ah.

rkhb
  • 14,159
  • 7
  • 32
  • 60
  • For point #5 you could add a reference to something like Ralph Brown's interrupt list for [int 21h/ah=0ah](http://www.ctyme.com/intr/rb-2563.htm) . I'd up vote then. – Michael Petch Oct 31 '15 at 18:42
  • @MichaelPetch: Done :-). I guess the OP should have known that, because `filename` is defined like a structure and the concatenation starts with `lea si, filename...add si, 2`. – rkhb Oct 31 '15 at 19:18
  • 2
    Unless he was copying and pasting snippets of code from around the internet and didn't have any idea how it worked ;-) – Michael Petch Oct 31 '15 at 19:55
  • I didn't include the others I just decided to out the snippet where I think my code has been wrong. – rjk Nov 01 '15 at 03:45