Making a calculator program that can calculate integer addition / subtraction

Click For Summary
The MIPS calculator program is encountering issues with correctly parsing and calculating integer addition and subtraction from a string input. The primary problem lies in the handling of the '-' character, which can represent both a unary operator for negative numbers and a binary operator for subtraction. This ambiguity leads to incorrect results, such as producing 23 instead of the expected -83 for the input "-100+20+3". Additionally, using common assembler directives like "word" as data labels is discouraged, as it can lead to confusion. Improving the logic for distinguishing between unary and binary operators is crucial for accurate calculations.
fiksx
Messages
77
Reaction score
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
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

Similar threads

  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
10K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 3 ·
Replies
3
Views
13K
  • · Replies 4 ·
Replies
4
Views
21K