-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How negative value in a registers are handled? #3
Comments
Hi Vivek, The code above is correct. The array of In the example above, you can see that for the signed ADDI instruction, the value read from X[rs1] is cast to a signed integer, which means the operation is performing signed arithmetic. Once the operation is computed, its converted back to an unsigned result and stored away. The bit pattern for signed and unsigned is the same so this is perfectly correct and fairly basic behavior in C. Remember, the key is not what type the data is stored as, but how its cast before the arithmetic is performed. |
Don't we need to store negative numbers in 2' complement ? |
The stored bit-pattern for signed and unsigned numbers is the same. What's important is how that data gets used during an operation. The data doesn't know that its signed or unsigned, only the operation does.
I have no idea about rust, I've never looked at it. What I described is valid for C and C++. |
Further to this point, the data will be stored in twos compliment, because the result of the operation was is signed. We do not modify the bit pattern of the result when storing it. |
Could @bit-hack please explain how negative values in registers are handled?
For example say ADDI r5, r0, 100 and say r0 has value -100 (in 2's complement)
will following give correct answer ?
because X stores unsigned integer AFAIU then above seems not handled correctly.
The text was updated successfully, but these errors were encountered: