Write a routine called Abs that computes the absolute value of the 8-bit two’s complement number in the W register, and returns the result in W. If the MSB of W is 0, then W is positive so the routine should simply return. If the MSB of W is 1, then W is negative so the routine needs to perform a two’s complement operation. Hand in your Abs routine. Make sure that it is well-commented so the grader can understand how it works.
#include
#include #include void main(void) { int16_t i; int16_t absi; i = INT16_MIN + 1; /* Set i to one more than the most negative number */ absi = abs(i); printf(\"Argument = %\" PRId16 \". Absolute value of argument = %\" PRId16, i, absi); i--; /* i should now equal INT16_MIN */ absi = abs(i); printf(\"\ Argument = %\" PRId16 \". Absolute value of argument = %\" PRId16, i, absi); } int abs(int i) { /* compute absolute value of int argument */ return (i < 0 ? -i : i); } int sabs(int i) { int res; if (INT_MIN == i) { res = INT_MAX; } else { res = i < 0 ? -i : i; } return res; }