flat assembler
Message board for the users of flat assembler.

Index > Linux > How to include a text file?

Author
Thread Post new topic Reply to topic
ntos



Joined: 06 Jul 2023
Posts: 18
ntos 27 Jul 2023, 08:42
Hi,
I have a text file in the same folder as the source code and define the file like this:

Code:
format ELF64

section '.text' executable
public main
main:
        mov rax, 2 ; open system call
    mov rdi, filename ; pointer to file name
    mov rsi, flags ; flags for opening
    mov rdx, 0 ; mode (ignored)
    syscall ; invoke system call

; check if rax is negative (error)
cmp rax, 0
jl error

; rax contains a valid file descriptor
mov rdi, 1 ; file descriptor for stdout
mov rsi, rax ; file descriptor for test.txt
mov rdx, 0 ; offset (ignored)
mov r10, 0 ; count (0 means until EOF)
mov rax, 40 ; sendfile system call
syscall ; invoke system call

; close the file
mov rdi, rsi ; file descriptor for test.txt
mov rax, 3 ; close system call
syscall

mov rdi, 0 ; exit status
mov rax, 60 ; exit system call
syscall
error:

mov rdi, 1 ; exit status
mov rax, 60 ; exit system call
syscall

section '.data'
    filename file 'test.txt' ; null-terminated string
    flags    =  0 ; O_RDONLY
    


This code is run in SASM IDE but it throws this error:

processed: filename file 'test.txt'
error: file not found.

strace throws the same error, too.

Thanks for reading.
Post 27 Jul 2023, 08:42
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 27 Jul 2023, 10:33
'file' is reserved keyword in fasm to place object inline with no preprocessing or assembling.

i cant test this code but this part is obvious
Code:
filename file 'test.txt' ; null-terminated string    

try
Code:
filename db 'test.txt',0 ; null-terminated string    

_________________
Coding a 3D game engine with fasm is like trying to eat an elephant,
you just have to keep focused and take it one 'byte' at a time.
Post 27 Jul 2023, 10:33
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 27 Jul 2023, 13:49
If you want to include the file as part of the output binary.
Code:
section '.data'
    file 'test.txt'    
Post 27 Jul 2023, 13:49
View user's profile Send private message Visit poster's website Reply with quote
ntos



Joined: 06 Jul 2023
Posts: 18
ntos 27 Jul 2023, 21:56
@bitshifter. It worked. Thanks.
@revolution. So the file directive is to include some file as part of the output binary, nothing more nothing less, right? Like some license or readme file?
Post 27 Jul 2023, 21:56
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 27 Jul 2023, 22:03
ntos wrote:
@revolution. So the file directive is to include some file as part of the output binary, nothing more nothing less, right? Like some license or readme file?
It could be anything. A binary fine, whatever. It just makes a duplicate without any parsing of the contents.
Post 27 Jul 2023, 22:03
View user's profile Send private message Visit poster's website Reply with quote
ntos



Joined: 06 Jul 2023
Posts: 18
ntos 28 Jul 2023, 13:39
revolution wrote:
ntos wrote:
@revolution. So the file directive is to include some file as part of the output binary, nothing more nothing less, right? Like some license or readme file?
It could be anything. A binary fine, whatever. It just makes a duplicate without any parsing of the contents.


Then how do you access the file defined with File from fasm code?
Post 28 Jul 2023, 13:39
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 28 Jul 2023, 14:05
Put a label.
Code:
my_data: file 'test.txt'
;...
lea rax,[my_data]
mov bl,[rax]    
Post 28 Jul 2023, 14:05
View user's profile Send private message Visit poster's website Reply with quote
ntos



Joined: 06 Jul 2023
Posts: 18
ntos 28 Jul 2023, 22:10
revolution wrote:
Put a label.
Code:
my_data: file 'test.txt'
;...
lea rax,[my_data]
mov bl,[rax]    


Thank you very much.
Post 28 Jul 2023, 22:10
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.