1

the code at the beginning here the file_name does not get created and so the code cant write to it how do i use two file variables here i cant figure out why ???

as well it wont compile with gcc it does compile with the ld tho

 
 msg db 'Welcome to Assembly programming', 0xa ;
 len equ  $ - msg ;

 msg_done db 'Written to file ', 0xa ;
 len_done equ $ - msg_done ;
 
 
  ;make the write to file here last or there is a overflow and data gets added onnamly the next line
  file_name db 'myfile.txt', 
   Nile_2 db '/home/mark/Desktop/mynewfile.txt',
  

section .bss
 fd_out resb 1
 fd_in  resb 1
 info resb  26




section .text
   global _start         ;must be declared for using gcc
    
_start:                  ;tell linker entry point _start:
   ;create the file
   mov  eax, 8
   mov  ebx, file_name
   mov  ecx, 0777        ;read, write and execute by all
   int  0x80             ;call kernel
    
   mov [fd_out], eax
    
   ; write into the file
   mov  edx,len          ;number of bytes
   mov  ecx, msg         ;message to write
   mov  ebx, [fd_out]    ;file descriptor 
   mov  eax,4            ;system call number (sys_write)
   int  0x80             ;call kernel
    
   ; close the file
   mov eax, 6
   mov ebx, [fd_out]
    
   ; write the message indicating end of file write
   mov eax, 4
   mov ebx, 1
   mov ecx, msg_done
   mov edx, len_done
   int  0x80
    
   ;open the file for reading
   mov eax, 5
   mov ebx, Nile_2      ; was file_name
   mov ecx, 0             ;for read only access
  ; mov edx, 0777          ;read, write and execute by all
   int  0x80
    
   mov  [fd_in], eax
    
   ;read from file
   mov eax, 3
   mov ebx, [fd_in]
   mov ecx, info
   mov edx, 26
   int 0x80
    
   ; close the file
   mov eax, 6
   mov ebx, [fd_in]
   int  0x80    
    
   ; print the info 
   mov eax, 4
   mov ebx, 1
   mov ecx, info
   mov edx, 26
   int 0x80
       
   mov  eax,1             ;system call number (sys_exit)
   int  0x80              ;call kernel



 

the data in the Nile_2 file prints to the screen but the file_name file is not created i tryed using two section .data one on top and one on the bottom nothing has worked how can i use to variables for two different files !!!

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • 2
    Your `file_name` is not null-terminated. As well as `Nile_2`. Also file descriptor is integer, not a byte, `resb 1` is not enough to store it. – dimich Jul 24 '22 at 04:04
  • how do i null terminate them, i tryed increasing resb 1 to 16; no change, what is the difference between interger and byte... thank you for your help – NanoNebulas Jul 24 '22 at 05:47
  • I put the Nile_2 file in my home folder and so it shortened the length of the file name; so that seemed to work... except the file_name wrote the name + the name of Nile_2 to the file_name file which is => myfile.txtmynewfile.txt how would i stop that ??? – NanoNebulas Jul 24 '22 at 05:54
  • Linux syscalls `creat()` and `open()` return `int`, it is 32-bit value. `resb` (reserve bytes) reserves 8-bit bytes. You can use `resd 1` (reserve double words) in nasm to reserve one 32-bit value or `resb 4`. Once more: value of `file_name` is not null-terminated in your code. Kernel don't know where is the end of `file_name` and where is begin of subsequent values in memory. Add `, 0` after string literals. – dimich Jul 24 '22 at 07:51
  • 1
    There are two different problems: unterminated file names and invalid sizes for descriptors. Please don't mess between them. – dimich Jul 24 '22 at 09:23
  • 1
    @NanoNebulas To NUL terminate, append a NUL byte: `file_name db 'myfile.txt', 0` – fuz Jul 24 '22 at 16:03

1 Answers1

1
file_name db 'myfile.txt', 
 Nile_2 db '/home/mark/Desktop/mynewfile.txt',

the data in the Nile_2 file prints to the screen but the file_name file is not created

You forgot to zero-terminate the filespecs. The system was nonetheless able to open Nile_2 because a zero happened to be there. It was not the case for file_name, so the create failed. A failure that you let go by because you don't inspect what is in EAX after sys_creat returns!

file_name  db 'myfile.txt', 0
Nile_2     db '/home/mark/Desktop/mynewfile.txt', 0

fd_out resb 1
fd_in  resb 1

You don't reserve enough room for the 32-bit file descriptors that you will receive.

fd_out resd 1
fd_in  resd 1

; close the file
mov eax, 6
mov ebx, [fd_out]

You didn't close the file since you forgot to write int 0x80 in order to invoke sys_close.


sys_read provides you with the number of bytes that were actually read. Better use that instead of the hardcoded value (26).


All of these sys_??? calls return EAX=-1 in case of an error. You should not ignore this. Error processing is an important part of programming.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • 1
    To be fair, toy experiments to see how system calls work *can* use `strace` to let a human check system-call return values from outside the program, without writing a bunch of tedious error-checking. It's good to know how you *could* do it, though, to understand what a real program would do. – Peter Cordes Jul 24 '22 at 17:50
  • Thank for your help, I need a lot more im trying to get my perl scripts written in nasm so please help me with my questions if you can thanks !!! – NanoNebulas Jul 24 '22 at 18:57
  • Linux system calls return from -4096 to -1 in case of error, not just -1. – Timothy Baldwin Jul 29 '22 at 16:30