Nios/saturate2

From ASMBits

Sometimes when doing arithmetic, it is better to "saturate" (or clamp) numbers that overflow to the maximum or minimum possible value rather than wrap around.

Write a function that will convert a signed 32-bit number to (signed) 8 bits. If the 32-bit number is too big to fit, saturate (or clamp) the output to the closest possible signed 8-bit value. Since the output is an 8 bit value, the upper 24 bits of the return value should be zero.

char saturate(int n);


Sample Input

0x123

Sample Output

0x7f

Write your solution here

Upload a source file...