# Lab 7
Name: G Jaswanth
Roll No.: CS22B020
---
## Question 1
```
To convert 0.1 (in base ten) to binary, we can use the method of multiplying
by 2 and noting the integer part of the result at each step.
Here are the steps:
1. Multiply 0.1 by 2: 0.1 * 2 = 0.2, integer part is 0
2. Multiply 0.2 by 2: 0.2 * 2 = 0.4, integer part is 0
3. Multiply 0.4 by 2: 0.4 * 2 = 0.8, integer part is 0
4. Multiply 0.8 by 2: 0.8 * 2 = 1.6, integer part is 1
5. Multiply 0.6 by 2: 0.6 * 2 = 1.2, integer part is 1
6. Multiply 0.2 by 2: 0.2 * 2 = 0.4, integer part is 0
7. Multiply 0.4 by 2: 0.4 * 2 = 0.8, integer part is 0
8. Multiply 0.8 by 2: 0.8 * 2 = 1.6, integer part is 1
9. Multiply 0.6 by 2: 0.6 * 2 = 1.2, integer part is 1
We continue this process until we either reach the desired precision or the
fractional part becomes zero. The binary representation of 0.1 is therefore
0.0001100110011... (the digits 1100 repeat indefinitely).
```
### Output
```
0.0001100110011...
```
## Question 2
```
-> sign bit of 0.1 is 0
-> now it's exponent in scientific form is -4 so it's exponent is 123 (01111011)
since it's bias for precesion is 127
now 0.1/0.0625 is equal to 1.6
now representing 0.6 in binary form:
1. Start with 0.6.
2. Multiply 0.6 by 2:
- 0.6 * 2 = 1.2 ⟶ Bit: 1
3. Take the fractional part (0.2) and repeat:
- 0.2 * 2 = 0.4 ⟶ Bit: 0
- 0.4 * 2 = 0.8 ⟶ Bit: 0
- 0.8 * 2 = 1.6 ⟶ Bit: 1
- 0.6 * 2 = 1.2 ⟶ Bit: 1
- 0.2 * 2 = 0.4 ⟶ Bit: 0
- 0.4 * 2 = 0.8 ⟶ Bit: 0
- 0.8 * 2 = 1.6 ⟶ Bit: 1
- 0.6 * 2 = 1.2 ⟶ Bit: 1
- ... and so on.
The pattern repeats: 1001. Note that this pattern repeats every 4 bits after the binary point.
To get 23 bits after the binary point, we can extend this pattern:
0.6 in binary with 23 bits after the binary point:
0.10011001100110011001100
```
### Output:
```
0 01111011 10011001100110011001100
```
---
## Question 3
**Part1**
```
The exponent of 0.2 in scientific notation is -3 so it's value is 124
representation of 124 in binary is 01111100
when we divide 0.2/0.125 we get 1.6 , represent 0.6 in 23 bit binary number:
now representing 0.6 in binary form:
1. Start with 0.6.
2. Multiply 0.6 by 2:
- 0.6 * 2 = 1.2 ⟶ Bit: 1
3. Take the fractional part (0.2) and repeat:
- 0.2 * 2 = 0.4 ⟶ Bit: 0
- 0.4 * 2 = 0.8 ⟶ Bit: 0
- 0.8 * 2 = 1.6 ⟶ Bit: 1
- 0.6 * 2 = 1.2 ⟶ Bit: 1
- 0.2 * 2 = 0.4 ⟶ Bit: 0
- 0.4 * 2 = 0.8 ⟶ Bit: 0
- 0.8 * 2 = 1.6 ⟶ Bit: 1
- 0.6 * 2 = 1.2 ⟶ Bit: 1
- ... and so on.
The pattern repeats: 1001. Note that this pattern repeats every 4 bits after the binary point. To get 23 bits after the binary point, we can extend this pattern:
0.6 in binary with 23 bits after the binary point:
0.10011001100110011001100
```
```
now entire representation:
0 01111100 10011001100110011001100
```
**Part2**
```
now reverting back
sign bit 0
exponent is 124 so the exponent in scientific notation is -3
now we can see in binary form it is
```
$$
1 \times 2^{-1} + 0 \times 2^{-2} + 0 \times 2^{-3}+\ldots
$$
```
so we approximately get 0.6
so answer is 1.6*(0.125)
so value is 0.2
```
## Question 4
```
we can use a simple scheme where each floating-point number is represented by two registers:
one for the integer part and one for the fractional part.
We will assume a fixed number of decimal places for simplicity.
Let's represent the floating-point numbers using two registers (R1 and R2 for the integer and fractional parts, respectively).
Each register can store values from 0 to 9.
We'll perform addition using this representation and store the result in another pair of registers (R3 and R4).
Given the numbers to add are 5.391 and 3.012, we initialize the registers as follows:
- R1: 5 (integer part of 5.391)
- R2: 391 (fractional part of 5.391, after the decimal point)
- R3: 3 (integer part of 3.012)
- R4: 012 (fractional part of 3.012, after the decimal point)
Now, let's perform the addition:
1. Add the fractional parts:
- R2 (391) + R4 (012) = 403 (carry 0, result in R4)
2. Add the integer parts with the carry from the fractional part addition:
- R1 (5) + R3 (3) + carry (0) = 8 (result in R3)
The result stored in registers R3 and R4 is 8.403,
which corresponds to the addition of 5.391 and 3.012.
Note that this representation is simplistic and assumes fixed decimal places and limited range of values in each register.
```
### Question 5
```assembly=
.data
num1: .word 0b01000001001100000000000000000000 # 11.0
num2: .word 0b11000001001010000000000000000000 # -10.5
num3: .word 0
exmask: .word 0x7f800000
mmask: .word 0b00000000011111111111111111111111
extra1: .word 0b00000000100000000000000000000000
mantissa: .word 0b00000000010000000000000000000000
.text
lw x1 num1
lw x2 num2
srli a1 x1 31 # sign
srli a2 x2 31
lw t1 exmask
lw t2 mmask
lw t0 extra1
# Handling the sign parities seperately, increased redundancy
beq a1 a2 same
j notsame
same:
and a1 x1 t1
srli s1 a1 23 # exponent
mv s9 s1
and a1 x1 t2 # mantissa
or a1 a1 t0
and a2 x2 t1
srli s2 a2 23
mv s10 s2
and a2 x2 t2
or a2 a2 t0
expsame:
beq s10 s9 exit
blt s10 s9 inc
addi s9 s9 1
srli a1 a1 1
j expsame
inc:
addi s10 s10 1
srli a2 a2 1
j expsame
exit:
add t6 a1 a2 # mantissa sum
srli t5 t6 23 # extra mantissa
and t6 t6 t2
addi x1 x0 1
shiftexp:
beq x1 t5 exit1
addi s9 s9 1
srli t5 t5 1
srli t6 t6 1
j shiftexp
exit1:
#concatenate the answer
lw x1 num1
srli a1 x1 31
slli a1 a1 31
slli s9 s9 23
add a1 a1 s9
and t6 t6 t2
add a1 a1 t6
la x1 num3
sw a1 0(x1)
j exit3
notsame:
# Assuming abs(a) > abs(b)
# Otherwise need to swap'em
and a1 x1 t1
srli s1 a1 23 # exponent
mv s9 s1
and a1 x1 t2 # mantissa
or a1 a1 t0
and a2 x2 t1
srli s2 a2 23
mv s10 s2
and a2 x2 t2
or a2 a2 t0
expnotsame:
beq s10 s9 exit2
blt s10 s9 inc2
addi s9 s9 1
srli a1 a1 1
j expnotsame
inc2:
addi s10 s10 1
srli a2 a2 1
j expnotsame
exit2:
sub t6 a1 a2 # mantissa difference
lw s7 extra1
mv s4 t6
and s6 s7 t6
shiftexp2:
beq s6 s7 exit1
slli t6 t6 1
addi s9 s9 -1
and s6 s7 t6
j shiftexp2
exit3:
addi a7 x0 10
ecall
```
---