Advertisement
Artychenal

Untitled

Apr 15th, 2025
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
6502 TASM/64TASS 9.63 KB | Source Code | 0 0
  1. .model small
  2. .stack 100h
  3. .data
  4.     title_msg       db "Piecewise Function Calculator", 13, 10, '$'
  5.     equation_msg1   db "Function Z is defined as:", 13, 10, '$'
  6.     equation_msg2   db "   x-1               if x < 10", 13, 10, '$'
  7.     equation_msg3   db "Z= (3x^2+4)/(x-2)    if x = 10", 13, 10, '$'
  8.     equation_msg4   db "   (7x^2-56)/(2x-5)  if x > 10", 13, 10, '$'
  9.     instruction_msg db 13, 10, "Press ESC to exit, any other key to continue...", 13, 10, '$'
  10.     prompt_msg      db 13, 10, 'Enter value of x: $'
  11.     error_msg       db 13, 10, 'Invalid input! Please enter a valid number.$'
  12.     overflow_msg    db 13, 10, 'Overflow occurred during calculation!$'
  13.     div_zero_msg    db 13, 10, 'Error: Division by zero!$'
  14.     result_msg      db 13, 10, 'Result: $'
  15.     remainder_msg   db ' remainder $'
  16.     newline         db 13, 10, '$'
  17.     cont_msg        db 13, 10, 'Press ESC to exit or any key to continue...$'
  18.    
  19.     x               dw ?
  20.     result          dw ?
  21.     remainder       dw ?
  22.    
  23.     buffer          db 6, ?, 6 dup(?)  ; Buffer for input: max length, actual length, characters
  24.     is_negative     db 0               ; Flag for negative input
  25.    
  26. .code
  27. main proc
  28.     mov ax, @data
  29.     mov ds, ax
  30.    
  31.     ; Display title and equation system
  32.     call display_equation_system
  33.    
  34. main_loop:
  35.     ; Display prompt
  36.     mov ah, 09h
  37.     lea dx, prompt_msg
  38.     int 21h
  39.    
  40.     ; Get user input
  41.     call read_number
  42.    
  43.     ; Check for input errors
  44.     cmp ax, 0
  45.     je error_handler
  46.    
  47.     ; Store x value
  48.     mov x, bx
  49.    
  50.     ; Calculate Z based on the value of x
  51.     call calculate_z
  52.    
  53.     ; Display result
  54.     call display_result
  55.    
  56.     ; Check if user wants to continue
  57.     mov ah, 09h
  58.     lea dx, cont_msg
  59.     int 21h
  60.    
  61.     ; Wait for key press
  62.     mov ah, 01h
  63.     int 21h
  64.    
  65.     ; If ESC pressed, exit
  66.     cmp al, 27  ; ESC key scan code
  67.     je exit_program
  68.    
  69.     ; Clear screen for next calculation
  70.     call clear_screen
  71.    
  72.     ; Display equation system again
  73.     call display_equation_system
  74.    
  75.     ; Continue loop
  76.     jmp main_loop
  77.    
  78. error_handler:
  79.     mov ah, 09h
  80.     lea dx, error_msg
  81.     int 21h
  82.    
  83.     ; Check if user wants to continue
  84.     mov ah, 09h
  85.     lea dx, cont_msg
  86.     int 21h
  87.    
  88.     ; Wait for key press
  89.     mov ah, 01h
  90.     int 21h
  91.    
  92.     ; If ESC pressed, exit
  93.     cmp al, 27  ; ESC key scan code
  94.     je exit_program
  95.    
  96.     ; Clear screen for next calculation
  97.     call clear_screen
  98.    
  99.     ; Display equation system again
  100.     call display_equation_system
  101.    
  102.     ; Continue loop
  103.     jmp main_loop
  104.    
  105. exit_program:
  106.     ; Exit program
  107.     mov ax, 4C00h
  108.     int 21h
  109. main endp
  110.  
  111. ; Procedure to display equation system and instructions
  112. display_equation_system proc
  113.     mov ah, 09h
  114.     lea dx, title_msg
  115.     int 21h
  116.    
  117.     mov ah, 09h
  118.     lea dx, equation_msg1
  119.     int 21h
  120.    
  121.     mov ah, 09h
  122.     lea dx, equation_msg2
  123.     int 21h
  124.    
  125.     mov ah, 09h
  126.     lea dx, equation_msg3
  127.     int 21h
  128.    
  129.     mov ah, 09h
  130.     lea dx, equation_msg4
  131.     int 21h
  132.    
  133.     mov ah, 09h
  134.     lea dx, instruction_msg
  135.     int 21h
  136.    
  137.     ret
  138. display_equation_system endp
  139.  
  140. ; Procedure to clear screen
  141. clear_screen proc
  142.     mov ax, 0003h  ; AH=0, AL=3 (text mode 80x25)
  143.     int 10h
  144.     ret
  145. clear_screen endp
  146.  
  147. ; Procedure to read a number from user input
  148. read_number proc
  149.     ; Read string
  150.     mov ah, 0Ah
  151.     lea dx, buffer
  152.     int 21h
  153.    
  154.     ; Check if input is empty
  155.     mov al, buffer[1]
  156.     cmp al, 0
  157.     je input_error
  158.    
  159.     ; Initialize registers
  160.     xor bx, bx      ; BX will hold the result
  161.     mov cl, buffer[1]  ; CL = length of input
  162.     mov si, offset buffer + 2  ; SI points to first character
  163.    
  164.     ; Check for negative sign
  165.     mov byte ptr [is_negative], 0
  166.     cmp byte ptr [si], '-'
  167.     jne check_digits
  168.    
  169.     ; Set negative flag and move to next character
  170.     mov byte ptr [is_negative], 1
  171.     inc si
  172.     dec cl
  173.     cmp cl, 0       ; Check if only minus sign was entered
  174.     je input_error
  175.    
  176. check_digits:
  177.     ; Process each digit
  178.     xor ax, ax
  179.     mov al, [si]
  180.    
  181.     ; Check if character is a digit (0-9)
  182.     cmp al, '0'
  183.     jb input_error
  184.     cmp al, '9'
  185.     ja input_error
  186.    
  187.     ; Convert ASCII to numeric value
  188.     sub al, '0'
  189.    
  190.     ; Multiply current result by 10 and add new digit
  191.     mov dx, 10
  192.     mov ax, bx
  193.     mul dx
  194.     jc check_overflow  ; Check for overflow
  195.    
  196.     mov bx, ax
  197.     mov al, [si]
  198.     sub al, '0'
  199.    
  200.     ; Check for potential overflow before adding
  201.     add bx, ax
  202.     jc check_overflow  ; Check for overflow
  203.    
  204.     ; Check for maximum/minimum 16-bit integer values
  205.     cmp byte ptr [is_negative], 1
  206.     je check_negative_limit
  207.     cmp bx, 32767   ; Check max positive value for signed 16-bit
  208.     ja check_overflow
  209.     jmp continue_reading
  210.    
  211. check_negative_limit:
  212.     cmp bx, 32768   ; Check max negative value for signed 16-bit
  213.     ja check_overflow
  214.    
  215. continue_reading:
  216.     ; Move to next character
  217.     inc si
  218.     dec cl
  219.     jnz check_digits
  220.    
  221.     ; Apply negative sign if needed
  222.     cmp byte ptr [is_negative], 1
  223.     jne read_done
  224.     neg bx
  225.    
  226. read_done:
  227.     mov ax, 1       ; Success
  228.     ret
  229.    
  230. check_overflow:
  231.     ; Handle overflow - display error and wait for keypress
  232.     mov ah, 09h
  233.     lea dx, overflow_msg
  234.     int 21h
  235.    
  236. input_error:
  237.     xor ax, ax      ; Error
  238.     ret
  239. read_number endp
  240.  
  241. ; Procedure to calculate Z based on value of x
  242. calculate_z proc
  243.     mov ax, x
  244.     cmp ax, 10      ; Compare x with 10
  245.     jl less_than_10
  246.     je equal_to_10
  247.     jg greater_than_10
  248.    
  249. less_than_10:
  250.     ; Z = x - 1
  251.     mov ax, x
  252.     dec ax
  253.     mov result, ax
  254.     mov remainder, 0
  255.     ret
  256.    
  257. equal_to_10:
  258.     ; Z = (3x² + 4) / (x - 2)
  259.     ; Check for division by zero
  260.     mov ax, x
  261.     sub ax, 2
  262.     cmp ax, 0
  263.     je division_by_zero
  264.    
  265.     ; Calculate 3x²
  266.     mov ax, x
  267.     imul ax          ; AX = x²
  268.     jo overflow_error
  269.     mov bx, 3
  270.     imul bx          ; AX = 3x²
  271.     jo overflow_error
  272.    
  273.     ; Add 4
  274.     add ax, 4
  275.     jo overflow_error
  276.    
  277.     ; Divide by (x - 2)
  278.     mov bx, x
  279.     sub bx, 2
  280.    
  281.     ; Perform division
  282.     cwd              ; Convert word to doubleword (sign extend AX into DX:AX)
  283.     idiv bx          ; AX = quotient, DX = remainder
  284.    
  285.     mov result, ax
  286.     mov remainder, dx
  287.     ret
  288.    
  289. greater_than_10:
  290.     ; Z = (7x² - 56) / (2x - 5)
  291.     ; Check for division by zero
  292.     mov ax, x
  293.     shl ax, 1        ; AX = 2x
  294.     sub ax, 5        ; AX = 2x - 5
  295.     cmp ax, 0
  296.     je division_by_zero
  297.     mov bx, ax       ; Store denominator in BX
  298.    
  299.     ; Calculate 7x²
  300.     mov ax, x
  301.     imul ax          ; AX = x²
  302.     jo overflow_error
  303.     mov cx, 7
  304.     imul cx          ; AX = 7x²
  305.     jo overflow_error
  306.    
  307.     ; Subtract 56
  308.     sub ax, 56
  309.     jo overflow_error
  310.    
  311.     ; Divide by (2x - 5)
  312.     cwd              ; Convert word to doubleword (sign extend AX into DX:AX)
  313.     idiv bx          ; AX = quotient, DX = remainder
  314.    
  315.     mov result, ax
  316.     mov remainder, dx
  317.     ret
  318.    
  319. division_by_zero:
  320.     mov ah, 09h
  321.     lea dx, div_zero_msg
  322.     int 21h
  323.    
  324.     ; Wait for key press to continue
  325.     mov ah, 09h
  326.     lea dx, cont_msg
  327.     int 21h
  328.    
  329.     mov ah, 01h
  330.     int 21h
  331.    
  332.     ; If ESC pressed, exit
  333.     cmp al, 27  ; ESC key scan code
  334.     je exit_div_zero
  335.    
  336.     ; Clear screen and display equation system
  337.     call clear_screen
  338.     call display_equation_system
  339.    
  340.     ; Return to main loop
  341.     jmp main_loop
  342.    
  343. exit_div_zero:
  344.     mov ax, 4C01h    ; Exit with error code
  345.     int 21h
  346.    
  347. overflow_error:
  348.     mov ah, 09h
  349.     lea dx, overflow_msg
  350.     int 21h
  351.    
  352.     ; Wait for key press to continue
  353.     mov ah, 09h
  354.     lea dx, cont_msg
  355.     int 21h
  356.    
  357.     mov ah, 01h
  358.     int 21h
  359.    
  360.     ; If ESC pressed, exit
  361.     cmp al, 27  ; ESC key scan code
  362.     je exit_overflow
  363.    
  364.     ; Clear screen and display equation system
  365.     call clear_screen
  366.     call display_equation_system
  367.    
  368.     ; Return to main loop
  369.     jmp main_loop
  370.    
  371. exit_overflow:
  372.     mov ax, 4C02h    ; Exit with error code
  373.     int 21h
  374. calculate_z endp
  375.  
  376. ; Procedure to display calculation result
  377. display_result proc
  378.     mov ah, 09h
  379.     lea dx, result_msg
  380.     int 21h
  381.    
  382.     ; Display quotient (result)
  383.     mov ax, result
  384.     call display_number
  385.    
  386.     ; Check if there's a remainder to display
  387.     mov ax, remainder
  388.     cmp ax, 0
  389.     je no_remainder
  390.    
  391.     ; Display remainder part
  392.     mov ah, 09h
  393.     lea dx, remainder_msg
  394.     int 21h
  395.    
  396.     mov ax, remainder
  397.     call display_number
  398.    
  399. no_remainder:
  400.     mov ah, 09h
  401.     lea dx, newline
  402.     int 21h
  403.     ret
  404. display_result endp
  405.  
  406. ; Procedure to display a signed number
  407. display_number proc
  408.     ; Check if number is negative
  409.     test ax, ax
  410.     jns positive_number
  411.    
  412.     ; Display minus sign
  413.     push ax
  414.     mov dl, '-'
  415.     mov ah, 02h
  416.     int 21h
  417.     pop ax
  418.    
  419.     ; Convert to positive
  420.     neg ax
  421.    
  422. positive_number:
  423.     ; Convert number to string
  424.     mov bx, 10      ; Divisor
  425.     mov cx, 0       ; Digit counter
  426.    
  427. digit_loop:
  428.     xor dx, dx
  429.     div bx          ; AX / 10: quotient in AX, remainder in DX
  430.     push dx         ; Push digit on stack
  431.     inc cx          ; Increment digit counter
  432.     test ax, ax
  433.     jnz digit_loop  ; Continue if quotient is not zero
  434.    
  435. display_loop:
  436.     pop dx          ; Get digit from stack
  437.     add dl, '0'     ; Convert to ASCII
  438.     mov ah, 02h
  439.     int 21h         ; Display character
  440.     loop display_loop
  441.    
  442.     ret
  443. display_number endp
  444.  
  445. end main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement