简体   繁体   中英

NASM, read a file and print the content

I have this piece of code in NASM (for linux) that supposed to open an existing file, read it and print the content on the screen, but does not work, can someone tell me what am I doing wrong?( hello.txt is the name of the file)

section .data

file db "./hello.txt", 0

len equ 1024

section .bss 

buffer: resb 1024


section .text

global _start

_start:

    mov ebx, [file] ; name of the file  
    mov eax, 5  
    mov ecx, 0  
    int 80h     

    mov eax, 3  
    mov ebx, eax
    mov ecx, buffer 
    mov edx, len    
    int 80h     

    mov eax, 4  
    mov ebx, 1
    mov ecx, buffer 
    mov edx, len    
    int 80h     

    mov eax, 6  
    int 80h     

    mov eax, 1  
    mov ebx, 0 
    int 80h
mov ebx, [file] ; name of the file  
mov eax, 5  
mov ecx, 0  
int 80h     

Is wrong. Loose the square brackets around file . You are passing the file name instead of a pointer to the filename.

mov ebx, file ; const char *filename
mov eax, 5  
mov ecx, 0  
int 80h     

I see here a lot of mistakes, in order:

mov ebx, [file] ; name of the file  
mov eax, 5  
mov ecx, 0  
int 80h

Here, as said, u must lose square brackets (because the function needs a pointer, not a value)

mov eax, 3  
mov ebx, eax
mov ecx, buffer 
mov edx, len    
int 80h

Here, u must save the file descriptor from eax, before writing there value 3, else u just loose it

mov eax, 4  
mov ebx, 1
mov ecx, buffer 
mov edx, len    
int 80h

Well. Here u using ebx register, so better way is to save file descriptor in memory. And for display, you take 1024 bytes from buffer, which is not correct. After reading from the file, the eax register will contain the number of characters read, so after reading from the file it will be better to store the value from the eax register in edx

mov eax, 6  
int 80h

Again. U close the file, but ebx contains dirt, although it must contain a file descriptor

Correct code must look like this:

section .data
  file db "text.txt",0 ;filename ends with '\0' byte
section .bss
  descriptor resb 4 ;memory for storing descriptor
  buffer resb 1024
  len equ 1024
section .start
global _start
_start:
  mov eax,5 ;open
  mov ebx,file ;filename
  mov ecx,0 ;read only
  int 80h ;open filename for read only

  mov [descriptor],eax ;storing the descriptor

  mov eax,3 ;read from file
  mov ebx,[descriptor] ;your file descriptor
  mov ecx,buffer ;read to buffer
  mov edx,len ;read len bytes
  int 80h ;read len bytes to buffer from file

  mov edx,eax ;storing count of readed bytes to edx
  mov eax,4 ;write to file
  mov ebx,1 ;terminal
  mov ecx,buffer ;from buffer
  int 80h ;write to terminal all readed bytes from buffer

  mov eax,6 ;close file
  mov ebx,[descriptor] ;your file descriptor
  int 80h ;close your file

  mov eax,1
  mov ebx,0
  int 80h

This is not a perfect code, but it should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM