This preview shows page 1-2-3-4-24-25-26-50-51-52-53 out of 53 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Extended Precision OperationsLittle Endian vs Big EndianWhich is better?Multi-byte values in the MPLAB® IDEMulti-byte values in the MPLAB® IDE (cont).16-bit Addition using Carry Flag16-bit Addition16-bit Subtraction using Carry Flag16-bit Subtraction16-bit Increment/Decrement16-bit Logical Operations16-bit Right Shift/ Left Shift16-bit Left Shift16-bit Unsigned Right Shift16-bit Non-Zero TestAnother Example of 16-bit Testing16-bit Equality16-bit Inequality16-bit Greater-than (>) TestUnsigned vs. Signed DataSigned Integer RepresentationSigned Magnitude Representation1's Complement Representation2's Complement RepresentationA common Question from StudentsExample ConversionsSigned Decimal to 2’s complementSigned Decimal to Hex conversion (cont)Signed Decimal to 2’s complement SummaryHex to Signed Decimal Conversion RulesHex to Signed Decimal (cont)Hex to Signed Decimal (cont)2’s complement Hex to Decimal SummaryTwo’s Complement OverflowTwo’s Complement Overflow, AdditionTwo’s Complement Overflow, SubtractionSome ExamplesAdding Precision (unsigned)Adding Precision (two’s complement)Unsigned vs. Signed Arith. ImplementationSigned Right Shift (>>)Signed Right Shift in PIC18 AssemblySigned Left Shift (<<)?Signed ComparisonsPIC18 Signed CompareUsing N,V flags for Signed ComparePIC18 Signed Compare (16-bit)Signed Comparison Summarybranch versus gotobranch Machine Code examplesbranch Machine Code examples (cont)branch, goto Pros/ConsWhat do you need to know?V 0.6 1Extended Precision OperationsTo represent larger numbers, more bits are needed. N bits can represent the unsigned range 0 to 2N-1.short0 to 65,5352 (16 bits)int0 to 65,5352 (16 bits)0 to 4,294,967,2950 to 255Unsigned Rangelong4 (32 bits)char1 (8 bits)C Data Type (PIC18 HITECH C compiler)Bytes1 Byte = 8 bitsThe size of int, long depends on the C implementation; on some machines both int and long are 4 bytes, with a short being 2 bytes. On some machines a long is 8 bytes (64 bits).V 0.6 2Little Endian vs Big EndianByte-ordering for multi-byte integers can be stored least significant to most significant (little endian)or most significant to least significant (big endian)int i; long j;i = 0xA457; j = 0x38B83DF2;Assume i @ 0x20, and j@0x22Address: 0x20 0x21 0x22 0x23 0x24 0x25contents: 0x57 0xA4 0xF2 0x3D 0xB8 0x38 contents: 0xA4 0x57 0x38 0xB8 0x3D 0xF2 little endianbig endianjiV 0.6 3Which is better?• No inherent advantage to either byte ordering• If a processor only contains 8-bit registers, it is the choice of the programmer or compiler writer– Be consistent!• On processors that have 16-bit and 32-bit operations, the µP architects choose the byte ordering– Intel µPs use little endian, Motorola µPs uses big endian– It is a religious argument....• The PIC18 has some multi-byte registers that are arranged in little-endian order in the file registers, so little endian ordering is used in all examples.V 0.6 4Multi-byte values in the MPLAB®IDEint i; i = 0xC428; CBLOCK 0x040i_low, i_high ENDC;; i = 0xC428movlw 0x28movwf i_low ; LSB = 0x28movlw 0xC4movwf i_high ;MSB = 0xC4Explicity named each byte of i.Arranged in little endian order.C codePIC18 assemblyV 0.6 5Multi-byte values in the MPLAB®IDE (cont).This method is preferred; i+1 refers to the memory address computed as 0x020 + 1= 0x021, which contains the MSB of i.Copyright Thomson/Delmar Learning 2005. All Rights Reserved.V 0.6 616-bit Addition using Carry Flag0x 34 F0+C flag+ 0x 22 400x 57 30Add two LSBytes,if Cflag =1 after addition, then increment (+1) MSByte before MSByte additionV 0.6 716-bit AdditionPIC18 assemblyC codeint i,j; i = i + j; CBLOCK 0x040i:2,j:2 ENDC;; i = i + jmovf j,w ; w ← j (LSB)addwf i,f ;i LSB ← w + i LSBmovf j+1,w ; w ← j (MSB)addwfc i+1,f ; i MSB← w+i MSB+CLSByte additionMSByte additionaddwfc is “add W to F with carry”addwfc instruction does d ← f + w + C flag. Nifty, eh?V 0.6 816-bit Subtraction using Carry Flag0x 34 10- ~C flag- 0x 22 400x 11 D0Subtract two LSBytes,if Cflag =0 after subtraction (a borrow), then decrement (-1) MSByte before MSByte subtractionV 0.6 916-bit SubtractionPIC18 assemblyC codeCBLOCK 0x040i:2,j:2 ENDC;; i = i - jmovf j,w ; w ← j (LSB)subwf i,f ;i LSB ← i LSB - wmovf j+1,w ; w ← j (MSB)subwfb i+1,f ; i MSB←i MSB –w-~Cint i,j; i = i - j; LSByte subtractionMSByte subtractionsubwfb is “subtract W from F with borrow”subwfb instruction does d ← f - w - ~C flag. ‘tis Handy.There is also a subfbw instruction (d ← w - f - ~C flag) which has been included in the PIC18 instruction set for confusion purposes.V 0.6 1016-bit Increment/Decrementonly works for 16-bit values, does not extend to 32-bit values.The addwfc/subwfc methods are more general; remember those.Copyright Thomson/Delmar Learning 2005. All Rights Reserved.V 0.6 1116-bit Logical OperationsPIC18 assemblyC codemovf j,w ; w ← j (LSB)andwf i,f ;i LSB ← w & i LSBmovf j+1,w ; w ← j (MSB)andwf i+1,f ; i MSB← w & i MSBint i,j; i = i & j; Bitwise logical operations on multi-byte values are easy; just perform the same operation on each byte. The order in which the bytes are computed does not matter.V 0.6 1216-bit Right Shift/ Left ShiftUnsigned Right Shift (i >> 1)Shift MSByte first, then LSByte. Use Carry flag to propagate bit between bytes.MSByte LSByteb7:b0 C b7:b0Left Shift (i << 1)Shift LSByte first, then MSByte. Use Carry flag to propagate bit between bytes.MSByte LSByteb7:b0 C b7:b0V 0.6 1316-bit Left ShiftPIC18 assemblyC codeCBLOCK 0x040i:2 ENDC;; i = i << 1bcf STATUS,C ;clear carryrlcf i,f ;i LSB << 1rlcf i+1,f ;i MSB << 1int i; i = i << 1; Clear carry for first shift, use carry to propagate bit for second shift.V 0.6 1416-bit Unsigned Right ShiftPIC18 assemblyC codeCBLOCK 0x040i:2 ENDC;; i = i >> 1bcf STATUS,C ;clear carryrrcf i+1,f ;i MSB >> 1rrcf i,f ;i LSB >> 1unsigned int i; i = i >> 1; Clear carry for first shift, use carry to propagate bit for second shift.V 0.6 1516-bit Non-Zero TestBitwise OR the LSByte and MSByte; if result is non-zero, the 16-bit value is zero. You CANNOT just do:movf i,wmovf i+1,wbz end_ifin this case, Z flag is only based on MSByte!!!!Copyright Thomson/Delmar Learning 2005. All Rights Reserved.V 0.6 16Another Example of 16-bit TestingNote that the logical OR operation (||) has nothing to do with the fact that Bitwise OR is used for testing each 16-bit value for zero or non-zero! If the code is if (i && !j),


View Full Document

MSU ECE 3724 - Extended Precision Operations

Documents in this Course
Timers

Timers

38 pages

TEST 4

TEST 4

9 pages

Flags

Flags

6 pages

Timers

Timers

6 pages

Timers

Timers

54 pages

TEST2

TEST2

8 pages

Load more
Download Extended Precision Operations
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Extended Precision Operations and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Extended Precision Operations 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?