7203

singage · updated May 16, 2022
; Write a switch case driven X86/64 ALP to perform 64-bit hexadecimal arithmetic operations.

%macro print 2			
	mov rax, 1		 
	mov rdi, 1		    
	mov rsi, %1		    
	mov rdx, %2		   
	syscall		     
%endmacro

%macro input 2			
	mov rax, 0		    
	mov rdi, 0		   
	mov rsi, %1		   
	mov rdx, %2		    
	syscall		        
%endmacro

section .data			                 
	
	mssg db "Write a switch case driven X86/64 ALP to perform 64-bit hexadecimal arithmetic operations."
	mssg_len:equ $ - mssg
					
	mssg_name db " ABhijit singh"		
	mssg_name_len: equ $-mssg_name	
			
	mssg_sel db "--> Select an option from the menu :"
	mssg_sel_len: equ $ - mssg_sel

	mssg_menu db "--> Menu : <--", 10, "1. Addition", 10, "2. Substraction", 10, "3. Multiplication", 10, "4. Division", 10, "5. Exit", 10
	mssg_menu_len: equ $ - mssg_menu
	
	
	mssg1 db "Enter your 1st operand: "			
	mssg1_len: equ $-mssg1		
	mssg2 db "Enter your 2nd operand: "		
	mssg2_len: equ $-mssg2
	mssg3 db "Enter the divisor : "			
	mssg3_len: equ $-mssg3	
	mssg4 db "Enter the dividend : "			
	mssg4_len: equ $-mssg4	
	
	
	mssg_sum db "Sum of given operands : "
	mssg_sum_len: equ $-mssg_sum

	mssg_sub db "Substraction of given operands : "
	mssg_sub_len: equ $-mssg_sub

	mssg_mul db "Multiplication of given operands : "
	mssg_mul_len: equ $-mssg_mul

	mssg_quot db "Quotient : "
	mssg_quot_len: equ $-mssg_quot

	mssg_rem db "Remainder : "
	mssg_rem_len: equ $-mssg_rem
	
	mssg_error db "Error!!"				
	mssg_error_len:equ $-mssg_error		
	
	mssg0 db "Invalid Option!"	
	mssg0_len: equ $-mssg0

	space db " "	
	newline db 10						
	
section .bss			                
	num1 resb 17		                
	num2 resb 9
	hex1 resq 1		                  
	hex2 resq 1		                   
	divisor resd 1		             
	temp resb 16		                
	choice resb 2	
	
section .text						  
	global _start				
	_start: 					    
	
	print mssg, mssg_len				
	print newline, 1				
	print mssg_name, mssg_name_len		
	print newline, 1
	
	menu:
		
		print mssg_menu, mssg_menu_len	
		print newline, 1
		print mssg_sel, mssg_sel_len	
		input choice, 2
		
		case_1:
			cmp byte[choice], 31h		
			jne case_2
			call addition			 
			jmp menu
		case_2:	
			cmp byte[choice], 32h
			jne case_3
			call substraction		  
			jmp menu
		case_3:
			cmp byte[choice], 33h
			jne case_4
			call multiplication		      
			jmp menu
		case_4:
			cmp byte[choice], 34h
			jne case_5
			call division
			jmp menu
		case_5:
			cmp byte[choice], 35h
			je Exit			
			print mssg0, mssg0_len
			print newline, 1
			jmp menu
			
	Exit:
	mov rax, 60					       
	mov rdi, 0
	syscall
	err:
		print mssg_error, mssg_error_len	
		print newline, 1
		mov rax, 60				
		mov rdi, 0
		syscall
	
	
addition:
	print newline, 1
	print mssg1, mssg1_len
	input num1, 17			                  
	
	call ascii_hex64		              
	mov [hex1], rbx		                   
	
	print mssg2, mssg2_len
	input num1, 17			                  
	
	call ascii_hex64
	mov [hex2], rbx		                       
	
	mov rax, [hex1]		                        
	add rax, [hex2]		                   
	mov rbx, rax 			                 
	
	print mssg_sum, mssg_sum_len
	call display64			                 
	print newline, 1
	print newline, 1
	ret

substraction:
	print newline, 1
	print mssg1, mssg1_len
	input num1, 17			                  
			
	call ascii_hex64		                
	mov [hex1], rbx		                   
	
	print mssg2, mssg2_len
	input num1, 17			                
	
	call ascii_hex64
	mov [hex2], rbx		                    
	
	mov rax, [hex1]		                    
	sub rax, [hex2]		                  
	mov rbx, rax			                
	
	print mssg_sub, mssg_sub_len
	call display64			                  
	print newline, 1
	print newline, 1
	ret

multiplication:
	print newline, 1
	print mssg1, mssg1_len
	input num1, 17			                 
			
	call ascii_hex64		                 
	mov [hex1], rbx		                    
	
	print mssg2, mssg2_len
	input num1, 17			                 
	
	call ascii_hex64
	mov [hex2], rbx		                       
	
	mov rax, [hex1]		                        
	mov rbx, [hex2]		                     
	
	mul rbx			                       
	
	push rax
	push rdx
	print mssg_mul, mssg_mul_len	            
	pop rdx	
	
	mov rbx, rdx
	call display64			              
	print space, 1
	
	pop rax
	
	mov rbx, rax
	call display64			                    
	print newline, 1
	print newline, 1
	
	ret
	
division:
	print newline, 1
	print mssg4, mssg4_len		                
	input num1, 17
	
	print mssg3, mssg3_len  	               
	input num2, 9
	
	call ascii_hex32		               
	mov [divisor], rbx		                    
	
	
	mov rsi, num1			                    
	mov rdi, num2			                   	
	cld				                      
	mov cx, 8
	rep movsb			                 
	
	call ascii_hex32		                 	
	mov edx, ebx			                   
	
	mov rsi, num1+8		                     
	mov rdi, num2			                    
	
	cld				                            
	mov cx, 8
	rep movsb			                       
	
	push rdx
	call ascii_hex32		                   
	pop rdx
	mov eax, ebx			                   
	
	mov ebx, [divisor]		                   
	div ebx			                            
					                    
	
	push rdx
	push rax
	print mssg_quot, mssg_quot_len              
	pop rax			               
	
	mov ebx, eax
	call display64			                
	print newline, 1
	
	print mssg_rem, mssg_rem_len	           
	pop rdx			              
	
	mov ebx, edx			
	call display64			              
	print newline, 1
	print newline, 1
	ret
	
ascii_hex64:
	mov rcx, 16  					
	mov rsi, num1					
	mov rbx, 0 					
	next:						
		rol rbx, 4				               
		mov al, [rsi]				           
		
		cmp al, 2Fh 				           
		jbe err				                 
		cmp al, 47h	
		jge err				                   
		cmp al, 39h				
		jle ELSE				               
		cmp al, 40h				
		jle err				                    
		
		ELSE:
							
		cmp al, 39h				              
		jbe  sub30h				              
		sub al, 7h				               
		sub30h:
			sub al, 30h			                
		add bl, al				
		inc rsi				
		dec rcx				
	jnz next
	
	ret 						                


display64:
	mov rcx, 16					
	mov rsi, temp					           
	next1:	
		rol rbx, 4				                
		mov al, bl				
		and al, 0Fh				    
		cmp al, 9h				                
		jbe add30h				              
		add al, 7h				              

		add30h:
			add al, 30h			               
		mov [rsi], al				         
		inc rsi				
		dec rcx				
	jnz next1
	
	print temp, 16					           
	ret						                    

ascii_hex32:
	mov rcx, 8  					
	mov rsi, num2					        
	mov rbx, 0 					
	next3:						
		rol ebx, 4				               
		mov al, [rsi]				            
		
		cmp al, 2Fh 				         
		jbe err				                    
		cmp al, 47h	
		jge err				                 
		cmp al, 39h				
		jle ELSE2				                
		cmp al, 40h				
		jle err				                    
		
		ELSE2:
							
		cmp al, 39h				       
		jbe  Sub30h				          
		sub al, 7h				          
		Sub30h:
			sub al, 30h			                
		add bl, al				
		inc rsi				
		dec rcx				
	jnz next3
	
	ret 						                
Output

Comments

Please sign up or log in to contribute to the discussion.