Write Assembly language program (ALP) to perform multiplication of two hexadecimal numbers. Use successive addition and add and shift method. Accept input from the user.


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).

16 Bit Code              64 Bit Code

  • 16 Bit TASM Code
;***************MACRO*********************************************
scall macro x,y                  ;macro to take input and output
        lea dx,x
        mov ah,y
        int 21h
endm

.model small
.data

menu db 10d,13d,"                   MENU For Multiplication"
     db 10d,"1. Successive Addition"
     db 10d,"2. Shift and Add method"
     db 10d,"3. Exit"
     db 10d
     db 10d,"Enter your choice: $"
   
 
        m1 db 10d,13d,"Enter First Number: $"
        m2 db 10d,13d,"Enter Second Number: $"
        m3 db 10d,13d,"Answer: $"
        nwline db 10d,13d,'$'
        
        choice db 1 dup('0')       
        num1 db 2 dup('0')
        num2 db 2 dup('0')
        
.code
        mov ax,@data
        mov ds,ax
        
;**********************MAIN LOGIC****************************
main:
        scall menu,09h
        mov ah,01h
        int 21h
        
        cmp al,'3'
        jae exit
        
        mov [choice],al
        
        scall m1,09h
        call numinput
        mov [num1],bl
        
        scall m2,09h
        call numinput
        mov [num2],bl
        
        mov al,[choice]
        cmp al,'1'
        je case1
        cmp al,'2'
        je case2
        
exit:
       mov ah,4Ch
       int 21h
        
case1:
        mov bl,[num1]
        mov cl,[num2]
        mov ax,0               ;ax to store answer
        mov bh,0
        mov ch,0
              
        cmp cl,0               ;check multiplication with 0 condition
        je skip4
loop3:  
        add ax,bx
        loop loop3              ;auto-decrement cx and jmp
skip4:
        mov bx,ax               ;backup ax in bx
        scall m3,09h
        call numdisplay            ;display answer from bx register
jmp main


case2:
        mov bl,[num1]
        mov dl,[num2]
        mov ax,0               ;ax to store answer
        mov dh,0
        mov bh,0
        
        mov cl,16
up1: 
	shl ax,1
	rol bx,1
	jnc down1
	add ax,dx
down1:
	loop up1
	mov bx,ax
        

        mov bx,ax             ;backup ax in bx
        scall m3,09h
        call numdisplay       ;display answer from bx register

        
jmp main        


;******************PROCEDURES********************************
numinput proc
        mov bl,0h
        mov ch,02h

;code to input 2 digit numbers
loop1:
        mov ah,01h
        int 21h

        cmp al,39h
        jbe skip1
        sub al,07h
skip1:
        sub al,30h
        
        cmp ch,01H
        je skip2
        rol al,04H
skip2:
        add bl,al

        dec ch
        jnz loop1

ret
endp    ;End of Procedure

numdisplay proc
        mov dx,bx
        mov ch,04h
;code to display 4 digits
loop2:	
        rol dx,04h
        rol bx,04h
                
        and dl,0Fh
	cmp dl,09h
	jbe skip3
	add dl,07h
skip3:	        
        add dl,30h
		
	mov ah,02h
	int 21h

	
	mov dx,bx
	dec ch
	jnz loop2
        
        scall nwline,09h
ret
endp    ;End of Procedure

end     ;End of Program

  • 64 Bit NASM Code
%macro scall 4                    ;macro to take input and output
        mov rax,%1
        mov rdi,%2
        mov rsi,%3
        mov rdx,%4
        syscall
%endmacro

section .data

menu db 10d,13d,"                   MENU For Multiplication"
     db 10d,"1. Successive Addition"
     db 10d,"2. Shift and Add method"
     db 10d,"3. Exit"
     db 10d
     db 10d,"Enter your choice: "
lnmenu equ $-menu    
 
        m1 db 10d,13d,"Enter First Number: "
        l1 equ $-m1
        m2 db 10d,13d,"Enter Second Number: "
        l2 equ $-m2
        m3 db 10d,13d,"Answer: "
        l3 equ $-m3
        nwline db 10d,13d
        
section .bss
        choice resb 2
        answer resb 20
        num1 resb 20
        num2 resb 20
        temp resb 20
        
section .text
        global _start
        _start:
        
;**********************MAIN LOGIC****************************
main:
        scall 1,1,menu,lnmenu
        scall 0,0,choice,2
        
        cmp byte[choice],'3'
        jae exit
        
        scall 1,1,m1,l1
        scall 0,0,temp,17
        call asciihextohex
        mov qword[num1],rbx
        
        scall 1,1,m2,l2
        scall 0,0,temp,17
        call asciihextohex
        mov qword[num2],rbx
        
        cmp byte[choice],'1'
        je case1
        cmp byte[choice],'2'
        je case2
        
exit:
        mov rax,60
        mov rdi,0
        syscall
        
case1:
        mov rbx,qword[num1]
        mov rcx,qword[num2]
        mov rax,0               ;rax to store answer
        
        cmp rcx,0               ;check multiplication with 0 condition
        je skip3
        
loop1:  
        add rax,rbx
        loop loop1              ;auto-decrement rcx and jmp
skip3:
        mov rbx,rax             ;backup rax in rbx
        scall 1,1,m3,l3
        mov rax,rbx             ;restore rax which has answer
        call display            ;display answer from rax register
jmp main


case2:
        mov rbx,qword[num1]
        mov rdx,qword[num2]
        mov rax,0               ;rax to store answer
        
        mov cl,64
; 16(no. of digit) * 4 (no. of bits per digit) = 64
up1: 
	shl rax,1
	rol rbx,1
	jnc down1
	add rax,rdx
down1:
	loop up1
	mov rbx,rax
        

        mov rbx,rax             ;backup rax in rbx
        scall 1,1,m3,l3
        mov rax,rbx             ;restore rax which has answer
        call display            ;display answer from rax register

        
jmp main        


;******************PROCEDURES********************************
asciihextohex:
	mov rsi,temp
	mov rcx,16
	mov rbx,0
	mov rax,0
	
	
loop4:	rol rbx,04
	mov al,[rsi]
	cmp al,39h
	jbe skip1
	sub al,07h
skip1:	sub al,30h
	
	add rbx,rax
	
	inc rsi
	dec rcx
	jnz loop4
ret	

display:
        mov rsi,answer+15
        mov rcx,16

loop5:	mov rdx,0
        mov rbx,16
        div rbx
        cmp dl,09h
        jbe skip2
        
        add dl,07h
skip2:	add dl,30h
        mov [rsi],dl
        
        dec rsi
        dec rcx
        jnz loop5
        scall 1,1,answer,16      
ret

Output Of 64 Bit Nasm Code:

$ nasm -f elf64 prg.asm
$ ld -o a mil6.o
$ ./a

MENU For Multiplication
1. Successive Addition
2. Shift and Add method
3. Exit

Enter your choice: 1

Enter First Number: 1111111111111111

Enter Second Number: 0000000000000009

Answer: 9999999999999999
MENU For Multiplication
1. Successive Addition
2. Shift and Add method
3. Exit

Enter your choice: 2

Enter First Number: 0000000011111111

Enter Second Number: 000000000000000F

Answer: 00000000FFFFFFFF
MENU For Multiplication
1. Successive Addition
2. Shift and Add method
3. Exit

Enter your choice: 3
$

13 thoughts on “Write Assembly language program (ALP) to perform multiplication of two hexadecimal numbers. Use successive addition and add and shift method. Accept input from the user.

      1. so basically u mean ,when we write
        qword[num1] ,it accesses the value at variable num1.
        Also
        mov qword[num1],rax
        here it acts like a register? As it is able to directly take up a value?
        I am not very clear abt this.Could you please elaborate.
        Thank you in advance.

        Like

      2. Your assumptions are bit correct but not fully.
        I have used qword[num1] because of two reasons,
        1. qword because num1 is not of 64 bits and I am putting 64 bit data into it (so basically typecasting in the language of HLL) and
        2. Square brackets because its a user defined variable and I need to put value (not address) into it.
        Assuming it “like register” is wrong. It still has differences, for example direct copy will not be possible using such casted variables.

        Another example, “cmp byte[choice],’3′” I have used this somewhere in code, so basically 3 and choice are not similar in type or data. So we need to cast it to make it compatible.

        Like

Leave a comment