简体   繁体   中英

Sum of Absolute Differences in Assembler

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM