EE 308 Spring 2010Binary, Hex and Decimal Numbers (4-bit representation)Hex0123456789ABCDEFDecimal 0 1 2 3 4 5 6 7 8 9101112131415Binary00000001001000110100010101100111100010011010101111001101111011111EE 308 Spring 2010What does a number represent?(Signed 8−bit number)intends for the code.0x72’r’ (ASCII)INC (HC12 instruction)2.26V (Input from A/D converter)1141010Some possible codes:Set temperature in room to 69 FSet cruise control speed to 120 mph(Unsigned 8−bit number)+114Binary numbers are a code, and represent what the programmer2EE 308 Spring 2010Binary to Unsigned Decimal:111101121 x 2 + 1 x 2 + 1 x 25 4+ 1 x 2 + 0 x 2 + 1 x 2 + 1 x 26 2 1031 x 64 + 1 x 32 + 1 x 16 + 1 x 8 + 0 x 4 + 1 x 2 + 1 x 112310Convert Binary to Unsigned DecimalHex to Unsigned Decimal334948 x 16 + 2 x 16 + 13 x 16 + 6 x 1682D6162 1 038 x 4096 + 2 x 256 + 13 x 16 + 6 x 110Convert Hex to Unsigned Decimal3EE 308 Spring 2010Unsigned Decimal to Hex721 = 2D1Q721/16 45/16 2/1645 2 011321D210 16DivisionRDecimal HexConvert Unsigned Decimal to Hex4EE 308 Spring 2010If most significant bit is 0 (most significant hex digit 0−7), number is positive.Get decimal equivalent by converting number to decimal, and using + sign.Example for 8−bit number:If most significant bit is 1 (most significant hex digit 8−F), number is negative.Get decimal equivalent by taking 2’s complement of number, converting to decimal,and using − sign.Example for 8−bit number:Signed Number Representation in 2’s Complement Form:16 − ( 5 x 16 + 13 x 16 ) − ( 5 x 16 + 13 x 1 ) − 933A −> + ( 3 x 16 + 10 x 16 )+ ( 3 x 16 + 10 x 1 )+ 5816A3 −> − ( 5D )11001010161 01010105EE 308 Spring 2010One’s Complement Table Makes It Simple To Find 2’s Complements01234567 8DEF9ABCOne’s Complement TableTo take two’s complement, add one to one’s complement.Take two’s complement of :D0C32F3C + 1 = 2F3D6EE 308 Spring 2010Addition and Subtraction of Hexadecimal Numbers.Setting the C (Carry), V (Overflow), N (Negative) and Z (Zero) bitsHow the C, V, N and Z bits of the CCR are changedCondition Code Register Bits N, Z, V, CN bit is set if result of operation in negative (MSB = 1)Z bit is set if result of operation is zero (All bits = 0)V bit is set if operation produced an overflowC bit is set if operation produced a carry (borrow on subtraction)Note: Not all instructions change these bits of the CCR7EE 308 Spring 2010Addition of Hexadecimal NumbersADDITION:C bit set when result does not fit in wordV bit set when P + P = N N + N = P 7A+52 CC+52 2A 7C+8A AC 36+72 AC 1EN bit set when MSB of result is 1Z bit set when result is 0C: 0 C: 1V: 0C: 0V: 1C: 1V: 1 V: 0N: 1 N: 0 N: 1Z: 0 Z: 0 Z: 0N: 0Z: 08EE 308 Spring 2010Subtraction of Hexadecimal NumbersSUBTRACTION:C bit set on borrow (when the magnitude of the subtrahendV bit set when N − P = P P − N = N is greater than the minuend) 7A−5C 1E−5C 8A 2E 5C−8A D2 2C−72 BAC: 0V: 0C: 1V: 0C: 0V: 1V: 1C: 1N: 0 N: 0 N: 1 N: 1Z: 0Z: 0Z: 0Z: 0N bit set when MSB is 1Z bit set when result is 0 9EE 308 Spring 2010Writing Assembly Language Programs — Use Flowcharts to Help Plan Program StructureFlow chart symbols:STARTOPERATIONENDCONDITIONAL BRANCHLABEL:YESNO10EE 308 Spring 2010IF-THEN Flow Structure LDAB #5 ; var = 5EXAMPLE: { var = 5;} BRA L2 STAB varOR: L2: next instruction next instruction STAB varL2: CMPA #10 ; if (A < 10)if (A<10) BLT L1 ; signed numbersL1: LDAB #5 ; var = 5; CMPA #10 ; if (A < 10) BGE L2 ; signed numbersL2:C?Aif (C){} A;FALSETRUEL1:11EE 308 Spring 2010IF-THEN-ELSE Flow StructureL1: LDAB #5 ; var = 5L1:if (A<10){ var = 5;}else{ var = 0;}L2: next instruction BRA L2 STAB var CMPA #10 ; if (A < 10) BLT L1 ; signed numbers CLR VAR ; var = 0L2: B;BTRUEFALSEC?Aif (C){}else{} A;12EE 308 Spring 2010DO WHILE Flow Structure BLE L1 ; unsigned numbersdo{ table[i] = table[i]/2;}i = 0; i = i+1;while (i <= LEN); LDX #table CLRA ; i = 0L1: ASR 1,X+ ; table[i] /= 2 INCA ; i = i+1 CMPA #LEN ; while (i <= 10) EXAMPLE:TRUEL1:C?FALSEA{ A;}while (C);do13EE 308 Spring 2010WHILE Flow Structure INCA ; i = i + 1i = 0;while (i <= LEN){ table[i] = table[i]*2; i = i + 1;}L3:L1: BRA L1L3: next instruction LDX #table BLT L2 CLRA ; i = 0L1: CMPA #LEN ; while (i <= LEN) BRA L3L2: ASL 1,X+ ; table[i] /= 2EXAMPLE:C?FALSEwhile (C){} A;AL2:TRUE14EE 308 Spring 2010Use Good Structure When Writing Programs — Do Not Use Spaghetti CodeSPAGHETTI CODEDO NOT USE15EE 308 Spring 2010Example Program: Divide a table of data by 2Problem: Start with a table of data. The table consists of 5 values. Each value isbetween 0 and 255. Create a new table whose contents are the original table divided by 2.1. Determine where code and data will go in memory.Code at $2000, data at $1000.2. Determine type of variables to use.Because data will be between 0 and 255, can use unsigned 8-bit numbers.3. Draw a picture of the data structures in memory:COUNTtable1:table2:$100016EE 308 Spring 20104. Strategy: Because we are using a table of data, we will need pointers to each table sowe can keep track of which table element we are working on.Use the X and Y registers as pointers to the tables.5. Use a simple flow chart to plan s tructure of program.STARTDivideby 2StoreResultPointersIncInit EntryGetPointersCOUNTtable1table2XY17EE 308 Spring 20106. Need a way to determine when we reach the end of the table.One way: Use a counter (say, register A) to keep track of how many elements we haveprocessed.More?YESL1:NOSTARTSTOPDivideby 2StoreResultPointersIncInit EntryGetPointersCOUNTtable1table2XYInit CounterDecCounter18EE 308 Spring 20107. Add code to implement blocks:More?YESL1:NOLDAA #COUNTLDX #TABLE1LDY #TABLE2LDAB 0,XSTAB 0,YINXINYDECALSRB ; unsigned divideSTARTSTOPSWIBNE L1Divideby 2StoreResultPointersIncInit EntryGetPointersCOUNTtable1table2XYInit CounterDecCounter19EE 308 Spring 20108. Write program:; Program to divide a table by two; and store the results in memoryprog: equ $2000data: equ $1000count: equ 5org prog ;set program counter to 0x1000ldaa #count
View Full Document