Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # prune_targets( $a0 = uint_32 *targets_ptr, $a1 = size_t targets_count, $a2 = uint_32 *guess_ptr, $a3 = char* guess_score ) => int new_targets_count
- # removes elements in targets_ptr that don't have the same score as guess_score, returning the new length of targets_ptr array
- prune_targets:
- # push return address and save register state onto stack
- addi $sp, $sp, -32 # allocate 32 bytes onto stack
- sw $ra, 0($sp)
- sw $s0, 4($sp)
- sw $s1, 8($sp)
- sw $s2, 12($sp)
- sw $s3, 16($sp)
- sw $s4, 20($sp)
- sw $s5, 24($sp)
- sw $s6, 28($sp)
- # declare and move variables into save registers
- move $s0, $a0 # $s0 = targets_ptr
- move $s1, $a1 # $s1 = targets_remaining = targets_count
- move $s2, $a2 # $s2 = guess_ptr
- lb $s3, 0($a3) # $s3 = guess_inplace = guess_score[0]
- lb $s4, 1($a3) # $s4 = guess_misplaced = guess_score[1]
- subi $s3, $s3, '0' # cast guess_inplace from char to int
- subi $s4, $s4, '0' # cast guess_misplaced from char to int
- move $s5, $s0 # $s5 = new_targets_ptr = targets_ptr
- li $s6, 0 # new_targets_count = 0
- prune_targets_step:
- # branch to end if targets_remaining == 0
- beq $s1, 0, prune_targets_end
- # get score from target at current address by calling get_score(targets_ptr, guess_ptr)
- move $a0, $s0
- move $a1, $s2
- jal get_score
- # branch to prune_targets_step_continue if inplace and misplace returned by get_score are not equal to guess_inplace and guess_misplaced
- bne $s3, $v0, prune_targets_step_continue
- bne $s4, $v1, prune_targets_step_continue
- # copy target at targets_ptr to new_targets_ptr
- lw $t0, 0($s0) # temp = *targets_ptr
- sw $t0, 0($s5) # *new_targets_ptr = temp
- # advance new_target_ptr and new_targets_count
- addi $s5, $s5, 4 # new_targets_ptr += 4 (increment by 4 bytes to get to next available target address)
- addi $s6, $s6, 1 # new_targets_count++
- prune_targets_step_continue:
- # advance targets_ptr and targets_remaining
- addi $s0, $s0, 4 # targets_ptr += 4 (increment by 4 bytes to get to next target)
- addi $s1, $s1, -1 # targets_remaining--
- # continue
- j prune_targets_step
- prune_targets_end:
- # return new_targets_count
- move $v0, $s6
- # pop return address and save register state from stack
- lw $ra, 0($sp)
- lw $s0, 4($sp)
- lw $s1, 8($sp)
- lw $s2, 12($sp)
- lw $s3, 16($sp)
- lw $s4, 20($sp)
- lw $s5, 24($sp)
- lw $s6, 28($sp)
- addi $sp, $sp, 32 # deallocate 32 bytes from stack
- # return
- jr $ra
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement