How to read from and write to files using NASM for x86-64bit - linux

I have a NASM program for 64bit Linux system which works with standard I/O devices and it looks something like that:
section .data
prompt db "Enter your text: ", 10
length equ $ - prompt
text times 255 db 0
textSize equ $ - text
section .text
global main
main:
mov rax, 1
mov rdi, 1
mov rsi, prompt
mov rdx, length
syscall ;print prompt
mov rax, 0
mov rdi, 0
mov rsi, text
mov rdx, textSize
syscall ;read text input from keyboard
mov rcx, rax ; rcx - character counter
mov rsi, text ; a pointer to the current character starting from the beginning.
****
exit:
mov rax, 60
mov rdi, 0
syscall
I need the program to read from and write to the files, but I can't find anywhere which syscalls has to be used and how they should be used to achieve these results. So, I am wondering if someone of you could help me. Thanks in advance.

Use system calls "open" and "close":
Open a file under 64-bit Linux:
rax = 2
rdi = pointer to NUL-terminated filename
rsi = something like O_WRONLY
rdx = file flags if creating a file (e.g. 0644 = rw-r--r--)
syscall
now rax contains the file hanle
Close a file:
rax = 3
rdi = file handle
syscall
Reading/writing from/to a file:
rax = 0 or 1 (like keyboard/screen in/output)
rdi = file handle (instead of 0/1)
...

;this program is only overwrite the result.txt
;to write it on bottom you need to lseek to the end
section .data
prompt db "Enter your text: ",10
length equ $ - prompt
text times 255 db 0
textsize equ $-text
fname db "result.txt",0
fd dq 0
thefox dq 0
global _start
section .text
_start:
mov al,1
mov dil,al
mov esi,prompt
mov dl,length
syscall
mov al,0
mov dil,al
mov rsi,text
mov rdx,textsize
syscall
mov [thefox],rax
mov rax,2
mov rdi,fname
mov rsi,0102o
mov rdx,0666o
syscall
mov [fd],rax
mov rdx,[thefox]
mov rsi,text
mov rdi,[fd]
mov rax,1
syscall
mov rdi,[fd]
mov rax,3
syscall
mov rax,60
syscall

Related

I want my Assembly Code to takes user input and outputs it along with other text but the output isn't correct

My code works but the output isn't just right because when I enter the name, the output becomes this; "What is your member name? Welcome to the club, Bob!!!!!!!!!!!!!!!!!!, enjoy the party." with the "!!!!!!" at the end of the name. What am I doing wrong?
Here is my assembly code:
section .data
prompt: db "What is your member name? "
prompt_len: equ $-prompt
greet1: db "Welcome to the club, "
greet1_len: equ $-greet1
greet2: db ", enjoy the party."
greet2_len: equ $-greet2
inputbuffer_len: equ 256
inputbuffer: times inputbuffer_len db '!'
STDIN: equ 0
STDOUT: equ 1
SYS_READ: equ 0
SYS_WRITE: equ 1
SYS_EXIT: equ 60
section .text
global _start
_start:
mov rdx, prompt_len ;output prompt
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, prompt
syscall
mov rax, SYS_READ ;user input here
mov rdi, STDIN
mov rsi, inputbuffer
mov rdx, inputbuffer_len
syscall
mov rdx, greet1_len ; output "Welcome to the club, "
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, greet1
syscall
mov rdx, rax ;output user's inputted name
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, inputbuffer
syscall
mov rdx, greet2_len ; output ", enjoy the party."
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, greet2
syscall
mov rax, SYS_EXIT
mov rdi, 0
syscall
The length of Bob!!!!!!!!!!!!!!!!!! is the length of Welcome to the club, .
This is no coincidence.
Following the write(2) system call rax contains the number of successfully written Bytes.
(This might be less than the desired number of Bytes as the manual page describes.)
Like David C. Rankin commented you will need to mind the return value of read(2).
On success, read(2) returns the number of Bytes read in rax.
However, you are overwriting this value for and with the intervening write(2) system call.
Store and recall somewhere the number of successfully read Bytes (e. g. push/pop) and you’re good.
PS:
You could save one write(2) system call by rearranging the buffer to follow after greet_1.
Then you could write(2) rax + greet1_len Bytes at once.
But one problem at a time.

Nasm x86_64: Why can't I write and read from the same file?

I have a problem with file handling in Nasm x86_64.
I have opend correctly the file and i can write into it o read from it, but if I try to read something from the file after i have wrote something into it i don't get anything.
So i get read or write from a file.
The strange thing is that if i first read write I don't have any problem and everything works fine, so the problem is only when i first write and then read.
Could someone help me to solve this problem and to figure out the cause?
Here is the code to open the file:
mov rax, SYS_OPEN
mov rdi, filename
mov rsi, O_CREAT+O_RDWR+O_APPEND
mov rdx, 0744o
syscall
push rax
Code to close the file:
mov rax, SYS_CLOSE
mov rdi, r11
syscall
Code to print a string:
mov rdx, rax
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, temp
syscall
Code to of getLength (the argument is the string of which I want to get the length):
%macro getLength 1
mov r10, %1
mov r11, r10
%%begin:
cmp byte [r11], 10
je %%end
inc r11
jmp %%begin
%%end:
sub r11, r10
%endmacro
The code to write:
getLength msg
mov rax, SYS_WRITE
mov rdi, [rsp]
mov rsi, msg
mov rdx, r11
syscall
Code to read:
mov rax, SYS_READ
mov rdi, [rsp]
mov rsi, temp ;buffer to store the string read
mov rdx, 10
syscall
Both the code to read and the code to write works perfectly alone, the problem is when I use the code to read after the code to write.
So this code works.
%include "./standardlib.inc"
section .data
filename db "./file.txt", 0
msg db "hello", 10
section .bss
temp resb 10
section .text
global _start:
_start:
mov rax, SYS_OPEN
mov rdi, filename
mov rsi, O_CREAT+O_RDWR+O_APPEND
mov rdx, 0744o
syscall
push rax
mov rax, SYS_READ
mov rdi, [rsp]
mov rsi, temp
mov rdx, 10
syscall
mov rdx, rax
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, temp
syscall
getLength msg
mov rax, SYS_WRITE
mov rdi, [rsp]
mov rsi, msg
mov rdx, r11
syscall
mov rax, SYS_CLOSE
mov rdi, r11
syscall
exit
This coe doesn't work:
%include "./standardlib.inc"
section .data
filename db "./file.txt", 0
msg db "hello", 10
section .bss
temp resb 10
section .text
global _start:
_start:
mov rax, SYS_OPEN
mov rdi, filename
mov rsi, O_CREAT+O_RDWR+O_APPEND
mov rdx, 0744o
syscall
push rax
getLength msg
mov rax, SYS_WRITE
mov rdi, [rsp]
mov rsi, msg
mov rdx, r11
syscall
mov rax, SYS_READ
mov rdi, [rsp]
mov rsi, temp
mov rdx, 10
syscall
mov rdx, rax
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, temp
syscall
mov rax, SYS_CLOSE
mov rdi, r11
syscall
exit
So i understood that I have to use lseek to return to the beginning of the file.
Is this a good invocation for sys_lseek?
mov rax, 8 ;sys_lseek syscall ID
mov rdi, [rsp] ;file descriptor
mov rsi, 0 ;The offset
mov rdx, 0 ;I imagine the value of SEEK_SET
sys_lseek
I suppose that the offset value is wrong and I should have used ftell to find it, but I can't figure out how to call it.
ftell
Since the file are read sequentially, after a call to sys_read in append mode the cursor is moved to the end of the file, so if you try to read from that position you won't read anything.
To solve this problem you have to reposition the cursor at the beginning of the file.
To do that you can use the lseek system call.
mov rax, 8 ;system call Id for sys_lseek
mov rdi, [rsp] ;file descriptor
mov rsi, 0 ;offset value, so number of characters to move the cursor
mov rdx, 0 ;It indicates the initial position from which move the cursor, in this case the value 0 indicates that the initial position is the beginning of the file
syscall
After a call to this system call the cursor position will be at the beginning of the file and you will be able to read from it.

Printing binary string in assembly

I'm writing a program to print binary string of a hardcoded word. Here is how it looks like currently:
main.asm
section .text
global _start
extern _print_binary_content
_start:
push word [word_to_print] ; pushing word. Can we push just one byte?
call _print_binary_content
mov rax, 60
mov rdi, 0
syscall
section .data
word_to_print: dw 0xAB0F
printer.asm
SYS_BRK_NUM equ 0x0C
BITS_IN_WORD equ 0x10
SYS_WRITE_NUM equ 0x01
STD_OUT_FD equ 0x01
FIRST_BIT_BIT_MASK equ 0x01
ASCII_NUMBER_OFFSET equ 0x30
section .text
global _print_binary_content
_print_binary_content:
pop rbp
xor ecx, ecx ;zeroing rcx
xor ebx, ebx ;zeroing rbx
pop bx ;the word to print the binary content of
;sys_brk for current location
mov rax, SYS_BRK_NUM
mov rdi, 0
syscall
;end sys_brk
mov r12, rax ;save the current brake location
;sys_brk for memory allocation 16 bytes
lea rdi, [rax + BITS_IN_WORD]
mov rax, SYS_BRK_NUM
syscall
;end sys_brk
xor ecx, ecx
mov cl, byte BITS_IN_WORD - 1; used as a counter in the loop below
loop:
mov dx, bx
and dx, FIRST_BIT_BIT_MASK
add dx, ASCII_NUMBER_OFFSET
mov [r12 + rcx], dl
shr bx, 0x01
dec cl
cmp cl, 0
jge loop
mov rsi, r12
mov rax, SYS_WRITE_NUM
mov rdi, STD_OUT_FD
mov rdx, BITS_IN_WORD
syscall
push rbp ; pushing return address back
ret
If I compile link and run this program it works. But the question is about performance and maybe conventions of writing assembly programs. In the file printer.asm I cleaned ecx twice which looks kind of not optimal. Maybe some registers were used not by their purpose (I used intel-manual).
Can you please help me to improve this very simple program?

Linux system call for X86 64 echo program

I'm still learning assembly so my question may be trivial.
I'm trying to write an echo program with syscall, in which I get a user input and give it as output on the next line.
section .text
global _start
_start:
mov rax,0
mov rdx, 13
syscall
mov rsi, rax
mov rdx, 13
mov rax, 1
syscall
mov rax, 60
mov rdi, 0
syscall
I'm assuming all you want to do is return the input to the output stream, so to do that you need to do a few things.
First, create a section .bss in your code. This is for initializing data. You will initialize a string with any name you want and do so with label resb sizeInBits. for demonstration it will be a 32 bit string called echo.
Extra note, the ';' character is used for comments similar to what // is in c++.
Example code
section .data
text db "Please enter something: " ;This is 24 characters long.
section .bss
echo resb 32 ;Reserve 32 bits (4 bytes) into string
section .text
global _start
_start:
call _printText
call _getInput
call _printInput
mov rax, 60 ;Exit code
mov rdi, 0 ;Exit with code 0
syscall
_getInput:
mov rax, 0 ;Set ID flag to SYS_READ
mov rdi, 0 ;Set first argument to standard input
; SYS_READ works as such
;SYS_READ(fileDescriptor, buffer, count)
;File descriptors are: 0 -> standard input, 1 -> standard output, 2 -> standard error
;The buffer is the location of the string to write
;And the count is how long the string is
mov rsi, echo ;Store the value of echo in rsi
mov rdx, 32 ;Due to echo being 32 bits, set rdx to 32.
syscall
ret ;Return to _start
_printText:
mov rax, 1
mov rdi, 1
mov rsi, text ;Set rsi to text so that it can display it.
mov rdx, 24 ;The length of text is 24 characters, and 24 bits.
syscall
ret ;Return to _start
_printInput:
mov rax, 1
mov rdi, 1
mov rsi, echo ;Set rsi to the value of echo
mov rdx, 32 ;Set rdx to 32 because echo reserved 32 bits
syscall
ret ;Return to _start

Assembly, read in 2 ints

I've just started to learn assembler (2 days ago) for x86 arch (but I program on x86_64 see below). I want to read in 2 numbers and for that I use Linux system calls (64 bit system). Well I looked up the corresponding numbers for read/write in unitstd_64.h and seems to work. But one thing bothers me (first the code):
section .data
prompt1 db "Enter a number: ", 0
lenMsg equ $-prompt1
outmsg db "Entered: ", 0
lenOut equ $-outmsg
section .bss
input1 resd 1
input2 resd 1
segment .text
global _start
_start:
mov rax, 1
mov rdi, 1
mov rsi, prompt1
mov rdx, lenMsg
syscall
;read input number 1
mov rax, 0
mov rdi, 2
mov rsi, input1
mov rdx, 1
syscall
;prompt another number
mov rax, 1
mov rdi, 1
mov rsi, prompt1
mov rdx, lenMsg
syscall
;read input number 1
mov rax, 0
mov rdi, 2
mov rsi, input2
mov rdx, 1
syscall
;exit correctly
mov rax, 60
mov rdi, 0
syscall
The program does the following:
Shows prompt1
Let the user enter a number
Shows prompt1 again
quits (should'nt it let the user enter a number instead of quitting?)
Why is the fourth syscall simply ignored? Thanks in advance.
edit:
I use nasm. Object file created with nasm -f elf64 bla.asm. Linked with ld -o bla bla.o

Resources