Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .globl main
- .data
- input: .asciiz "15 5 * 7 8 + 5 * -" # The RPN (Reverse Polish Notation) expression to evaluate
- .text
- macros:
- .macro print_int(%reg)
- li $v0 1
- move $a0 %reg
- syscall
- .end_macro
- .macro terminate()
- li $v0 10
- syscall
- .end_macro
- .macro operation (%operator)
- lw $a0 0($sp)
- addi $sp $sp 4
- lw $a1 0($sp)
- addi $sp $sp 4
- %operator $v0 $a0 $a1
- addi $sp $sp -4
- sw $v0 0($sp)
- addi $t1 $t1 1
- j loop
- .end_macro
- main:
- la $s0 input
- loop:
- add $s2 $s0 $t1
- lb $t0 0($s2)
- beq $t0 0 end
- beq $t0 43 plus
- beq $t0 45 minus
- beq $t0 42 star
- beq $t0 32 space
- j number
- plus:
- operation(add)
- minus:
- operation(sub)
- star:
- operation(mul)
- space:
- addi $t1 $t1 1
- j loop
- number:
- move $a0 $s0
- move $a1 $t1
- jal str_to_int
- addi $sp $sp -4
- sw $v0 0($sp)
- add $t1 $t1 $v1
- j loop
- end:
- lw $s5 0($sp)
- addi $sp $sp 4
- print_int($s5)
- terminate()
- # Function to convert a string into an integer
- # $a0 -> The ASCII string which contains the number to convert - $a1 -> The index of the first character of the number to convert
- # $v0 <- The converted number value expressed as a word - $v1 <- The number of digits which compose the converted number
- str_to_int:
- li $v0 0
- li $v1 0
- add $t7 $a0 $a1
- init:
- add $t8 $t7 $v1
- lb $t9 0($t8)
- bgt $t9 57 not_a_number
- blt $t9 48 not_a_number
- mul $v0 $v0 10
- subi $t9 $t9 48
- add $v0 $v0 $t9
- addi $v1 $v1 1
- j init
- not_a_number:
- jr $ra
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement