0

I have as an assignment to make an assembly program that reads the users input as postfix and when you input '=' it gives you the result, and to do that I'm required to use a stack. The problem is that the stack I use has character type inputs and I don't know how to turn them to integers to do the required operations.

That's what I have done till now:

.text
.globl main


main:
    #Preload the operations
    li $s1, '+'
    li $s2, '-'
    li $s3, '*'
    li $s4, '/'
    li $s5, '='
    
    li $v0,4
    la $a0,prompt
    syscall
    
    li $v0, 12
    syscall
    move $t0, $v0
    
    sub $sp, $sp, 4
    sw $t0, ($sp)
    
    bne $t0, '=', loop
    
    li $v0, 4
    la $a0, error
    syscall
    
    li $v0, 10
    syscall
loop:

    
    li $v0,4
    la $a0, prompt
    syscall
    
    li $v0,12
    syscall
    move $t1, $v0
    
    
    beq $t1, '=', result_process
    beq $t1, ' ', loop
    
    sub $sp, $sp, 4
    sw $t1, ($sp)
    
    
    blt $t1, '0', operations
    bgt $t1, '9', operations
    j loop 
    
    
    li $v0, 10
    syscall
    
operations:
    lw $t2, ($sp)
    add $sp, $sp, 4
    
    lw $t3, ($sp)
    add $sp, $sp, 4
    
    beq $t1, '+', add_process
    beq $t1, '-', sub_process
    beq $t1, '*', mul_process
    beq $t1, '/', div_process
    
    
    
add_process:
    add $t4, $t2, $t3
    
    sub $sp, $sp, 4
    sw $t4, ($sp)
    
    j loop
    
sub_process:
    sub $t4, $t2, $t3
    
    sub $sp, $sp, 4
    sw $t4, ($sp)
    
    j loop
    
mul_process:
    mul $t4, $t2, $t3
    
    sub $sp, $sp, 4
    sw $t4, ($sp)
    
    j loop
    
div_process:
    beq $t3, '0', error_div
    div $t4, $t2, $t3
    
    sub $sp, $sp, 4
    sw $t4, ($sp)
    
    j loop
    
    
result_process:
    lw $t5, ($sp)
    add $sp, $sp, 4
    
    li $v0,4
    la $a0, result
    syscall
    
    move $a0, $t5
    li $v0, 11
    syscall
    
    li $v0,10
    syscall
    
    
    
error_div:
    li $v0,4
    la $a0, div_error
    syscall
    
    li $v0, 10
    syscall
    
.data
prompt: .asciiz "\nPostfix (input): "
error: .asciiz "\nInvalid Postfix!"
div_error: .asciiz "\nDivide by zero!"
test: .asciiz  "\nWorks!"
result: .asciiz "\nPostfix evaluation: "

My only question is how do I turn a character input to integer in assembly, like is there a command ?(I know it stills need work btw) thanks in advance for any information!

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53
  • You have character data and want integers. You can use syscall #5 to read multiple characters and return you an integer, but if you use syscall #12 to get one character at a time, or syscall #8 to read a whole string, you'll have to assemble numbers yourself, using math, which is to say if you have two ascii characters that are digits (a number in string form) then the integer value is `(first-'0')*10 + second-'0'`. One term for this called `atoi` so you can search for that. It is not assembly specific, just simple math, so you may find one written in C or other. – Erik Eidt Dec 23 '22 at 15:40
  • If you have only one digit then the conversion is super simple, just subtract the value of the character '0' from the ascii byte value and you'll have an integer. If you have more digits then × 10 will be needed. – Erik Eidt Dec 23 '22 at 15:47

0 Answers0