TG
TG
data
prompt_msg db "Enter operation (e.g., 15 + 3): ", 0
prompt_len equ $ - prompt_msg
result_msg db "Result: ", 0
result_len equ $ - result_msg
error_msg db "Invalid input or operation", 10, 0
error_len equ $ - error_msg
section .bss
input resb 100
result resb 20
section .text
global _start
_start:
; Display prompt
mov eax, 4
mov ebx, 1
mov ecx, prompt_msg
mov edx, prompt_len
int 0x80
; Read input
mov eax, 3
mov ebx, 0
mov ecx, input
mov edx, 100
int 0x80
; Parse input
mov esi, input
xor eax, eax ; First number
xor ebx, ebx ; Second number
xor ecx, ecx ; Operation
parse_first:
mov dl, [esi]
cmp dl, ' '
je skip_space1
cmp dl, '0'
jl parse_op
cmp dl, '9'
jg parse_op
sub dl, '0'
imul eax, 10
add eax, edx
inc esi
jmp parse_first
skip_space1:
inc esi
jmp parse_first
parse_op:
mov cl, [esi]
inc esi
parse_second:
mov dl, [esi]
cmp dl, ' '
je skip_space2
cmp dl, 10 ; newline
je calculate
cmp dl, '0'
jl error
cmp dl, '9'
jg error
sub dl, '0'
imul ebx, 10
add ebx, edx
inc esi
jmp parse_second
skip_space2:
inc esi
jmp parse_second
calculate:
cmp cl, '+'
je add_op
cmp cl, '-'
je sub_op
cmp cl, '*'
je mul_op
cmp cl, '/'
je div_op
jmp error
add_op:
add eax, ebx
jmp print_result
sub_op:
sub eax, ebx
jmp print_result
mul_op:
imul eax, ebx
jmp print_result
div_op:
cmp ebx, 0
je error
xor edx, edx
idiv ebx
jmp print_result
print_result:
; Convert result to string
mov esi, result
add esi, 19
mov byte [esi], 0
mov ebx, 10
convert_loop:
xor edx, edx
div ebx
add dl, '0'
dec esi
mov [esi], dl
test eax, eax
jnz convert_loop
; Print result
mov eax, 4
mov ebx, 1
mov ecx, esi
mov edx, result
add edx, 20
sub edx, esi
int 0x80
; Print newline
mov eax, 4
mov ebx, 1
mov ecx, 10
push ecx
mov ecx, esp
mov edx, 1
int 0x80
pop ecx
jmp exit
error:
mov eax, 4
mov ebx, 1
mov ecx, error_msg
mov edx, error_len
int 0x80
exit:
mov eax, 1
xor ebx, ebx
int 0x80