In: Computer Science
Write a procedure that accepts a byte string(base address in
$a0, length in $ a1)
and returns its XOR checksum, defined as the exclusive-OR of all
its bytes, in $v0.
(by MiniMIPS)
Greetings!!
Code:
#DATA SEGMENT
.data
string: .ascii "Hello Welcome!!"
#CODE SEGMENT
.text
#MAIN STARTS HERE
main:
la $a0,string #load address of the string in a0 as 1st parameter
li $a1,14 #load length-1 to a1 as 2nd parameter
#FUNCTION CALL
jal checksum #function call
#RETURN FROM MAIN
#STANDARD TERMINATION
li $v0,10 #parameter for termination
syscall #system call
#END OF MAIN
#FUNCTION DEFINITION
checksum:
lb $t0,0($a0) #read first byte
loop:
beq $a1,$0,end #check the length is 0 or not. if 0 go to end
addi $a0,$a0,1 #increment array index for accessing next element
lb $t1,0($a0) #read next byte
xor $t0,$t0,$t1 #calculating checksum
addi $a1,$a1,-1 #decrement the length
j loop #repeat
end:
add $v0,$0,$t0 #loading the checksum in v0 for return
jr $ra #return to main
#END OF FUNCTION
Output screenshot:
Hope this helps
Thank You