boolean algebra discrete mathematics and its applications baojian hua [email protected]

41
Boolean Algebra Discrete Mathematics a nd Its Applications Baojian Hua [email protected]

Post on 21-Dec-2015

227 views

Category:

Documents


0 download

TRANSCRIPT

Boolean Algebra

Discrete Mathematics andIts Applications

Baojian [email protected]

Outline

Data representation Program representation Program execution Final goal: Design your own CPU

programming assignment #5

Number System Decimal

digit 0~9 Ex: 21

Binary digit 0 or 1

Ex: 00010101 (8-bit representation of 21) Hexadecimal

digit 0~9, letter a~f Ex: 15h (or 0x15)

Memory and Address

Computer memory could be think of as a big array

Memory and Address

Computer memory could be think of as a big array each slot has

a continuous address

0

1

2

3

4

Memory and Address Computer

memory could be think of as a big array each slot has a

continuous address

each slot is divided into 8 bits (a byte)

0

1

2

3

4

Ex: Store 21

The storage of integer number of 21 in a single byte!

0

1

2

3

4

01234567

10 0 0 1 010

Ex: Store 21 The storage of

integer number of 21 in a single byte! However, most

current machines are called “32-bit”, what does this mean?

0

1

2

3

4

01234567

10 0 0 1 010

Ex: Store 21 The storage of

integer number of “21” in a single byte! However, most

current machines are 32-bits, what does this mean?

Answer: an integer value occupies 32-bit (or 4 bytes)

0

1

2

3

4

01234567

10 0 0 1 010

Ex: Store 21 “21” in 32-bit:

00000000 00000000 00000000 00010101 how to store

this 32-bit long binary number in memory?

0

1

2

3

4

01234567

Big-endian and Little-endian Big-endian and little-endian are name

d after the convention of storing multibyte data in memory

Big-endian: most-significant byte at low address Ex: Sun Sparc

Little-endian: reverse Ex: Pentium

Big-endian and Little-endian

“21” in 32-bit: 00000000 00000000 00000000 00010101 Little-endian

in the right figure

See demo…

0

1

2

3

4

01234567

0

0

0 0 1 0 1 0 1

0 0 0 0 0 0 0

0 0 0 00 0 0 0

0 0 0 0 0 0 0 0

Negative Integers Two’s complement:

absolute value one’s complement add 1

Ex: -21 00000000,00000000,00000000,00010101 11111111,11111111,11111111,11101010 11111111,11111111,11111111,11101011

Moral: Data in Machine Data at the machine level has no

assumed meaning Ex: 11111111 255? or -1? or …?

Slogan: meaning = bits + context bits: the data context: how this data is interpreted used

We’d see more exciting examples below

Sign Extension

Sign extension happens when data sizes are changed: decrease increase

Consider the rightwhen 8<==>32

0

1

2

3

4

01234567

0

0

0 0 1 0 1 0 1

0 0 0 0 0 0 0

0 0 0 00 0 0 0

0 0 0 0 0 0 0 0

Size Decrease For unsigned numbers, all the deleted

significant bits must be “0” Ex: 003fh ==> 3fh Ex: 103fh ==> 3fh (wrong)

For signed numbers: all the deleted bits must be “0”, and the

new leading bit is also “0” all the deleted bits must be “1”, and the

new leading bit is also “1” Ex: 003fh ==> 3fh Ex: ff3fh ==> 3fh (wrong)

Size Increase For unsigned numbers, all the inserted

bits must be “0” Ex: 3fh ==> 003fh Ex: f3h ==> 00f3h

For signed numbers: all the inserted bits must be same with leading bit Ex: 3fh ==> 003fh Ex: f3h ==> fff3h

Important: machine does not under this

Example from Cunsigned char uc = 0xff;signed char sc = 0xff;

printf (“%d\n", uc);printf ("%d\n", sc); int ui = (int) uc;int si = (int) sc;

printf ("%d\n", ui);printf ("%d\n", si);

Typical Pitfalls in Cint fgetc (FILE *stream);// fgetc returns the next character of stream as// an unsiged char (converted to an int), or EOF// if end of file or error occurs// Note: EOF is typical -1

char c;while ((c=fgetc(file))!=EOF){ …;}

// Is this right?

Program Representation

What the binary form of program look like? A program in binary form is also just a ser

ies of 0 and 1 (as we expect) typical in readable and friendly assembly

form next, we turn back to our miniPentium la

nguage in programming assignment #2

miniPentiumprog -> instruction prog | instruction instruction -> movl v, id | addl v, id | subl v, id | mull v, id | print v v -> id | num

// Sample program:

movl 1, x

movl 2, y

addl x, y

print y

But how to represent all these on a machine? (who only knows about binary)

Binary Encodingprog -> instruction prog | instruction instruction -> movl v, id | addl v, id | subl v, id | mull v, id | print v v -> id | num

Our goal is to put these instructions in a series of types. For simplicity, we assume a single byte.

01234567

Binary Encodingprog -> instruction prog | instruction instruction -> movl v, id | addl v, id | subl v, id | mull v, id | print v v -> id | num

Our goal is to put these instructions in a series of types. For simplicity, we assume a single byte.

01234567

a[0]~a[2]movl: 000addl: 001subl: 010mull: 011print: 100

Binary Encodingprog -> instruction prog | instruction instruction -> movl v, id | addl v, id | subl v, id | mull v, id | print v v -> id | num

Our goal is to put these instructions in a series of types. For simplicity, we assume a single byte.

01234567

a[0]~a[2]:movl: 000addl: 001subl: 010mull: 011print: 100

a[3]:id: 0num: 1

Binary Encodingprog -> instruction prog | instruction instruction -> movl v, id | addl v, id | subl v, id | mull v, id | print v v -> id | num

Our goal is to put these instructions in a series of types. For simplicity, we assume a single byte.

01234567

a[0]~a[2]:movl: 000addl: 001subl: 010mull: 011print: 100

a[3]:id: 0num: 1

a[4]~a[5]:id: addressnum: value

Binary Encodingprog -> instruction prog | instruction instruction -> movl v, id | addl v, id | subl v, id | mull v, id | print v v -> id | num

Our goal is to put these instructions in a series of types. For simplicity, we assume a single byte.

01234567

a[0]~a[2]:movl: 000addl: 001subl: 010mull: 011print: 100

a[3]:id: 0num: 1

a[4]~a[5]:id: addressnum: value

a[6]~a[7]:id:

address

Binary Encodingprog -> instruction prog | instruction instruction -> movl v, id | addl v, id | subl v, id | mull v, id | print v v -> id | num

Exercise: represent this:movl 1, xmovl 2, yaddl x, yprint y

01234567

a[0]~a[2]:movl: 000addl: 001subl: 010mull: 011print: 100

a[3]:id: 0num: 1

a[4]~a[5]:id: addressnum: value

a[6]~a[7]:id:

address

Binary Encodingprog -> instruction prog | instruction instruction -> movl v, id | addl v, id | subl v, id | mull v, id | print v v -> id | num

Exercise: x@0, y@1movl 1, xmovl 2, yaddl x, yprint y

01234567

a[0]~a[2]:movl: 000addl: 001subl: 010mull: 011print: 100

a[3]:id: 0num: 1

a[4]~a[5]:id: addressnum: value

a[6]~a[7]:id:

address

Binary Encodingprog -> instruction prog | instruction instruction -> movl v, id | addl v, id | subl v, id | mull v, id | print v v -> id | num

Exercise: x@0, y@1movl 1, xmovl 2, yaddl x, yprint y

01234567

a[0]~a[2]:movl: 000addl: 001subl: 010mull: 011print: 100

a[3]:id: 0num: 1

a[4]~a[5]:id: addressnum: value

a[6]~a[7]:id:

address

Solution:00011000011010000100000100010100

Binary Encodingprog -> instruction prog | instruction instruction -> movl v, id | addl v, id | subl v, id | mull v, id | print v v -> id | num

Exercise: x@0, y@1movl 1, xmovl 2, yaddl x, yprint y

01234567

a[0]~a[2]:movl: 000addl: 001subl: 010mull: 011print: 100

a[3]:id: 0num: 1

a[4]~a[5]:id: addressnum: value

a[6]~a[7]:id:

address

Solution:00011000011010000100000100010100

Put all these In Memory// x@0, y@1movl 1, xmovl 2, yaddl x, yprint y

// binary form00011000011010000100000100010100

01234567

0

1

2

3

4

5

6

7

8

Put all these In Memory// x@0, y@1movl 1, xmovl 2, yaddl x, yprint y

// binary form00011000011010000100000100010100

# # # # # # # #

01234567

# # # # # # # #

0 0 0 1 1 0 0 0

0 1 1 0 1 0 0 0

0 1 0 0 0 0 0 1

0 0 0 1 0 1 0 0

0

1

2

3

4

5

6

7

8

Adding a CPUmovl 1, xmovl 2, yaddl x, yprint y

# # # # # # # #

01234567

# # # # # # # #

0 0 0 1 1 0 0 0

0 1 1 0 1 0 0 0

0 1 0 0 0 0 0 1

0 0 0 1 0 1 0 0

0

1

2

3

4

5

6

7

8

Adding a CPUmovl 1, xmovl 2, yaddl x, yprint y

0 0 0 0 0 0 0 1

01234567

# # # # # # # #

0 0 0 1 1 0 0 0

0 1 1 0 1 0 0 0

0 1 0 0 0 0 0 1

0 0 0 1 0 1 0 0

0

1

2

3

4

5

6

7

8

Adding a CPUmovl 1, xmovl 2, yaddl x, yprint y

0 0 0 0 0 0 0 1

01234567

0 0 0 0 0 0 1 0

0 0 0 1 1 0 0 0

0 1 1 0 1 0 0 0

0 1 0 0 0 0 0 1

0 0 0 1 0 1 0 0

0

1

2

3

4

5

6

7

8

Adding a CPUmovl 1, xmovl 2, yaddl x, yprint y

0 0 0 0 0 0 0 1

01234567

0 0 0 0 0 0 1 1

0 0 0 1 1 0 0 0

0 1 1 0 1 0 0 0

0 1 0 0 0 0 0 1

0 0 0 1 0 1 0 0

0

1

2

3

4

5

6

7

8

Adding a CPUmovl 1, xmovl 2, yaddl x, yprint y// print “3” on screen

0 0 0 0 0 0 0 1

01234567

0 0 0 0 0 0 1 1

0 0 0 1 1 0 0 0

0 1 1 0 1 0 0 0

0 1 0 0 0 0 0 1

0 0 0 1 0 1 0 0

0

1

2

3

4

5

6

7

8

Some Real World Issues The binary encoding strategy for miniPenti

um discussed above is far from real Pentium Pentium is 32-bit instruction is of variant-length (1-6 bytes) more complex addressing mode abundant instructions (700+ pages) …

However, the key idea is essential

CISC and RISC Complex Instruction Set Computer:

designed in 60’-70’ last century popular in desktop applications Ex: Intel’ Pentium

Reduced Instruction Set Computer: Uniform instruction representation (just as w

e’ve done for miniPentium) relatively few instructions (<100) simple addressing mode designed from 80’, popular in embedded area Ex: MIPS, ARM, etc

Programming Assignment #4:CPU Design and Implementation Design a binary encoding strategy to encod

e the miniPentium (in 32-bit) Use your favorite language, implement the

memory. Make the following assumption to simplify your life: integers unsigned memory infinite data and program isolated

Write a function to make your CPU run write some test programs (binaries by hand)

Programming Assignment #4:CPU Design and Implementation

Connect your CPU with the miniVC compiler in the programming assignment #3 write programs in miniC miniVC compiles miniC to mimiPentium write an assembler assembles into binari

es run your CPU