2

Lets assume the function in pseudocode.

   int abs_diff(int l, int r) {
      int abs_diff = abs(l - r);
      return abs_diff;
   }

I was able to implement this function in assembler.

abs_diff:
    sub $t1, $a0, $a1
    sra $t2,$t1,31   
    xor $t1,$t1,$t2   
    sub $v0,$t1,$t2    
    jr $ra  #Return

Now I want to implement an extension of this function in assembler. The pseudocode for the new function is

   int abs_diff_new(int r1, int g1, int b1, int r2, int g2, int b2) {
      int abs_diff_new = abs(r1-r2) + abs(g1-g2) + abs(b1-b2);
      return abs_diff_new;
    } 

I don't know how to implement this functions, since this new function requires 6 arguments, but MIPS only provides 4 registers ($a0-$a3) to pass the arguments. How can i modify my abs_diff ?

waqarrt
  • 21
  • 1
  • Easiest thing would probably be to call the `abs_diff` function on each of the three pairs, then call the `abs_diff_new` on the results of each return value. The other option would be to push the arguments onto the stack, but I'm not familiar with how to do that on mips. – anonmess Apr 24 '19 at 17:53
  • Managing a stack in MIPS is not very difficult. But as only the difference of values matters, the simplest (and fastest) IMHO, is to use 3 arguments that are the differences. To call the function, instead of using `add $a1,$t5,$0 add $a2, $t6, $0` use instead `add $a1,$t5,$0 sub $a1,$a1, $t6` and then you just have to compute the absolute values of $a1,$a2, $a3 and to sum them. Simple and you gain 3 instructions. – Alain Merigot Apr 24 '19 at 19:12

1 Answers1

0

The convention for calling functions with more than four arguments is to store the extra arguments in memory. See this answer for more details.

Before calling abs_diff you will need to store any extra arguments:

sw $s0, 4($sp) # Assuming $s0 = g2
sw $s1, 8($sp) # Assuming $s1 = b2

Then you can fetch them inside of abs_diff with:

lw $t0, 20($sp) # Assuming you subtracted $sp by 16
lw $t1, 24($sp)

You can then use $t0 and $t1 to perform the rest of your calculation.