Nikita Demodov
Nikita Demodov

Reputation: 571

Assembly read user input doesn't wait

I have a assembly program which should write "What's your name?", then wait for user input, and then print "Hello, ". This is my program(Mac, nasm):

section .data
 question db "What's your name?"
 answer db "Hello, "
 ln db 10

section .bss
 name resb 16

section .text
global start 
start:
 mov rsi, question
 mov rdi, 17
 call print
 call getName
 mov rsi, answer
 mov rdi, 7
 call print
 mov rsi, ln
 mov rdi, 1
 call print
 mov rax, 0x02000001
 mov rdi, 0
 syscall

print:
 mov rax, 0x02000004
 mov rdi, 1
 syscall
 ret
getName:
 mov rax, 0x02000003
 mov rdi, 0
 mov rsi, name
 mov rdx, 16
 syscall
 ret

But this program writes "What's your name?Hello," and only then awaits for user input. Why doesn't it wait for the input before it writes "Hello,"?

Upvotes: 1

Views: 737

Answers (2)

ThefoxAzubirs
ThefoxAzubirs

Reputation: 1

Your code is totally wrong. Register RAX must the value of service syscall. I will help you to correct the code for full program.

compile it with:

nasm -f elf64 -o objname.o sourcecode.asm

ld -m elf_x86_64 -o execname objname.o

%macro print 2
mov rax,1
mov rdi,rax
mov rsi,%1
mov rdx,%2
syscall
%endmacro

%macro getinput 2
mov rax,0
mov rdi,rax
mov rsi,%1
mov rdx,%2
syscall
%endmacro

%macro exit 0
mov rax,60
xor rdi,rdi
syscall
%endmacro

section .data
 question db "What's your name?"
lenques equ $-question
 answer db "Hello, "
lenans equ $-answer
 ln db 10
lenname equ 30

section .bss
 name resb lenname

section .text
global _start
_start:
print question,lenques
push rax
getinput name,lenname
print answer,lenans
pop rbx
print name,rbx
print ln,1
exit

Upvotes: 0

Nikita Demodov
Nikita Demodov

Reputation: 571

I mixed up rdx and rdi in start and forgot to print out the name. My bad. EDIT: For some reason I cannot accept this answer, so: This it the accepted answer!

Upvotes: 1

Related Questions