MP Codes
MP Codes
; 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.
; 20173
syscall
syscall
section .data
msg1 db "Number : " ; msg1 variable with content "Number : " created
section .bss
_start :
syscall
write temp, 16 ; calling write macro with temp as message and 16 as its
length
;20173
syscall
syscall
%endmacro
; ending macro
section .data
msg1 db "Enter a String : " ; msg1 variable with content "Enter a string : " is
created
section .bss
section .text
_start :
syscall
; Roll No : 20173
; Write a switch case driven X86/64 ALP to perform 64-bit hexadecimal arithmetic
operations(+,-,*,/) using suitable macros. Define procedure for each operation.
%macro write 2
syscall
%endmacro
%macro read 2
syscall
%endmacro
section .data
msg db "------------------MENU------------------", 10
endl db 10
section .bss
choice resb 2
num resb 17
num1 resq 1
num2 resq 1
temp resb 16
section .text
global _start:
_start:
main_menu :
read choice, 2
je case1
je case2
cmp byte[choice], "3"
je case3
je case4
je case5
jmp main_menu
case1 :
call addition
jmp main_menu
case2 :
call subtraction
jmp main_menu
case3 :
call division
jmp main_menu
case4 :
call multiplication
jmp main_menu
case5 :
mov rax, 60
mov rdi, 0
syscall
addition :
write msg1, len
ret
subtraction :
read num, 17
call ascii_hex
read num, 17
call ascii_hex
call display
ret
multiplication :
read num, 17
call ascii_hex
read num, 17
call ascii_hex
mul rbx ; multiplying rax by rbx the resultant's first half is stored in rdx and the
remaining in rax
call display
ret
division :
read num, 17
call ascii_hex
read num, 17
call ascii_hex
mov edx, 0
div ecx ; the quotient is stored in the accumulator while the remainder is stored in the
data register.
mov rbx, 0
call display
pop rbx
call display
ret
ascii_hex:
mov rbx, 0
mov rcx, 16
next :
rol rbx, 4
jbe sub30h
sub al, 7h
sub30h :
add bl, al
inc rsi
loop next
ret
; Procedure for converting hexadecimal number into ascii characters and displaying them.
display :
mov rcx, 16
next1 :
rol rbx, 4
mov al, bl
cmp al, 9
jbe add30h
add al, 7h
add30h :
mov [rsi], al
inc rsi
loop next1
write temp, 16
write endl, 1
ret
4)
;20173
%macro print 2
syscall
%endmacro
%macro input 2
syscall
%endmacro
_start:
fl:
push rcx
jmp skip
nci:
skip:
pop rcx
mov rdi,0
syscall
next1:
jbe minus30h ;if value is less than or equal then minus30h will be called
minus30h:
loop next1
next:
jbe addition30h ;if value is less than or equal then addition30h will be called
addition30h:
loop next
ret ;returning
; 5)
; Roll NO : 20173
; Write X86/64 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.
(Wherever necessary, use 64-bit registers).
; Date : 19/04/2022
%macro write 2
mov rax, 1
mov rdi, 1
mov rsi, %1
mov rdx, %2
syscall
%endmacro
%macro read 2
mov rax, 0
mov rdi, 0
mov rsi, %1
mov rdx, %2
syscall
%endmacro
section .data
msg db "------------------MENU------------------", 10
endl db 10
section .bss
num resd 1
choice resb 2
a_hex resb 5
a_bcd resb 6
buffer resb 5
temp resb 4
section .text
global _start
_start :
main_menu :
read choice, 2
cmp byte[choice], "1"
je case1
je case2
je case3
jmp main_menu
case1 :
read a_hex, 5
call hex_bcd
jmp main_menu
case2 :
read a_bcd, 6
call bcd_hex
jmp main_menu
case3 :
mov rax, 60
mov rdi, 0
syscall
hex_bcd:
call ascii_hex ; first convert the number to hex form
mov rcx, 5
convert :
push dx ; store the remainder in the stack, while the quotient remains in the
accumualator
loop convert
mov rcx, 5
print_bcd :
loop print_bcd
write endl, 1
ret
bcd_hex :
mov rcx, 5
b_convert :
loop b_convert
mov bx, ax
call display
ret
ascii_hex:
mov rbx, 0
mov rcx, 4
next :
rol bx, 4
jbe sub30h
sub al, 7h
sub30h :
add bl, al
inc rsi
loop next
ret
display :
mov rcx, 4
next1 :
rol bx, 4
mov al, bl
cmp al, 9
jbe add30h
add al, 7h
add30h :
mov [rsi], al
inc rsi
loop next1
write temp, 4
write endl, 1
ret
; 6)
; Roll NO : 20173
; Write X86/64 ALP to perform non-overlapped block transfer without string specific
instructions. Block containing data can be defined in the data segment.
; Date : 26/04/2022
%macro write 2
mov rax, 1
mov rdi, 1
mov rsi, %1
mov rdx, %2
syscall
%endmacro
%macro read 2
mov rax, 0
mov rdi, 0
mov rsi, %1
mov rdx, %2
syscall
%endmacro
section .data
msg db "------------------MENU------------------", 10
endl db 10
tab db 20h
b db "Before : ", 10
a db "After : ", 10
l equ $-a
section .bss
temp resb 16
choice resb 2
section .text
global _start
_start :
main_menu :
read choice, 2
cmp byte[choice], "1"
je case1
je case2
je case3
case1 :
write b, l
mov rcx, 5
mov rax, 0
init1 :
add rsi, 8
loop init1
call displayBlock
write endl, 1
write a, l
mov rcx, 5
mov rsi, src
next :
add rsi, 8
add rdi, 8
loop next
call displayBlock
write endl, 1
jmp main_menu
case2 :
write b, l
mov rcx, 5
mov rax, 0
init2 :
add rsi, 8
loop init2
call displayBlock
; Logic for transferring the blocks
write endl, 1
write a, l
mov rcx, 5
cld
next2 :
movsq
loop next2
call displayBlock
write endl, 1
jmp main_menu
case3 :
mov rax, 60
mov rdi, 0
syscall
displayBlock :
mov rcx, 5
nextD :
push rcx
push rsi
push rdi
mov rbx, [rsi]
call display
write tab, 1
pop rax
push rax
call display
write endl, 1
pop rdi
pop rsi
pop rcx
add rdi, 8
add rsi, 8
loop nextD
write endl, 1
mov rcx, 5
nextA :
push rcx
push rsi
push rdi
write tab, 1
pop rax
push rax
call display
write endl, 1
pop rdi
pop rsi
pop rcx
add rdi, 8
add rsi, 8
loop nextA
ret
display :
mov rcx, 16
next1 :
rol rbx, 4
mov al, bl
cmp al, 9
jbe add30h
add al, 7h
add30h :
mov [rsi], al
inc rsi
loop next1
write temp, 16
ret
; 7)
; Roll NO : 20173
; Write X86/64 ALP to perform overlapped block transfer with string specific instructions Block
containing data can be defined in the data segment.
; Date : 26/04/2022
%macro write 2
mov rax, 1
mov rdi, 1
mov rsi, %1
mov rdx, %2
syscall
%endmacro
%macro read 2
mov rax, 0
mov rdi, 0
mov rsi, %1
mov rdx, %2
syscall
%endmacro
section .data
msg db "------------------MENU------------------", 10
endl db 10
tab db 20h
b db "Before : ", 10
a db "After : ", 10
l equ $-a
section .bss
temp resb 16
choice resb 2
section .text
global _start
_start :
main_menu :
read choice, 2
je case1
cmp byte[choice], "2"
je case2
je case3
case1 :
write b, l
mov rcx, 3
mov rax, 0
init1 :
add rdi, 8
loop init1
call displayBlock
write endl, 1
write a, l
mov rcx, 3
next :
mov rax, [rsi]
add rsi, 8
add rdi, 8
loop next
call displayBlock
write endl, 1
jmp main_menu
case2 :
write b, l
mov rcx, 3
mov rax, 0
init2 :
add rdi, 8
loop init2
call displayBlock
write endl, 1
write a, l
mov rcx, 3
mov rsi, src
cld
next2 :
movsq
loop next2
call displayBlock
write endl, 1
jmp main_menu
case3 :
mov rax, 60
mov rdi, 0
syscall
displayBlock :
mov rcx, 6
nextD :
push rcx
push rsi
call display
write tab, 1
pop rbx
push rbx
call display
write endl, 1
pop rsi
pop rcx
add rsi, 8
loop nextD
write endl, 1
ret
display :
mov rcx, 16
next1 :
rol rbx, 4
mov al, bl
cmp al, 9
jbe add30h
add al, 7h
add30h :
inc rsi
loop next1
write temp, 16
ret
; 8)
; Roll NO : 20173
; Write X86/64 ALP to perform multiplication of two 8-bit hexadecimal numbers. Use
successive addition and add and shift method. (Use of 64-bit registers is expected).
; Date : /04/22
%macro write 2
mov rax, 1
mov rdi, 1
mov rsi, %1
mov rdx, %2
syscall
%endmacro
%macro read 2
mov rax, 0
mov rdi, 0
mov rsi, %1
mov rdx, %2
syscall
%endmacro
section .data
msg db "------------------MENU------------------", 10
endl db 10
section .bss
choice resb 2
num resb 3
num1 resb 1
num2 resb 1
temp resd 1
section .text
global _start
_start :
main_menu :
read choice, 2
je case1
cmp byte[choice], "2"
je case2
je case3
jmp main_menu
case1 :
read num, 3
call ascii_hex
mov [num1], bl
read num, 3
call ascii_hex
mov [num2], bl
call successive_addition
jmp main_menu
case2 :
read num, 3
call ascii_hex
mov [num1], bl
read num, 3
call ascii_hex
mov [num2], bl
call add_shift
jmp main_menu
case3 :
mov rax, 60
mov rdi, 0
syscall
successive_addition:
mov rbx, 0
next_add :
add bx, ax
loop next_add
call display
ret
; Procdure to perform Add and Shift method to compute multiplication of two numbers
add_shift:
mov rbx, 0
mov cl, 8
next_shift :
bt rdx, 0
jnc ahead
add bx, ax
ahead :
rol ax, 1
ror dl, 1
loop next_shift
call display
ret
ascii_hex:
mov bl, 0
mov rcx, 2
next :
rol bl, 4
jbe sub30h
sub al, 7h
sub30h :
add bl, al
inc rsi
loop next
ret
; Procedure to convert hexdecimal number to ascii
display :
mov rcx, 4
next1 :
rol bx, 4
mov al, bl
cmp al, 9
jbe add30h
add al, 7h
add30h :
mov [rsi], al
inc rsi
loop next1
write temp, 4
write endl, 1
ret