Monday, March 1, 2021

Write an X86/64 ALP to accept a string and to display its length.

 ;String length in Assembly Lanuguage


;macros begin

%macro write 2    ;will be used to write on the screen

mov rax,01h

mov rdi,0h

mov rsi,%1

mov rdx,%2

syscall

%endmacro


%macro read 2    ;will be used to read from the screen

mov rax,0h

mov rdi,1h

mov rsi,%1

mov rdx,%2

syscall

%endmacro


;macros end


section .data



msg2: db "Enter the string ",10

len2: equ $-msg2

msg3 : db "The length of the string is "

len3: equ $-msg3


section .bss


orgstr resb 50  ;original string

wordlen resb 1  ;length of the string

ascii resb 2  ;len in ascii

count resb 1


section .text

global _start:

_start:

write msg2,len2   ;display message



length:   ;the length option


mov al,0  ;initialize al with zero

read orgstr,50

mov byte[wordlen],al

dec byte[wordlen] ;now len has the length of the string without the null character

mov esi,wordlen

mov edi,ascii

mov byte[count],2

mov al,byte[esi]

loop:

rol al,04

mov bl,al

and bl,0Fh

cmp bl,09h

jbe nocorrection

add bl,7h

nocorrection:

add bl,30h

mov byte[edi],bl

inc edi

dec byte[count]

jnz loop


write ascii,2





exit:   ;to exit from the program

mov rax,60

mov rdx,0h

syscall






Write an X86/64 ALP to accept five 64 bit Hexadecimal numbers from user and store them in an array and display the accepted numbers.

 msg1 db "Enter 5 64-bit numbers ",10,13

len1 equ $-msg1


msg2 db "Entered 5 64-bit numbers are ",10,13

len2 equ $-msg2


section .bss

array resb 120

count resb 1


section .text

global _start

_start:


;logic to read 5 64-bit numbers


mov bh,05        ; count is in bh

mov rbx,00      ; array index


up:  mov rax,00

        mov rdi,00

        mov rsi,array

        add rsi,rbx

        mov rdx,17

        syscall

        add rbx,17

        dec byte[count]  ; dec count

        jnz up                  ; check count is 0 or not using zero flag i;



mov byte[count],05       ; count is in bh

mov rbx,00                     ;array index


up1: mov rax,01

        mov rdi,01

        mov rsi,array

        add rsi,rbx

        mov rdx,17

        syscall

        add rbx,17

        dec byte[count]      ; dec count

        jnz up1                    ; check count is 0 or not using zero flag i;



;exit syscall

mov rax, 60

mov rdi,00

syscall