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
CodePudding user response:
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.