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

Leave a comment