How can I fix my code in assembly language to enable me to input more than one digit and also to display the results accordingly?

  Kiến thức lập trình
.model small
.stack 1000h
.386

.data
    prompt db "Welcome to the Main Menu", 13, 10, "1. Adding 2 numbers", 13, 10, "2. Odd and Even", 13, 10, "3. Positive and Negative", 13, 10, "4. Exit", 13, 10,"Enter your choice: $"
    invalid db "Invalid choice. Please try again.$"
    newline db 13, 10, "$"
    msg1 DB 10,13,'Enter the first digit: $'
    msg2 DB 10,13,'Enter the second digit: $'
    msg3 DB 10,13,'The sum is: $'
    msg4 DB 10,13,'Enter first number: $'
    msg5 DB 10,13,'Enter second number: $'
    msg6 DB 10,13,'The results are: $'
    msg7 DB 10,13, "Enter a number (e.g., +5 or -3): $"
    positive_msg db 13, 10, "The number is positive.$"
    negative_msg db 13, 10, "The number is negative.$"
    zero_msg db 13, 10, "The number is zero.$"
    buffer db 255, "$" ; Buffer for user input
    even_msg db ' is even.$'
    odd_msg db ' is odd.$'
    x DB ?
    y DB ?
    sum DB ?

.code
start:
    ; Initialize data segment
    mov ax, @data
    mov ds, ax

display_menu:
    ; Display welcome message and menu
    mov ah, 09h
    lea dx, newline
    int 21h
    mov ah, 09h
    lea dx, prompt
    int 21h

read_choice:
    ; Read user input
    mov ah, 01h
    int 21h
    sub al, '0'  ; Convert ASCII digit to numeric value

    ; 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

option1_selected:
    ; Display message to enter first digit
    mov ah, 09h
    lea dx, msg1
    int 21h
    
    ; Read first digit
    mov ah, 01h
    int 21h
    sub al, '0'
    mov x, al
    
    ; Display message to enter second digit
    mov ah, 09h
    lea dx, msg2
    int 21h
    
    ; Read second digit
    mov ah, 01h
    int 21h
    sub al, '0'
    mov y, al
    
    ; Calculate sum
    mov al, x
    add al, y
    mov sum, al
    
    ; Display sum
    mov ah, 09h
    lea dx, msg3
    int 21h
    
    ; Display sum value
    mov dl, sum
    add dl, '0'
    mov ah, 02h
    int 21h
    jmp display_menu  ; Return to main menu

option2_selected:
    ; Display prompts for the numbers
    mov ah, 9
    lea dx, msg4
    int 21h

    ; Read the first number
    mov ah, 1
    int 21h
    mov bl, al ; Store the first number

    ; Display prompts for the numbers
    mov ah, 9
    lea dx, msg5
    int 21h

    ; Read the second number
    mov ah, 1
    int 21h
    mov cl, al ; Store the second number

    ; Display result message
    mov ah, 9
    lea dx, msg6
    int 21h

    ; Display the first number
    mov ah, 2
    mov dl, bl
    int 21h

    ; Display whether the first number is even or odd
    mov ah, 9
    mov al, bl
    test al, 1
    jz first_even
    lea dx, odd_msg
    int 21h
    jmp second_check

first_even:
    lea dx, even_msg
    int 21h

    ; Display the second number
second_check:
    mov ah, 2
    mov dl, cl
    int 21h

    ; Display whether the second number is even or odd
    mov ah, 9
    mov al, cl
    test al, 1
    jz second_even
    lea dx, odd_msg
    int 21h

second_even:
    lea dx, even_msg
    int 21h
    jmp display_menu  ; Return to main menu

option3_selected:
; Display input message
    mov ah, 09h
    lea dx, msg7
    int 21h

    ; Read user input as a string
    mov ah, 0Ah
    lea dx, buffer
    int 21h

    ; Check if the input is positive, negative, or zero
    mov si, offset buffer + 2  ; Point SI to the beginning of the input string
    mov al, [si]               ; Load the first character of the input
    cmp al, '+'                ; Check if it's a positive sign
    je positive_number
    cmp al, '-'                ; Check if it's a negative sign
    je negative_number
    cmp al, '0'                ; Check if it's '0'
    je zero_number
    jmp invalid_number

positive_number:
    ; Display positive message
    mov ah, 09h
    lea dx, positive_msg
    int 21h
    jmp display_menu

negative_number:
    ; Display negative message
    mov ah, 09h
    lea dx, negative_msg
    int 21h
    jmp display_menu

zero_number:
    ; Display zero message
    mov ah, 09h
    lea dx, zero_msg
    int 21h
    jmp display_menu  ; Return to main menu

invalid_number:
    ; Display invalid number message
    mov ah, 09h
    lea dx, newline
    int 21h
    mov ah, 09h
    lea dx, invalid
    int 21h
    jmp display_menu  ; Return to main menu

exit_program:
    ; Display newline and exit
    mov ah, 09h
    lea dx, newline
    int 21h

    ; Terminate program
    mov ah, 4Ch
    int 21h

invalid_choice:
    ; Display invalid choice message
    mov ah, 09h
    lea dx, newline
    int 21h 
    mov ah, 09h
    lea dx, invalid
    int 21h
    jmp display_menu  ; Restart the main menu

end start

I have a code here that runs fine. The only issue I’m facing is how to input more than one digit and also display the results properly. For example, if I input 9 + 10, the sum should be 19. Similarly, if I input 12 and 15, the output should be “12 is even. 15 is odd” (vice versa). Additionally, in option 3, how can I fix my code so that I can display two different numbers that is in positive and negative and also it will results if the number is positive or negative. Moreover,if I input to all options a symbols or letters, it should display “invalid”.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website Kho Theme wordpress Kho Theme WP Theme WP

LEAVE A COMMENT