# LAB 7
#### Name:Aseem Anand
#### Roll No:CS22B008
## Question 1
0 ÷ 2 = 0 R 0
.
0.1 × 2 = 0.2 + 0
0.2 × 2 = 0.4 + 0
0.4 × 2 = 0.8 + 0
0.8 × 2 = 0.6 + 1
0.6 × 2 = 0.2 + 1
0.2 × 2 = 0.4 + 0
0.4 × 2 = 0.8 + 0
0.8 × 2 = 0.6 + 1
0.6 × 2 = 0.2 + 1
0.2 × 2 = 0.4 + 0
0.4 × 2 = 0.8 + 0
0.8 × 2 = 0.6 + 1
.
.
.
Reading the remainders from the whole portion from the bottom up and the fractional portion from the top down:
0.000110011001...
## Question 2
We will solve for single precision
from above :
binary of 0.1 is 0.000110011001...
As the number is positive hence sign bit is equal to 0
Normalised form:1.10011001100... x 2^-4
Hence,exponential decimal form of exponential bit is 123
So,exponential bit is 01111011.
And, fraction is 10011001100110011001100
Hence,
final representation is
0 01111011 10011001100110011001100
## Question 3
We will solve for single precision.
binary of 0.2 is 0.00110011001100110011...
As the number is positive hence sign bit is equal to 0.
Normalised form:1.10011001100... x 2^-3.
Hence,exponential decimal form of exponential bit is 124.
So,exponential bit is 01111100.
And, fraction is 10011001100110011001100.
Hence,
final representation is
0 01111100 10011001100110011001100
###### Now reverse
IEEE 754 standard representation:
0 01111100 10011001100110011001100
Sign bit is 0.hence,the number is positive.
Now,After Converting exponential bit to decimal we get 124.
Hence, the number will be 1.fraction x 2^-3
So,the number will be 0.00110011001100110011001100 in binary.
hence,the number will be 0.19999998807907104492
## Question 4
To design a representation for floating-point numbers and perform addition, we can create a custom format with separate registers or memory locations for the integer and fractional parts. Here's a simple design:
1. **Sign bit**: 1 bit to represent the sign of the number (0 for positive, 1 for negative).
2. **Integer part**: Multiple registers or memory locations to store the integer part of the number.
3. **Fractional part**: Multiple registers or memory locations to store the fractional part of the number.
4. **Decimal point**: We can assume a fixed position for the decimal point.
Let's say we have 4 registers to store each part (1 for sign, 1 for integer part, 1 for fractional part, and 1 for the decimal point).
To add two floating-point numbers, we perform the following steps:
1. Add the integer parts.
2. Add the fractional parts.
3. If the sum of the fractional parts exceeds 1, carry over to the integer part.
4. Handle overflow or underflow if necessary.
5. Combine the sign, integer part, and fractional part to get the result.
Let's perform the addition of 5.391 and 3.012:
Integer part of 5.391 = 5
Fractional part of 5.391 = 0.391
Integer part of 3.012 = 3
Fractional part of 3.012 = 0.012
Adding integer parts: 5 + 3 = 8
Adding fractional parts: 0.391 + 0.012 = 0.403
The result is 8.403.
In our custom representation, we would store:
- Sign: 0 (positive)
- Integer part: 8
- Fractional part: 403 (assuming each register can hold 3 digits)
- Decimal point: At a fixed position (e.g., after the third digit)
So, the final representation in our custom format would be:
Sign: 0
Integer part: 8
Fractional part: 403
Decimal point: At a fixed position
This is a basic representation and addition process. Real-world floating-point representations are more complex and typically follow standards like IEEE 754 for floating-point arithmetic.
## Question 5
.data
num1: .word 0x800000 # IEEE 745 bin(0x800000)='00000000100000000000000000000000'
num2: .word 0x800000 # IEEE 745 bin(0x800000)='00000000100000000000000000000000'
mentissa: .word 0x7fffff # for retrieving the mentissa part
sign: .word 0x80000000 # for retrieving the sign
exponent: .word 0x7f800000 # for retrieving the exponent part
bit24: .word 0x800000 # for adding 1 before decimal point
ans: .word 0x0
.text
main:
lw x11 mentissa
lw x12 exponent
lw x8 num1
lw x9 num2
lw x13 sign
lw x14 bit24
#sign bit of 1st number
and x7 x8 x13
srli x7 x7 31
#exponent of 1st number
and x6 x8 x12
srli x6 x6 23
#mentissa of 1st number
and x5 x8 x11
add x5 x5 x14
#sign bit of 2nd number
and x30 x9 x13
srli x30 x30 31
#exponent of 2nd number
and x8 x9 x11
add x8 x8 x14
#mentissa of 2nd number
and x29 x9 x12
srli x29 x29 23
#checking if both number have same power or not
bge x6 x29 func1
mv a5 x5
mv a6 x6
mv a7 x7
mv x5 x8 #changing the mantissa according to their exponent
mv x6 x29
mv x7 x30
mv x8 a5
mv x29 a6
mv x30 a7
func1:
sub x18 x6 x29
sra x8 x8 x18
beq x7 x0 func2 #jump to func2 if sign is +ve
#o/w making x5 -ve
sub x5 x0 x5
func2:
beq x30 x0 func3 #jump to func3 if sign is +ve
#o/w making x8 -ve
sub x8 x0 x8
func3:
add x19 x5 x8
loop1:
bge x19 x14 loop2
slli x19 x19 1
addi x6 x6 -1
j loop1
loop2:
addi t6 x0 0
#checking the sign bit
bge x19 x0 func4
sub x19 x0 x19
addi t6 x0 1
func4:
srli x20 x19 24
#check for carry
beq x20 x0 Exit
srli x19 x19 1
addi x6 x6 1
Exit:
li a5 0
slli t6 t6 31
add a5 a5 x6
slli a5 a5 23
and x19 x19 x11
add a5 a5 x19
or a5 a5 t6
la x2 ans
sw a5 0(x2)
li a7 34
add a0 x0 a5
ecall
###### After using chatgpt
.data
num1: .float 3.14 # First floating-point number
num2: .float 2.71 # Second floating-point number
result: .float 0.0 # Memory location to store the result
.text
.globl main
main:
la t0, num1 # Load address of num1 into t0
flw ft0, (t0) # Load num1 into ft0 (floating-point register)
la t0, num2 # Load address of num2 into t0
flw ft1, (t0) # Load num2 into ft1 (floating-point register)
fadd.s ft2, ft0, ft1 # Add ft0 and ft1, store result in ft2
la t0, result # Load address of result into t0
fsw ft2, (t0) # Store ft2 (result) at memory location
# Exit program
li a7, 10
ecall