Write a switch case driven X86/64 ALP to perform 64-bit hexadecimal arithmetic operations.
%macro print 2 ;Macro for printing
mov rax, 1 ;rax = 1 for sys_write
mov rdi, 1 ;unsigned int file descriptor
mov rsi, %1 ;Buffer address
mov rdx, %2 ;Buffer length
syscall ;Makes system call
%endmacro
%macro input 2 ;Macro for reading
mov rax, 0 ;rax = 0 for sys_read
mov rdi, 0 ;unsigned int file descriptor
mov rsi, %1 ;Buffer address
mov rdx, %2 ;Buffer length
syscall ;Makes system call
%endmacro
section .data ;For declaring initialized 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 " Aman Thakur 3205 🚀" ;Initialising mssg_name
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 : <-- -="" .bss="" .text="" 0="" 0fh="" 10="" 16="" 17="" 1="" 1st="" 2="" 2nd="" 31h="" 32="" 32h="" 33h="" 34h="" 35h="" 3="" 4="" 60="" 64="" 8="" 9="" :="" _start:="" _start="" add="" addition:="" addition="" aking="" alling="" ame="" and="" ascii_hex32="" ascii_hex64:="" ascii_hex64="" be="" bit="" bits="" by="" byte.="" byte="" call="" case_1:="" case_2:="" case_2="" case_3:="" case_3="" case_4:="" case_4="" case_5:="" case_5="" chars="" choice="" cld="" cmp="" code="" conversion="" ctual="" cx="" db="" declared="" declaring="" direction="" display64="" display="" div="" dividend="" divides="" division:="" division="" divisor="" double="" during="" eax="" ebx="" edx="" emainder="" entry="" equ="" equal="" err:="" eserving="" essage="" estination="" esult="" exit:="" exit="" first="" flag="" for="" form="" from="" fter="" gets="" given="" global="" got="" has="" hex1="" hex2="" hexadecimal="" hoosing="" in="" incremented.="" input.="" input="" instruction="" into="" invalid="" is="" it="" ivision="" je="" jmp="" jne="" last="" ld="" learing="" left="" linker="" making="" menu:="" menu="" messages="" mov="" moving="" movsb="" mssg0="" mssg0_len:="" mssg0_len="" mssg1="" mssg1_len:="" mssg1_len="" mssg2="" mssg2_len:="" mssg2_len="" mssg3="" mssg3_len:="" mssg3_len="" mssg4="" mssg4_len:="" mssg4_len="" mssg="" mssg_error="" mssg_error_len:equ="" mssg_error_len="" mssg_len="" mssg_menu="" mssg_menu_len:="" mssg_menu_len="" mssg_mul="" mssg_mul_len:="" mssg_mul_len="" mssg_name="" mssg_name_len="" mssg_quot="" mssg_quot_len:="" mssg_quot_len="" mssg_rem="" mssg_rem_len:="" mssg_rem_len="" mssg_sel="" mssg_sel_len="" mssg_sub="" mssg_sub_len:="" mssg_sub_len="" mssg_sum="" mssg_sum_len:="" mssg_sum_len="" mul="" multiplication:="" multiplication="" newline="" next:="" now="" nput="" nter="" num1="" num2="" number.="" nvalid="" of="" onverting="" operand.="" operand1.="" operand1="" operand2.="" operand2="" operand:="" operands="" operation="" oping="" option="" or="" otating="" ource="" oving="" point.="" points="" pop="" present.="" print="" printed="" printing="" procedure="" push="" quad="" quotient="" rax.="" rax="" rbx.="" rbx="" rcx="" rdi="" rdx:rax="" rdx="" registers.="" remainder="" reminder="" rep="" resb="" resd="" resq="" result="" ret="" rinting="" roblem="" rol="" roll="" rror="" rsi="" second="" section="" sking="" so="" space="" statement.="" stored="" sub="" substraction:="" substraction="" sustraction="" sys_exit.="" syscall="" tells="" temp="" that="" the="" then="" to="" toring="" ubstraction="" ubtracting="" ultiplication="" um="" uotient="" use="" user="" value="" variables.="" when="" will="" word="" xiting="" your=""> F0h
mov al, [rsi] ;Assigning [rsi] to al
cmp al, 2Fh ;Checking For Error
jbe err ;Case: al < 30h (0)
cmp al, 47h
jge err ;Case: al > 46h (F)
cmp al, 39h
jle ELSE ;Case: al <= 39h (9), No Error
cmp al, 40h
jle err ;Case al > 39(9) and al < 41(A)
ELSE:
cmp al, 39h ;Here we convert ascii to hex
jbe sub30h ;If al <= 39h (9), subtract 30h
sub al, 7h ;Else subtract 37h
sub30h:
sub al, 30h ;Subtracting 30h from al
add bl, al
inc rsi
dec rcx
jnz next
ret ;Return
display64:
mov rcx, 16
mov rsi, temp ;rsi points to len
next1:
rol rbx, 4 ;rotating by 4 bits to left
mov al, bl
and al, 0Fh ;To get only last digit, eg. 5Ah & 0Fh = 0Ah
cmp al, 9h ;Case : al <= 9h (9)
jbe add30h ;If True add 30h
add al, 7h ;Else add 37h
add30h:
add al, 30h ;Adding 30h to al
mov [rsi], al ;Storing al in temp
inc rsi
dec rcx
jnz next1
print temp, 16 ;Printing number in ascii
ret ;Return
ascii_hex32:
mov rcx, 8
mov rsi, num2 ;rsi points to num1
mov rbx, 0
next3:
rol ebx, 4 ;Rotating by 4 bits to left, 0Fh -> F0h
mov al, [rsi] ;Assigning [rsi] to al
cmp al, 2Fh ;Checking For Error
jbe err ;Case: al < 30h (0)
cmp al, 47h
jge err ;Case: al > 46h (F)
cmp al, 39h
jle ELSE2 ;Case: al <= 39h (9), No Error
cmp al, 40h
jle err ;Case al > 39(9) and al < 41(A)
ELSE2:
cmp al, 39h ;Here we convert ascii to hex
jbe Sub30h ;If al <= 39h (9), subtract 30h
sub al, 7h ;Else subtract 37h
Sub30h:
sub al, 30h ;Subtracting 30h from al
add bl, al
inc rsi
dec rcx
jnz next3
ret ;Return
-->
0 Comments
If you have any doubt pls let me know..