I am beginner at assembly language and I don't understand why the code#1 works and code#2 doesn't... Does anybody know how to help me?
I want to print a new line, so i have copied this code#1:
section .data
newline_char: db 10
section .text
global _start
print_newline:
mov rax, 1
mov rdi, 1
mov rsi, newline_char
mov rdx, 1
syscall
But if i try the code#2, it doesn't work and the newline is not printed:
; I have removed the newline_char from here
section .text
global _start
print_newline:
mov rax, 1
mov rdi, 1
mov rsi, 10 ; I tried to put immediatly in here and have already tested '0x0a' '0ah'
mov rdx, 1
syscall
Does anyone know what I am doing wrong?
Related
I am completely new to the assembly thing and googling for several hours and searching on SO didn't clear things out so I came to ask here.
What I want to achieve:
[first second]: hello (stays on the screen for 1 second)
[second second]: world (hello disappeared and now we have `world` in place of it)
And this flow is in an infinite loop
In other words, I want my terminal's stdout to flicker(change) between hello and world without appending any newlines, writing strings or any other things, I just want the existing, already printed text to be replaced with some another text.
I have written the infinite loop that will print hello, then wait for a second, then print world, and wait for a second. I have also put this code in an infinite loop.
Here is the code I have as of now:
section .data
hello db "hello",10,0
world db "world",10,0
delay dq 1,0
section .text
global _start
_start:
mov rax, 1
mov rdi, 1
mov rsi, hello
mov rdx, 6
syscall
mov rax, 35
mov rdi, delay
mov rsi, 0
syscall
mov rax, 1
mov rdi, 1
mov rsi, world
mov rdx, 6
syscall
mov rax, 35
mov rdi, delay
mov rsi, 0
syscall
call _start
Note that I use elf64 asm format and it is highly appreciated to suggest a solution in that format.
Print a \r carriage return (ASCII code 13) to put the cursor at the beginning of the line without scrolling the terminal the way \n line feed (ASCII 10) does.
Then you can overwrite the last thing you wrote. If it's shorter, you can print spaces after your visible characters to "erase" the later characters still there.
e.g. since both words are the same length you could do this:
section .rodata
hello db 13, "hello" ; `\rhello`
hello_len equ $ - hello
world db 13, "world"
world_len equ $ - world
Notice that , 0 is not needed in your data because you're passing these buffers to write not printf, so they don't need to be 0-terminated implicit-length C strings. You also don't need to hard-code mov rdx, 6, you can use mov rdx, hello_len with the assembler calculating that for you.
For the sleeping part, you can use the sleep libc function, but for raw system calls you'll have to use nanosleep. (Like you're already doing.)
For the looping, don't use call _start; use jmp. You don't want to push a return address; that would eventually stack overflow (after about 1 million seconds: 8MiB stack size limit and call pushes an 8-byte return address.)
Solved this using helpful guidance of Peter Cordes.
The working code looks like this:
section .data
hello db "hello",13,0
world db "world",13,0
delay dq 1,0
section .text
global _start
_start:
mov rax, 1
mov rdi, 1
mov rsi, hello
mov rdx, 6
syscall
mov rax, 35
mov rdi, delay
mov rsi, 0
syscall
mov rax, 1
mov rdi, 1
mov rsi, world
mov rdx, 6
syscall
mov rax, 35
mov rdi, delay
mov rsi, 0
syscall
jmp _start
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.
This is my code with commented explanations:
SECTION .data ; Section containing initialised data
EatMsg: db "Eat at Joe's!",10
EatLen: equ $-EatMsg
SECTION .bss ; Section containing uninitialized data
SECTION .text ; Section containing code
global _start ; Linker needs this to find the entry point!
_start:
nop ; This no-op keeps gdb happy...
mov rax,1 ; Code for Sys_write call
mov rdi, 1 ; Specify File Descriptor 1: Standard Output
mov rsi, EatMsg ; Pass offset of the message
mov rdx, EatLen ; Pass the length of the message
mov R9, [EatMsg] ; move the adresse of Msg into R9
syscall
mov rcx, 5
DoMore:
mov rax, 1 ; Code for Sys_write call
mov rdi, 1 ; Specify File Descriptor 1: Standard Output
mov rsi, EatMsg ; Pass offset of the message
mov rdx, EatLen ; Pass the length of the message
dec rcx
jnz DoMore
syscall ; Make kernel call
mov rax, 1 ; Code for exit
mov rdi, 0 ; Return a code of zero
syscall ; Make kernel call
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
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