Presentation is loading. Please wait.

Presentation is loading. Please wait.

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; }

Similar presentations


Presentation on theme: "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; }"— Presentation transcript:

1 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:

2 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 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: 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 T EndIf:stab PTT ; Write value to PORT T adda #1 ; Increment J inx ; Increment A[J] inx ; Need to increment by 2 bra Loop ; Repeat Loop EndLoop: ; do something else Will this code work? NO! ldd overwrite value of J How can we correct this?

3 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 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: 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 T EndIf: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 Loop EndLoop: ; do something else We need to store value of J to memory and reload it when needed.

4 4 ECE 372 – Microcontroller Design Time for Fun (or maybe not?) 10 Gallon7 Gallon3 Gallon

5 5 ECE 372 – Microcontroller Design Humor There are 10 types of people in the world: Those who get binary and those who don’t.

6 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 19 th October 17, 2006 $250 1.8 Trillion

7 7 ECE 372 – Microcontroller Design Information vs. Data Information vs. Data Information is data with context or meaning September 19 th 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

8 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

9 9 ECE 372 – Microcontroller Design Data Structure (Encodings) Decimal Numbers Uses the ten numbers from 0 to 9 Each column represents a power of 10

10 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 2727 2424 2020 2121 2626 2525 23232 00100010

11 11 ECE 372 – Microcontroller Design Data Structure (Encodings) Binary Numbers 2727 2424 2020 2121 2626 2525 23232 00100110 1281612643284 00100110 = 0*2 7 + 0*2 6 + 1*2 5 + 0*2 4 + 1*2 3 + 0*2 2 + 0*2 1 + 1*2 0 = 0*128 + 0*64 + 1*32 + 0*16 + 1*8 + 0*4 + 0*2 + 1*1 = 41

12 12 ECE 372 – Microcontroller Design Data Structure (Encodings) Convert the following value from binary (zero’s and one’s) to a decimal value: 100110 2 Choose your answer: A) 100,110 B) 22 C) 38 D) 42

13 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 2 2 6 -12 0 0 1 Decimal NumberBinary Number insert remainder Continue dividing since quotient (6) is greater than 0 6 divide by 2 2 -6 0 0 1 insert remainder 0 2 3 Continue dividing since quotient (3) is greater than 0

14 14 ECE 372 – Microcontroller Design Data Structure (Encodings) Example: Convert decimal number 12 to binary (cont.) 3 divide by 2 2 1 Decimal NumberBinary Number insert remainder Continue dividing since quotient (1) is greater than 0 0 1 0 2 1 -2 1 4 1 divide by 2 2 1 insert remainder 0 1 0 2 1 4 0 -0 1 8 Since quotient is 0, we can conclude that 12 is 1100 in binary

15 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) 110110 B) 100010 C) 1000010 D) 10100100 54 10

16 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…

17 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 16 7 16 4 16 0 16 1 16 6 16 5 16 3 16 2 005C00A0 BinaryDecimalHexadecimal 0 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000 10001 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0123456789ABCDEF??0123456789ABCDEF??

18 18 ECE 372 – Microcontroller Design Data Structure (Encodings) 16 7 16 4 16 0 16 1 16 6 16 5 16 3 16 2 005C00A0 = A*16 3 + 0*16 2 + C*16 1 + 5*16 0 = 10*4096 + 0*256 + 12*16 + 5*1 = 41,157 10

19 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 F0 BinaryDecimalHexadecimal 0 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000 10001 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0123456789ABCDEF??0123456789ABCDEF??

20 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) 110111101010 B) 110001011001 C) 110010101011 D) 110010111010 CAB 16

21 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$CCLDD immediate addressing $4001$00MSB of value to be stored in D $4002$00LSB of value to be stored in D $4003$8CCPD immediate addressing $4004$03Compare to Value 1000 $4005$E8 …

22 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

23 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 1011 2 + 0101 2 = 0000 2 (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)

24 24 ECE 372 – Microcontroller Design Positive vs. Negative Numbers +4 +3 +2 +1 0 -2 -3 -4 - 0 11 0 10 0 01 0 00 1 01 1 10 1 11 - 0 11 0 10 0 01 0 00 1 10 1 01 1 00 - 0 11 0 10 0 01 0 00 1 11 1 10 1 01 1 00 Decimal Signed- Magnitude One’s Complement Two’s Complement Are we missing a value?

25 25 ECE 372 – Microcontroller Design Positive vs. Negative Numbers Data Ranges Unsigned 0 to 2 n -1 Signed-Magnitude -2 n-1 -1 to 2 n-1 -1 One’s Complement -2 n-1 -1 to 2 n-1 -1 Two’s Complement -2 n-1 to 2 n-1 -1

26 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): -11 -15 22 -101

27 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

28 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 0111 1000 +0001 1111 0111 +0100 1000 1111 +1011 no overflow

29 29 ECE 372 – Microcontroller Design Overflow 3

30 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 10 11 12 13 14 15 BinaryBCD 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 01234567890123456789

31 31 ECE 372 – Microcontroller Design ASCII Characters 0123456789ABCDEF0123456789ABCDEF 0 1 2 3 4 5 6 7 Control charactersPrintable characters (96)

32 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.

33 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 2 M + 1 / 2 N 2323 2020 2 -4 2 -32 2121 2 -1 2 -2 00001001.

34 34 ECE 372 – Microcontroller Design Fixed Point Numbers 2323 2020 2 -4 2 -32 2121 2 -1 2 -2 00001001. = 0*2 3 + 1*2 2 + 0*2 1 + 0*2 0 + 0*2 -1 + 1*2 -2 + 0*2 -3 + 0*2 -4 81 1 / 16 1/81/8 42 1/21/2 1/41/4 00001001. = 0*8 + 1*4 + 0*2 + 0*1 + 0*.5 + 1*.25 + 0*.125 + 0*.0625 = 4.25

35 35 ECE 372 – Microcontroller Design Fixed Point Numbers 2323 2020 2 -4 2 -32 2121 2 -1 2 -2 11100110. = 1*2 3 + 0*2 2 + 1*2 1 + 1*2 0 + 1*2 -1 + 0*2 -2 + 0*2 -3 + 1*2 -4 81 1 / 16 1/81/8 42 1/21/2 1/41/4 11100110. = 1*8 + 0*4 + 1*2 + 1*1 + 1*.5 + 0*.25 + 0*.125 + 1*.0625 = 11.5625

36 36 ECE 372 – Microcontroller Design Fixed Point Numbers 2323 2020 2 -4 2 -32 2121 2 -1 2 -2 11101111. = 1*2 3 + 1*2 2 + 1*2 1 + 1*2 0 + 1*2 -1 + 1*2 -2 + 0*2 -3 + 1*2 -4 81 1 / 16 1/81/8 42 1/21/2 1/41/4 11101111. = 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

37 37 ECE 372 – Microcontroller Design Floating Point Numbers Floating Point Numbers Real number representation similar to scientific notation x = M * B E 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 2381 310 SEM

38 38 ECE 372 – Microcontroller Design Floating Point Numbers Floating Point Numbers Real number representation similar to scientific notation x = M * 2 E 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) 2381 310 SEM 2020 2 -3 2 -23 2 -22 2 -1 2 -2 2 -21 1 …

39 39 ECE 372 – Microcontroller Design Floating Point Numbers Floating Point Numbers Real number representation similar to scientific notation x = M * 2 E Exponent (E) Needs to represent both positive and negative values Stored exponent is adjust using the exponent bias Exponent bias = 2 N-1 -1, where N is the number of bits in the exponent E Actual = E Stored – E Bias Example, 8-bit exponent: E Bias = 2 8-1 -1 = 127 E Stored = 1000 0000 2 = 128 E Actual = 128 - 127 = 1 2381 310 SEM

40 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 * 2 6 4. Determine exponent E Stored = E Actual + E Bias E Stored = 6 + 127 = 133 10 = 10000101 2 310 SEM 1 118.625 Mantissa 1101101010000000000000010000101

41 41 ECE 372 – Microcontroller Design Floating Point Numbers Floating Point Numbers Real number representation similar to scientific notation x = M * 2 E 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 2381 310 SEM

42 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 2381 310 SEM 52111 630 SEM

43 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: 1011 2 =B (Undefined) 1001 2 = 9 10 ASCII: ’9’ HC12 Opcode: $39 = ADCA (Add with Carry to A, Ext. Addressing)

44 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

45 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?

46 46 ECE 372 – Microcontroller Design Humor


Download ppt "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; }"

Similar presentations


Ads by Google