Write an ALP to accept a string and to display it’s length.

3. Write an ALP to accept a string and to display it’s length.

section .bss
strng    resb 10
len_hex resd 1
len_ascii resd 1

section .data
msg    db     10, “enter the string  :   ”
msglen  equ      $-msg
msg2    db    10, “the string is     :   ”
msg2len equ    $-msg2
msg3    db    10, “the length is    :    ”
msg3len    equ    $-msg3
newline    db    0ah

section .text
global _start

_start:
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, msglen
int 80h

mov eax, 3
mov ebx, 0
mov ecx, strng
mov edx, 10
int 80h

dec eax
mov [len_hex], eax
cmp eax, 09h
jbe add_30h
add eax, 07h
add_30h:
add eax, 30h

mov [len_ascii], eax

; display stng

mov eax, 4 ;display the msg “the name is”
mov ebx, 1
mov ecx, msg2
mov edx, msg2len
int 80h

mov eax, 4   ; display actual name which was read from kb
mov ebx, 1
mov ecx, strng
mov edx, [len_hex]
int 80h

;display len

mov eax, 4    ; display msg “length is”
mov ebx, 1
mov ecx, msg3
mov edx, msg3len
int 80h

mov eax, 4    ; display the actual length
mov ebx, 1
mov ecx, len_ascii
mov edx, 4
int 80h

mov eax, 4    ; display new_line
mov ebx, 1
mov ecx, newline
mov edx, 1
int 80h

mov eax, 1 ;sys_exit call
mov ebx, 0
int 80h

OUTPUT:

cg-151@cg151:~/Final MA/A3$ nasm -f elf string.asm
cg-151@cg151:~/Final MA/A3$ ld -m elf_i386 -s -o string string.o
cg-151@cg151:~/Final MA/A3$ ./string

enter the string  :   saibaba
the string is     :   saibaba
the length is    :    7
cg-151@cg151:~/Final MA/A3$

Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *