# Lab 7 Name: AMAN ANAND Roll No.: CS22B054 --- ## Question 1 ### Step 1 - convert inegral part to binary As we know 0 in decimal = 0 in binary ### Step 2 - convert fractional part to binary 0.1 x 2 = 0.2, integral part = 0 0.2 x 2 = 0.4, integral part = 0 0.4 x 2 = 0.8, integral part = 0 0.8 x 2 = 1.6, integral part = 1 0.6 x 2 = 1.2, integral part = 1 . . . combine all intergral parts to get binary: .1 in decimal = .00011.... in binary ### Step 3 - combine both parts 0.1 in decimal = 0.00011.... in binary --- **final answer** (0.1) –> (0.00011(0011)recurring) ## Question 2 ### Step 1 - Find sign bit sign bit = 0 ### Step 2 - write 0.1 in base 2 sintific notation 0.1 / 2<sup>-1</sup> = 0.2 0.1 / 2<sup>-2</sup> = 0.4 0.1 / 2<sup>-3</sup>= 0.8 __0.1 / 2<sup>-4</sup> = 1.6__ 0.1 in base 2 scientific = 1.6 x 2<sup>-4</sup> sign = 0 mantesa = 6 exponent = -4 ### Step 3 - convert 0.6 to binary mantesa has 23 bits in IEEE notation. Therefore, 0.6 in binary can be approximated up to 23 bits. Python script to convert 0.6 to binary ```python= fraction = 0.6 integer = 0 for i in range(23): fraction *= 2; integer =int(fraction); print(integer,end="") fraction -= integer ``` 0.6 in binary approximated to 23 bits = 0.10011001100110011001100 matesa = 10011001100110011001100 ### Step 4 - convert -4 to binary Exponent has bais of 127 in IEEE format. therefore, -4 is represented as -4 + 127 = 123 123 in binary = 01111011 exponent = 01111011 ### Step 5 - combine sign, mantesa and exponent 0.1 in binary = 0 01111011 10011001100110011001100 --- ## Question 3 ### Step 1 - Sign bit Sign is positive so Sign bit is 0. ### Step 2 - Convert to binary `code` #include <iostream> using namespace std; int main() { double frac=0.2; int i=0; int n=0; for(i=0;i<23;i++) { frac=frac*2; n=int(frac); cout<<n; frac=frac-n; } return 0; } 0.2 10=0.001100110011001100110011002 ### Step 3 - Convert binary to scientific notation 0.001100110011001100110011002=(1.10011001100110011001100 x 2-3)2 ### Step 4 - Exponent part Exponent has a bias of 127 in IEEE754 format, so -3 is represented as -3 +127 = 124 12410=1111100 2 ### Step 5 - Combining sign,exponent,mantisa part 0.1 10=0 1111100 10011001100110011001100 in IEEE754 binary format ### Step 5 - Converting back 0 1111100 10011001100110011001100 => Sign bit 0 =>Positive Exponent part 1111100 2=124 10 Actual exponent =124-127=-3 Actual value =(1.10011001100110011001100 x 2 -3)2 =0.001100110011001100110011002 =0.1999999880790710449210 ## 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: ### Sign bit: 1 bit to represent the sign of the number (0 for positive, 1 for negative). ### Integer part: Multiple registers or memory locations to store the integer part of the number. Fractional part: Multiple registers or memory locations to store the fractional part of the number. ### 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 `code` .data w1: .word 0x3E4CCCCD w2: .word 0x3F333333 w3: .word 0x0 .text la x1 w1 lw x2 0(x1) la x3 w2 lw x4 0(x3) li x5 0x80000000 and x6 x2 x5 addi x31 x0 31 srl x6 x6 x31 li x5 0x7f800000 and x7 x2 x5 addi x31 x0 23 srl x7 x7 x31 li x5 0x007fffff and x8 x2 x5 li x31 0x800000 add x8 x8 x31 beq x6 x0 loop addi x25 x0 -1 mul x8 x8 x25 loop: li x5 0x80000000 and x9 x4 x5 addi x31 x0 31 srl x9 x9 x31 li x5 0x7f800000 and x10 x4 x5 addi x31 x0 23 srl x10 x10 x31 li x5 0x007fffff and x11 x4 x5 li x31 0x800000 add x11 x11 x31 beq x9 x0 loop1 addi x25 x0 -1 mul x11 x11 x25 loop1: sub x12 x7 x10 blez x12 change srl x11 x11 x12 add x29 x7 x0 add x23 x29 x0 j ans change: addi x25 x0 -1 mul x12 x12 x25 srl x8 x8 x12 add x29 x10 x0 add x23 x29 x0 ans: add x13 x11 x8 beq x13 x0 end beq x6 x9 ans1 j ans2 ans1: li x14, 0x7FFFFF and x15 x13 x14 slli x6 x6 31 slli x29 x29 23 or x28 x6 x29 or x28 x28 x15 la x26 w3 sw x28 0(x26) j end ans2: add x31 x13 x0 addi x22 x0 31 srl x6 x31 x22 li x28 1 loop2: li x31 0x800000 and x29 x13 x31 srai x29 x29 23 beq x29 x28 ans3 srl x13 x13 x28 j loop2 ans3: li x14 0xFFFFF and x15 x13 x14 slli x6 x6 31 slli x23 x23 23 or x28 x6 x23 or x28 x28 x15 la x26 w3 sw x28 0(x26) j end end: add x31 x0 x0 addi a7 10 ecall