Question
DO NOT ANSWER IF NOT EXPERT ON ASSEMBLY LABGUAGE PROGRAMMING X86 DOSBox-X This is assembly language code, it has no warning or errors but doesn't
DO NOT ANSWER IF NOT EXPERT ON ASSEMBLY LABGUAGE PROGRAMMING X86 DOSBox-X
This is assembly language code, it has no warning or errors but doesn't run as expected. This program asks the user for 2 numbers that are only from 0 to 9. Then should print all the results from the operations which are add, subtract, divide and multiply these 2 numbers. Please fix. Here is the code:
.model small .stack 100h
.data prompt1 db 10,13,'Enter first digit: $' prompt2 db 10,13,'Enter second digit: $' result dw ? error_message db 'Invalid input, enter a digit between 0 and 9: $' divide_by_zero_message db 'Cannot divide by 0, enter a non-zero second digit: $'
.code main proc mov ax, @data mov ds, ax
call GET_FIRST_DIGIT call GET_SECOND_DIGIT
; Check if the second digit is 0 cmp dx, 0 je DIVIDE_BY_ZERO
; Automatically perform all four arithmetic operations mov bx, ax mov cx, dx
; Addition add ax, cx mov result, ax call SHOW_RESULT
; Subtraction mov ax, bx sub ax, cx mov result, ax call SHOW_RESULT
; Multiplication mov ax, bx mul cx mov result, ax call SHOW_RESULT
; Division mov ax, bx div cx mov result, ax call SHOW_RESULT
jmp END_PROGRAM
DIVIDE_BY_ZERO: ; Display an error message lea dx, divide_by_zero_message call DISPLAY_PROMPT
END_PROGRAM: ret main endp
; get the first digit GET_FIRST_DIGIT proc ; display the prompt lea dx, prompt1 call DISPLAY_PROMPT
; get the digit call GET_DIGIT ret GET_FIRST_DIGIT endp
; get the second digit GET_SECOND_DIGIT proc ; display the prompt lea dx, prompt2 call DISPLAY_PROMPT
; get the digit call GET_DIGIT ret GET_SECOND_DIGIT endp
; display the result SHOW_RESULT proc ; display the result mov dx, result mov ax, result call DISPLAY_NUMBER ret
SHOW_RESULT endp
; display a prompt DISPLAY_PROMPT proc ; display the prompt mov ah, 9 int 21h ret DISPLAY_PROMPT endp
; get a digit GET_DIGIT proc ; get the digit call GET_CHAR sub al, 48 cmp al, 0 jge VALID_DIGIT cmp al, 9 jl VALID_DIGIT ; display an error message ERROR: lea dx, error_message call DISPLAY_PROMPT jmp GET_DIGIT
VALID_DIGIT: mov ah, 0 ret GET_DIGIT endp
GET_CHAR proc ; get the character mov ah, 1 int 21h cmp al, 13 je EXIT_GET_CHAR cmp al, 10 je EXIT_GET_CHAR jmp GET_CHAR
EXIT_GET_CHAR: ret GET_CHAR endp
DISPLAY_DIGIT PROC mov ah, 2 int 21h ret DISPLAY_DIGIT ENDP
; display a number DISPLAY_NUMBER proc ; get the number of digits in the result mov cx, result cmp cx, 0 je DISPLAY_ZERO
mov bx, 10 xor dx, dx mov bp, 0 div bx inc bp cmp dx, 0 jne @F
@B: ; get the current digit mov dx, cx mov ax, 10 mul bp div ax add dl, 48 ; display the digit call DISPLAY_DIGIT ; prepare for next digit dec bp mov cx, ax cmp ax, 0 jne @B
jmp @E
@F: inc bp jmp @B
@E: ret
DISPLAY_ZERO: mov dl, 48 call DISPLAY_DIGIT ret
DISPLAY_NUMBER endp
END main
This is the output of the program:
C:\>cd final
C:\FINAL>final.exe
Enter first digit: 2
Enter second digit: 3 00
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started