Making a calculator program that can calculate integer addition / subtraction

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 2K views
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   Reactions: berkeman