section .data
choice: db "Enter your choice: 1.Addition 2.Subtraction 3.Multiplication 4.Division", 0Ah
choicelen: equ $-choice
msg: db "The Addition is: ", 0Ah
len: equ $-msg
msg1: db "The Subtraction is: ", 0Ah
len1: equ $-msg1
msg2: db "The Multiplication is: ", 0Ah
len2: equ $-msg2
msg3: db "The Division is: ", 0Ah
len3: equ $-msg3
div_error_msg: db "Error: Division by zero!", 0Ah
div_error_len: equ $-div_error_msg
arr dq 0x2222222222222222, 0x11111, 0x1111, 0x1111, 0x1111
temp dq 0
section .bss
count: resb 1
result: resb 17
result1: resb 17
result2: resb 17
result3: resb 17
%macro disp 2
mov rax, 1
mov rdi, 1
mov rsi, %1
mov rdx, %2
syscall
%endmacro
section .text
global _start
_start:
disp choice, choicelen
mov rax, 0
mov rdi, 0
mov rsi, temp
mov rdx, 1
syscall
mov al, [temp] ; Load first byte of user input
cmp al, '1' ; Compare with '1'
je ch1
cmp al, '2'
je ch2
cmp al, '3'
je ch3
cmp al, '4'
je ch4
jmp exit ; Invalid input, exit
ch1:
call Addition
jmp exit
ch2:
call Subtraction
jmp exit
ch3:
call Multiplication
jmp exit
ch4:
call Division
jmp exit
Addition:
disp msg, len
mov byte [count], 5
mov rax, 0
mov rsi, arr
loop1:
add rax, [rsi]
add rsi, 8
dec byte [count]
jnz loop1
mov rbx, rax
mov rsi, result
call display
disp result, 17
ret
Subtraction:
disp msg1, len1
mov byte [count], 4
mov rsi, arr
mov rax, [rsi]
add rsi, 8
loop2:
sub rax, [rsi]
add rsi, 8
dec byte [count]
jnz loop2
mov rbx, rax
mov rsi, result1
call display
disp result1, 17
ret
Multiplication:
disp msg2, len2
mov byte [count], 4
mov rsi, arr
mov rax, [rsi]
add rsi, 8
loop3:
mov rcx, [rsi]
mul rcx
add rsi, 8
dec byte [count]
jnz loop3
mov rbx, rax
mov rsi, result2
call display
disp result2, 17
ret
Division:
disp msg3, len3
mov byte [count], 4
mov rsi, arr
mov rax, [rsi]
add rsi, 8
loop4:
mov rcx, [rsi]
cmp rcx, 0 ; Check for division by zero
je div_error
div rcx
add rsi, 8
dec byte [count]
jnz loop4
mov rbx, rax
mov rsi, result3
call display
disp result3, 17
ret
div_error:
disp div_error_msg, div_error_len
jmp exit
display:
mov rdi, rsi ; Destination pointer
mov rcx, 0 ; Digit count
convert:
mov rdx, 0 ; Clear remainder
mov rbx, 10
div rbx ; RAX / 10, remainder in RDX
add dl, '0' ; Convert remainder to ASCII
push rdx ; Store character
inc rcx
test rax, rax
jnz convert
write_digits:
pop rax
mov [rdi], al
inc rdi
loop write_digits
mov byte [rdi], 0Ah ; Newline character
ret
exit:
mov rax, 60
xor rdi, rdi
syscall
To embed this project on your website, copy the following code and paste it into your website's HTML: