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

x
 
1
# A test case to test your function with
2
.global _start
3
_start:
4
    movia r4, 0x123    # First parameter is always in r4
5
    call saturate
6
    br _start        # End of testing code
7
8
# Convert signed 32 bit to 8 bit, with saturation
9
saturate:
10
    
11
    
Upload a source file...