Making a calculator program that can calculate integer addition / subtraction

In summary: It's better to name your data labels something more descriptive, such as input_str, output_str, or result.
  • #1
fiksx
77
1
Homework Statement
trying to complete mips program code about a calculator program that can calculate integer addition / subtraction written using the MIPS assembler.
Relevant Equations
addition / subtraction written using the MIPS assembler.
im trying to complete mips program code about a calculator program that can calculate integer addition / subtraction written using the MIPS assembler.
im having hard times to debug this.

The input is given to the array of Formula char (base address $ s0) in the form of a formula.
The null character (\ 0, ASCII code 0) is placed at the end. The calculation result is given to the register $ s1 and the overflow is ignored.
For example, if the input is Formula = “100 + 20 + 3”, $ s1 = 123, and if Formula = “− 100 + 20-3”, $ s1 = −83 is output.
The '+' and-in the program are the characters + 'and-, in ASCII respectively, and the ASCII characters are 0..9 are 48 (10), 49 (10) and 57 (10).

Code:
    .text
    .globl main
  
            main:
            la $s0,word ##copy base address of array to t1
           add $s1, $zero, $zero
            add $s2, $zero, $zero
            Addi $s3,$zero,1
            Addi $t1, $zero,'+'
            Addi $t2,$zero,'-'
            addi $t3,$zero,10
  
            Loop: lb $t0,0($s0)
            Addiu $s0,$s0,1
            Beq $t0,$t1,Plus
            Beq $t0,$t2,Minus
            Beq $t0,$zero,Cal
            Addi $t0,$t0,-48
            Mul $s2, $s2, $t3
            Add $s2,$s2,$t0
            J Loop
  
            End:
          
  
  
    li $v0,1
    move $a0,$s1
    syscall
            li $v0, 10
    syscall
  
              Plus: addi $s4, $zero, 1
              j Cal
              Minus: addi $s4, $zero, 0
              Cal: beq $s3,$zero,Subn
              Addn:
            
              add $s1,$s1,$s2
              beq $s4,1,Join // this part I am not sure, maybe causing trouble and i don't know how can i save the after the +\- symbol
            
              Subn: sub $s2,$s1,$s2
              Join : add $s2,$zero,$zero
              Beq $t0,$zero,End
               Add $s3,$s4,$zero
               J Loop
  
  
    Exit:
    li $v0, 10
    syscall
  
    .data
    word: .asciiz "-100+20+3"
    result: .asciiz "$s1->"
    prompt: .asciiz "$s0->"

However when debugging this code produce 23, but the correct result is -83
where is the wrong part?
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
fiksx said:
Problem Statement: trying to complete mips program code about a calculator program that can calculate integer addition / subtraction written using the MIPS assembler.
Relevant Equations: addition / subtraction written using the MIPS assembler.

im trying to complete mips program code about a calculator program that can calculate integer addition / subtraction written using the MIPS assembler.
im having hard times to debug this.

The input is given to the array of Formula char (base address $ s0) in the form of a formula.
The null character (\ 0, ASCII code 0) is placed at the end. The calculation result is given to the register $ s1 and the overflow is ignored.
For example, if the input is Formula = “100 + 20 + 3”, $ s1 = 123, and if Formula = “− 100 + 20-3”, $ s1 = −83 is output.
The '+' and-in the program are the characters + 'and-, in ASCII respectively, and the ASCII characters are 0..9 are 48 (10), 49 (10) and 57 (10).

Code:
    .text
    .globl main
  
            main:
            la $s0,word ##copy base address of array to t1
           add $s1, $zero, $zero
            add $s2, $zero, $zero
            Addi $s3,$zero,1
            Addi $t1, $zero,'+'
            Addi $t2,$zero,'-'
            addi $t3,$zero,10
  
            Loop: lb $t0,0($s0)
            Addiu $s0,$s0,1
            Beq $t0,$t1,Plus
            Beq $t0,$t2,Minus
            Beq $t0,$zero,Cal
            Addi $t0,$t0,-48
            Mul $s2, $s2, $t3
            Add $s2,$s2,$t0
            J Loop
  
            End:
          
  
  
    li $v0,1
    move $a0,$s1
    syscall
            li $v0, 10
    syscall
  
              Plus: addi $s4, $zero, 1
              j Cal
              Minus: addi $s4, $zero, 0
              Cal: beq $s3,$zero,Subn
              Addn:
            
              add $s1,$s1,$s2
              beq $s4,1,Join // this part I am not sure, maybe causing trouble and i don't know how can i save the after the +\- symbol
            
              Subn: sub $s2,$s1,$s2
              Join : add $s2,$zero,$zero
              Beq $t0,$zero,End
               Add $s3,$s4,$zero
               J Loop
  
  
    Exit:
    li $v0, 10
    syscall
  
    .data
    word: .asciiz "-100+20+3"
    result: .asciiz "$s1->"
    prompt: .asciiz "$s0->"

However when debugging this code produce 23, but the correct result is -83
where is the wrong part?
Your logic in parsing the input string is overly simplistic. The problem is that '-' is used for two different purposes: as a unary operator for negative number, and as a binary operator between two numbers. When your program reads (loads) a byte from the input string, you need logic to decide whether the '-' character indicates a negative number or it indicates that two numbers are subtracted.

Your program also produces incorrect results if the input string happens to be "+100 + 20 + 3" -- resulting in 23.

BTW, you have word as one of your data labels. This isn't an error, but it's not a good idea to use assembler directives such as word, float, asciiz, and the like as data labels (i.e., variables).
 
  • Like
Likes berkeman

FAQ: Making a calculator program that can calculate integer addition / subtraction

1. How do I create a calculator program that can perform integer addition and subtraction?

To create a calculator program that can perform integer addition and subtraction, you will need to use a programming language such as Java, C++, or Python. You will also need to understand the basic concepts of programming, such as variables, functions, and control structures. There are many tutorials and resources available online that can guide you through the process of creating a calculator program.

2. What is the difference between integer addition and subtraction?

Integer addition is the process of combining two or more numbers to find their sum, while integer subtraction is the process of finding the difference between two numbers. In addition, the result of integer addition is always a larger number, while the result of integer subtraction can be either a larger or smaller number depending on the numbers being subtracted.

3. How do I handle errors in my calculator program?

To handle errors in your calculator program, you can use conditional statements to check for any potential errors, such as dividing by zero or entering invalid input. You can also use try-catch blocks to catch any exceptions that may occur during the execution of your program. It is important to thoroughly test your program and handle any potential errors to ensure its accuracy and reliability.

4. Can a calculator program handle decimal numbers?

Yes, a calculator program can handle decimal numbers by using floating-point data types such as double or float. These data types allow for the representation of numbers with a decimal point, allowing for more precise calculations. However, it is important to note that due to the limitations of computer hardware, there may be small discrepancies in the results of calculations involving decimal numbers.

5. How can I improve the user interface of my calculator program?

To improve the user interface of your calculator program, you can use graphical user interface (GUI) libraries or frameworks such as JavaFX, Qt, or Tkinter. These tools allow you to create a more visually appealing and user-friendly interface for your calculator program. You can also add features such as buttons, input fields, and error messages to make your program more intuitive and user-friendly.

Similar threads

Replies
4
Views
4K
Replies
7
Views
2K
Replies
2
Views
4K
Replies
4
Views
6K
Replies
1
Views
2K
Replies
1
Views
7K
Replies
1
Views
4K
Replies
4
Views
9K
Back
Top