Lab 2: Real Number Representation OBJECTIVES This lab asks the student to implement positive real number representation in binary. The final deliverable is a program that is able to represent a...

1 answer below »







Lab 2: Real Number Representation OBJECTIVES This lab asks the student to implement positive real number representation in binary. The final deliverable is a program that is able to represent a positive real number without using float coprocessor. Background Algorithm for translating real numbers into fixed decimal notation: A) Translate 6.3578 into fixed decimal notation using a shift of 4 bits. 6.3578 x 2^4 gives 101.7248. Truncate it to just the integer part, giving 101. Now if that is translated into binary, the equivalent binary number is 0110 0101. Keep in mind that we moved the decimal place by 4, so it needs to be noted in our binary number. When we add the radix point, the binary number fixed representation becomes 0110.0101. Translating that back to decimal gives 6.375. This is due to our low resolution of 4 bits. B) What if the resolution was doubled to 8 bits instead of 4 bits? So instead of 2^4, multiply by 2^8. 6.3578x2^8 = 1627.5968. Truncating the decimal portion, becomes 1627. The binary equivalent is 0110 0101 1011. After adding decimal point, binary becomes 0110.0101 1011. See how the resolution increased on the fractional portion of the number (extra four bits at the end)? Now, if you convert it back into base 10, the number is 6.35546875. This is closer to the original number than the 4 bit resolution. Another name for this technique is called the Q number format. This is used in many digital signals processing (DSP) applications. Why does this algorithm work? What is essentially happening is that we are taking advantage of bit pattern. Reasoning The algorithm is taking advantage of three properties. 1) A number has something called a bit pattern in binary. For example, the bit pattern for 4 is “1000”. Bit pattern for 23 is “0001 0111”. 2) Imagine for integers, there is a radix point trailing each bit pattern, followed by 0’s. For example, 4 is “1000.0000”, and 23 is “0001 0111.0000”. 3) In base 2, shifting right divides by two. For example, shifting “0001 0111.0000”, which represents 23, to the right by one will get the result of “0000 1011.1000”. Note the bit pattern stays the same! However, the value of each bit changes due to the position in relation to the radix point. In this case, converting it back to base 10 will give you 11.5. In the aforementioned algorithm, because there is no way to input into binary fractional without using floating point, the radix point is implicit in the binary bits. For example: 2^3 2^2 2^1 2^0 Radix point 2^-1 2^-2 2^-3 2^-4 0 1 0 0 . 1 1 0 0 Figure 1a 2^3 2^2 2^1 2^0 2^-1 2^-2 2^-3 2^-4 Radix point 0 1 0 0 1 1 0 0 . Figure 1b 4.75 would have the bit pattern of 0100.1100 To the computer, this data is 01001100 (Figure 1a), but to the application this represents 4.75 (Figure 1b), because the application designer designed the notational scheme as 4.4. Since the inputs are represented in the computer as integer numbers, but really represent fractional numbers, fractional inputs have to be scaled. So for example, 4.75 gets scaled to 475. But this presents a problem. This bit pattern 475 is: 2^11 2^10 2^9 2^8 2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0 Radix point 0 0 0 1 1 1 0 1 1 0 1 1 . Figure 2 This bit pattern is different from the bit pattern in figure 1. This is because we scaled our input so that we can input the numbers as integer. In order to get the correct bit pattern, the scaled number needs to be scaled down using base 10^n, where n is the number of times we scaled by 10. However, if you just scale 475 bit pattern in figure 2 by integer division, then the resultant would just be 4, losing all information about the fractional parts. 2^11 2^10 2^9 2^8 2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0 Radix point 0 0 0 0 0 0 0 0 0 1 0 0 . Figure 3 What needs to be done is to shift the number by a certain number of bits so that more resolution is captured. If 475 is shifted by 4 spaces, it becomes: 2^15 2^14 2^13 2^12 2^11 2^10 2^9 2^8 2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0 Radix point 0 0 0 1 1 1 0 1 1 0 1 1 0 0 0 0 . Figure 4 Figure 3 still shows the incorrect bit pattern, however, now if we divide by 10^2, we will get some of the resolution. The bit pattern that we get after dividing by 10^2 is shown in figure 4. Bits 0-3 represent the fractional 0.75 and bits 4-7 represent the integer number 4. 2^3 2^2 2^1 2^0 2^-1 2^-2 2^-3 2^-4 0 1 0 0 1 1 0 0 Figure 5 Practice Familiarize yourself with shortcut algorithm by practicing the following problems. Convert each into binary by using two different methods. First, use Binary Coded Decimal. Then convert using Fixed Point Decimal. 1) 6.75 2) 1.0625 3) 1.7 For fixed point decimal, terminate this after 10 places. Convert this back to decimal. Compare it with the original number. 4) 1.2 Code Implement the following: a) Ask the user to input the number in two stages. Have the user input the numbers to the right of the decimal and the numbers without the decimal point. For example, 6.35 would be input 1 of 35 and input 2 of 635 (note: you will need to figure out how many decimal places the radix point moved for the second input in base 10. This is needed to perform the correct truncation. In this example, the decimal moved two spaces to the right, so once you shift the number by the resolution value, you’ll need to divide the shifted value by 10^2 to get the correct value). Convert user’s number into a fixed decimal notation in which 16 bits are used to store the integer portion of the number and 16 bits are used to store the fractional portion. For example, decimal number 3.25 will be store as the following format: 0000 0000 0000 0011 . 0100 0000 0000 0000 Note: The integer portion is stored in the least significant portion of the upper 16 bits. The fractional portion is stored in the most significant portion of the lower 16 bits. Also note, ignore the case where there is a leading 0. For example a number like 3.05. Part 1: Get user input and change the number into fixed decimal notation. Set the fractional resolution equal to the number of places that the number has for its fractional portion. You will still use 16 bits to hold that information, but the resolution will change according to the number. For example, 5.64 would use resolution of 2 bits to convert, but would still use 16 bits to hold the fractional information. 5.764 would use 3 bits to convert, but would still use 16 bits to hold the fractional information. Part 2: Get user input and change the number into fixed decimal notation. Set the fractional resolution equal set 8 bits no matter what the user input is. b) Implement adding functionality by using your method for two user inputs. Once they are in fixed decimal notation, add the two inputs. Display the output on the screen. RUBRIC (Total 100 Points) Points Description 30 Practice 15 Getting user input 15 Part A1: Converting fractional portion according to the fractional resolution of the inputted number 15 Part A2: Converting fractional portion according to the fixed resolution of 16 bits 15 Part B adding 10 Part B displaying Bonus: Change user input to string and parse that information instead of having the two inputs. 4 COA – Game Dev and Comp Sci Degree Program – Full Sail University
Answered Same DayNov 14, 2021

Answer To: Lab 2: Real Number Representation OBJECTIVES This lab asks the student to implement positive real...

Gaurav answered on Nov 17 2021
137 Votes
.data
inputString: .space 16
tmpBuffer: .space 16
floatOne: .space 4
floatTwo: .space 4
errorString: .asciiz "Invalid Number format Kindly check\n\r"
prompt:    
.asciiz "Enter the Number : "
string:     .asciiz "6.0128125"
binary:     .asciiz "Binary : "
decimal: .asciiz "Decimal : "
addition: .asciiz "Addition\n\r"
newline: .asciiz "\n\r"
    .text
main:
    # get first number
    la    $a0, prompt
    li    $v0, 4
    syscall
    
    la     $a0, inputString
    li    $a1, 16
    li    $v0, 8
    syscall
    
    jal     convert
    move     $s0, $v0
    move    $a0, $s0
    jal     displayBinary    
    
    move    $a0, $s0
    jal     displayDecimal
    
    la    $a0, newline
    li    $v0, 4
    syscall
    # get Second Number
    la    $a0, prompt
    li    $v0, 4
    syscall
        
    la     $a0, inputString
    li    $a1, 16
    li    $v0, 8
    syscall
    
    jal     convert
    move     $s1, $v0
    
    move    $a0, $s1
    jal     displayBinary
        
    move    $a0, $s1
    jal     displayDecimal
    la    $a0, newline
    li    $v0, 4
    syscall
    
    # add two numbers and display the result
    add    $s3, $s0, $s1
    la    $a0, addition
    li    $v0, 4
    syscall    
    
    move    $a0, $s3
    jal     displayBinary
        
    move    $a0, $s3
    jal     displayDecimal         
    
    
    li    $v0, 10
    syscall
# Convert the given String to fixed Decimal Point
# $a0 - address of the string
# $v0 - fixed decimal point integer
                 
convert:
    sw     $ra, 0($sp)        # store the return address
    addi     $sp, $sp, -4
    
    la    $t0, 1
    li    $t1, 0    
    li    $t4, 0
        
    move     $t2, $a0        # address of the string
    
strEnd:
    lb    $t3, 0($t2)        # get each byte from the string
    addi    $t2, $t2, 1        # increment the address pointer
    bne     $t3, 0x0A, strEnd    # find the end of the string
    addi    $t2, $t2, -1        # address of the delimiting character
    addi    $t1, $t2, -1        #
    
loop:        
    addi    $t2, $t2, -1        # address of...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here