Paras
Paras

Reputation: 238

How to take a string as a input in Assembly x64

I am writing a program to check if a string is Palindrome or not. I want to take a string as input from user. The string can contain any character ranging from digits to special characters. How can I take input from user. I have tried the following code.

global _start
section .bss
    string resb 9

section .text
_start:
    mov rax,0            ;Am I doing this correct ?
    mov rdi,0
    mov rsi,string
    mov rdx,8
    syscall

    xor rax,rax
    mov rdx,[string]
    mov rax,1
    mov rdi,1
    mov rsi,rdx
    mov rdx,8
    syscall

    mov rax,0
    mov rdi,0
    syscall

Is the above code correct because when I output the string it shows segmentation fault. The error is

Segmentation fault (core dumped)

I am coding in nasm in Linux(Ubuntu 14.04)

Upvotes: 1

Views: 2548

Answers (2)

Shipof123
Shipof123

Reputation: 234

Usually Linux requires you to use the exit

mov rax, 60
xor rdi, rdi
syscall

Upvotes: 0

Jester
Jester

Reputation: 58822

For printing you also need to pass the address so mov rdx, [string] is wrong, you need mov rdx, string or lea rdx, [string]. Also, your final syscall is wrong, because that's a read again. You probably want mov rax, 60 to make it exit.

See, that's why you should post a Minimal, Complete, and Verifiable example.

Upvotes: 2

Related Questions