0% found this document useful (0 votes)
38 views

COMP0068 Lecture10 High Level Data Types

The document discusses high-level data types including characters, strings, and real numbers represented in fixed point and floating point notation. It explains how these data types are represented and handled in computer systems using the MIPS architecture as an example. Key concepts covered include ASCII character encoding, null-terminated strings, binary and decimal conversions, and the IEEE 754 single-precision floating point format.

Uploaded by

h802651
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

COMP0068 Lecture10 High Level Data Types

The document discusses high-level data types including characters, strings, and real numbers represented in fixed point and floating point notation. It explains how these data types are represented and handled in computer systems using the MIPS architecture as an example. Key concepts covered include ASCII character encoding, null-terminated strings, binary and decimal conversions, and the IEEE 754 single-precision floating point format.

Uploaded by

h802651
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

COMP0068 Computer Architecture &

Operating Systems
Lecture 10: High-level Data Types
Background Reading:
Irv Englander (Chapter 3 & 5)
Harris & Harris Section 5.3
Lecture Overview
• We have looked at how MIPS assembly can process a
number of key data types:
– Signed and unsigned 32-bit integer words (integers).
– Bytes and half-words (shorts).
– Arrays of integers

• This lecture will look at how high-level data types are


handled within the computer:
– Characters and Strings
– Real numbers in fixed, and floating point notation.

• We will check that the data layout directives in MARS


actually lay out our data in the data segment as we
would expect.
• We will create a simple MIPS floating point calculator.
CHARACTER DATA

ASCII - American Standard Code


for Information Interchange
Represents Characters

7-bit code for teletype machines


form basis of many existing sets
Basic Unix character set.

NB: 0x30 = ‘0’, 0x31 = ‘1’


0x41 = ‘A’, 0x61 = ‘a’

Control Characters (00à1F) still used

HT (0916) (horizontal) tab character


LF (0A16) line feed character
CR (0D16) (carriage) return character
ESC (1B16) escape character
BS (0816) backspace character
CHARACTER DATA

Strings of Characters
• Python class “str” is internally complex and
employs Unicode characters.
• But C-language representation of a string is much
simpler:

– Assuming ASCII encoding Hello !


we have …
– Terminated with a
null character (0x00).

0x48 0x65 0x6c 0x6c 0x6f 0x20 0x21 0x00


– Test the MARS ‘.asciiz’ data directive …
does MARS correctly lay out the memory as expected ?
Recall converting binary to decimal …

2 symbols in binary – 0,1

11012 = 1.23 + 1.22 + 0.21 + 1.20 = 1310

5
And decimal to binary …
One way of converting from decimal to binary is by
first dividing by the largest power of two … and
then successively dividing by smaller ones …

Quotient
(Result)

MSB 1 0 1 LSB

22 ) 5 21 ) 1 20 ) 1 0

à 1012
Remainder
REAL NUMBERS

Representation of Real Numbers


The power series representation of integers can be extended to
provide for real numbers:-
n 3 2 1 0 -1 -2 -m
R = ... + sn2 + ... s32 + s22 + s12 + s02 + s-12 + s-22 +.... + s-m2 + ...

The series
-1 -2 -m
s-12 + s-22 + ............. + s-m2 + ...

provides the fractional part of the number


where s-1 s-2...s-m have to be binary digits equal to 0 or 1.

2-1 = 1/2 or 0.5 2-2 = 1/4 or 0.25 2-3 = 1/8 or 0.125

3.62510 = 11.1012

11.1012 = 1*2 + 1*1 + 1*0.5 + 0*0.25 + 1*0.125 = 3.62510


Decimal Real Numbersà Binary
Again fairly easy from binary to decimal:
11.1012 = 1*2 + 1*1 + 1*0.5 + 0*0.25 + 1*0.125 = 3.62510

Decimal to binary – do the same but continue when we get to ½ , ¼, etc.


5.7510 = (?)2

a) Conversion of the integer part: same as before – repeated division by


2
510 = 1012
b) Conversion of the fractional part: perform a repeated multiplication by
2 and extract the integer part of the result
0.75 x 2 = 1.5 Þ extract 1
0.5 x 2 = 1.0 Þ extract 1
0 Þ stop 0.7510 = 0.112

Combine the results from integer and fractional part, 5.7510 = 101.112
Fixed Point Representation of Real Numbers

This holds the n bit symbols surrounding the binary point


in an n-bit integer and uses integer ops to do maths

i.e. sn-m-1 ... s1 s0 · s-1 s-2 .. s-m

i.e. The real number 0 0 0 0 0 1.1 0 (decimal 1.5)


is treated like 0 0 0 0 0 1 1 0 (decimal 6 !)

The position of the fixed point is not recorded anywhere –


the software has to remember where it is.
Fixed Point Examples
Let register A hold 1.12 and register B hold 1.02

Assume 12-bit fixed point with 4 fractional bits:


point between bits 3 & 4
A = 000000011000 & B = 000000010000

position of fractional point


Result of integer multiply
A+B = A*B=
000000011000 000000011000
000000010000 000000010000
000000101000 000000000000000110000000
= 2.510
Need to shift right by 4 to lose last 4 zeroes
à 000000011000
REAL NUMBERS

2’s Complement of Real Numbers


Complement all the bits in the usual way and
then add 1 to the rightmost fractional bit

Example: 1.510 = 1.12

– 1.12 = –00001.10002 complement the bits

= 11110.01112 + 00000.00012
= 11110.10002
Check the result:- …000001.12
1.1 + (-1.1) = 0 …111110.12
…000000.0

ADDITION OF 1 GOES INTO LEAST SIGNIFICANT BIT


Going from fixed point real numbers to floating
point format numbers … reviewing Exponential
(or Scientific) Notation.
We know that a number like 12345 can be represented as:
12345 = 1.2345 * 104
Any number can be represented in exponential or scientific
notation:
exponent
(sign) (mantissa) * (base)
We use a base equal to 10 for decimal numbers.
We can always make the mantissa lie between 1.0
(inclusive) and 10.0 (exclusive) by adjusting the exponent
appropriately:
-0.0012345 = -1.2345 * 10-3
Exponential Notation for Binary Numbers

Analogous results apply for the binary … in this case the


base for the exponential notation is 2 :
4 –3
10010 = 1.0010 * 2 and –0.0011 = –1.1 * 2

Format needs to consider the sign of the number:


exponent
(sign) (mantissa) * 2

Again we can always make the mantissa lie between


1.0 and 2.0 (exclusive) by adjusting the exponent
appropriately … in this case (because it’s binary) …
the mantissa would always start with a 1 …unless?
Floating Point Representation of Real Numbers
IEEE 754 floating point standard represents a real number, X,
by putting X into the normalized form:
S E - Bias
X = (-1) * 1.F * 2 What ?

S is the sign bit:


S = 0 for a positive number and s = 1 for a negative number,
since (-1)0 = 1 while (-1)1 = -1
F is all the bits to the right of the leftmost 1 in the mantissa
(get greater accuracy from our bits if we assume a leading 1)
The Bias is fixed integer (127, 1023,..)
And (E – Bias) is the exponent.
Why this form?
S, E and F are used to represent X
Floating Point – IEEE single precision format

IEEE single precision is a 32-bit format that is equivalent


to the Java or C-language ‘float’ data type.

Sign is represented by a single bit (0=positive, 1=negative)


The exponent is a 8- bit number biased by 12710
The mantissa consists of 23- bits (with an implicit leading 1)

Sign Exponent Mantissa

S E F
1-bit 8-bits 23-bits
IEEE Single Precision Format Example
Turn –12.7510 into single precision form.

-12.7510 = -1100.112 = -1.100112* 23 = (-1)1 * 1.100112* 2130-127


not 2’s complement
S = 1 E = 13010 = 100000102 F = 1001100…00..0

S E F
1-bit 8-bits 23-bits

1 1000 0010 100 1100 0000 0000 0000 0000

è1100 0001 0100 1100 0000 0000 0000 0000


èC14C000016
The IEEE Single Precision Floating Point
Format is Complex – 5 Minute Activity

• Someone come up with a floating point number.


• Spend 5 minutes trying to come up with the IEEE
single precision 32-bit format of the floating point
number in hexadecimal.
• Work through the calculation on the whiteboard.

• We will then test your manual result using MARS


IEEE Single Precision range

• Smallest (absolute value) normalized number is:

1.00000000000000000000000 x 2-126
Min E = 0000 00012

• Largest normalized number is:

1.11111111111111111111111 x 2127 ~ 2 * 2127 = 2128


Max E = 1111 11102

Not all numbers can be handled in Normalised Form …


Denormalised Numbers: very small
numbers and zero YES – YES –
that’s zero plus one

Representation: X = (-1)S * 0.F * 2E+1-Bias : E=0


E = 0 identifies a denormalised number : F is 23-bits
Zero is: (-1)0 * 20+1-Bias * 0.000000000

S=0 E = 0 =000000002 F = 0000000…00..0

0 0000 0000 000 0000 0000 0000 0000 0000

Floating point zero is same as integer 0!


Normalised and denormalised numbers

• Denormalised numbers are used to :

1. Fill the gap between 0 and the smallest normalised float


2. Provide gradual underflow to zero
Special cases: Zero, Infinity, and NaN
Zero
• Exponent field E = 0 and F =0

Infinity
• Infinity is a special value represented with maximum E and
F =0
• For single precision with 8 bit exponent: maximum E= 255
• Infinity can result from overflow
• +infinity and –Infinity are possible according to sign bit S

NaN (Not a Number)


• NaN is a special value represented with maximum E and
F <> 0 (since 00.. represent infinity)
• Result from exceptional situations, such as 0/0 or sqrt(-ve)
• Operation on a NaN results is NaN: Op(X, NaN) = NaN
MIPS has a floating point co-processor
built in with 32 floating point registers
CPU MEMORY
Control Unit ALU
(Control Logic) Arithmetic
Logic Unit
Instruction Register (IR)
Floating Point
Coprocessor 1
Program Counter (PC)
Floating Point
General Purpose Registers
Registers

MIPS ASSEMBLY TO LOAD AND STORE VALUES


lwc1 $f1, 100($s2) Load word to coprocessor 1.
swc1 $f1, 100($s2) Store word from coprocessor 1.
MIPS Floating Point Instructions
Instruction Description
add.s $f2, $f4, $f6 FP add (single precision)
sub.s $f2, $f4, $f6 FP subtract (single precision)
mul.s $f2, $f4, $f6 FP multiple (single precision)
div.s $f2, $f4, $f6 FP divide (single precision)
add.d $f2, $f4, $f6 FP add (double precision)
sub.d $f2, $f4, $f6 FP subtract (double precision)
mul.d $f2, $f4, $f6 FP multiple (double precision)
div.d $f2, $f4, $f6 FP divide (double precision)

Floating point registers go from $f0 to $f31.


Double precision is 64-bit and uses 2 registers in a row
(hence the values $f0, $f2, $f4, etc. are only allowed)
Floating Point Calculator
# Floating-Point Calculator Version 2.0
.data
vals: .float 3.14, 10.0, 0.0
.text
.globl main
main:
la $4, vals # Load $4 with data address.
lwc1 $f0, 0($4) # Load first 32-bit float.
lwc1 $f1, 4($4) # Load second 32-bit float.
mul.s $f2, $f0, $f1 # Multiple floats.
swc1 $f2, 8($4) # Save result to memory.
li $v0 10 # Exit program using
syscall # syscall 10 (exit)

See how it works in MARS


Lecture Summary
• This lecture has been about storing different types of data
in memory including characters, strings and real numbers
(in both fixed point format and floating point format).
• We looked in detail at the IEEE single-precision format but
the double precision format is similar with more bits
available for mantissa and exponent.
• We have looked at normalized format (0 < E < 255) and
denormalized format (E = 0).
• We have looked at the representation of special cases:
zero, +/-infinity and NaN
• We introduced the FPU (Floating Point Unit) of the MIPS
CPU and creating a floating point calculator.

You might also like