Let’s write into a text file named new_file.txt with an assembler program.
First, you need to create this text file using the command touch.
1 2 3 4 5 6 7 8 9 | [mythcat@desk fasm]$ vim write_file.fasm [mythcat@desk fasm]$ ./fasm.x64 write_file.fasm flat assembler version 1.73.16 (16384 kilobytes memory, x64) 2 passes, 256 bytes. [mythcat@desk fasm]$ touch new_file.txt [mythcat@desk fasm]$ cat new_file.txt [mythcat@desk fasm]$ ./write_file [mythcat@desk fasm]$ cat new_file.txt FASM : Hello, world! |
The assembler FASM let us to write in the file using the INT 80 and some functions used with rax, rbx, rcx, rdx registers.
For example the seek function of write operation can be used like this:
1 2 3 4 5 6 7 8 | mov rax, 19 pop rbx ; move pointer mov rcx, 0 ; SEEK_SET = 0 mov rdx, 0 ; execute function with INT 80 int 0x80 |
Let’s see the full source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | format ELF64 executable entry _start filename db "new_file.txt", 0 my_text db "FASM : Hello, world!", 0xA, 0 length_my_text = $-my_text _start: mov rax, 5 ; set the filename to rbx mov rbx, filename mov rcx, 1 int 0x80 ; save rax push rax ; set rax to 19 mov rax, 19 ; get rbx pop rbx mov rcx, 0 ; move cursor ; use seek on rdx mov rdx, 0 int 0x80 ; write with rax 4 with rcx and rdx int 0x80 mov rax, 4 mov rcx, my_text mov rdx, length_my_text int 0x80 ; close it with rax 6 mov rax, 6 int 0x80 call exit exit: mov rax, 1 mov rbx, 0 int 0x80 |