Please disable adblock to view this page.

← Go home

String Manipulation – length, reverse and pallindrome check (Assembly x64)

April 23, 2016
Published By : Pratik Kataria
Categorised in: ,

section .data
m db 10,13,”Enter your choice : “,10,”1.Calculate Length.”,10,”2.Reverse the string.”,10,”3.Check if Pallindrome.”,10
ml equ $-m
msg db 10 ,”Enter a string : ”
msglen equ $-msg
msg1 db 10 ,”Length of string is:”
msg1len equ $-msg1
msg2 db 10 ,”Reverse string is:”
msg2len equ $-msg2
msg3 db 10 ,”String is a Pallindrome!”
msg3len equ $-msg3
msg4 db 10 ,”String is not a Pallindrome!”
msg4len equ $-msg4
msg5 db 10 ,”Do you want to continue(y/n)? : ”
msg5len equ $-msg5
nl db 10

section .bss

string resb 50
strl equ $-string
str_len resb 40
revstring resb 50
num resb 16
choice resb 2

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

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

section .text

global _start
_start:
display msg,msglen
accept string,50
dec rax
mov [str_len],rax

display m,ml
accept choice,2
cmp byte[choice],’1′
je A1
cmp byte[choice],’2′
je A2
cmp byte[choice],’3′
je A2

A1:    display msg1,msg1len
mov rbx,[str_len]
call original_to_ascii
jmp conti

A2:    mov rsi,string
mov rdi,revstring
mov rcx,[str_len]
add rsi,rcx
dec rsi

l1:
mov al,[rsi]
mov [rdi],al
dec rsi
inc rdi
loop l1

display msg2,msg2len
display revstring,50
display nl,1
cmp byte[choice],’3′
je A3
jmp conti

A3:    mov rsi,string
mov rdi,revstring
mov rcx,[str_len]

l4:
mov al,[rsi]
mov bl,[rdi]
cmp al,bl
je l5

display msg4,msg4len
jmp conti
l5:
inc rsi
inc rdi
loop l4

display msg3,msg3len
display nl,1

conti:    display msg5,msg5len
accept choice,2
cmp byte[choice],’y’
je _start
exit:
mov rax,60
mov rbx,0
syscall

original_to_ascii :
mov rcx,16
mov rdi,num

l2:
rol bx,4
mov dl,bl
and dl,0fh
cmp dl,09h
jbe l3
add dl,07h

l3:
add dl,30h
mov [rdi],dl
inc rdi
loop l2
display num,16
ret

View Article Page
Download

Pratik Kataria is a budding programmer, web designer and developer.