You are on page 1of 7

Title: Write X86/64 ALP to perform multiplication of two 8-bit hexadecimal numbers.

Use
successive addition and add and shift method. (use of 64-bit registers is expected)

**************** Succesive addition *************


%macro dispmsg 2
mov rax,4
mov rbx,1
mov rcx,%1
mov rdx,%2
int 80h
%endmacro

%macro accept 2
mov rax,3
mov rcx,%1
mov rdx,%2
int 80h
%endmacro

section .data

msgmenu db 10,"******************MENU*******************",10
db 10,"1).Succesive addition",10
db 10,"2).Exit",10
db 10,"Enter your choice: ",10

menulen equ $-msgmenu

msg1 db "Enter 1st number:",10


msg1len equ $-msg1

msg2 db "Enter 2nd number:",10


msg2len equ $-msg2

msg3 db "Multiplication is",10


msg3len equ $-msg3

section .bss
acii1 resb 3
acii1len equ $-acii1

acii2 resb 3
acii2len equ $-acii2

multi1 resb 1
multi2 resb 1
resl resb 1
resh resb 1
dispnum resb 4
displen equ $-dispnum
choice resb 2
choice_len:equ $-choice

section .text
global _start:
_start:

menu:
dispmsg msgmenu,menulen
accept choice,choice_len
case1:
cmp byte[choice],'1'
jne case2
dispmsg msg1,msg1len
accept acii1,3
mov rsi,acii1
call accept8
mov [multi1],bl

dispmsg msg2,msg2len
accept acii2,3
mov rsi,acii2
call accept8
mov [multi2],bl

xor rax,rax
xor rcx,rcx
mov al,[multi1]
mov bl,[multi2]
mov rdx,00
addup:
add ecx,eax
dec bl
jnz addup
mov eax,ecx
call display

case2:
cmp byte[choice],'3'
je ext
jmp menu
ext:
mov rax,1
mov rbx,0
int 80h

accept8:
xor rbx,rbx
mov rcx,02

L12: shl bx,4


mov al,[rsi]
cmp al,39h
jbe sub30
sub al,7h

sub30: sub al,30h


add bl,al
inc rsi
loop L12
ret

display:
mov rsi,dispnum+3
mov rcx,4
xor rbx,rbx
cnt: mov rdx,0

mov rbx,16
div rbx
cmp dl, 09h
jbe add30
add dl, 07h
add30:
add dl,30h
mov [rsi],dl
dec rsi
dec rcx
jnz cnt
dispmsg dispnum,4

************** OUTPUT**********************

proglabii@102proglabii25:~$ nasm -f elf64 as4.asm


proglabii@102proglabii25:~$ ld -o as4 as4.o
proglabii@102proglabii25:~$ ./as4

******************MENU*******************

1).Succesive addition

2).Exit

Enter your choice:


1
Enter 1st number:
12
Enter 2nd number:
10
0120

Title:
Add & Shift Method

%macro dispmsg 2
mov rax,4
mov rbx,1
mov rcx,%1
mov rdx,%2
int 80h
%endmacro

%macro accept 2
mov rax,3
mov rbx,1
mov rcx,%1
mov rdx,%2
int 80h
%endmacro

section .data

msgmenu db 10,"******************MENU*******************",10
db 10,"1)shift & add",10

db 10,"2).Exit",10
db 10,"Enter your choice: ",10
menulen equ $-msgmenu

msg1 db "Enter 1st number:",10


msg1len equ $-msg1

msg2 db "Enter 2nd number:",10


msg2len equ $-msg2

msg3 db "Multiplication is",10


msg3len equ $-msg3

section .bss
acii1 resb 3
acii1len equ $-acii1

acii2 resb 3
acii2len equ $-acii2

multi1 resb 1
multi2 resb 1
resl resb 1
resh resb 1
dispnum resb 4
displen equ $-dispnum

choice resb 2
choice_len:equ $-choice

section .text
global _start:
_start:

menu:
dispmsg msgmenu,menulen
accept choice,choice_len

case1:
cmp byte[choice],'1' ;compare choice
jne case2
dispmsg msg1,msg1len ;display msg
accept acii1,3
mov rsi,acii1 ;move ascii to rsi
call accept8 ;call to accept8
mov [multi1],bl ;point bl to 2nd no.

dispmsg msg2,msg2len ;display msg


accept acii2,3
mov rsi,acii2 ;move ascii to rsi
call accept8 ;call to accept8
mov [multi2],bl ;point bl to 2nd no.
mov al,[multi1] ;mov no.1 to al
mov cl,0 ;clear cl reg.
mov edx,0 ;clear edx reg.
mov edx,08h ;mov 08 to edx

ad:
rcr al,01 ;raotate al reg. with 01 carry to right
jnc next1
mov bh,00h ;clear bh reg.
shl bx,cl ;shift left cl to bx
add [resl],bx ;add bx to resl
mov bl,[multi2] ;mov no.2 into bl
next1:
inc cl ;increament cl reg.
dec edx ;decreament edx reg.
jnz ad ;jump if not zero back to ad
mov ax,[resl] ;mov content of resl to al reg.
call display ;call to display

case2:
cmp byte[choice],'2' ;compare choice
je exit ;jump if equql to exit
jmp menu ;jump back to menu
exit:
mov rax,1 ;mov 01 to rax
mov rbx,0 ;mov 0 to rbx
int 80h ;call int 80h

accept8: ;accept procedure


xor rbx,rbx ;clear rbx reg.
mov rcx,02 ;mov 02 into rcx reg.

L12:
shl bx,4 ;shift left bx with 4bit
mov al,[rsi] ;mov content of rsi to al
cmp al,39h ;compare al with 39
jbe sub30
sub al,7h ;substract al by 7

sub30: sub al,30h ;subtract al with 30


add bl,al ;add al to bl reg.
inc rsi ;increament rsi reg.
loop L12
ret ;return

display:
mov rsi,dispnum+3 ; load last byte address of char_ans in rsi
mov rcx,4 ; number of digits
xor rbx,rbx
cnt:
mov rdx,0
mov rbx,16
div rbx
cmp dl, 09h ; check for remainder in RDX
jbe add30
add dl, 07h
add30:
add dl,30h ; calculate ASCII code
mov [rsi],dl ; store it in buffer
dec rsi ; point to one byte back

dec rcx ; decrement count


jnz cnt ; if not zero repeat

dispmsg dispnum,4 ; display result on screen


ret

******************* OUTPUT***********************

proglabii@102proglabii25:~$ nasm -f elf64 adshf.asm


proglabii@102proglabii25:~$ ld -o adshf adshf.o
proglabii@102proglabii25:~$ ./adshf

******************MENU*******************

1)shift & add

2).Exit

Enter your choice:


1
Enter 1st number:
10
Enter 2nd number:
12
0120

You might also like