0% found this document useful (0 votes)
11 views3 pages

TG

This document is an assembly language program that prompts the user to enter a mathematical operation, reads the input, parses the numbers and the operation, performs the calculation, and displays the result. It handles basic operations such as addition, subtraction, multiplication, and division, while also managing invalid inputs. The program uses system calls for input and output operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

TG

This document is an assembly language program that prompts the user to enter a mathematical operation, reads the input, parses the numbers and the operation, performs the calculation, and displays the result. It handles basic operations such as addition, subtraction, multiplication, and division, while also managing invalid inputs. The program uses system calls for input and output operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

section .

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 message


mov eax, 4
mov ebx, 1
mov ecx, result_msg
mov edx, result_len
int 0x80

; 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

You might also like