Monday, January 28, 2019

Write X86/64 ALP to perform multiplication of two 8-bit hexadecimal numbers. Use add and shift method.(use of 64-bit registers is expected)




Flowchart of Multiplication Algorithm

     Example:
                 Using 4-bit numbers, perform the multiplication 9 × 12 (1001 × 1100).

                        
Step
Ax
Dx
Bx
Operation
0
0000 0000
1100
0000 1001
Initialization
1
0000 0000
0000 0000

1100
0110

0001 0010
0001 0010
Shift left B
Shift right Q
2
0000 0000
0000 0000

0110
0011
0010 0100
0010 0100
Shift left B
Shift right Q
3
0010 0100
0010 0100
0010 0100

0011
 0011
0001

0010 0100
0100 1000 0100 1000
Add B to A
Shift left B
Shift right Q
4
0110 1100
0110 1100
0110 1100

0001
 0001
 0000

0100 1000
1001 0000
1001 0000

Add B to A
 Shift left B
Shift right Q
                                                    



64-bit nasm code

section .data
            innum1 db "Enter First 8 bit HEX no: ",10
            lennum1 equ $ -innum1
            innum2 db "Enter Second 8 bit HEX no: ",10
            lennum2 equ $ -innum2
            product db "Multiplication of two HEX no is: ",10
            productlen equ $ -product

section .bss
            num1 resb 3
            num2 resb 3
            result resb 4

%macro output 2                  ;macro for output
            mov rax,01h
            mov rdi,01h
            mov rsi,%1
            mov rdx,%2
            syscall
%endmacro

%macro input 2                    ;macro for input
            mov rax,00h
            mov rdi,00h
            mov rsi,%1
            mov rdx,%2
            syscall
%endmacro

section .text
global _start
 _start:
            output innum1,lennum1
            input num1,03
            output innum2,lennum2
            input num2,03
           
            mov rsi,num1
           call AtoH
           mov bx,ax

            mov rsi,num2
           call AtoH
           mov dx,ax
; Actual logic for multiplication of two numbers using add and shift method
                                                                
            xor ax,ax
            mov ch,08
again:
            bt dx,0   
            jnc skip1   
           add ax,bx   
skip1:   
           shl bx,1  
            shr dx,1
            dec ch
            jnz again

; logic to convert HEX to ASCII

            mov rsi,result
            mov ch,04
            mov cl,04
again1:
            rol ax,cl
            mov bl,al
            and bl,0fh
            cmp bl,09h
            jng skip2
            add bl,07h
skip2:
            add bl,30h
            mov [rsi],bl
            inc rsi
            dec ch
            jnz again1

            output product, productlen
            output result,04
mov rax,3ch
mov rdi,00
syscall

;procedure to convert ASCII to HEX

AtoH:
            xor ax,ax
            mov cl,04
            mov ch,02
up:
            cmp byte[rsi],39h
            jng down
            sub byte[rsi],07h
down:
            sub byte[rsi],30h
            shl ax,cl
            add al,[rsi]
            inc rsi
            dec ch
            jnz up
ret

Output:
[root@localhost ~]# nasm -f elf64 mul.asm
[root@localhost ~]# ld -o mul mul.o
[root@localhost ~]# ./mul
Enter First 8 bit HEX no: 15
Enter Second 8 bit HEX no: 12
Multiplication of two HEX no is: 017A