I have following problem:
I wanted to add two numbers -1.25 and 3.25 (the result is obviously 2) in RISC-V assembly using 32-bit registers.
I assumed Q format (imaginary decimal point), so that there would be 29 integer positions (I know that is not necessary and I can have better resolution with allocating more positions to fractional part, but this is just for simplicity) and 3 fractional positions in U2 format.
So I converted -1.25 in dec to bin (U2) (wihout imaginary decimal point):
11111111111111111111111111111110.110
And 3.25:
00000000000000000000000000000011.010
And then converted it to HEX in order to store it in my program.
My code is:
.data
newLine: .asciz "n"
num: .word 0xFFFFFFFEC
num2: .word 0x000000034
.text
li a7, 4
la a0, newLine
ecall
la t5, num
lw t5, 0(t5)
la t6, num2
lw t6, 0(t6)
add t3, t5, t6
li a7, 4
la a0, newLine
ecall
li a7, 35
mv a0, t3
ecall
And in the end I get:
00000000000000000000000000100000
What I’ve got is 32 in dec and I’m very confused.
Because I thought that if I have 3 slots after the decimal point it means that if, at the end, I shift right the result by 3 bits I would get the integer part. But for the moment if I shift my result by 3 bits I would get:
00000000000000000000000000000100
Which is 4, not 2.
Why doesn’t it work that way?
Also I have another question:
How to print the result in RARS simulator so that I would have this result in my desired format? For example if I would add 3.33 and 1.50 I would get: 4.83. I know how to get the integer part (by shifting certain amount of bits) but don’t know how to do it for fractional part.
Thank you in advance.