### Lab7 NAME: LUKALAPU S V S RAGHURAM ROLL NO: CS22B035 ***Q1*** ``` Converting 0.1 to binary involves representing its fractional part in binary. However, representing 0.1 precisely in binary is not possible as it is a repeating decimal in base 2. We can only represent it approximately using a finite number of bits. Using the process of multiplication by 2 and extracting the integer part of the result, we can approximate the binary representation of 0.1. 1. 0.1 * 2 = 0.2 ⟶ Bit: 0 2. 0.2 * 2 = 0.4 ⟶ Bit: 0 3. 0.4 * 2 = 0.8 ⟶ Bit: 0 4. 0.8 * 2 = 1.6 ⟶ Bit: 1 5. 0.6 * 2 = 1.2 ⟶ Bit: 1 6. 0.2 * 2 = 0.4 ⟶ Bit: 0 This process repeats indefinitely because 0.1 in binary is a repeating fraction, similar to how 1/3 is represented as 0.333... in decimal. So, a typical approximation of 0.1 in binary would be 0.0001100110011.. ``` ---- ***Q2*** ``` sign bit of 0.1 is 0 now it's exponent in scientific form is -4 so it's exponent is 123 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 ``` ``` 0 01111011 10011001100110011001100 ``` ***Q3*** ``` sign bit of 0.2 is 0 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 ``` ``` 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 ``` 1x2<sup>-1</sup>+0x2<sup>-2</sup>+.......... ``` so we approximately get 0.6 so answer is 1.6*(0.125) so value is 0.2 ``` ***Q4*** ``` To represent floating-point numbers and perform addition, let's use a simple representation where each floating-point number is split into two parts: one for the integer part and another for the fractional part. We'll store these parts in separate registers. For this example, let's use two registers to store each floating-point number: one for the integer part and another for the fractional part. Here's how we can represent and perform addition for the given example of adding 5.391 with 3.012: 1. **Representation**: - Register 1: Store the integer part of the floating-point number. - Register 2: Store the fractional part of the floating-point number. 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 2. **Performing Addition**: - Add the fractional parts together. If the sum exceeds 999 (or the maximum value of the fractional part), carry over to the integer part. - Add the integer parts together along with the carry, if any. Let's perform the addition: - Fractional part: 391 + 012 = 403 - Since 403 is within the range of the fractional part (0 to 999), no carryover is needed. - Integer part: 5 + 3 (no carryover) = 8 So, the result is: - Register 5: Integer part = 8 - Register 6: Fractional part = 403 Therefore, the result of adding 5.391 and 3.012 is 8.403. ``` ---- ***Q5*** ``` .section .data # Define the two floating-point numbers in IEEE 754 format num1: .float 3.14159 # Example: 3.14159 num2: .float -3.14159 # Example: -3.14159 # Define the memory location to store the result result: .float 0.0 # Initialize to 0.0 .section .text .globl _start _start: # Load the address of num1 into a register la t0, num1 # Load the address of num2 into another register la t1, num2 # Load the first floating-point number into a floating-point register (f0) flw f0, 0(t0) # Load the second floating-point number into another floating-point register (f1) flw f1, 0(t1) # Perform floating-point addition fadd.s f2, f0, f1 # Store the result in the memory location la t2, result fsw f2, 0(t2) # Exit the program li a0, 0 # Exit status li a7, 93 # Exit system call number ecall # Invoke system call ``` ---