1) Translate the following C function abs() into MIPS assembly procedure. You mu
ID: 3678108 • Letter: 1
Question
1) Translate the following C function abs() into MIPS assembly procedure. You must minimize the number of registers used by your MIPS procedure: use the $s0 register to hold any temporary values for the computation performed by the MIPS procedure. You must use the stack to save and restore the contents of any cpu registers that may be used by the callee in compliance with the established MIPS conventions. You must also follow MIPS register use conventions to pass and receive arguments and return values. You may not use pseudo-instructions for this assignment. Comment the MIPS assembly code generously to explain its functionality clearly.
/* Determine the absolute value of an integer x. */
int abs(int x) {
int result;
if (x >= 0) {
result = x;
else
{ result = -x;
}
return result;
}
Below is a template for your abs() function in MIPS Assembly:
# abs; assume arg1 is in $a0; put return value in $v0 .
globl abs
abs:
# insert your code here
end_abs:
jr $ra
Explanation / Answer
Answer for Question:
This below assembly language code is simplified abs function representation
#assume you want the absolute value of r1
ori $2, $zero, $1 #copy r1 into r2
slt $3, $1, $zero #is value < 0 ?
beq $3, $zero, foobar #if r1 is positive, skip next inst
sub $2, $zero, $1 #r2 = 0 - r1
foobar:
#r2 now contains the absolute value of r1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.