Question: .data newline:.asciiz prompt: .asciiz Enter postfix expression: strpostfix: .space 50 result_decimal: .asciiz Result in decimal: result_hex: .asciiz Result in hexa :

.data

newline:.asciiz " "

prompt: .asciiz "Enter postfix expression: "

strpostfix: .space 50

result_decimal: .asciiz "Result in decimal: "

result_hex: .asciiz "Result in hexa : "

# define a stack of size 5

mystack: .word 0, 0, 0, 0, 0

.text

main:

li $s0, 0 # s0 = current stack pointer

la $s1, mystack # s1 = stack address

# Call function read_postfix

jal read_postfix

# Call function parse_postfix

jal parse_postfix # v0 = result of the expression

move $t0, $v0 # Save result in t0

# Print result in decimal

li $v0, 4

la $a0, result_decimal # print message for decimal

syscall

move $a0, $t0

li $v0, 1

syscall

# Print new line

li $v0, 4

la $a0, newline

syscall

# Print result in hexa

li $v0, 4

la $a0, result_hex

syscall

move $a0, $t0

li $v0, 34

syscall

# exit program

li $v0, 10

syscall

# Function convert character to number, example 'A' -> 10, 'B' -> 11, '0' -> 0, '1' -> 1, etc

# Assume that char is only in 0123456789ABCDEF

char_to_number:

blt $a0, 58, is_digit

addi $v0, $a0, -55 # A: 65 - 55 = 10, B: 66 - 55 = 11, etc

j get_out

is_digit:

addi $v0, $a0, -48 # 0: 48 - 48 = 0, 1: 49 - 48 = 1, etc

get_out:

# exit function

jr $ra

# Function read a postfix string from user

read_postfix:

# print prompt

li $v0, 4

li $a1, 50

la $a0, prompt

syscall

# read string in strpostfix

li $v0, 8

la $a0, strpostfix

syscall

# exit function

jr $ra

# Function parse a postfix string and calculate the expression

parse_postfix:

# save ra to register

addi $sp, $sp, -4

sw $ra, ($sp)

la $t0, strpostfix # t0 points to strpostfix[0]

read_loop:

lb $t1 ($t0) # t1 = strpostfix[i]

beq $t1, 10, end_read_loop # 10 is new line character, end loop

beq $t1, 32, next_char # if read char is a space then do nothing, scan next char

bne $t1, 42, not_mul # it char is a * or not

# it is a *

# Pop two operandsfrom stack

jal pop # Call pop

move $t2, $v0 # Store poped value in t2

jal pop # Call pop

move $t3, $v0 # Store poped value in t3

mul $a0, $t3, $t2 # a0 = t3 * t2

jal push # push a0 to stack

j next_char # go to scan next char

not_mul:

bne $t1, 43, not_plus

# it is a +

# Pop two operandsfrom stack

jal pop # Call pop

move $t2, $v0 # Store poped value in t2

jal pop # Call pop

move $t3, $v0 # Store poped value in t3

add $a0, $t3, $t2 # a0 = t3 + t2

jal push # push a0 to stack

j next_char

not_plus:

bne $t1, 45, not_minus

# it is a -

# Pop two operandsfrom stack

jal pop # Call pop

move $t2, $v0 # Store poped value in t2

jal pop # Call pop

move $t3, $v0 # Store poped value in t3

sub $a0, $t3, $t2 # a0 = t3 - t2

jal push # push a0 to stack

j next_char

not_minus:

bne $t1, 47, not_div

# it is a /

# Pop two operandsfrom stack

jal pop # Call pop

move $t2, $v0 # Store poped value in t2

jal pop # Call pop

move $t3, $v0 # Store poped value in t3

div $a0, $t3, $t2 # a0 = t3 / t2

jal push # push a0 to stack

j next_char

not_div:

# If character is not +, -, * or /, then it is operand

move $a0, $t1

# convert char to number

jal char_to_number

move $a0, $v0

jal push # push operand to stack

next_char:

addi $t0, $t0, 1 # point t0 to next char in the string

j read_loop # back to lopp

end_read_loop:

jal pop # Call pop to get the value of expression in the stack

# restore ra

lw $ra, 0($sp)

addi $sp, $sp, 4

# exit function

jr $ra

# Function: push a value to stack

push:

# save ra to register

addi $sp, $sp, -4

sw $ra, ($sp)

# increase stack pointer, push value in a0 to stack

addi $s0, $s0, 1

sw $a0, ($s1)

addi $s1, $s1, 4

# restore ra

lw $ra, 0($sp)

addi $sp, $sp, 4

# exit function

jr $ra

# Function: pop a value from stack

pop:

# save ra to register

addi $sp, $sp, -4

sw $ra, ($sp)

# decrease stack pointer, return value in v0

addi $s0, $s0, -1

addi $s1, $s1, -4

lw $v0, ($s1)

# restore ra

lw $ra, 0($sp)

addi $sp, $sp, 4

# exit function

jr $ra

The above code is what i have so far and that only works for single digit posfix expressions. Can anybody help me figure out what i need to change or add to my program for it to be able to work for multi-digit postfix expressions and to detect overflow and invalid answers.

MUST BE DONE IN ASSEMBLY

3 64 69 - + 1B1 * = -866 (dec), -362 (hex)

F 10E0 10 - + * 4A33 is invalid

F GH 5 - + 4A33 * is invalid

3 F E0 150 52 4EE * - + * overflows my stack of 5

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!