Write a switch case driven ALP to perform 64-bit hexadecimal arithmetic operations (+,-,*, /) using suitable macros. Define procedure for each operation
section .data
menumsgdb 10,'****** Menu ******',
db 10,'+: Addition'
db 10,'-: Subtraction'
db 10,'*: Multiplication'
db 10,'/: Division'
db 10,10,'Enter your choice:: '
menumsg_len: equ $-menumsg
addmsgdb 10,'Welcome to additon',10
addmsg_lenequ $-addmsg
submsgdb 10,'Welcome to subtraction',10
submsg_lenequ $-submsg
mulmsgdb 10,'Welcome to Multiplication',10
mulmsg_lenequ $-mulmsg
divmsgdb 10,'Welcome to Division',10
divmsg_lenequ $-divmsg
wrchmsgdb 10,10,'Wrong CHoice Entered...!',10
wrchmsg_lenequ $-wrchmsg
no1 dq 0ff05h
no2 dq 0ffh
resmsgdb 10,'Result is:'
resmsg_lenequ $-resmsg
qmsgdb 10,'Quotient::'
qmsg_lenequ $-qmsg
rmsgdb 10,'Remainder::'
rmsg_lenequ $-rmsg
nwmsgdb 10
reshdq 0
resldq 0
section .bss
accbuffresb 2
dispbuffresb 16
%macro print 2
moveax, 4
movebx, 1
movecx, %1
movedx, %2
int 80h
%endmacro
section .text
global _start
start: print menumsg,menumsg_len
mov eax,03
mov ebx,01
movecx,accbuff
mov edx,02
int 80h
cmp byte [accbuff],'+'
jne case2
call add_proc
jmp exit
case2:
cmp byte [accbuff],'-'
jne case3
call sub_proc
jmp exit
case3:
cmp byte [accbuff],'*'
jne case4
call mul_proc
jmp exit
case4:
cmp byte [accbuff],'/'
jnecaseinv
call div_proc
jmp exit
caseinv:printwrchmsg,wrchmsg_len
exit: mov eax,01
mov ebx,0
int 80h
add_proc:
print addmsg,addmsg_len
movrax,[no1]
add rax,[no2]
jnc addnxt1
inc qword [resh]
addnxt1:
mov [resl],rax
print resmsg,resmsg_len
movrbx,[resh]
call disp64num
movrbx,[resl]
call disp64num
print nwmsg,1
ret
sub_proc:
print submsg,submsg_len
movrax,[no1]
sub rax,[no2]
jnc addnxt1
inc qword [resh]
subnxt1:
mov [resl],rax
print resmsg,resmsg_len
movrbx,[resh]
call disp64num
movrbx,[resl]
call disp64num
print nwmsg,1
ret
mul_proc:
print mulmsg,mulmsg_len
movrax,[no1]
movrbx,[no2]
mulrbx
mov [resh],rdx
mov [resl],rax
print resmsg,resmsg_len
movrbx,[resh]
call disp64num
movrbx,[resl]
call disp64num
print nwmsg,1
ret
div_proc:
print divmsg,divmsg_len
movrax,[no1]
mov rdx,0
movrbx,[no2]
div rbx
mov [resh],rdx ;Remainder
mov [resl],rax ;Quotient
print rmsg,rmsg_len
movrbx,[resh]
call disp64num
print qmsg,qmsg_len
movrbx,[resl]
call disp64num
print nwmsg,1
ret
disp64num:
mov ecx,16
movedi,dispbuff
dup1:
rol rbx,4
moval,bl
and al,0fh
cmp al,09
jbedskip
add al,07h
dskip: add al,30h
mov [edi],al
incedi
loop dup1
print dispbuff,16
ret
Output
;[mahendra@(none) alp]$ nasm -f elf64 msmalsw.asm
;[mahendra@(none) alp]$ ld -o msmalswmsmalsw.o
;[mahendra@(none) alp]$ ./msmalsw
;****** Menu ******
;+: Addition
;-: Subtraction
;*: Multiplication
;/: Division
;Enter your choice:: +
;Welcome to additon
;Result is:00000000000000000000000000010004
;[mahendra@(none) alp]$ ./msmalsw
;****** Menu ******
;+: Addition
;-: Subtraction
;*: Multiplication
;/: Division
;Enter your choice:: -
;Welcome to subtraction
;Result is:0000000000000000000000000000FE06
;[mahendra@(none) alp]$ ./msmalsw
;****** Menu ******
;+: Addition
;-: Subtraction
;*: Multiplication
;/: Division
;Enter your choice:: *
;Welcome to Multiplication
;Result is:00000000000000000000000000FE05FB
;[mahendra@(none) alp]$ ./msmalsw
;****** Menu ******
;+: Addition
;-: Subtraction
;*: Multiplication
;/: Division
;Enter your choice:: /
;Welcome to Division
;Remainder::0000000000000005
;Quotient::0000000000000100
;[mahendra@(none) alp]$
Full of errors
ReplyDelete