Hello,

I want to display content of a file with the name has been entered by the user, but I have to enter the name of the file when I run the program (eg:. / myprogram test.asm). I would not have to specify the file name each time you run, but whether the user to do so when prompted for the name. I said that reading and viewing work properly. That's what I've done so far:

segment .data 
nom_fichier : db "Entrez le nom du fichier : ", 10
nom_fichier_len equ $-nom_fichier
 bufsize dw      1024

segment .bss
fichier        resb 32
 buf     resb    1024
;; Message qui demande à l'utilisateur d'entrer un nom de fichier
	mov eax, 4
	mov ebx, 1
	mov ecx, nom_fichier
	mov edx, nom_fichier_len
	int 0x80
	
	;; Lecture nom du fichier
	mov eax, 3
	mov ebx, 0
	mov ecx, fichier
	mov edx, 32
	int 0x80

  ; get the filename in ebx 
	pop   ebx
    pop   ebx               
    pop   ebx              
    pop   ebx    
	
  ; ouvrir le fichier - open
    mov   eax,  5           ; ouverture
    mov   ecx,  0           ;   lecture seulement
    int   80h               
  ; lire le fichier - read
    mov     eax,  3         ; lecture
    mov     ebx,  eax       ;   descripteur
    mov     ecx,  buf       ;   *buf,
    mov     edx,  bufsize   ;   *bufsize
    int     80h             

  ; afficher le contenu à l'écran- display
    mov     eax,  4         ; affiche à l'écran
    mov     ebx,  1         
    int     80h

Thanks

Dani AI

Generated

— the approach is fine (prompt, read filename, open, read, write) but the posted snippet has a few common mistakes: using pop ebx to "get" the filename (that just pops stack data and clobbers registers), not null-terminating / stripping the trailing newline from the name read from stdin, and not using the file-descriptor returned by open. is also correct that you can avoid interactive input entirely and use argv[1] (stack: [esp] argc, [esp+4] argv0, [esp+8] argv1), but below is a small, working NASM x86-32 example that implements the interactive prompt and handles those issues.

section .data
prompt     db "Enter filename: ", 10
prompt_len equ $-prompt

section .bss
fname resb 128
buf   resb 4096

section .text
global _start

_start:
    ; print prompt
    mov eax,4
    mov ebx,1
    mov ecx,prompt
    mov edx,prompt_len
    int 0x80

    ; read filename from stdin
    mov eax,3
    mov ebx,0
    mov ecx,fname
    mov edx,127
    int 0x80
    cmp eax,1
    jl  _exit
    mov esi,eax        ; bytes read
    dec esi
    mov al,[fname+esi]
    cmp al,10          ; newline?
    jne .term_after
    mov byte [fname+esi],0
    jmp .open_file
.term_after:
    mov byte [fname+esi+1],0

.open_file:
    mov eax,5          ; sys_open
    mov ebx,fname
    mov ecx,0          ; O_RDONLY
    int 0x80
    js  _exit
    mov esi,eax        ; esi = fd

.read_loop:
    mov eax,3
    mov ebx,esi
    mov ecx,buf
    mov edx,4096
    int 0x80
    cmp eax,0
    jle .close_fd
    mov edx,eax
    mov eax,4
    mov ebx,1
    mov ecx,buf
    int 0x80
    jmp .read_loop

.close_fd:
    mov eax,6
    mov ebx,esi
    int 0x80

_exit:
    mov eax,1
    xor ebx,ebx
    int 0x80

Notes and troubleshooting

  • Always null-terminate the filename after read and strip the trailing newline; open expects a C-style string.
  • Use the syscall return values: open returns fd in eax (save it), read/write return count in eax; test for negative/zero to detect errors/EOF.
  • The example uses Linux x86 32-bit int 0x80. On x86_64 you must use the 64-bit syscall ABI or assemble/link for 32-bit (nasm -f elf32 file.asm && ld -m elf_i386 file.o -o file).

Hi,
I think that your code is for a linux system. If I am right, you can read this:
http://board.flatassembler.net/topic.php?t=5361
It seems that at the program start the arguments are in the stack:
[esp] argc.
[esp + 4] argv[0]
[esp + 8] argv[1]
...
This, of course, if you program for 32 bits.

Bon courage.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.