Advertisement
Zhang_Beihei

[MIPS] prune_targets

Nov 26th, 2021
2,878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # 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
  2. # removes elements in targets_ptr that don't have the same score as guess_score, returning the new length of targets_ptr array
  3. prune_targets:
  4.  
  5.     # push return address and save register state onto stack
  6.  
  7.     addi $sp, $sp, -32 # allocate 32 bytes onto stack
  8.     sw $ra, 0($sp)
  9.     sw $s0, 4($sp)
  10.     sw $s1, 8($sp)
  11.     sw $s2, 12($sp)
  12.     sw $s3, 16($sp)
  13.     sw $s4, 20($sp)
  14.     sw $s5, 24($sp)
  15.     sw $s6, 28($sp)
  16.  
  17.     # declare and move variables into save registers
  18.  
  19.     move $s0, $a0 # $s0 = targets_ptr
  20.     move $s1, $a1 # $s1 = targets_remaining = targets_count
  21.     move $s2, $a2 # $s2 = guess_ptr
  22.     lb $s3, 0($a3) # $s3 = guess_inplace = guess_score[0]
  23.     lb $s4, 1($a3) # $s4 = guess_misplaced = guess_score[1]
  24.     subi $s3, $s3, '0' # cast guess_inplace from char to int
  25.     subi $s4, $s4, '0' # cast guess_misplaced from char to int
  26.  
  27.     move $s5, $s0 # $s5 = new_targets_ptr = targets_ptr
  28.     li $s6, 0 # new_targets_count = 0
  29.  
  30.     prune_targets_step:
  31.        # branch to end if targets_remaining == 0
  32.         beq $s1, 0, prune_targets_end
  33.  
  34.         # get score from target at current address by calling get_score(targets_ptr, guess_ptr)
  35.         move $a0, $s0
  36.         move $a1, $s2
  37.         jal get_score
  38.  
  39.         # branch to prune_targets_step_continue if inplace and misplace returned by get_score are not equal to guess_inplace and guess_misplaced
  40.         bne $s3, $v0, prune_targets_step_continue
  41.         bne $s4, $v1, prune_targets_step_continue
  42.  
  43.         # copy target at targets_ptr to new_targets_ptr
  44.  
  45.         lw $t0, 0($s0) # temp = *targets_ptr
  46.         sw $t0, 0($s5) # *new_targets_ptr = temp
  47.  
  48.         # advance new_target_ptr and new_targets_count
  49.         addi $s5, $s5, 4 # new_targets_ptr += 4 (increment by 4 bytes to get to next available target address)
  50.         addi $s6, $s6, 1 # new_targets_count++
  51.  
  52.     prune_targets_step_continue:
  53.  
  54.         # advance targets_ptr and targets_remaining
  55.         addi $s0, $s0, 4  # targets_ptr += 4 (increment by 4 bytes to get to next target)
  56.         addi $s1, $s1, -1 # targets_remaining--
  57.        
  58.         # continue
  59.         j prune_targets_step
  60.  
  61.     prune_targets_end:
  62.  
  63.         # return new_targets_count
  64.         move $v0, $s6
  65.  
  66.         # pop return address and save register state from stack
  67.  
  68.         lw $ra, 0($sp)
  69.         lw $s0, 4($sp)
  70.         lw $s1, 8($sp)
  71.         lw $s2, 12($sp)
  72.         lw $s3, 16($sp)
  73.         lw $s4, 20($sp)
  74.         lw $s5, 24($sp)
  75.         lw $s6, 28($sp)
  76.         addi $sp, $sp, 32 # deallocate 32 bytes from stack
  77.  
  78.         # return
  79.         jr $ra
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement