Assembly Programming Arrays Assembly Programming Arrays Array For Loop Example: unsigned short a[]; for(j=; j<; j++) { if(a[j]==) PORTT=x; else PORTT=x; } Programming Steps: Initialize J Compare J to If Not Less than, End Loop Else Load a[j] If a[j] == PORT T = Else PORT T = Increment J Repeat Loop (Step ) Array For Loop Example: unsigned short a[]; for(j=; j<; j++) { if(a[j]==) PORTT=x; else PORTT=x; } Programming Steps: Initialize J Compare J to If Not Less than, End Loop Else Load a[j] If a[j] == PORT T = Else PORT T = Increment J Repeat Loop (Step ) Assembly Code: ldaa # ; Initialize J ldx #$ ; Initialize index to A[] Loop: cmpa # ; Compare J to bge EndLoop ; Else!(J<) ldd,x ; load A[J] cpd # ; Compare J to bne Else ; Else!(A[J]==) ldab # ; Value to write to PORT T bra EndIf Else: ldab # ; Value to write to PORT T EndIf:stab PTT ; Write value to PORT T adda # ; Increment J inx ; Increment A[J] inx ; Need to increment by bra Loop ; Repeat Loop EndLoop: ; do something else Will this code work? NO! ldd overwrite value of J How can we correct this? Assembly Programming Arrays Array For Loop Example: unsigned short a[]; for(j=; j<; j++) { if(a[j]==) PORTT=x; else PORTT=x; } Programming Steps: Initialize J Compare J to If Not Less than, End Loop Else Load a[j] If a[j] == PORT T = Else PORT T = Increment J Repeat Loop (Step ) Assembly Code: ldaa # ; Initialize J ldx #$ ; Initialize index to A[] Loop: cmpa # ; Compare J to bge EndLoop ; Else!(J<) staa $ ; Store J to RAM ldd,x ; load A[J] cpd # ; Compare J to bne Else ; Else!(A[J]==) ldab # ; Value to write to PORT T bra EndIf Else: ldab # ; Value to write to PORT T EndIf:stab PTT ; Write value to PORT T ldaa $ ; Read J from RAM adda # ; Increment J inx ; Increment A[J] inx ; Need to increment by bra Loop ; Repeat Loop EndLoop: ; do something else We need to store value of J to memory and reload it when needed Time for Fun (or maybe not?) Gallon Gallon Gallon
Humor There are types of people in the world: Those who get binary and those who don t Information Information vs Data 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 th October, $ Trillion Information vs Data Information vs Data Information is data with context or meaning 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 International Talk Like a Pirate Day: Date of your first ECE midterm: How much Bill Gates earns per second: Number of pennies to fill Empire State Building September th October, $ Trillion
Decimal Numbers Uses the ten numbers from to Each column represents a power of Thousands ( ) column Hundreds ( ) column Tens ( ) column Ones ( ) column Binary Numbers Uses the two numbers from to Every column represents a power of Eights ( ) column Fours ( ) column Twos ( ) column Ones ( ) column = x + x + x + x = x + x + x + x Binary Numbers Convert the following value from binary (zero s and one s) to a decimal value: = * + * + * + * + * + * + * + * = * + * + * + * + * + * + * + * = Choose your answer: A), B) C) D)
Converting from Decimal to Binary: Divide decimal number by and insert remainder into new binary number Continue dividing quotient by until the quotient is Example: Convert decimal number to binary Decimal Number divide by - insert remainder Binary Number Continue dividing since quotient () is greater than divide by - insert remainder Example: Convert decimal number to binary (cont) Decimal Number Binary Number divide by - insert remainder Continue dividing since quotient () is greater than divide by - insert remainder Since quotient is, we can conclude that is in binary Continue dividing since quotient () is greater than Convert the following decimal value to a binary (zero s and one s) value: Choose your answer: A) B) C) D) Generally, a number can be converted from one base to another by Converting the number to base Then, converting the base ten number to the desired base using the divide-by-n method May not always be the easiest way
Hexadecimal Numbers Uses the ten numbers from to and six letter A to F Each column represents a power of A C Binary Decimal Hexadecimal A B C D EF?? A C = A* + * + C* + * = * + * + * + * =, Hexadecimal Numbers Each position actually represents four base two positions Used as compact means to write binary numbers Often called just hex F Binary Decimal Hexadecimal A B C D EF?? Convert the following hexadecimal value to a binary (zero s and one s) value: CAB Choose your answer: A) B) C) D)
Big vs Little Endian Big Endian MSB (Most Significant Byte) is at lowest address, MIPS, Sparc, HC Little Endian LSB (Least Significant Byte is at lowest address x, DEC $FFFE lower address $FFFF higher address MSB LSB Bit $ $ $ $ $ $ $CC LDD immediate addressing $ MSB of value to be stored in D $ LSB of value to be stored in D $C CPD immediate addressing $ Compare to Value $E 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 - would be: One s Complement Invert all bits for negative numbers So - would be: Positive vs Negative Numbers 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 - would be: + = (with carry of, ignored) Invert all bits and add to get complement of a number Fast conversion: find first from right, invert after Decimal + + + + - Signed- Magnitude - One s Two s Complement Complement - - - () + (-) () - - - - Are we missing a value?
Positive vs Negative Numbers Data Ranges Unsigned to n - Signed-Magnitude - n- - to n- - One s Complement - n- - to n- - Two s Complement - n- to n- - Positive vs Negative Numbers Determine the two s complement representation for the following decimal numbers (assume we are using -bit binary numbers): - - - - 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 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 no overflow
Overflow Binary Coded Decimal Binary Coded Decimal Each digit of a decimal number is represented as a -bit binary number Often used for -segment displays RBI RBO LT A A A A +V +V CC a b c d e f g Common-anode LED display a b c d e f g dp dp +V Binary Undefined Undefined Undefined Undefined Undefined Undefined BCD } = (BCD) ASCII Characters Real Numbers A B C D E F Real Numbers How can we represent a real number (ie numbers that contain a fractional part)? Fixed Point Numbers Note: our C compiler already has built-in routines to deal with real numbers However the computational needs will be significant Control characters Printable characters ()
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: to M + / N - - - - Fixed Point Numbers = * + * + * + * + - - - - * - + * - + * - + * - = * + * + * + * + / / / / * + * + * + * = Fixed Point Numbers = * + * + * + * + - - - - * - + * - + * - + * - = * + * + * + * + / / / / * + * + * + * = Fixed Point Numbers () + () = * + * + * + * + - - - - * - + * - + * - + * - = * + * + * + * + / / / / * + * + * + * =
Floating Point Numbers Floating Point Numbers Real number representation similar to scientific notation Real number representation similar to scientific notation x = M * B E x = M * E Base (B) Base of the numbering systems considered Binary () for computer based implementations We will assume base of for remaining description Mantissa (M) Digits corresponding to the magnitude Stored in a normalized form, where the first bit is assumed to be is the only possible non-zero number in binary Remaining bits correspond to fraction values (similar to fixed point) Sign (S) Indicating positive of negative number Positive, Negative - - - - - - Floating Point Numbers Floating Point Numbers Real number representation similar to scientific notation x = M * E Exponent (E) Needs to represent both positive and negative values Stored exponent is adjust using the exponent bias Exponent bias = N- -, where N is the number of bits in the exponent E Actual = E Stored E Bias Example, -bit exponent: E Bias = - - = E Stored = = E Actual = - = Example: Convert the value to floating point representation Determine sign bit: - is negative, S = Convert to binary: = Normalize number = * Determine exponent E Stored = E Actual + E Bias Mantissa E Stored = + = =
Floating Point Numbers Real number representation similar to scientific notation Zero x = M * E Due to assuming a leading in the mantissa, we cannot directly represent the value using floating point Defined special case for value of Define special case: Exponent and mantissa of all s corresponds to the value Other special cases exist: +/- Infinity Demoralized value Not a Number Floating Point Numbers IEEE Standard for Binary Floating-Point Arithmetic (IEEE ) Single Precision Floating Point Double Precision Floating Point Data vs Information What does the bit pattern mean: Unsigned: decimal Sign-Magnitude: - decimal s complement: - decimal s complement: - decimal Fixed point bitbit: decimal BCD: =B (Undefined) = ASCII: HC Opcode: $ = ADCA (Add with Carry to A, Ext Addressing) Information Representation Text Page page letter x inches, x point characters ASCII bit: xx = bits Kbytes Fax Page Scan Fax low resolution x dots/inch xxx = dots Kbytes (B/W) DVD Mbit/sec hours xxx = Mbit Approximately Gbytes
Information Representation Humor Digital Audio khz sampling rate bit/channel =^ Stereo How many seconds of sound fit in kbyte EPROM mono? Mbyte floppy mono? Mbyte CD Rom stereo?