---
# System prepended metadata

title: Lab 7

---

# Lab 7
Name: Sai Kowshik Allu 
Roll No.: CS22B004

---
## Question 1
To convert 0.1ten to binary, we can use the method of successive multiplication by 2:

0.1 * 2 = 0.2, take 0
0.2 * 2 = 0.4, take 0
0.4 * 2 = 0.8, take 0
0.8 * 2 = 1.6, take 1
0.6 * 2 = 1.2, take 1
0.2 * 2 = 0.4, take 0
0.4 * 2 = 0.8, take 0
0.8 * 2 = 1.6, take 1

The binary representation of 0.1ten is 0.00011001100110011...

## Question 2
  Sign bit of 0.1: 0

Exponent in scientific form: -4 (biased exponent: 123, bias: 127)

0.1 divided by 0.0625 equals 1.6

Binary representation of 0.6:
- Start with 0.6.
- Multiply 0.6 by 2:
  - 0.6 * 2 = 1.2 ⟶ Bit: 1
- Take the fractional part (0.2) and repeat:
  - 0.2 * 2 = 0.4 ⟶  0
  - 0.4 * 2 = 0.8 ⟶  0
  - 0.8 * 2 = 1.6 ⟶  1
  - 0.6 * 2 = 1.2 ⟶  1
  - 0.2 * 2 = 0.4 ⟶  0
  - 0.4 * 2 = 0.8 ⟶  0
  - 0.8 * 2 = 1.6 ⟶  1
  - 0.6 * 2 = 1.2 ⟶  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
- Exponent: 01111011 (123 in decimal)
- Mantissa: 10011001100110011001100

Representation in IEEE 754 format:
0 01111011 10011001100110011001100


## Question 3
For a floating-point number 0.2, the sign bit is 0.

In scientific notation, the exponent of 0.2 is -3, leading to a binary representation of 124: 01111100.

When dividing 0.2 by 0.125, the result is 1.6.

To represent 0.6 in a 23-bit binary format:

1. Starting with 0.6, we multiply it by 2:
   - 0.6 * 2 = 1.2 ⟶ Bit: 1
2. Then, we focus on the fractional part (0.2) and continue the process:
   - 0.2 * 2 = 0.4 ⟶  0
   - 0.4 * 2 = 0.8 ⟶  0
   - 0.8 * 2 = 1.6 ⟶  1
   - 0.6 * 2 = 1.2 ⟶  1
   - 0.2 * 2 = 0.4 ⟶  0
   - 0.4 * 2 = 0.8 ⟶  0
   - 0.8 * 2 = 1.6 ⟶  1
   - 0.6 * 2 = 1.2 ⟶  1
   - ... and so forth.

The repeating pattern is: 1001, which recurs every 4 bits after the binary point.

0.6 in binary with 23 bits after the binary point:
0.10011001100110011001100

Complete IEEE 754 representation:
0 01111100 10011001100110011001100

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 * 2^-1 +0 * 2^-2+…
so we approximately get 0.6
so answer is 1.6*(0.125)
so value is 0.2

## Question 4
Representation and Addition of Floating-Point Numbers:

To represent and perform addition for floating-point numbers, we'll adopt a straightforward representation using registers. Each floating-point number will be split into two parts: the integer part and the fractional part. For our example, we'll use two registers to store each floating-point number—one for the integer part and another for the fractional part.

Representation:
- Register Structure:
  - Register 1: Integer part of the floating-point number.
  - Register 2: Fractional part of the floating-point number.

For instance, considering the numbers 5.391 and 3.012:
- For 5.391:
  - Register 1: Integer part = 5
  - Register 2: Fractional part = 391
- For 3.012:
  - Register 3: Integer part = 3
  - Register 4: Fractional part = 012

Addition Procedure:
1. Adding Fractional Parts:
   - Add the fractional parts together. If the sum exceeds 999 (the maximum value of the fractional part), carry over to the integer part.
2. Adding Integer Parts:
   - Add the integer parts together along with any carryover generated from the fractional part addition.

Performing Addition:
- Fractional Part Addition: 
  - 391 + 012 = 403
  - Since 403 is within the range of the fractional part (0 to 999), no carryover is needed.
- Integer Part Addition:
  - 5 + 3 = 8 (no carryover)

Result:
- Resultant Floating-Point Number:
  - Register 5: Integer part = 8
  - Register 6: Fractional part = 403

Therefore, the result of adding 5.391 and 3.012 is 8.403.
Use separate registers to hold the integer and fractional parts of the floating-point numbers. Initially, add the integer parts and fractional parts separately. If there's a carry from the fractional addition, incorporate it into the sum of the integer parts. Keep the results in the same format.


 ## 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

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:
    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:
    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



```


