ECE 372 Microcontroller Design Assembly Programming Arrays Array
ECE 372 – Microcontroller Design Assembly Programming – Arrays Array For Loop Example: unsigned short a[10]; for(j=0; j<10; j++) { if(a[j]==1) PORTT=0 x 04; else PORTT=0 x 00; } Programming Steps: 1. 2. 3. 4. Initialize J Compare J to 10 If Not Less than 10, 1. End Loop Else 1. 2. 3. 4. 5. Load a[j] If a[j] == 1 1. PORT T = 4 Else 1. PORT T = 0 Increment J Repeat Loop (Step 2) 1
ECE 372 – Microcontroller Design Assembly Programming – Arrays Array For Loop Example: Assembly Code: unsigned short a[10]; for(j=0; j<10; j++) { if(a[j]==1) PORTT=0 x 04; else PORTT=0 x 00; } ldaa #0 ldx #$3800 Loop: cmpa #10 bge End. Loop ldd 0, X cpd #1 bne Else ldab #4 bra End. If Else: ldab #0 End. If: stab PTT adda #1 inx bra Loop End. Loop: Programming Steps: 1. 2. 3. 4. Initialize J Compare J to 10 If Not Less than 10, 1. End Loop Else 1. 2. 3. 4. 5. Load a[j] If a[j] == 1 1. PORT T = 4 Else 1. PORT T = 0 Increment J Repeat Loop (Step 2) ; ; ; ; Initialize J Initialize index to A[0] Compare J to 10 Else !(J<10) load A[J] Compare J to 1 Else !(A[J]==1) Value to write to PORT T ; ; ; ; Value to write to PORT T Write value to PORT T Increment J Increment A[J] Need to increment by 2 Repeat Loop do something else Will this code work? NO! ldd overwrite value of J How can we correct this? 2
ECE 372 – Microcontroller Design Assembly Programming – Arrays Array For Loop Example: Assembly Code: unsigned short a[10]; for(j=0; j<10; j++) { if(a[j]==1) PORTT=0 x 04; else PORTT=0 x 00; } ldaa #0 ldx #$3800 Loop: cmpa #10 bge End. Loop staa $3814 ldd 0, X cpd #1 bne Else ldab #4 bra End. If Else: ldab #0 End. If: stab PTT ldaa $3814 adda #1 inx bra Loop End. Loop: Programming Steps: 1. 2. 3. 4. Initialize J Compare J to 10 If Not Less than 10, 1. End Loop Else 1. 2. 3. 4. 5. Load a[j] If a[j] == 1 1. PORT T = 4 Else 1. PORT T = 0 Increment J Repeat Loop (Step 2) ; ; ; ; ; Initialize J Initialize index to A[0] Compare J to 10 Else !(J<10) Store J to RAM load A[J] Compare J to 1 Else !(A[J]==1) Value to write to PORT T ; ; ; ; Value to write to PORT T Write value to PORT T Read J from RAM Increment J Increment A[J] Need to increment by 2 Repeat Loop do something else We need to store value of J to memory and reload it when needed. 3
ECE 372 – Microcontroller Design Time for Fun (or maybe not? ) 10 Gallon 7 Gallon 3 Gallon 4
ECE 372 – Microcontroller Design Humor n There are 10 types of people in the world: Those who get binary and those who don’t. 5
ECE 372 – Microcontroller Design Information vs. Data n Information n n 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 n n Individual fact value or set of facts or values Measurement or storage September 19 th October 17, 2006 $250 1. 8 Trillion 6
ECE 372 – Microcontroller Design Information vs. Data n Information is data with context or meaning International Talk Like a Pirate Day: September 19 th Date of your first ECE 372 midterm: October 17, 2006 How much Bill Gates earns per second: $250 Number of pennies to fill Empire State Building 1. 8 Trillion 7
ECE 372 – Microcontroller Design Data Representation n The same data can be represented with: n Different symbols n n Different numeric bases n n English, Cyrillic, Arabic Binary, Octal, Hexadecimal, Decimal Different formats n Little Endian, Big Endian, Binary Coded Decimal 8
ECE 372 – Microcontroller Design Data Structure (Encodings) n Decimal Numbers n n Uses the ten numbers from 0 to 9 Each column represents a power of 10 9
ECE 372 – Microcontroller Design Data Structure (Encodings) n Binary Numbers n n Uses the two numbers from 0 to 1 Every column represents a power of 2 0 0 1 27 26 25 24 23 22 21 20 10
ECE 372 – Microcontroller Design Data Structure (Encodings) n Binary Numbers 0 0 1 27 26 25 24 23 22 21 20 0 0 1 128 64 32 16 8 4 2 1 = 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 11
ECE 372 – Microcontroller Design Data Structure (Encodings) n Convert the following value from binary (zero’s and one’s) to a decimal value: 1001102 Choose your answer: A) 100, 110 B) 22 C) 38 D) 42 12
ECE 372 – Microcontroller Design Data Structure (Encodings) n Converting from Decimal to Binary: n Divide decimal number by 2 and insert remainder into new binary number. n n Continue dividing quotient by 2 until the quotient is 0. Example: Convert decimal number 12 to binary Decimal Number 6 2 12 divide by 2 -12 0 insert remainder Binary Number 0 1 Continue dividing since quotient (6) is greater than 0 2 3 6 divide by 2 -6 0 insert remainder 0 2 0 1 Continue dividing since quotient (3) is greater than 0 13
ECE 372 – Microcontroller Design Data Structure (Encodings) n Example: Convert decimal number 12 to binary (cont. ) Decimal Number Binary Number 1 1 0 0 2 3 divide by 2 4 2 1 -2 1 insert remainder Continue dividing since quotient (1) is greater than 0 2 0 1 1 divide by 2 8 -0 1 insert remainder 1 4 0 2 0 1 Since quotient is 0, we can conclude that 12 is 1100 in binary 14
ECE 372 – Microcontroller Design Data Structure (Encodings) n Convert the following decimal value to a binary (zero’s and one’s) value: 5410 Choose your answer: A) 110110 B) 100010 C) 1000010 D) 10100100 15
ECE 372 – Microcontroller Design Data Structure (Encodings) n Generally, a number can be converted from one base to another by n n n 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… 16
ECE 372 – Microcontroller Design Data Structure (Encodings) n Hexadecimal Numbers n n 0 Uses the ten numbers from 0 to 9 and six letter A to F Each column represents a power of 16 0 0 0 A 0 C 5 167 166 165 164 163 162 161 160 Binary 0 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000 10001 Decimal Hexadecimal 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 A 11 B 12 C 13 D 14 E 15 F 16 ? 17
ECE 372 – Microcontroller Design Data Structure (Encodings) 0 0 A 0 C 5 167 166 165 164 163 162 161 160 = A*163 + 0*162 + C*161 + 5*160 = 10*4096 + 0*256 + 12*16 + 5*1 = 41, 15710 18
ECE 372 – Microcontroller Design Data Structure (Encodings) n Hexadecimal Numbers n n n Each position actually represents four base two positions Used as compact means to write binary numbers Often called just hex Binary Decimal Hexadecimal 11110000 F 0 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 0 1 2 3 4 5 6 7 8 9 A B C D E F ? ? 19
ECE 372 – Microcontroller Design Data Structure (Encodings) n Convert the following hexadecimal value to a binary (zero’s and one’s) value: CAB 16 Choose your answer: A) 110111101010 B) 110001011001 C) 11001011 D) 110010111010 20
ECE 372 – Microcontroller Design Big vs. Little Endian n Big Endian n MSB (Most Significant Byte) is at lowest address 68000, MIPS, Sparc, HC 12 Little Endian n n LSB (Least Significant Byte is at lowest address 80 x 86, DEC 7 0 $FFFE lower address MSB $FFFF higher address LSB 16 Bit $4000 $4001 $4002 $4003 $4004 $4005 … $CC $00 $8 C $03 $E 8 LDD immediate addressing MSB of value to be stored in D LSB of value to be stored in D CPD immediate addressing Compare to Value 1000 21
ECE 372 – Microcontroller Design Positive vs. Negative Numbers n Negative numbers are common n n How can we represent negative numbers in binary? Signed-magnitude n Use leftmost bit for sign bit n n So -5 would be: 1101 One’s Complement n Invert all bits for negative numbers n So -5 would be: 1010 22
ECE 372 – Microcontroller Design Positive vs. Negative Numbers n Two’s Complement n Allows us to perform subtraction using addition n n Two’s complement of a number added to the number itself will equal zero n n n No need for dedicated subtractor within CPU’s ALU So -5 would be: 10112 + 01012 = 00002 (with carry of 1, ignored) Invert all bits and add 1 to get complement of a number n Fast conversion: find first 1 from right, invert after 1 0011 1000 (56) + 1111 0110 (-10) 0010 1110 (46) 23
ECE 372 – Microcontroller Design Positive vs. Negative Numbers Decimal Signed. Magnitude One’s Two’s Complement +4 - - - +3 0 11 +2 0 10 +1 0 01 0 0 00 -1 1 01 1 10 1 11 -2 1 10 1 01 1 10 -3 1 11 1 00 1 01 -4 - - 1 00 Are we missing a value? 24
ECE 372 – Microcontroller Design Positive vs. Negative Numbers n Data Ranges n Unsigned n n Signed-Magnitude n n -2 n-1 -1 to 2 n-1 -1 One’s Complement n n 0 to 2 n-1 -2 n-1 -1 to 2 n-1 -1 Two’s Complement n -2 n-1 to 2 n-1 -1 25
ECE 372 – Microcontroller Design Positive vs. Negative Numbers n Determine the two’s complement representation for the following decimal numbers (assume we are using 8 -bit binary numbers): n n n -1 -15 22 -101 26
ECE 372 – Microcontroller Design Overflow n n Occurs then a result cannot be represented with given number of bits Either the result is too large magnitude of positive or negative 27
ECE 372 – Microcontroller Design Overflow n Detecting Overflow n n 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 n Adding a positive and negative can’t exceed largest magnitude positive or negative 0 1 1 1 1 0 0 0 +0 0 0 1 +1 0 0 0 +0 1 1 0 0 0 overflow 0 1 1 1 overflow 1 1 no overflow 28
ECE 372 – Microcontroller Design Overflow 3 29
ECE 372 – Microcontroller Design Binary Coded Decimal n n Each digit of a decimal number is represented as a 4 -bit binary number Often used for 7 -segment displays Binary 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 Undefined Undefined BCD 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 30
ECE 372 – Microcontroller Design ASCII Characters 0 1 2 3 4 5 6 7 8 9 A B C D E F Control characters Printable characters (96) 31
ECE 372 – Microcontroller Design Real Numbers n How can we represent a real number (i. e. numbers that contain a fractional part)? n n n Fixed Point Numbers Floating Point Numbers Note: our C compiler already has built-in routines to deal with real numbers n However the computational needs will be significant. 32
ECE 372 – Microcontroller Design Fixed Point Numbers n Real number with fixed number of digits before and after radix point n n n N-bits used to represent integer part M-bits used to represent fractional part Unsigned range: 0 to 2 M+1/2 N 0 1 0 23 22 21 0. 0 1 0 0 20 2 -1 2 -2 2 -3 2 -4 33
ECE 372 – Microcontroller Design Fixed Point Numbers 0 1 0 23 22 21 0 8 4 2 0. 0 1 0 0 = 0*23 + 1*22 + 0*21 + 0*20 + 20 2 -1 2 -2 2 -3 2 -4 0*2 -1 + 1*2 -2 + 0*2 -3 + 0*2 -4 0. 0 1 0 0 = 0*8 + 1*4 + 0*2 + 0*1 + 1 1 1 /2 /4 /8 /16 0*. 5 + 1*. 25 + 0*. 125 + 0*. 0625 = 4. 25 34
ECE 372 – Microcontroller Design Fixed Point Numbers 1 0 1 23 22 21 1 0 1 8 4 2 1. 1 0 0 1 = 1*23 + 0*22 + 1*21 + 1*20 + 20 2 -1 2 -2 2 -3 2 -4 1*2 -1 + 0*2 -2 + 0*2 -3 + 1*2 -4 1. 1 0 0 1 = 1*8 + 0*4 + 1*2 + 1*1 + 1 1 1 /2 /4 /8 /16 1*. 5 + 0*. 25 + 0*. 125 + 1*. 0625 = 11. 5625 35
ECE 372 – Microcontroller Design Fixed Point Numbers 0100 (4. 25) + 1011. 1001 (11. 5625) 1111. 1101 1 23 22 21 1 8 4 2 1. 1 1 0 1 = 1*23 + 1*22 + 1*21 + 1*20 + 20 2 -1 2 -2 2 -3 2 -4 1*2 -1 + 1*2 -2 + 0*2 -3 + 1*2 -4 1. 1 1 0 1 = 1*8 + 1*4 + 1*2 + 1*1 + 1 1 1 /2 /4 /8 /16 1*. 5 + 1*. 25 + 0*. 125 + 1*. 0625 = 15. 8125 36
ECE 372 – Microcontroller Design Floating Point Numbers n Real number representation similar to scientific notation n n Base (B) n Base of the numbering systems considered Binary (2) for computer based implementations n We will assume base of 2 for remaining description n n x = M * BE Sign (S) n n Indicating positive of negative number 0 – Positive, 1 – Negative S E M 1 8 23 31 0 37
ECE 372 – Microcontroller Design Floating Point Numbers n Real number representation similar to scientific notation x = M * 2 E n n Mantissa (M) Digits corresponding to the magnitude Stored in a normalized form, where the first bit is assumed to be 1 n n n 1 is the only possible non-zero number in binary Remaining bits correspond to fraction values (similar to fixed point) n 1 20 2 -1 2 -2 2 -3 … 2 -21 2 -22 2 -23 S E M 1 8 23 31 0 38
ECE 372 – Microcontroller Design Floating Point Numbers n Real number representation similar to scientific notation n n x = M * 2 E Exponent (E) n n Needs to represent both positive and negative values Stored exponent is adjust using the exponent bias n n n Exponent bias = 2 N-1 -1, where N is the number of bits in the exponent EActual = EStored – EBias Example, 8 -bit exponent: n n n EBias = 28 -1 -1 = 127 EStored = 1000 00002 = 128 EActual = 128 - 127 = 1 S E M 1 8 23 31 0 39
ECE 372 – Microcontroller Design Floating Point Numbers n n Example: Convert the value − 118. 625 to floating point representation 1. Determine sign bit: n n -118. 625 is negative, S = 1 2. Convert to binary: n 118. 625 = 1110110. 101 118 n 3. Normalize number n . 625 1110110. 101 = 1. 110110101 * 26 Mantissa n 4. Determine exponent n n S 1 31 EStored = EActual + EBias EStored = 6 + 127 = 13310 = 100001012 E 10000101 M 1101101010000000 0 40
ECE 372 – Microcontroller Design Floating Point Numbers n Real number representation similar to scientific notation n n Zero n n x = M * 2 E 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: n n n +/- Infinity Denormalized value Not a Number S E M 1 8 23 31 0 41
ECE 372 – Microcontroller Design Floating Point Numbers n n IEEE Standard for Binary Floating-Point Arithmetic (IEEE 754) Single Precision Floating Point S E M 1 8 23 31 n 0 Double Precision Floating Point S E M 1 11 52 63 0 42
ECE 372 – Microcontroller Design Data vs. Information n What does the bit pattern mean: 1011 1001 n n n Unsigned: 185 decimal Sign-Magnitude: -57 decimal 1’s complement: -70 decimal 2’s complement: -71 decimal Fixed point 4 bit: 11. 5625 decimal BCD: n n 10112 =B (Undefined) 10012 = 910 ASCII: ’ 9’ HC 12 Opcode: $39 = ADCA (Add with Carry to A, Ext. Addressing) 43
ECE 372 – Microcontroller Design Information Representation n Text Page n n n 4 Kbytes Fax Page Scan n n 1 page letter 8. 5 x 11 inches, 60 x 68 12 point characters ASCII 8 bit: 60 x 68 x 8 = 32640 bits Fax low resolution 204 x 98 dots/inch 8. 5 x 204 x 11 x 98 = 1869252 dots 235 Kbytes (B/W) DVD n n n 8 Mbit/sec 2 hours 2 x 60 x 8 = 57600 Mbit Approximately 7. 2 Gbytes 44
ECE 372 – Microcontroller Design Information Representation n Digital Audio n n 44. 2 k. Hz sampling rate 16 bit/channel 1. . 65536=2^16 Stereo How many seconds of sound fit in n 32 kbyte EPROM mono? 1. 4 Mbyte floppy mono? 660 Mbyte CD Rom stereo? 45
ECE 372 – Microcontroller Design Humor 46
- Slides: 46