Pergunta

i'm having a bit of trouble with assembly, I'm using TASM for compiling and TLINK for linking (yes I know these are old and outdated, but switching isn't currently an option so please don't suggest that). Here is the situation. A user types in a path in console, program jumps to that directory and tries to open any files inside. I only know how to access files when I know their name. So how would one do something like this?

P.S. though general logic is fine too, if you write any code please comment it, I'm very much new to this.

Foi útil?

Solução

You have to scan the directory. Depending upon your SO, this is done by different ways. For example, if you are in DOS environment, which is what I assume by the tools you are using, this is accomplished by using function 4Eh of int 21h. Then, you can use service 4Fh to get subsequents files.

DS:DX must point to the ASCIIZ string containing the path where you want to search for files. The path must include the file name, or some kind of wilcard (for example *.* if you want to scan the complete directory). This is for function 4Eh. Function 4Fh resumes the scan from the file following the one that was found by function 4Eh or a previous call to function 4Fh.

A code for this would be:

;setup a DTA for scanning directories
        mov dx,offset of your DTA block
        mov ax,segment of your DTA block (normally your current data segment)
        mov ds,ax
        mov ah,1Ah
        int 21h

;setup registers for int 21h,4Eh
;including DS:DX = ASCIIZ string with path and possibly, wilcard.
;......
;......

        mov ah,4Eh
        int 21h
        cmp cx,0
        jnz NoMoreFiles

NextFile:
;Parse DTA to obtain filename and extension of the first file found (at offset 30d)
;......
;......

        mov ah,4Fh
        int 21h
        jc NoMoreFiles
        jmp NextFile

NoMoreFiles:

More details about what DOS services to use and what parameters they expect, here: http://bbc.nvg.org/doc/Master%20512%20Technical%20Guide/m512techb_int21.htm

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top