Problem Statement: 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 accept 2
mov rax,0
mov rdi,0
mov rsi,%1
mov rdx,%2
syscall
%endmacro
%macro print 2
mov rax,1
mov rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro
SECTION .data
msg1: db "Enter number: "
len1: equ $-msg1
msg2: db "Result: "
len2: equ $-msg2
msg3: db "",10
len3: equ $-msg3
msg4: db "1. Booths algorithim",10,"2. Successive addition",10
len4: equ $-msg4
SECTION .bss
result: resb 4
count1: resb 2
count2: resb 2
num: resb 3
temp: resb 1
choice: resb 2
SECTION .text
global main
main:
menu:
print msg4,len4
accept choice,2
cmp byte[choice],31h
je ch1
cmp byte[choice],32h
je ch2
ch1:
mov byte[count2],10h
print msg1,len1
accept num,3
call asc_hex
mov r9,rbx
print msg1,len1
accept num,3
call asc_hex
mov r10,rbx
mov cx,00h
mov rcx,r9
bt cx,0
jc q1
jnc q2
q1:
mov rax,r10
shl rax,16
sub ecx,eax
jmp q2
q3:
mov rax,r10
shl rax,16
add ecx,eax
jmp q2
q2:
bt cx,0h
jc carryset
jnc carrynotset
back:
sar ecx,01h
dec byte[count2]
jnz mul
jz x
carryset:
mov al,01h
mov byte[temp],al
jmp back
carrynotset:
mov al,00h
mov byte[temp],al
jmp back
mul:
mov al,byte[temp]
cmp al,0h
jz x1
jnz x2
x1:
bt cx,0
jc q1
jnc q2
x2:
bt cx,0
jc q2
jnc q3
x:
xor rbx,rbx
mov rbx,rcx
call hex_asc
print msg2,len2
print result,16
print msg3,len3
jmp exit
ch2:
print msg1,len1
accept num,3
call asc_hex
mov r9,rbx
print msg1,len1
accept num,3
call asc_hex
mov cx,bx
xor rax,rax
add1:
add rax,r9
dec cl
jnz add1
mov rbx,rax
call hex_asc
print msg2,len2
print result,16
print msg3,len3
jmp exit
exit:
mov rax,60
mov rdi,1
syscall
asc_hex:
mov rbx,00
mov r11,0
mov ch,02
loop:
mov al,[num+r11]
cmp al,61h
jb next
sub al,57h
jmp next2
next:
cmp al,41h
jb next1
sub al,37h
jmp next2
next1:
sub al,30h
next2:
shl rbx,04h
add rbx,rax
inc r11
dec ch
jnz loop
ret
hex_asc:
mov byte[count1],16h
mov r11,rbx
mov r12,result
l1:
rol rbx,04h
mov dl,bl
and dl,0Fh
cmp dl,09h
jbe l2
add dl,07h
l2:
add dl,30h
mov [r12],dl
inc r12
dec byte[count1]
jnz l1
ret
%macro accept 2
mov rax,0
mov rdi,0
mov rsi,%1
mov rdx,%2
syscall
%endmacro
%macro print 2
mov rax,1
mov rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro
SECTION .data
msg1: db "Enter number: "
len1: equ $-msg1
msg2: db "Result: "
len2: equ $-msg2
msg3: db "",10
len3: equ $-msg3
msg4: db "1. Booths algorithim",10,"2. Successive addition",10
len4: equ $-msg4
SECTION .bss
result: resb 4
count1: resb 2
count2: resb 2
num: resb 3
temp: resb 1
choice: resb 2
SECTION .text
global main
main:
menu:
print msg4,len4
accept choice,2
cmp byte[choice],31h
je ch1
cmp byte[choice],32h
je ch2
ch1:
mov byte[count2],10h
print msg1,len1
accept num,3
call asc_hex
mov r9,rbx
print msg1,len1
accept num,3
call asc_hex
mov r10,rbx
mov cx,00h
mov rcx,r9
bt cx,0
jc q1
jnc q2
q1:
mov rax,r10
shl rax,16
sub ecx,eax
jmp q2
q3:
mov rax,r10
shl rax,16
add ecx,eax
jmp q2
q2:
bt cx,0h
jc carryset
jnc carrynotset
back:
sar ecx,01h
dec byte[count2]
jnz mul
jz x
carryset:
mov al,01h
mov byte[temp],al
jmp back
carrynotset:
mov al,00h
mov byte[temp],al
jmp back
mul:
mov al,byte[temp]
cmp al,0h
jz x1
jnz x2
x1:
bt cx,0
jc q1
jnc q2
x2:
bt cx,0
jc q2
jnc q3
x:
xor rbx,rbx
mov rbx,rcx
call hex_asc
print msg2,len2
print result,16
print msg3,len3
jmp exit
ch2:
print msg1,len1
accept num,3
call asc_hex
mov r9,rbx
print msg1,len1
accept num,3
call asc_hex
mov cx,bx
xor rax,rax
add1:
add rax,r9
dec cl
jnz add1
mov rbx,rax
call hex_asc
print msg2,len2
print result,16
print msg3,len3
jmp exit
exit:
mov rax,60
mov rdi,1
syscall
asc_hex:
mov rbx,00
mov r11,0
mov ch,02
loop:
mov al,[num+r11]
cmp al,61h
jb next
sub al,57h
jmp next2
next:
cmp al,41h
jb next1
sub al,37h
jmp next2
next1:
sub al,30h
next2:
shl rbx,04h
add rbx,rax
inc r11
dec ch
jnz loop
ret
hex_asc:
mov byte[count1],16h
mov r11,rbx
mov r12,result
l1:
rol rbx,04h
mov dl,bl
and dl,0Fh
cmp dl,09h
jbe l2
add dl,07h
l2:
add dl,30h
mov [r12],dl
inc r12
dec byte[count1]
jnz l1
ret
Output
amodi@ubuntu:~/MIL/Assign6$ nasm -f elf64 -l asgn6.lst
asgn6.asm
amodi@ubuntu:~/MIL/Assign6$ ld -o asgn6 asgn6.o
ld: warning: cannot find entry symbol _start; defaulting to 00000000004000b0
amodi@ubuntu:~/MIL/Assign6$ ./asgn6
1. Booths algorithim
2. Successive addition
1
Enter number: 02
Enter number: 05
Result: 00000000000000A
amodi@ubuntu:~/MIL/Assign6$ ./asgn6
1. Booths algorithim
2. Successive addition
2
Enter number: 02
Enter number: 03
Result: 000000000000006
amodi@ubuntu:~/MIL/Assign6$ ld -o asgn6 asgn6.o
ld: warning: cannot find entry symbol _start; defaulting to 00000000004000b0
amodi@ubuntu:~/MIL/Assign6$ ./asgn6
1. Booths algorithim
2. Successive addition
1
Enter number: 02
Enter number: 05
Result: 00000000000000A
amodi@ubuntu:~/MIL/Assign6$ ./asgn6
1. Booths algorithim
2. Successive addition
2
Enter number: 02
Enter number: 03
Result: 000000000000006
No comments:
Post a Comment