;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)
;——————————p5.asm————————————————
%macro dispmsg 2
mov eax,4
mov ebx,1
mov ecx,%1
mov edx,%2
int 80h
%endmacro
%macro accept 2
mov eax,3
mov ebx,0
mov ecx,%1
mov edx,%2
int 80h
%endmacro
section .data
msg db 10,"Enter two digit Number::"
msg_len equ $-msg
res db 10,"Multiplication of elements is::"
res_len equ $-res
choice db 10,13,"****MENU****"
db 10,13,"1.Successive Addition"
db 10,13,"2.Add and Shift method"
db 10,13,"3.Exit"
db 10,13,"Enter choice="
choice_len equ $-choice
section .bss
num resb 03
num1 resb 01
result resb 04
cho resb 2
section .text
global _start
_start:
dispmsg choice,choice_len
accept cho,2
cmp byte[cho],"1"
je a
cmp byte[cho],"2"
je b
jmp exit
a:
call Succe_addition
jmp _start
b:
call Add_shift
jmp _start
exit:
mov eax,1
int 80h
convert:
mov ebx,0
mov ecx,0
mov eax,0
mov ecx,02
mov esi,num
up1:
rol bl,04
mov al,[esi]
cmp al,"9"
jbe sub30
sub al,37h
sub30:
sub al,30h
add bl,al
inc esi
dec ecx
jnz up1
ret
display:
mov ecx,4
mov edi,result
dup1:
rol bx,4
mov al,bl
and al,0fh
cmp al,09h
jg p3
add al,30h
jmp p4
p3: add al,37h
p4:mov [edi],al
inc edi
loop dup1
dispmsg result,4
ret
Succe_addition:
dispmsg msg,msg_len
accept num,3
call convert
mov [num1],bl
dispmsg msg,msg_len
accept num,3
call convert
mov ecx,0
mov eax,0
mov eax,[num1]
repet:
add ecx,eax
dec bl
jnz repet
mov [result],ecx
dispmsg res,res_len
mov ebx,[result]
call display
ret
Add_shift:
dispmsg msg,msg_len
accept num,3
call convert
mov [num1],bl
dispmsg msg,msg_len
accept num,3
call convert
mov [num],bl
mov ebx,0
mov ecx,0
mov edx,0
mov eax,0
mov dl,08
mov al,[num1]
mov bl,[num]
p11:
shr bx,01
jnc p
add cx,ax
p:
shl ax,01
dec dl
jnz p11
mov [result],ecx
dispmsg res,res_len
mov ebx,[result]
call display
ret
OUTPUT:
swlab@swlab-Veriton-M200-H81:~$ nasm -f elf mult.asm
swlab@swlab-Veriton-M200-H81:~$ ld -m elf_i386 -s -o mult mult.o
swlab@swlab-Veriton-M200-H81:~$ ./mult
****MENU****
1.Successive Addition
2.Add and Shift method
3.Exit
Enter choice=1
Enter two digit Number::03
Enter two digit Number::05
Multiplication of elements is::000F
****MENU****
1.Successive Addition
2.Add and Shift method
3.Exit
Enter choice=2
Enter two digit Number::03
Enter two digit Number::04
Multiplication of elements is::000C
****MENU****
1.Successive Addition
2.Add and Shift method
3.Exit
Enter choice=3
swlab@swlab-Veriton-M200-H81:~$
;Accept input from the user. (use of 64-bit registers is expected)
;——————————p5.asm————————————————
%macro dispmsg 2
mov eax,4
mov ebx,1
mov ecx,%1
mov edx,%2
int 80h
%endmacro
%macro accept 2
mov eax,3
mov ebx,0
mov ecx,%1
mov edx,%2
int 80h
%endmacro
section .data
msg db 10,"Enter two digit Number::"
msg_len equ $-msg
res db 10,"Multiplication of elements is::"
res_len equ $-res
choice db 10,13,"****MENU****"
db 10,13,"1.Successive Addition"
db 10,13,"2.Add and Shift method"
db 10,13,"3.Exit"
db 10,13,"Enter choice="
choice_len equ $-choice
section .bss
num resb 03
num1 resb 01
result resb 04
cho resb 2
section .text
global _start
_start:
dispmsg choice,choice_len
accept cho,2
cmp byte[cho],"1"
je a
cmp byte[cho],"2"
je b
jmp exit
a:
call Succe_addition
jmp _start
b:
call Add_shift
jmp _start
exit:
mov eax,1
int 80h
convert:
mov ebx,0
mov ecx,0
mov eax,0
mov ecx,02
mov esi,num
up1:
rol bl,04
mov al,[esi]
cmp al,"9"
jbe sub30
sub al,37h
sub30:
sub al,30h
add bl,al
inc esi
dec ecx
jnz up1
ret
display:
mov ecx,4
mov edi,result
dup1:
rol bx,4
mov al,bl
and al,0fh
cmp al,09h
jg p3
add al,30h
jmp p4
p3: add al,37h
p4:mov [edi],al
inc edi
loop dup1
dispmsg result,4
ret
Succe_addition:
dispmsg msg,msg_len
accept num,3
call convert
mov [num1],bl
dispmsg msg,msg_len
accept num,3
call convert
mov ecx,0
mov eax,0
mov eax,[num1]
repet:
add ecx,eax
dec bl
jnz repet
mov [result],ecx
dispmsg res,res_len
mov ebx,[result]
call display
ret
Add_shift:
dispmsg msg,msg_len
accept num,3
call convert
mov [num1],bl
dispmsg msg,msg_len
accept num,3
call convert
mov [num],bl
mov ebx,0
mov ecx,0
mov edx,0
mov eax,0
mov dl,08
mov al,[num1]
mov bl,[num]
p11:
shr bx,01
jnc p
add cx,ax
p:
shl ax,01
dec dl
jnz p11
mov [result],ecx
dispmsg res,res_len
mov ebx,[result]
call display
ret
OUTPUT:
swlab@swlab-Veriton-M200-H81:~$ nasm -f elf mult.asm
swlab@swlab-Veriton-M200-H81:~$ ld -m elf_i386 -s -o mult mult.o
swlab@swlab-Veriton-M200-H81:~$ ./mult
****MENU****
1.Successive Addition
2.Add and Shift method
3.Exit
Enter choice=1
Enter two digit Number::03
Enter two digit Number::05
Multiplication of elements is::000F
****MENU****
1.Successive Addition
2.Add and Shift method
3.Exit
Enter choice=2
Enter two digit Number::03
Enter two digit Number::04
Multiplication of elements is::000C
****MENU****
1.Successive Addition
2.Add and Shift method
3.Exit
Enter choice=3
swlab@swlab-Veriton-M200-H81:~$
No comments:
Post a Comment