This is the second part of the program of String

extern cat1, cat2, cat3, s1, s2, num, len1, len2, len3, sublen, mainlen, occ
extern msg1, msg1len, msg2, msg2len, msg3, msg3len, msg4, msg4len, msg5, msg5len, msg6, msg6len

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

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

;——————————-logic for concatenation———————–
;Notes:-
;mov cat1 in rsi and cat3 in rdi
;transfer rsi to rdi
;mov cat2 in rsi
;transfer rsi to rdi

global concat, no_of_occ    ;export procedures
concat:
disp msg1, msg1len
accept cat1, 50
dec rax
mov [len1], rax
disp msg2, msg2len
accept cat2, 50
dec rax
mov [len2], rax

mov rsi, cat1
mov rdi, cat3
mov cl, [len1]
a1:    mov al, [rsi]
mov [rdi], al
inc rsi
inc rdi
dec cl
jnz a1

mov rsi, cat2
mov cl, [len2]
a2:    mov al, [rsi]
mov [rdi], al
inc rsi
inc rdi
dec cl
jnz a2

disp msg3, msg3len
disp cat3, 50

ret

;————————-logic for number of occurance of string—————————–
;Notes:-
;put main string in rsi and sub string in rdi
;cmp rsi and rdi
;if they are equal then inc bl(count of char), rsi, rdi, check whether bl==sublen
;if they are not equal then just inc rsi, mov s2 to rdi and bl=0
no_of_occ:
disp msg4, msg4len
accept s1, 50
dec rax
mov [mainlen], rax
disp msg5, msg5len
accept s2, 50
dec rax
mov [sublen], rax

mov bx, 0        ;bx is used for character count of sub-string
mov cx, 0        ;cx is used for loop iteration
mov byte[num], 0    ;num is used for counting occurances

mov rsi, s1
mov rdi, s2

mov cx, [mainlen]
b1:    mov al, [rsi]
cmp [rdi], al
je b2
cmp [rdi], al
jne b3

b2:                    ;if rsi and rdi are equal then
inc rsi
inc rdi
inc bl
cmp byte[sublen], bl
je b4
cmp byte[sublen], bl
jne b5

b4:    inc byte[num]            ;if bl==sublen
mov bl, 0
mov rdi, s2

jmp b0

b5:    jmp b0                ;if bl != sublen

b3:                ;if rsi and rdi are not equal then
inc rsi
mov rdi, s2
mov bl, 0
b0:
dec cl
jnz b1

mov cx, 4
mov bx, [num]

c1:    push cx        ;logic to display the num
rol bx, 4
mov dl, bl
and dl, 0fh
add dl, 30h
mov [occ], dl
disp occ, 2
pop cx
dec cl
jnz c1

ret

Advertisements

Write 8086 ALP to perform string manipulation. The strings to be accepted from the user is to be stored in data segment of program_l and write FAR PROCEDURES in code segment program_2 for following ;operations on the string:(a) Concatenation of two strings (b) Number of occurrences of a sub-string in the given string Use PUBLIC and EXTERN directive. Create .OBJ files of both the modules and link them to create an EXE file.

;Write 8086 ALP to perform string manipulation. The strings to be accepted from the user is to be stored in
;data segment of program_l and write FAR PROCEDURES in code segment program_2 for following
;operations on the string:(a) Concatenation of two strings (b) Number of occurrences of a sub-string in the given string Use PUBLIC
;and EXTERN directive. Create .OBJ files of both the modules and link them to create an EXE file.

;————————-commands to run this programme————————–
;amit@amit-pc:~$ nasm -f elf64 stringcat1.asm
;amit@amit-pc:~$ nasm -f elf64 stringcat2.asm
;amit@amit-pc:~$ ld -o stringcat1 stringcat2.o stringcat1.o    ;linking of files
;amit@amit-pc:~$ ./stringcat1

;————————macro declaration—————————–
%macro disp 2
mov rax, 1
mov rdi, 1
mov rsi, %1
mov rdx, %2
syscall
%endmacro

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

;—————————data section——————————
;GLOBAL DIRECTIVE:-
;This directive is used to export variables and procedures
;Variables or procedures should be defined as global before their defination
;EXTERN DIRECTIVE:-
;This directive is used to import variables and procedures

section .data
global msg1, msg1len, msg2, msg2len, msg3, msg3len, msg4, msg4len, msg5, msg5len, msg6, msg6len    ;export varibles(before defination)
menu:     db ‘MENU’, 10
db ‘1. Concatenation of two strings’, 10
db ‘2. Number of occurrences of a sub-string’, 10
db ‘3. Exit’, 10
db ‘Enter your choice:’, 10
menulen: equ $-menu
msg1: db ‘Enter first string:’, 10
msg1len: equ $-msg1
msg2: db ‘Enter second string:’, 10
msg2len: equ $-msg2
msg3: db ‘The concatenation of two strings is:’, 10
msg3len: equ $-msg3
msg4: db ‘Enter string:’, 10
msg4len: equ $-msg4
msg5: db ‘Enter sub-string:’, 10
msg5len: equ $-msg5
msg6: db ‘Number of occurance of sub-string is:’, 10
msg6len: equ $-msg6
msg7: db ‘Thank you for using programme.’, 10
msg7len: equ $-msg7
nl: db ”, 10, 13
nllen: equ $-nl

;—————————-bss section————————-
section .bss
global cat1, cat2, cat3, s1, s2, num, len1, len2, len3, sublen, mainlen, occ        ;export variables
choice: resb 2
cat1: resb 50
len1: resb 5
cat2: resb 50
len2: resb 5
cat3: resb 50
len3: resb 5
s1: resb 50
mainlen: resb 5
s2: resb 50
sublen: resb 5
num: resb 5
occ: resb 5

;———————-text section—————————-
section .text
extern concat, no_of_occ    ;import procedures
global _start
_start:
disp menu, menulen
accept choice, 2

cmp byte[choice], 31h
je _concat
cmp byte[choice], 32h
je _no_of_occ
cmp byte[choice], 33h
jae exit

_concat:
call concat
disp nl, nllen

jmp _start

_no_of_occ:
call no_of_occ
disp nl, nllen

jmp _start

exit:
disp msg7, msg7len
mov rax, 3Ch
mov rdi, 00h
syscall

Write X86/64 ALP to perform multiplication of two 8-bit hexadecimal numbers. Use successive addition and add and shift method. Accept input from the user. (use of 64-bit registers is expected)

;Aim:-
;Write X86/64 ALP to perform multiplication of two 8-bit hexadecimal numbers. Use successive addition
;and add and shift method. Accept input from the user. (use of 64-bit registers is expected)

;———————–macro declaration————————–
%macro disp 2
mov rax, 1
mov rdi, 1
mov rsi, %1
mov rdx, %2
syscall
%endmacro

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

;———————-data section————————————————-
section .data
menu:     db ‘MENU’, 10
db ‘1. Multiplication using successive addition method’, 10
db ‘2. Multiplication using shift and add method’, 10
db ‘3. Exit’, 10
db ‘Enter your choice:’, 10
menulen: equ $-menu
msg1: db ‘Enter multiplicand:’, 10
msg1len: equ $-msg1
msg2: db ‘Enter multiplier:’, 10
msg2len: equ $-msg2
msg3: db ‘The multiplication using successive addition method is:’, 10
msg3len: equ $-msg3
msg4: db ‘The multiplication using shift and add method is:’, 10
msg4len: equ $-msg3
msg5: db ‘Thank you for using programme.’, 10
msg5len: equ $-msg5
nl: db ”, 10, 13
nllen: equ $-nl

;—————————–bss section————————————-
section .bss
choice: resb 2
var1: resb 3
ml: resb 3
mr: resb 3
var2: resb 3
result: resb 3

;————————————text section———————————–
section .text
global _start
_start:
disp menu, menulen
accept choice, 2
cmp byte[choice], 31h
je succ_add
cmp byte[choice], 32h
je shift_add
cmp byte[choice], 33h
jae exit

;——————————logic for successive addition—————————-
;Notes:-
;Convert input numbers
;move multiplier in rcx
;add multiplicant in loop
adjust ASCII of result and show
succ_add:
disp msg1, msg1len
accept var1, 3
call convert
mov [ml], bx
disp msg2, msg2len
accept var1, 3
call convert
mov [mr], bx

mov rcx, [mr]
mov al, 00h
mov ah, 00h
b1:    add al, [ml]
loop b1
mov [var2], ax
disp msg3, msg3len
call show
disp nl, nllen

jmp _start

;———————logic for shift and add method——————————-
;This block gives correct result but output is not proper.
;Notes:-
;move ml in al
;move mr in bl
;shr mr and if carry(means logical high(1 bit) is discarded in shifting then carry is generated) then add ml in result
;If not carry then just shl ml
shift_add:
disp msg1, msg1len
accept var1, 3
call convert
mov [ml], bx
disp msg2, msg2len
accept var1, 3
call convert
mov [mr], bx

mov al, [ml]
mov bl, [mr]
mov rcx, 00h
mov dl, 08
d1:    shr bl, 01
jnc d2
add cl, al
d2:    shl al, 01
dec dl
jnz d1
mov [var2], cl
disp msg4, msg4len
call show
disp nl, nllen

jmp _start

;———————–exit block————————————————
exit:
disp msg5, msg5len
mov rax, 60
mov rdi, 00
syscall

;———————–convert number———————————–
convert:
mov bl, 00h
mov bh, 00h
mov rsi, var1

mov al, [rsi]
cmp al, 39h
jbe a1
sub al, 07h
a1:    sub al, 30h
shl al, 4
add bl, al
inc rsi

mov al, [rsi]
cmp al, 39h
jbe a2
sub al, 07h
a2:    sub al, 30h
shl al, 0
add bl, al

ret

;—————————Show in proper ASCII logic———————-
show:
mov ax, [var2]
shr ax, 4
and al, 0fh
cmp al, 09h
jbe c1
add al, 07h
c1:    add al, 30h
mov [result], al
disp result, 3

mov ax, [var2]
shr ax, 0
and al, 0fh
cmp al, 09h
jbe c2
add al, 07h
c2:    add al, 30h
mov [result], al
disp result, 3

ret

Write X86/64 ALP to perform multiplication of two 8-bit hexadecimal numbers. Use successive addition and add and shift method. Accept input from the user. (use of 64-bit registers is expected)

;Aim:-
;Write X86/64 ALP to perform multiplication of two 8-bit hexadecimal numbers. Use successive addition
;and add and shift method. Accept input from the user. (use of 64-bit registers is expected)

;———————–macro declaration————————–
%macro disp 2
mov rax, 1
mov rdi, 1
mov rsi, %1
mov rdx, %2
syscall
%endmacro

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

;———————-data section————————————————-
section .data
menu:     db ‘MENU’, 10
db ‘1. Multiplication using successive addition method’, 10
db ‘2. Multiplication using shift and add method’, 10
db ‘3. Exit’, 10
db ‘Enter your choice:’, 10
menulen: equ $-menu
msg1: db ‘Enter multiplicand:’, 10
msg1len: equ $-msg1
msg2: db ‘Enter multiplier:’, 10
msg2len: equ $-msg2
msg3: db ‘The multiplication using successive addition method is:’, 10
msg3len: equ $-msg3
msg4: db ‘The multiplication using shift and add method is:’, 10
msg4len: equ $-msg3
msg5: db ‘Thank you for using programme.’, 10
msg5len: equ $-msg5
nl: db ”, 10, 13
nllen: equ $-nl

;—————————–bss section————————————-
section .bss
choice: resb 2
var1: resb 3
ml: resb 3
mr: resb 3
var2: resb 3
result: resb 3

;————————————text section———————————–
section .text
global _start
_start:
disp menu, menulen
accept choice, 2
cmp byte[choice], 31h
je succ_add
cmp byte[choice], 32h
je shift_add
cmp byte[choice], 33h
jae exit

;——————————logic for successive addition—————————-
;Notes:-
;Convert input numbers
;move multiplier in rcx
;add multiplicant in loop
adjust ASCII of result and show
succ_add:
disp msg1, msg1len
accept var1, 3
call convert
mov [ml], bx
disp msg2, msg2len
accept var1, 3
call convert
mov [mr], bx

mov rcx, [mr]
mov al, 00h
mov ah, 00h
b1:    add al, [ml]
loop b1
mov [var2], ax
disp msg3, msg3len
call show
disp nl, nllen

jmp _start

;———————logic for shift and add method——————————-
;This block gives correct result but output is not proper.
;Notes:-
;move ml in al
;move mr in bl
;shr mr and if carry(means logical high(1 bit) is discarded in shifting then carry is generated) then add ml in result
;If not carry then just shl ml
shift_add:
disp msg1, msg1len
accept var1, 3
call convert
mov [ml], bx
disp msg2, msg2len
accept var1, 3
call convert
mov [mr], bx

mov al, [ml]
mov bl, [mr]
mov rcx, 00h
mov dl, 08
d1:    shr bl, 01
jnc d2
add cl, al
d2:    shl al, 01
dec dl
jnz d1
mov [var2], cl
disp msg4, msg4len
call show
disp nl, nllen

jmp _start

;———————–exit block————————————————
exit:
disp msg5, msg5len
mov rax, 60
mov rdi, 00
syscall

;———————–convert number———————————–
convert:
mov bl, 00h
mov bh, 00h
mov rsi, var1

mov al, [rsi]
cmp al, 39h
jbe a1
sub al, 07h
a1:    sub al, 30h
shl al, 4
add bl, al
inc rsi

mov al, [rsi]
cmp al, 39h
jbe a2
sub al, 07h
a2:    sub al, 30h
shl al, 0
add bl, al

ret

;—————————Show in proper ASCII logic———————-
show:
mov ax, [var2]
shr ax, 4
and al, 0fh
cmp al, 09h
jbe c1
add al, 07h
c1:    add al, 30h
mov [result], al
disp result, 3

mov ax, [var2]
shr ax, 0
and al, 0fh
cmp al, 09h
jbe c2
add al, 07h
c2:    add al, 30h
mov [result], al
disp result, 3

ret

Write X86/64 ALP for the following operations on the string entered by the user. (use of 64-bit registers is expected) a) Calculate Length of the string b) Reverse the string c) Check whether the string is palindrome OR Make your program user friendly by providing MENU like: (a) Enter the string b) Calculate length of string c) Reverse string d) Check palindrome e) Exit Display appropriate messages to prompt the user while accepting the input and displaying the result.

;Aim:-
;Write X86/64 ALP for the following operations on the string entered by the user. (use of 64-bit registers is
;expected)
;a) Calculate Length of the string b) Reverse the string
;c) Check whether the string is palindrome
;OR
;Make your program user friendly by providing MENU like:
;(a) Enter the string b) Calculate length of string c) Reverse string d) Check palindrome e) Exit
;Display appropriate messages to prompt the user while accepting the input and displaying the result.

;—————————-declaration of macro———————————
%macro disp 2
mov rax, 1
mov rdi, 1
mov rsi, %1
mov rdx, %2
syscall
%endmacro

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

;———————–data section—————————————
section .data
menu:     db ‘MENU’, 10
db ‘1. Enter the string’, 10
db ‘2. Calculate length of string’, 10
db ‘3. Reverse string’, 10
db ‘4. Check palindrome’, 10
db ‘5. Exit’, 10
db ‘Enter your choice:’, 10
menulen: equ $-menu
msg1: db ‘Enter string:’, 10
msg1len: equ $-msg1
msg2: db ‘The length of given string is:’, 10
msg2len: equ $-msg2
msg3: db ‘The reverse of the string is:’
msg3len: equ $-msg3
msg4: db ‘The given string is palindrome.’, 10
msg4len: equ $-msg4
msg5: db ‘The given string is not palindrome.’, 10
msg5len: equ $-msg5
msg6: db ‘Thank you for using programme.’, 10
msg6len: equ $-msg6
nl: db ”, 10, 13
nllen: equ $-nl

;————————–bss section————————————
;Notes:-
;Remeber size of strlen and len
section .bss
string: resb 50
choice: resb 2
strlen: resb 8
len: resb 16
rev: resb 50

;——————————–text section————————————–
section .text
global _start
_start:
disp menu, menulen
accept choice, 2

cmp byte[choice], 31h
je get_string
cmp byte[choice], 32h
je length
cmp byte[choice], 33h
je reverse
cmp byte[choice], 34h
je palindrome
cmp byte[choice], 35h
jae exit

get_string:
disp msg1, msg1len
accept string, 50
dec rax
mov [strlen], rax
jmp _start

;—————–logic for length calculation——————————-
;Notes:-
;Get length in bx
;rol bx to get its one digit
;and with 0fh
;Adjust ASCII
;Move it in rdi

length:
mov bx, [strlen]
mov rdi, len
mov rcx, 16
d1:    rol bx, 4
mov dl, bl
and dl, 0fh
cmp dl, 09h
jbe d2
add dl, 07h
d2:    add dl, 30h
mov [rdi], dl
inc rdi
loop d1

disp msg2, msg2len
disp len, 4
disp nl, nllen
jmp _start

;————————-logic for reversing the string—————————–
;Notes:-
;Put one string in rsi
;put the variable of resulting reverse string in rdi
;Add length in rsi
;transfer rsi to rdi decrementing it
reverse:
mov rsi, string
mov rdi, rev
add rsi, [strlen]
mov rcx, [strlen]
a1:    mov al, [rsi]
mov [rdi], al
dec rsi
inc rdi
loop a1

mov al, [rsi]            ;This extra iteration is required for completion of reverse string.
mov [rdi], al

disp msg3, msg3len
disp rev, 50
disp nl, nllen
jmp _start

;—————–logic to check palindrome—————————————
;Notes:-
;put the string in rsi and rdi
;add length in rsi and decrement it once
;compare elements of rsi and rdi
palindrome:            ;************Can be implemented by using string instruction -> repe cmpsb**************
mov rsi, string
mov rdi, string
add rsi, [strlen]
dec rsi                ;This is decremented because go get access of last element
mov rcx, [strlen]
b1:    mov al, [rsi]
cmp [rdi], al
jne b2
dec rsi
inc rdi
loop b1

disp msg4, msg4len
jmp _start
b2:    disp msg5, msg5len
jmp _start

;—————————————exit block————————————–
exit:        disp msg6, msg6len
mov rax, 60
mov rdi, 0
syscall

Write 64 bit ALP to convert 4-digit Hex number into its equivalent BCD number and 5-digit BCD number into its equivalent HEX number. Make your program user friendly to accept the choice from user for: (a) HEX to BCD b) BCD to HEX (c) EXIT. Display proper strings to prompt the user while accepting the input and displaying the result. (use of 64-bit registers is expected)

;Aim:-
;Write 64 bit ALP to convert 4-digit Hex number into its equivalent BCD number and 5-digit BCD number
;into its equivalent HEX number. Make your program user friendly to accept the choice from user for:
;(a) HEX to BCD b) BCD to HEX (c) EXIT.
;Display proper strings to prompt the user while accepting the input and displaying the result. (use of 64-bit
;registers is expected)

;———————–macro declaration——————————
%macro disp 2
mov rax, 1
mov rdi, 1
mov rsi, %1
mov rdx, %2
syscall
%endmacro

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

;————————-data section————————————
;Notes:-
;See declaration of array for BCD to HEX conversion
section .data
menu: db ‘MENU’, 10
db ‘1. HEX to BCD’, 10
db ‘2. BCD to HEX’, 10
db ‘3. EXIT’, 10
db ‘Enter your choice:’, 10
menulen: equ $-menu
msg1: db ‘Enter 4 digit hex number:’, 10
msg1len: equ $-msg1
msg2: db ‘The BCD conversion of given number is:’, 10
msg2len: equ $-msg2
msg5: db ‘Enter 5 digit BCD number:’, 10
msg5len: equ $-msg5
msg6: db ‘The HEX conversion of given number is:’, 10
msg6len: equ $-msg6
msg7: db ‘Thank you for using programme.’, 10
msg7len: equ $-msg7
nl: db ”, 10, 13
nllen: equ $-nl

array: dd 10000, 1000, 100, 10, 1

;————————————–bss section—————————–
section .bss
choice: resb 2
hex1: resb 5
var1: resb 5
bcd1: resb 5
hex2: resb 5
bcd2: resb 5
var2: resb 5

;————————————–text section—————————–
section .text
global _start
_start:
disp menu, menulen
accept choice, 2

cmp byte[choice], 31h
je HEXtoBCD
cmp byte[choice], 32h
je BCDtoHEX
cmp byte[choice], 33h
jae exit

;——————-HEX to BCD conversion logic—————————————-
;Notes:-
;Revise algorithm of div instruction.
;In div ax, stores quotient and dx stores remainder.
;We are going to divide the hex1 by 10 and going to preserve the remainders in reverse order.
;See condition for loop ->add ax, 0 and then jnz b1
HEXtoBCD:
disp msg1, msg1len
accept var1, 5
call convert_hex
mov [hex1], bx

mov ax, [hex1]
mov rdi, bcd1
add rdi, 4
mov bx, 10
b1: mov dx,0 ;If we forget this instruction then programme give floating point exception.
div bx
add dl, 30h ;Now dl contains the remainder of the division.
mov byte[rdi], dl
dec rdi
add ax, 0
jnz b1

disp msg2, msg2len
disp bcd1, 5
disp nl, nllen

jmp _start

;——————-BCD to HEX conversion logic——————————-
;Unable to understand some part
;Notes:-
;First declare array in data section of 10000, 1000, 100, 10, 1
;go on multiplying the array element with given BCD
;then add 4 in array in every iteration.
BCDtoHEX:
disp msg5,msg5len
accept var2, 5
mov rdi, var2
mov cx,5

mov rsi, array
mov rbx,0
loop3: ;loop3 is the main block of conversion
mov ax,0
sub byte[rdi],30h
mov al,[rdi]
mov dx,[rsi]
mul dx
add rbx,rax
add rsi,4
inc rdi
dec cx
jnz loop3

mov rdi,hex2
mov cx,4
loop4:
rol bx,4
mov dl,bl
and dl,0fh
cmp dl,09h
jbe no
add dl,07h
no:
add dl,30h
mov [rdi],dl
inc rdi
dec cx
jnz loop4

disp msg6,msg6len
disp hex2, 5

jmp _start

;——————————exit block———————————–
exit: disp msg7, msg7len
mov rax, 60
mov rdi, 00
syscall

;———————–ASCII to decimal conversion for HEX input—————————–
convert_hex:
mov rsi, var1
mov bl, 00h
mov bh, 00h

mov al, [rsi]
cmp al, 39h
jbe a1
sub al, 07h
a1: sub al, 30h
shl al, 12
add bl, al
inc rsi

mov al, [rsi]
cmp al, 39h
jbe a2
sub al, 07h
a2: sub al, 30h
shl al, 8
add bl, al
inc rsi

mov al, [rsi]
cmp al, 39h
jbe a3
sub al, 07h
a3: sub al, 30h
shl al, 4
add bl, al
inc rsi

mov al, [rsi]
cmp al, 39h
jbe a4
sub al, 07h
a4: sub al, 30h
shl al, 0
add bl, al

ret

;———————–ASCII to decimal conversion for BCD input—————————–
convert_bcd:
mov rsi, var2
mov bl, 00h
mov bh, 00h

mov al, [rsi]
cmp al, 39h
jbe d1
sub al, 07h
d1: sub al, 30h
shl al, 16
add bl, al
inc rsi

mov al, [rsi]
cmp al, 39h
jbe d2
sub al, 07h
d2: sub al, 30h
shl al, 12
add bl, al
inc rsi

mov al, [rsi]
cmp al, 39h
jbe d3
sub al, 07h
d3: sub al, 30h
shl al, 8
add bl, al
inc rsi

mov al, [rsi]
cmp al, 39h
jbe d4
sub al, 07h
d4: sub al, 30h
shl al, 4
add bl, al
inc rsi

mov al, [rsi]
cmp al, 39h
jbe d5
sub al, 07h
d5: sub al, 30h
shl al, 0
add bl, al

ret

Write X86/64 Assembly language program (ALP) to add array of N hexadecimal numbers stored in the memory. Accept input from the user.

;Aim:-
;Write X86/64 Assembly language program (ALP) to add array of N hexadecimal numbers stored in the
;memory. Accept input from the user.
;—————-macro declaration—————————–
%macro disp 2
mov rax, 1
mov rdi, 1
mov rsi, %1
mov rdx, %2
syscall
%endmacro

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

;——————–data section——————————————-
section .data
msg1: db ‘Enter total number of elements:’, 10
msg1len: equ $-msg1
msg2: db ‘Enter element:’, 10
msg2len: equ $-msg2
msg3: db ‘The addition of given hexadecimal numbers is:’, 10
msg3len: equ $-msg3
msg4: db ‘Thank you for using programme.’, 10
msg4len: equ $-msg4
enter: db ”, 10, 13
enterlen: equ $-enter

;—————————-bss section——————————-
section .bss
count: resb 2
result: resb 5
num: resb 5
var: resb 1
temp: resb 2

;————————-text section————————————-
section .text
global _start
_start:
mov word[result], 0000h
disp msg1, msg1len
accept count, 2
cmp byte[count], 39h ;If you wite here word instead of byte then also programme behaves awkward.
jbe a1
sub byte[count], 07h
a1: sub byte[count], 30h

a2: disp msg2, msg2len
accept num, 5
call convert
mov al, [temp]
add byte[result], al
dec byte[count]
cmp byte[count], 00
jne a2

disp msg3, msg3len
call display

jmp exit

;——————-conversion logic for input numbers————
;Notes:-
;Take one temp variable.
;Put input number in rsi
; mov element of rsi in al
;adjust value of al(ASCII adjust)
;shl by required bits
;add it in temp
;inc rsi
;Repeat the same

convert:
mov byte[temp], 00
mov bl, 00
mov bh, 00
mov rsi, num

mov al, [rsi]
cmp al, 0x39
jbe b1
sub al, 07h
b1: sub al, 30h
shl al, 12
add [temp], al
inc rsi

mov al, [rsi]
cmp al, 0x39
jbe b2
sub al, 07h
b2: sub al, 30h
shl al, 8
add [temp], al
inc rsi

mov al, [rsi]
cmp al, 0x39
jbe b3
sub al, 07h
b3: sub al, 30h
shl al, 4
add [temp], al
inc rsi

mov al, [rsi]
cmp al, 0x39
jbe b4
sub al, 07h
b4: sub al, 30h
shl al, 0
add [temp], al

ret

;———————–display logic———————–
;Here we show result bit by bit
;mov result in bx
;shr the bx
;AND to get unit digit
;Adjust ASCII
;mov that digit in var
;display var
;Change shifting bits to get complete result

display:
mov bx, [result]
shr bx, 12
and bx, 0000Fh
cmp bl, 09h
jbe c1
add bl, 07h
c1: add bl, 30h
mov byte[var], bl
disp var, 1

mov bx, [result]
shr bx, 8
and bx, 0000Fh
cmp bl, 09h
jbe c2
add bl, 07h
c2: add bl, 30h
mov byte[var], bl
disp var, 1

mov bx, [result]
shr bx, 4
and bx, 0000Fh
cmp bl, 09h
jbe c3
add bl, 07h
c3: add bl, 30h
mov byte[var], bl
disp var, 1

mov bx, [result]
shr bx, 0
and bx, 0000Fh
cmp bl, 09h
jbe c4
add bl, 07h
c4: add bl, 30h
mov byte[var], bl
disp var, 1
ret

;———————exit————————-
exit:
disp enter, enterlen
disp msg4, msg4len
disp enter, enterlen
mov rax, 60
mov rdi, 00
syscall

Write X86/64 ALP to perform non-overlapped and overlapped block transfer (with and without string specific instructions). Block containing data can be defined in the data segment.

;Aim:-
;Write X86/64 ALP to perform non-overlapped and overlapped block transfer (with and without string
;specific instructions). Block containing data can be defined in the data segment.

;—————–macro section—————————
%macro disp 2
mov rax, 1
mov rdi, 1
mov rsi, %1
mov rdx, %2
syscall
%endmacro

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

;—————-data section—————————–
section .data
menu: db 10, ‘MENU’, 10
db ‘1. Non overlapped block transfer’, 10
db ‘2. Overlapped block transfer’, 10
db ‘3. Exit’, 10
db ‘Enter your choice:’, 10
menulen: equ $-menu
msg1: db ‘Enter string:’, 10
msg1len: equ $-msg1
msg2: db ‘The result of non overlapped block transfer is:’, 10
msg2len: equ $-msg2
msg3: db ‘The result of overlapped block transfer is:’, 10
msg3len: equ $-msg3
msg4: db ‘Thank you for using programme.’, 10
msg4len: equ $-msg4
msg5: db ‘Enter initial posotion to overlap:’,10
msg5len: equ $-msg5
enter: db ”, 10, 13
enterlen: equ $-enter

;———————-bss section——————-
section .bss
choice: resb 2
string1: resb 50
stringlen1: resb 2
result1: resb 50
string2: resb 50
stringlen2: resb 2
result2: resb 50
loc: resb 1

;———————-text section——————–
section .text
global _start
_start:
disp menu, menulen
accept choice, 2

cmp byte[choice], 31h
je nonoverlapped
cmp byte[choice], 32h
je overlapped
cmp byte[choice], 33h
jae exit

;——————————non overlapped block transfer———————–
;Notes:-
;Put input string in rsi
;get length of input string
;Mov resulting string in rdi
;transfer rsi to rdi twice
;in second loop cl is used and is decremented instead of rcx because segmentation fault.(exact reason is unknown)

nonoverlapped:
disp msg1, msg1len
accept string1, 50

dec rax ;See operation to get length of string
mov [stringlen1], rax

mov rsi, string1
mov rdi, result1
mov rcx, [stringlen1]

l1: mov al, [rsi] ;This complete loop can be replaced by single instruction ->rep movsb
mov [rdi], al
inc rsi
inc rdi
loop l1

mov rsi, string1
mov cl, [stringlen1]

l2: mov al, [rsi] ;This complete loop can be replaced by single instruction ->rep movsb
mov [rdi], al
mov [rdi], al
inc rsi
inc rdi
dec cl
jnz l2

disp msg2, msg2len
disp result1, 50

jmp _start

;——————-Logic of overlapped block transfer———————–
;This block is very confusing and sizes in bss section must be specified same as given
;This block gives error sometimes for unknown reason
;Byheart this block properly.
overlapped:
disp msg1,msg1len
accept string2, 50
dec rax
mov [stringlen2], rax

disp msg5,msg5len
accept loc,1

cmp byte[loc],39h
jbe m5
sub byte[loc],07h
m5: sub byte[loc],30h

mov rsi, string2
mov rdi,result2
mov rcx,[stringlen2]
m3:
mov al,[rsi]
mov [rdi],al
inc rsi
inc rdi
loop m3
mov rsi, string2
mov rdi, result2
add rdi, [loc]
mov cl, [stringlen2]
m4:
mov al,[rsi]
mov [rdi],al
inc rsi
inc rdi
dec cl
jnz m4

disp msg3, msg3len
disp result2,50
jmp _start

exit:
disp msg4, msg4len
mov rax, 60
mov rdi, 00
syscall

Assembly language program to print mean, mode and median

section .data
welmsg db 10,”*** WELCOME TO 64 BIT PROGRAMMING WITH 8087 NDP ***”
welmsg_len equ $-welmsg

meanmsg db 10,”CALCULATED MEAN IS:-”
meanmsg_len equ $-meanmsg

sdmsg db 10,”CALCULATED STANDARD DEVIATION IS:-”
sdmsg_len equ $-sdmsg

varmsg db 10,”CALCULATED VARIANCE IS:-”
varmsg_len equ $-varmsg

thanxmsg db 10,10,10,”ARRAY GIVEN IS= 102.56,198.21,100.67,230.78,67.93″,10
thanxmsg_len equ $-thanxmsg

array dd 102.56,198.21,100.67,230.78,67.93
arraycnt dw 05

dpoint db ‘.’
hdec dq 100

section .bss
dispbuff resb 1
resbuff rest 1

mean resd 1
variance resd 1
%macro linuxsyscall 4
mov rax,%1
mov rdi,%2
mov rsi,%3
mov rdx,%4
syscall
%endmacro

section .text
global _start
_start:

linuxsyscall 01,01,welmsg,welmsg_len
finit                    ;?
fldz                    ;?
mov rbx,array
mov rsi,00
xor rcx,rcx
mov cx,[arraycnt]
up:    fadd dword[RBX+RSI*4]            ;?
inc rsi
loop up

fidiv word[arraycnt]            ;?
fst dword[mean]                ;?
linuxsyscall 01,01,meanmsg,meanmsg_len
call dispres

MOV RCX,00
MOV CX,[arraycnt]
MOV RBX,array
MOV RSI,00
FLDZ                        ;?
up1:    FLDZ
FLD DWORD[RBX+RSI*4]
FSUB DWORD[mean]                ;?
FST ST1
FMUL                        ;?
FADD                        ;?
INC RSI
LOOP up1
FIDIV word[arraycnt]                ;?
FST dWORD[variance]
FSQRT                        ;?

linuxsyscall 01,01,sdmsg,sdmsg_len
CALL dispres

FLD dWORD[variance]
linuxsyscall 01,01,varmsg,varmsg_len
CALL dispres

linuxsyscall 01,01,thanxmsg,thanxmsg_len

exit: mov rax,60
mov rdi,0
syscall

disp8_proc:
mov rdi,dispbuff
mov rcx,02
back:    rol bl,04
mov dl,bl
and dl,0FH
cmp dl,09
jbe next1
add dl,07H
next1:  add dl,30H
mov [rdi],dl
inc rdi
loop back
ret

dispres:
fimul dword[hdec]
fbstp tword[resbuff]
xor rcx,rcx
mov rcx,09H
mov rsi,resbuff+9
up2:    push rcx
push rsi
mov bl,[rsi]
call disp8_proc
linuxsyscall 01,01,dispbuff,2
pop rsi
dec rsi
pop rcx
loop up2

linuxsyscall 01,01,dpoint,1

mov bl,[resbuff]
call disp8_proc
linuxsyscall 01,01,dispbuff,2
ret

;————–steps to run——–

;sitrc@sitrccomp:~$ nasm -f elf64 -o 8087_mean.o 8087_mean.asm
;sitrc@sitrccomp:~$ ld -o 8087_mean 8087_mean.o
;sitrc@sitrccomp:~$ ./8087_mean