1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10];...

46
1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0; j<10; j++) { if(a[j]==1) PORTT=0x04; else PORTT=0x00; } Array For Loop Example: 1. Initialize J 2. Compare J to 10 3. If Not Less than 10, 1. End Loop 4. Else 1. Load a[j] 2. If a[j] == 1 1. PORT T = 4 3. Else 1. PORT T = 0 4. Increment J 5. Repeat Loop (Step 2) Programming Steps:

description

3 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0; j

Transcript of 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10];...

Page 1: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

1

ECE 372 – Microcontroller Design Assembly Programming – Arrays

unsigned short a[10];for(j=0; j<10; j++){ if(a[j]==1) PORTT=0x04; else PORTT=0x00;}

Array For Loop Example:

1. Initialize J2. Compare J to 103. If Not Less than 10,

1. End Loop4. Else

1. Load a[j]2. If a[j] == 1

1. PORT T = 43. Else

1. PORT T = 04. Increment J5. Repeat Loop (Step

2)

Programming Steps:

Page 2: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

2

ECE 372 – Microcontroller Design Assembly Programming – Arrays

unsigned short a[10];for(j=0; j<10; j++){ if(a[j]==1) PORTT=0x04; else PORTT=0x00;}

Array For Loop Example:

1. Initialize J2. Compare J to 103. If Not Less than 10,

1. End Loop4. Else

1. Load a[j]2. If a[j] == 1

1. PORT T = 43. Else

1. PORT T = 04. Increment J5. Repeat Loop (Step 2)

Programming Steps:

Assembly Code: ldaa #0 ; Initialize J ldx #$3800 ; Initialize index to A[0]Loop: cmpa #10 ; Compare J to 10 bge EndLoop ; Else !(J<10) ldd 0,X ; load A[J] cpd #1 ; Compare J to 1 bne Else ; Else !(A[J]==1) ldab #4 ; Value to write to PORT T bra EndIf Else: ldab #0 ; Value to write to PORT TEndIf:stab PTT ; Write value to PORT T adda #1 ; Increment J inx ; Increment A[J] inx ; Need to increment by 2 bra Loop ; Repeat LoopEndLoop: ; do something else

Will this code work?NO! ldd overwrite value of JHow can we correct this?

Page 3: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

3

ECE 372 – Microcontroller Design Assembly Programming – Arrays

unsigned short a[10];for(j=0; j<10; j++){ if(a[j]==1) PORTT=0x04; else PORTT=0x00;}

Array For Loop Example:

1. Initialize J2. Compare J to 103. If Not Less than 10,

1. End Loop4. Else

1. Load a[j]2. If a[j] == 1

1. PORT T = 43. Else

1. PORT T = 04. Increment J5. Repeat Loop (Step 2)

Programming Steps:

Assembly Code: ldaa #0 ; Initialize J ldx #$3800 ; Initialize index to A[0]Loop: cmpa #10 ; Compare J to 10 bge EndLoop ; Else !(J<10) staa $3814 ; Store J to RAM ldd 0,X ; load A[J] cpd #1 ; Compare J to 1 bne Else ; Else !(A[J]==1) ldab #4 ; Value to write to PORT T bra EndIf Else: ldab #0 ; Value to write to PORT TEndIf:stab PTT ; Write value to PORT T ldaa $3814 ; Read J from RAM adda #1 ; Increment J inx ; Increment A[J] inx ; Need to increment by 2 bra Loop ; Repeat LoopEndLoop: ; do something else

We need to store value of J to memory and reload it when

needed.

Page 4: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

4

ECE 372 – Microcontroller Design Time for Fun (or maybe not?)

10 Gallon 7 Gallon 3 Gallon

Page 5: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

5

ECE 372 – Microcontroller Design Humor

There are 10 types of people in the world: Those who get binary and those who don’t.

Page 6: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

6

ECE 372 – Microcontroller Design Information vs. Data

Information An abstract description of facts, processes or perceptions How can we represent information? How can we represent changing information? We need to associate different values with different events

Data Individual fact value or set of facts or values Measurement or storage

September 19th

October 17, 2006$250

1.8 Trillion

Page 7: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

7

ECE 372 – Microcontroller Design Information vs. Data

Information vs. Data Information is data with context or meaning

September 19th

October 17, 2006$250

1.8 Trillion

International Talk Like a Pirate Day:Date of your first ECE 372 midterm:

How much Bill Gates earns per second:Number of pennies to fill Empire State

Building

Page 8: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

8

ECE 372 – Microcontroller Design Data Representation

Data Representation The same data can be represented with:

Different symbols English, Cyrillic, Arabic

Different numeric bases Binary, Octal, Hexadecimal, Decimal

Different formats Little Endian, Big Endian, Binary Coded Decimal

Page 9: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

9

ECE 372 – Microcontroller Design Data Structure (Encodings)

Thousands (10 3) columnHundreds (10 2) column

Tens (10 1) columnOnes (10 0) column

1999 10 = 1x10 3 + 9x10 2 + 9x10 1 + 9x10 0

Decimal Numbers Uses the ten numbers from 0 to 9 Each column represents a power of 10

Page 10: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

10

ECE 372 – Microcontroller Design Data Structure (Encodings)

Binary Numbers Uses the two numbers from 0 to 1 Every column represents a power of 2

10012= 1x2 3 + 0x2 2 + 0x2 1 + 1x2 0

Eights (2 3) columnFours (2 2) columnTwos (2 1) columnOnes (2 0) column

27 24 202126 25 23 22

0 0 100 0 1 0

Page 11: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

11

ECE 372 – Microcontroller Design Data Structure (Encodings)

Binary Numbers

27 24 202126 25 23 22

0 0 100 1 1 0

128 16 1264 32 8 40 0 100 1 1 0

= 0*27 + 0*26 + 1*25 + 0*24 + 1*23 + 0*22 + 0*21 + 1*20

= 0*128 + 0*64 + 1*32 + 0*16 + 1*8 + 0*4 + 0*2 + 1*1

= 41

Page 12: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

12

ECE 372 – Microcontroller Design Data Structure (Encodings)

Convert the following value from binary (zero’s and one’s) to a decimal value:

1001102

Choose your answer:A) 100,110B) 22C) 38D) 42

Page 13: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

13

ECE 372 – Microcontroller Design Data Structure (Encodings)

Converting from Decimal to Binary: Divide decimal number by 2 and insert remainder into

new binary number. Continue dividing quotient by 2 until the quotient is 0.

Example: Convert decimal number 12 to binary

12 divide by 226

-120

01

Decimal Number Binary Number

insert remainder

Continue dividing since quotient (6) is greater than 0

6 divide by 22-60

01

insert remainder

02

3

Continue dividing since quotient (3) is greater than 0

Page 14: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

14

ECE 372 – Microcontroller Design Data Structure (Encodings)

Example: Convert decimal number 12 to binary (cont.)

3 divide by 22

1

Decimal Number Binary Number

insert remainder

Continue dividing since quotient (1) is greater than 0

01

02

1

-2

14

1 divide by 22

1 insert remainder

01

02

14

0

-0

18

Since quotient is 0, we can conclude that 12 is 1100 in binary

Page 15: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

15

ECE 372 – Microcontroller Design Data Structure (Encodings)

Convert the following decimal value to a binary (zero’s and one’s) value:

Choose your answer:A) 110110B) 100010C) 1000010D) 10100100

5410

Page 16: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

16

ECE 372 – Microcontroller Design Data Structure (Encodings)

Generally, a number can be converted from one base to another by

Converting the number to base 10 Then, converting the base ten number to the desired base

using the divide-by-n method May not always be the easiest way…

Page 17: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

17

ECE 372 – Microcontroller Design Data Structure (Encodings)

Hexadecimal Numbers Uses the ten numbers from 0 to 9 and six letter A to

F Each column represents a power of 16

167 164 160161166 165 163 1620 0 5C0 0 A 0

Binary

Decimal

Hexadecimal

01

1011

100101110111

10001001101010111100110111101111

1000010001

0123456789

1011121314151617

0123456789ABCDEF??

Page 18: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

18

ECE 372 – Microcontroller Design Data Structure (Encodings)

167 164 160161166 165 163 1620 0 5C0 0 A 0 = A*163 + 0*162 + C*161 + 5*160

= 10*4096 + 0*256 + 12*16 + 5*1

= 41,15710

Page 19: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

19

ECE 372 – Microcontroller Design Data Structure (Encodings)

Hexadecimal Numbers Each position actually represents four base two

positions Used as compact means to write binary numbers Often called just hex

11110000

F 0

Binary

Decimal

Hexadecimal

01

1011

100101110111

10001001101010111100110111101111

1000010001

0123456789

1011121314151617

0123456789ABCDEF??

Page 20: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

20

ECE 372 – Microcontroller Design Data Structure (Encodings)

Convert the following hexadecimal value to a binary (zero’s and one’s) value:

Choose your answer:A) 110111101010B) 110001011001C) 110010101011D) 110010111010

CAB16

Page 21: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

21

ECE 372 – Microcontroller Design Big vs. Little Endian

Big Endian MSB (Most Significant Byte) is at lowest address 68000, MIPS, Sparc, HC12

Little Endian LSB (Least Significant Byte is at lowest address 80x86, DEC

$FFFE lower address

$FFFF higher address

7 0

16 Bit

MSB

LSB

$4000 $CC

LDD immediate addressing

$4001

$00

MSB of value to be stored in D

$4002

$00

LSB of value to be stored in D

$4003 $8C CPD immediate addressing$4004 $03 Compare to Value 1000$4005 $E8

Page 22: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

22

ECE 372 – Microcontroller Design Positive vs. Negative Numbers

Negative numbers are common How can we represent negative numbers in binary?

Signed-magnitude Use leftmost bit for sign bit

So -5 would be: 1101 One’s Complement

Invert all bits for negative numbers So -5 would be: 1010

Page 23: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

23

ECE 372 – Microcontroller Design Positive vs. Negative Numbers

Two’s Complement Allows us to perform subtraction using addition

No need for dedicated subtractor within CPU’s ALU Two’s complement of a number added to the number

itself will equal zero So -5 would be: 1011 10112 + 01012 = 00002 (with carry of 1, ignored)

Invert all bits and add 1 to get complement of a number Fast conversion: find first 1 from right, invert after 1

0011 1000 (56)+ 1111 0110 (-10)

0010 1110 (46)

Page 24: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

24

ECE 372 – Microcontroller Design Positive vs. Negative Numbers

+4+3+2+10-1-2-3-4

-0

110

100

010

001

011

101

11-

-0

110

100

010

001

101

011

00-

-0

110

100

010

001

111

101

011

00

DecimalSigned-

MagnitudeOne’s

ComplementTwo’s

Complement

Are we missing a value?

Page 25: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

25

ECE 372 – Microcontroller Design Positive vs. Negative Numbers

Data Ranges Unsigned

0 to 2n-1 Signed-Magnitude

-2n-1-1 to 2n-1-1 One’s Complement

-2n-1-1 to 2n-1-1 Two’s Complement

-2n-1 to 2n-1-1

Page 26: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

26

ECE 372 – Microcontroller Design Positive vs. Negative Numbers

Determine the two’s complement representation for the following decimal numbers (assume we are using 8-bit binary numbers):

-1 -11 -15 22 -101

Page 27: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

27

ECE 372 – Microcontroller Design Overflow

Overflow Occurs then a result cannot be

represented with given number of bits

Either the result is too large magnitude of positive or negative

Page 28: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

28

ECE 372 – Microcontroller Design Overflow

Detecting Overflow Can detect overflow by detecting when the two numbers’ sign

bits are the same but the result’s sign bit is different If the two numbers’ sign bits are different, overflow is impossible

Adding a positive and negative can’t exceed largest magnitude positive or negative

overflow overflow

0 1 1 1

1 0 0 0

+ 00 0 11 1 1 1

0 1 1 1

+ 01 0 01 0 0 0

1 1 1 1

+ 10 1 1

no overflow

Page 29: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

29

ECE 372 – Microcontroller Design Overflow

3

Page 30: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

30

ECE 372 – Microcontroller Design Binary Coded Decimal

Binary Coded Decimal Each digit of a decimal number is

represented as a 4-bit binary number Often used for 7-segment displays

Undefined

Undefined

Undefined

Undefined

Undefined

Undefined

101112131415

7447

A2

A0

A1

A3

1

10

0

RBO

RBI

LT

+VCC

+5V

abcdefg

0110000

dp

abcdefg

dp

Common-anodeLED display

+5V

7447

A2

A0

A1

A3

1

10

0

RBO

RBI

LT

+VCC

+5V

abcdefg

0110000

dp

abcdefg

dp

Common-anodeLED display

+5V

Binary BCD0000000100100011010001010110011110001001

0123456789

0001 0010 0011

12310 = 0001 0010 0011(BCD)

12310

0001 0010 0011

12310 = 0001 0010 0011(BCD)

12310

Page 31: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

31

ECE 372 – Microcontroller Design ASCII Characters

0123456789ABCDEF

0 1 2 3 4 5 6 7

Control characters

Printable characters (96)

Page 32: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

32

ECE 372 – Microcontroller Design Real Numbers

Real Numbers How can we represent a real number (i.e. numbers that

contain a fractional part)? Fixed Point Numbers Floating Point Numbers

Note: our C compiler already has built-in routines to deal with real numbers

However the computational needs will be significant.

Page 33: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

33

ECE 372 – Microcontroller Design Fixed Point Numbers

Fixed Point Numbers Real number with fixed number of digits before and

after radix point N-bits used to represent integer part M-bits used to represent fractional part Unsigned range: 0 to 2M+1/2N

23 20 2-42-322 21 2-1 2-2

0 0 001 0 0 1.

Page 34: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

34

ECE 372 – Microcontroller Design Fixed Point Numbers

23 20 2-42-322 21 2-1 2-20 0 001 0 0 1. = 0*23 + 1*22 + 0*21 + 0*20 +

0*2-1 + 1*2-2 + 0*2-3 + 0*2-4

8 1 1/161/84 2 1/2

1/4

0 0 001 0 0 1. = 0*8 + 1*4 + 0*2 + 0*1 + 0*.5 + 1*.25 + 0*.125 + 0*.0625

= 4.25

Page 35: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

35

ECE 372 – Microcontroller Design Fixed Point Numbers

23 20 2-42-322 21 2-1 2-21 1 100 1 1 0. = 1*23 + 0*22 + 1*21 + 1*20 +

1*2-1 + 0*2-2 + 0*2-3 + 1*2-4

8 1 1/161/84 2 1/2

1/4

1 1 100 1 1 0. = 1*8 + 0*4 + 1*2 + 1*1 + 1*.5 + 0*.25 + 0*.125 + 1*.0625

= 11.5625

Page 36: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

36

ECE 372 – Microcontroller Design Fixed Point Numbers

23 20 2-42-322 21 2-1 2-21 1 101 1 1 1. = 1*23 + 1*22 + 1*21 + 1*20 +

1*2-1 + 1*2-2 + 0*2-3 + 1*2-4

8 1 1/161/84 2 1/2

1/4

1 1 101 1 1 1. = 1*8 + 1*4 + 1*2 + 1*1 + 1*.5 + 1*.25 + 0*.125 + 1*.0625

= 15.8125

0100.0100 (4.25)+ 1011.1001 (11.5625)

1111.1101

Page 37: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

37

ECE 372 – Microcontroller Design Floating Point Numbers

Floating Point Numbers Real number representation similar to scientific notation

x = M * BE

Base (B) Base of the numbering systems considered Binary (2) for computer based implementations We will assume base of 2 for remaining description

Sign (S) Indicating positive of negative number 0 – Positive, 1 – Negative

238131 0

S E M

Page 38: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

38

ECE 372 – Microcontroller Design Floating Point Numbers

Floating Point Numbers Real number representation similar to scientific notation

x = M * 2E

Mantissa (M) Digits corresponding to the magnitude Stored in a normalized form, where the first bit is assumed to be 1

1 is the only possible non-zero number in binary Remaining bits correspond to fraction values (similar to fixed

point)

238131 0

S E M

20 2-3 2-232-222-1 2-2 2-211 …

Page 39: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

39

ECE 372 – Microcontroller Design Floating Point Numbers

Floating Point Numbers Real number representation similar to scientific notation

x = M * 2E

Exponent (E) Needs to represent both positive and negative values Stored exponent is adjust using the exponent bias

Exponent bias = 2N-1-1, where N is the number of bits in the exponent EActual = EStored – EBias

Example, 8-bit exponent: EBias = 28-1-1 = 127 EStored = 1000 00002 = 128 EActual = 128 - 127 = 1

238131 0

S E M

Page 40: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

40

ECE 372 – Microcontroller Design Floating Point Numbers

Floating Point Numbers Example: Convert the value −118.625 to floating point

representation 1. Determine sign bit:

-118.625 is negative, S = 1 2. Convert to binary:

118.625 = 1110110.101

3. Normalize number 1110110.101 = 1.110110101 * 26

4. Determine exponent EStored = EActual + EBias EStored = 6 + 127 = 13310 = 100001012

31 0

S E M1

118 .625

Mantissa

1101101010000000000000010000101

Page 41: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

41

ECE 372 – Microcontroller Design Floating Point Numbers

Floating Point Numbers Real number representation similar to scientific notation

x = M * 2E

Zero Due to assuming a leading 1 in the mantissa, we cannot directly represent

the value 0 using floating point Defined special case for value of 0 Define special case: Exponent and mantissa of all 0’s corresponds to the

value 0

Other special cases exist: +/- Infinity Denormalized value Not a Number

238131 0

S E M

Page 42: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

42

ECE 372 – Microcontroller Design Floating Point Numbers

Floating Point Numbers IEEE Standard for Binary Floating-Point Arithmetic (IEEE 754) Single Precision Floating Point

Double Precision Floating Point

238131 0

S E M

5211163 0

S E M

Page 43: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

43

ECE 372 – Microcontroller Design Data vs. Information

What does the bit pattern mean: 1011 1001 Unsigned: 185 decimal Sign-Magnitude: -57 decimal 1’s complement: -70 decimal 2’s complement: -71 decimal Fixed point 4bit.4bit: 11.5625 decimal BCD:

10112 =B (Undefined) 10012 = 910

ASCII: ’9’ HC12 Opcode: $39 = ADCA (Add with Carry to A, Ext.

Addressing)

Page 44: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

44

ECE 372 – Microcontroller Design Information Representation

Text Page 1 page letter 8.5x11 inches, 60x68 12 point characters ASCII 8bit: 60x68x8 = 32640 bits 4 Kbytes

Fax Page Scan Fax low resolution 204x98 dots/inch 8.5x204x11x98 = 1869252 dots 235 Kbytes (B/W)

DVD 8Mbit/sec 2 hours 2x60x60x8 = 57600 Mbit Approximately 7.2 Gbytes

Page 45: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

45

ECE 372 – Microcontroller Design Information Representation

Digital Audio 44.2kHz sampling rate 16bit/channel 1..65536=2^16 Stereo How many seconds of sound fit in

32kbyte EPROM mono? 1.4 Mbyte floppy mono? 660Mbyte CD Rom stereo?

Page 46: 1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0;…

46

ECE 372 – Microcontroller Design Humor