I have following code which reverses any input provided using recursion. I am trying to understand what each line of code does. so I can have a better understanding.
so I have some questions, wondering if someone can help me understand whats happening in the code. I have commented each line where I am a question. any help will be greatly appreciated.
I don't understand anything under the label notend. please share any thoughts you might have .. thanks
.data
mess1: .asciiz "Enter string: "
mess2: .asciiz "Reversed string: "
input: .space 128
newln: .asciiz "\n\n"
output: .space 2
.text
.align 2
main:
li $v0, 4 #print string
la $a0, mess1
syscall
li $v0, 8 #read string
la $a0, input #pointing to 128(bytes) of space reserved by input
la $a1, 127 #read upto 127 characters
syscall
li $v0, 4 #print string
la $a0, newln
syscall
li $v0, 4 #print string
la $a0, mess2
syscall
la $a0, output #output will be single character
addi $a0,$a0,1 -- whats in a0 at this point and why are we adding??
add $a1,$zero,$zero -- whats happening here?? why we setting $a1 to zero?
sb $a1, 0($a0) #set null at the end of output -- dont undertand ??
la $a0, input #prepare for proc call ??
li $a1, 0 # $a1 contains index ??
jal rev
rev:
add $t1,$a1,$a0
lbu $t0, 0($t1) #load cur chacter -- what does 0($t1) mean?
bne $t0, 0xa, notend # what is 0xa ??
jr $ra # which line is this jumping to ?? line 33 sb $a1, 0($a0) ??
notend:
addi $sp, $sp, -8 # whats happening here? why substract 8 from stack pointer?
sw $ra, 4($sp) # save return address -- why?
sw $t0, 0($sp) # save cur char
addi $a1, $a1, 1 # increase index
jal rev
la $a0, output # stuff the cur char into the 'output'
lw $a1, 0($sp)
sb $a1, 0($a0)
li $v0, 4 # print current char
la $a0, output
syscall
lw $ra, 4($sp) # restore caller state
addi $sp, $sp, 8 # free up the stack space used
jr $ra