0% found this document useful (0 votes)
16 views14 pages

Assignment 3 Answer (1) FINAL............. Coal

The document contains a series of assignments analyzing floating-point numbers in IEEE 754 format, including conversions from hexadecimal and decimal to binary, as well as programming examples for comparing and finding maximum values of floating-point numbers. Each question provides detailed solutions, including calculations for sign bits, exponents, mantissas, and the final decimal values. Additionally, it includes MIPS assembly code for various operations involving single and double precision floating-point numbers.

Uploaded by

mirzafarazmirza9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views14 pages

Assignment 3 Answer (1) FINAL............. Coal

The document contains a series of assignments analyzing floating-point numbers in IEEE 754 format, including conversions from hexadecimal and decimal to binary, as well as programming examples for comparing and finding maximum values of floating-point numbers. Each question provides detailed solutions, including calculations for sign bits, exponents, mantissas, and the final decimal values. Additionally, it includes MIPS assembly code for various operations involving single and double precision floating-point numbers.

Uploaded by

mirzafarazmirza9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Assignment # 3:

Section: A7:
Azfar Amjad F2023376185
Basim Abrar F2023376177
Question 1. Analyze the value of the bit pattern 0x0C000000 if it represents a floating

point number in IEEE 754 single-precision format.

Solution: 0x0C000000 = 0000 1100 0000 0000 0000 0000 0000 0000

Floating point representation:

Sign bit = 0

Exponent = 00011000 = 24

Mantissa = 00000000000000000000000 = 0

Value = (-1)s X (1 + fraction) X (2)exponent – bias = (-1)0 X (1 + 0) X (2)24– 127

Value = 1 X 2-103

Question 2. Analyze how the decimal number 63.25 is represented as a floating-point

number in IEEE 754 single-precision format. Provide the binary representation.

Solution: (63.25)10 = (111111.01)2

In scientific notation it will be written as 1.1111101 x 25

Floating point representation:

Sign bit = 0

exponent = 10000100 = 5

mantissa = 11111010000000000000000 = 1111101

Biased exponent = 6 + 127 = 133 = 10000100

Question 3. Analyze the floating-point representation of -1024.75 in IEEE 754 single


precision format. What are the binary and hexadecimal representations?

Solution: (-1024.75)10 = (-10000000000.11)2

In scientific notation it will be written as 1.000000000011 x 210

Floating point representation:

Sign bit = 1

exponent = 10001001 = 10

mantissa = 00000000001100000000000 = 000000000011

Biased exponent = 10 + 127 = 137 = 10001001

Question 4. Analyze the decimal value represented by the bit pattern 0xC1A40000 in

IEEE 754 single-precision format.

Solution: 0xC1A40000 = 1100 0001 1010 0100 0000 0000 0000 0000

Floating point representation:

Sign bit = 1

Exponent = 10000011 = 131 – 127 – 4

Mantissa = 01001000000000000000000 =.01001 = 0.28125

Value = (-1)s X (1 + fraction) X (2)exponent – bias = (-1)1 X (1 + 0.28125) X (2)131– 127

Value = 1.28125 X 24 = 20.5

Question 5. Analyze the IEEE 754 single-precision binary representation for the

decimal number 250.5.

Solution: (250.5)10 = (11111010.1)2

In scientific notation it will be written 1.11110101x 27

Floating point representation:

Sign bit = 0

Exponent =10000110 = 7
Mantissa = 11110101000000000000000 = 11110101

Biased exponent = 7 + 127 = 134 = 10000110

Question 6. Analyze the value of the IEEE 754 double-precision bit pattern

0x4022000000000000. What decimal number does it represent?

Solution: 0x4022000000000000 = 0100 0000 0010 0010 0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000

Floating point representation:

Sign bit= 0

Exponent = 10000000010 = 1026

Mantissa = 0010000000000000000000000000000000000000000000000000

Bias = 1023

Value = (-1)s X (1 + fraction) X (2)exponent – bias = (-1)0 X (1 + 0.125) X (2) 1026 – 1023

Value = 1.125 X 23 = 9.0

Question 7. Analyze how the IEEE 754 double-precision format encodes the decimal

number 3.14. Provide the bit pattern.

Solution:

Convert the decimal number 3.14 to binary:

(3.14)10= (11.00100011110101110001)2

Binary Representation: 3.14 ≈(11.00100011110101110001) × 2^1

Exponent = 1

Calculate the biased exponent by adding 1023 to the exponent.

Biased Exponent = 1 + 1023 = 1024

Sign Bit: 0

Convert the biased exponent to binary:


Biased Exponent = 10000000000

Mantissa = 100100011110101110001

Assemble the IEEE 754 Double-Precision Representation:

Sign Bit: 0

Biased Exponent: 10000000000

Mantissa: 10010001111010111000

Mantissa=10000000000000000000000000000000

Question 8. Analyze how 0x3FD8000000000000 is represented in IEEE 754 double

precision format. What decimal number does it correspond to?

Solution:

Convert the Hexadecimal to Binary:

The hexadecimal number is: 0x3FD8000000000000

Convert it to binary:

00111111 11011000 00000000 00000000 00000000 00000000 00000000 00000000

Sign Bit: The first bit is 0, so the number is positive.

Exponent: The next 11 bits are: 11111111100

Mantissa: The remaining 52 bits are: 1101100000000000000000000000000000000000000000000000

Exponent value = (Exponent) - Bias

= (11111111100) - 1023

= 1021

1021-1023= -2
Mantissa value = 0.5

Value = (-1)s X (1 + fraction) X (2)exponent – bias = (-1)0 X (1 + 0.125) X (2) 1021 – 1023

= 1.5X2-2=0.375

Question 9. Analyze the IEEE 754 double-precision representation for -0.001953125.

Provide both the binary and hexadecimal representations.

Solution:

Convert the Decimal Number to Binary:

(-0.001953125)10= (0.000000001)2

= 1.000000000X2-9

Sign bit=1

Exponent = -9

Mantissa=000000000

Biased Exponent:

Calculate the biased exponent by adding 1023 to the exponent.

Biased Exponent = -9 + 1023 = 1014

Convert the biased exponent to binary:

Biased Exponent = 01111110110

Sign Bit = 1 (since the number is negative)


Sign Bit: 1

Mantissa = 00000000000000000000

Question 10. Analyze a program that compares two single-precision floating-point

numbers and branches accordingly (i.e., greater than, less than, and equal to).

Solution:

.data

num1: .float 69.8

num2: .float 44.5

gt: .asciiz "Number 1 is greater than Number 2.\n"

lt: .asciiz "Number 1 is less than Number 2.\n"

eq: .asciiz "Number 1 is equal to Number 2.\n"

.text

.globl main

main:

la $t0, num1

lwc1 $f0, 0($t0) # Load num1 into $f0

la $t1, num2

lwc1 $f1, 0($t1) # Load num2 into $f1


c.lt.s $f0, $f1 # Compare $f0 < $f1

bc1t less_than # Branch if true (less than)

c.eq.s $f0, $f1 # Compare $f0 == $f1

bc1t equal # Branch if true (equal)

greater_than:

# Number 1 is greater than Number 2

la $a0, gt

li $v0, 4 # Syscall for print string

syscall

j end_program # Jump to end

less_than:

# Number 1 is less than Number 2

la $a0, lt

li $v0, 4 # Syscall for print string

syscall

j end_program # Jump to end

equal:

# Number 1 is equal to Number 2

la $a0, eq

li $v0, 4 # Syscall for print string

syscall

end_program:

li $v0, 10 # Exit syscall


syscall

Question 11. Analyze a program that finds the maximum of two double-precision

floating-point numbers using the c.le.d instruction.

Solution:

.data

num1: .double 69.8

num2: .double 44.5

a: .double 0.0 # for holding the maximum number

r: .asciiz "The maximum number is: "

.text

.globl main

main:

l.d $f0, num1 # Load num1 into $f0

l.d $f2, num2 # Load num2 into $f2

# Comparing $f0 and $f2


c.le.d $f0, $f2

# If $f0 <= $f2, $f2 is the max. else $f0 is the max

bc1t storing_f2 # Branch if condition is true (f0 <= f2)

storing_f0:

s.d $f0, a # Store $f0 in a if it is max

j result # Jump to print_result

storing_f2:

s.d $f2, a # Store $f2 in a if it is max

result:

li $v0, 4 # Load syscall code for printing string

la $a0, r # Load the address of the message

syscall

li $v0, 3 # Load syscall code for printing double

l.d $f12, a # Load the result into $f12

syscall

li $v0, 10 # Exit program

syscall
Question 12. Analyze a program that compares the absolute values of two single

precision floating-point numbers.

Solution:

.data

num1: .float 69.8 # First floating-point number

num2: .float 44.5 # Second floating-point number

num1gt: .asciiz "Number 1 has the greater absolute value."

num2gt: .asciiz "Number 2 has the greater absolute value."

same: .asciiz "Both have the same absolute value."

.text

.globl main

main:

l.s $f0, num1 # load num1 into $f0

l.s $f1, num2 # load num2 into $f1

abs.s $f2, $f0 # atoring the absolute value of num1 in $f2

abs.s $f3, $f1 # storing the absolute value of num2 in $f3


c.lt.s $f3, $f2 # checking if abs(num2) < abs(num1)

bc1t greater_abs_num1 # branch if true

c.eq.s $f3, $f2 # checking if abs(num2) == abs(num1)

bc1t equal_abs # branch if true

greater_abs_num2:

# if abs(num2) > abs(num1)

la $a0, num2gt

li $v0, 4

syscall

j exit

greater_abs_num1:

# if abs(num1) > abs(num2)

la $a0, num1gt

li $v0, 4

syscall

j exit

equal_abs:

# if abs(num1) == abs(num2)

la $a0, same

li $v0, 4

syscall

exit:
# Exit the program

li $v0, 10

syscall

Question 13. Analyze a program that finds how many single-precision floating-point

numbers in an array of 10 elements are greater than a given threshold of 3.0

Solution:

.data

array: .float 4.8, 8.5, 6.0, 0.8, 5.0, 6.7, 2.3, 9.5, 3.9, 2.3

threshold: .float 3.0

r: .asciiz "Number of elements greater than the threshold are: "

a: .word 0 # using this to store the count

.text

.globl main

main:

# initializing the pointers and counters

la $t0, array # loading base address of array into $t0

li $t1, 10 # seting array size for loop counter

li $t2, 0 # initializing counter for numbers > threshold

l.s $f1, threshold # load threshold value into $f1


l1:

beq $t1, 0, end_l # if counter reaches 0 exiting the loop

l.s $f0, 0($t0) # loading current array element into $f0

c.le.s $f0, $f1 # comparing $f0 <= $f1

bc1t skip # if true skiping the incrementing counter

addi $t2, $t2, 1 # increment counter for numbers > threshold

skip:

addi $t0, $t0, 4 # moving to the next element in the array

subi $t1, $t1, 1 # decrement counter

j l1 # repeating the loop

end_l:

# storing the result in memory

sw $t2, a

# printing the result

la $a0, r

li $v0, 4

syscall

lw $a0, a # loading a into $a0

li $v0, 1

syscall
li $v0, 10 # exiting the program

syscall

You might also like