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!