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


mess1: .asciiz "Enter string: "
mess2: .asciiz "Reversed string: "
input: .space 128
newln: .asciiz "\n\n"
output: .space 2
.align 2

li $v0, 4 #print string
la $a0, mess1

li $v0, 8 #read string
la $a0, input #pointing to 128(bytes) of space reserved by input
la $a1, 127 #read upto 127 characters

li $v0, 4 #print string
la $a0, newln

li $v0, 4 #print string
la $a0, mess2

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

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) ??


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

lw $ra, 4($sp) # restore caller state

addi $sp, $sp, 8 # free up the stack space used

jr $ra

# Set Output buffer terminator (one character in length)
		# output[0] = character,  output[1] = 0   terminator

	la $a0, output		# output will be single character
	move $a1,$zero		# Set our source buffer index to 0.
	sb $a1, 1($a0)		# Set output buffer terminator for ASCIIZ output

	la $a0, input		# prepare for proc call ??
	jal rev					# Call rev()

# Need our end of program!!!!

#	____________
#	Function rev() - <Recursive Function>
#	$a1 Index, and $a0 Input Buffer
#	Recursively step through the characters in the buffer
#	NOTE: Recursion is wasteful for this call, could merely scan forward
#		then decrement back to 1st character in buffer!	(index < 0)
#     Using the pointer -1 to get the previous character to print!

	add $t1,$a1,$a0     #calculate pointer for this character  input+index

	lbu $t0, 0($t1)        # $t0 = *(input + index + 0)
	bne $t0, 0xa, notend	# Loop until LineFeed (10) 0Ah 	# what is 0xa ??

	jr $ra					# Return from function call Start un-nest recursion 


	addi $sp, $sp, -8		# Adjuste stack for a recursive call

	sw $ra, 4($sp)			# push return address on stack  save return address 
	sw $t0, 0($sp)			# save our char 
	addi $a1, $a1, 1		# increase index  *			; Recursion index
	jal rev 				# RECURSIVELY Call rev()

		# write character to output buffer!
	la $a0, output			# Get output buffer address

	lw $a1, 0($sp)			# pop the character
	sb $a1, 0($a0) 			# put the character into output buffer for printing

	li $v0, 4				# print current char

	lw $ra, 4($sp)			# restore Return address
	addi $sp, $sp, 8		# restore the stack (this nest)

	jr $ra					# return from function call
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.