.model small
.stack 1000h
.data
prompt db “Welcome to the Main Menu”, 13, 10, “1. Adding 2 numbers”, 13, 10, “2. Odd or Even”, 13, 10, “3. Positive or Negative”, 13, 10, “4. Exit”, 13, 10, “Enter your choice: $”
invalid db “Invalid choice. Please try again.$”
newline db 13, 10, “$”
choice db 0
option1 db “Enter two numbers to add:”, 13, 10, “$”
option2 db “Enter a number to check if it’s odd or even:”, 13, 10, “$”
option3 db “Enter a number to check if it’s positive or negative:”, 13, 10, “$”
result db “Result: $”
buffer db 6 dup(?) ; Buffer for input numbers or result
.code
; Function to display newline
display_newline:
mov ah, 09h
lea dx, newline
int 21h
ret
; Function to read user input (single digit)
read_digit:
mov ah, 01h
int 21h
sub al, ‘0’ ; Convert ASCII digit to numeric value
ret
; Function to read user input (string)
read_string:
mov ah, 0Ah
mov dx, offset buffer
int 21h
ret
; Function to display a string
display_string:
mov ah, 09h
lea dx, buffer
int 21h
ret
; Function to add two numbers
add_numbers:
mov ah, 09h
lea dx, option1
int 21h
call display_newline
; Read first number
call read_string
mov bl, buffer
; Display prompt for second number
mov ah, 09h
lea dx, option1
int 21h
call display_newline
; Read second number
call read_string
add bl, buffer
; Display result
mov ah, 09h
lea dx, result
int 21h
mov ah, 02h
mov dl, bl
add dl, '0' ; Convert result back to ASCII character
int 21h
ret
; Function to check if a number is odd or even
check_odd_even:
mov ah, 09h
lea dx, option2
int 21h
call display_newline
; Read the number
call read_string
mov bl, buffer
; Check if the number is odd or even
mov ah, 09h
lea dx, result
int 21h
mov ax, bl
test al, 1
jnz number_odd ; Jump if not zero (odd)
mov dx, offset is_even
jmp display_string
number_odd:
mov dx, offset is_odd
jmp display_string
; Function to check if a number is positive or negative
check_positive_negative:
mov ah, 09h
lea dx, option3
int 21h
call display_newline
; Read the number
call read_string
mov bl, buffer
; Check if the number is positive or negative
mov ah, 09h
lea dx, result
int 21h
mov ax, bl
test ax, ax
jns number_positive ; Jump if negative
mov dx, offset is_negative
jmp display_string
number_positive:
mov dx, offset is_positive
jmp display_string
start:
; Initialize data segment
mov ax, @data
mov ds, ax
; Display welcome message and menu
mov ah, 09h
lea dx, prompt
int 21h
; Read user input
call read_digit
; Check user's choice
cmp al, 1
je option1_selected
cmp al, 2
je option2_selected
cmp al, 3
je option3_selected
cmp al, 4
je exit_program
jmp invalid_choice
exit_program:
; Display newline and exit
call display_newline
; Terminate program
mov ah, 4Ch
int 21h
option1_selected:
; Call function to add numbers
call add_numbers
jmp start
option2_selected:
; Call function to check odd or even
call check_odd_even
jmp start
option3_selected:
; Call function to check positive or negative
call check_positive_negative
jmp start
invalid_choice:
; Display invalid choice message
mov ah, 09h
lea dx, invalid
int 21h
jmp start ; Restart the main menu
is_even db “The number is even.$”
is_odd db “The number is odd.$”
is_positive db “The number is positive.$”
is_negative db “The number is negative.$”
end start
Im trying to fix this code and im expecting someone can help me
Alchemykeys is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.