12.07.2015 Views

Extended Precision Operations Little Endian vs Big Endian Which is ...

Extended Precision Operations Little Endian vs Big Endian Which is ...

Extended Precision Operations Little Endian vs Big Endian Which is ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Extended</strong> <strong>Prec<strong>is</strong>ion</strong> <strong>Operations</strong>To represent larger numbers, more bits are needed.N bits can represent the unsigned range 0 to 2 N -1.Bytes1 Byte = 8 bits1 (8 bits)2 (16 bits)4 (32 bits)Unsigned Range0 to 2550 to 65,5350 to 4,294,967,295C Data Type(PIC16)charintlongThe size of int, long depends on the C implementation; on somemachines both int and long are 4 bytes, with a short int being 2bytes. On some machines a long <strong>is</strong> 8 bytes (64 bits).V 0.1 1<strong>Little</strong> <strong>Endian</strong> <strong>vs</strong> <strong>Big</strong> <strong>Endian</strong>Byte-ordering for multi-byte integers can be storedleast 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 0x38contents: 0xA4 0x57 0x38 0xB8 0x3D 0xF2ilittle endianbig endianjV 0.1 2<strong>Which</strong> <strong>is</strong> better?• No inherent advantage to either byte ordering• On 8-bit processors, it <strong>is</strong> the choice of theprogrammer or compiler writer– Be cons<strong>is</strong>tent!• 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 <strong>is</strong> a religious argument....• In these examples, will use little endianC codeMulti-byte values in MPLABint i;i = 0xC428;Explicity named eachbyte of i.Arranged in littleendian order.PIC16 assemblyCBLOCK 0x040i_low, i_highENDC;; i = 0xC428movlw 0x28movwf i_low ; LSB = 0x28movlw 0xC4movwf i_high ;MSB = 0xC4V 0.1 3V 0.1 4C codeMulti-byte values in MPLABint i;i = 0xC428;Reserve two bytes ofspace for i.i refers to loc 0x20i+1 refers to loc 0x21PIC16 assemblyCBLOCK 0x040i:2ENDC;; i = 0xC428movlw 0x28movwf i ; LSB = 0x28movlw 0xC4movwf i+1 ;MSB = 0xC416-bit Addition using Carry Flag+C flag0x 34 F0+ 0x 22 400x 57 30Add two LSBytes,if Cflag =1 after addition, then increment (+1) MSByte beforeMSByte additionV 0.1 5V 0.1 61


C codeint i,j;i = i + j;LSByte addditionif C=1, add 1 toMSByteMSByte adddition16-bit AdditionPIC16 assemblyCBLOCK 0x040i:2,j:2ENDC;; i = i + jmovf j,w ; w ← j (LSB)addwf i,f ;i LSB ← w + i LSBbtfsc STATUS, Cincf i+1,f ;i MSB ← i MSB + 1movf j+1,w ; w ← j (MSB)addwf i+1,f ; i MSB← w +i MSB16-bit Subtraction using Carry Flag0x 34 10- 0x 22 400x 11 D0- ~C flagSubtract two LSBytes,if Cflag =0 after subtraction (a borrow), then decrement (-1)MSByte before MSByte subtractionV 0.1 7V 0.1 8C codeint i,j;i = i - j;LSByte subtractionif C=0, subtract 1from MSByteMSByte subtraction16-bit SubtractionPIC16 assemblyCBLOCK 0x040i:2,j:2ENDC;; i = i - jmovf j,w ; w ← j (LSB)subwf i,f ;i LSB ← i LSB - wbtfss STATUS, Cdecf i+1,f ;i MSB ← i MSB - 1movf j+1,w ; w ← j (MSB)subwf i+1,f ; i MSB← i MSB - wV 0.1 9addwfc f, dsubwfb f, dPIC16 assemblyPIC18 Add/Sub with Carrymovf j,w ; w ←j(LSB)addwf i,f ;LSB additionbtfsc STATUS, Cincf i+1,f ;MSB+1movf j+1,w ; w← j (MSB)addwf i+1,f ;MSB addition; d ← d + f + Cflag (add with carry); d ← f - d - ~Cflag (subtract with borrow)int i,j;i = i + j;PIC18 assemblymovf j,w ; w ←j(LSB)addwf i,f ; LSB additionmovf j+1,w ; w← j (MSB)addwfc i+1,f ;MSB additionMSByte, use add with carryV 0.1 1016-bit Increment/DecrementOn the PIC18, the increment/decrement instructions affectthe Carry flag, so can do increment/decrement of LSBytefollowed by add-with-carry/subtract-with-borrow to MSByte.On the PIC16, the increment/decrement instructions onlyaffect the Z flag, so cannot use C flag approach if incf/decfused (can always just do add + 1). Use the procedure below:0x33 FF0x34 00+ 1- 10x34 000x33 FFIf LSByte = 0 after increment,then increment MSbyteIf LSByte = 0 before increment,then decrement MSbyteV 0.1 11C codeint i;i++;LSByte incrementif Z=0, continueZ=1, so MSByteincrement16-bit IncrementPIC16 assemblyCBLOCK 0x040i:2ENDC;; i++;incf i,f ; i(LSB)+ 1btfsc STATUS,Z;Z=0?incf i+1,f ;i MSB ← i MSB + 1skip...continue...V 0.1 122


C codeint i;i--;MSBytedecrement ifLSByte==0decrementLSByte16-bit DecrementPIC16 assemblyCBLOCK 0x040i:2ENDC;; i--;movf i,f; ; test LSBytebtfsc STATUS, Zdecf i+1; ; MSByte--decf i; ; LSByte--V 0.1 1316-bit Right Shift/ Left ShiftUnsigned Right Shift (i >> 1)Shift MSByte first, then LSByte. Use Carry flag topropagate bit between bytes.MSByteLSByteb7:b0 C b7:b0Left Shift (i > 1Clear carry forfirst shift, usecarry to propagatebit for secondshift.bcf STATUS,C ;clear carryrlf i ;i LSB 1rrf i ;i LSB >> 1V 0.1 15V 0.1 1616-bit Logical <strong>Operations</strong>C codePIC16 assemblymovf j,w ; w ← j (LSB)int i,j;andwf i,f ;i LSB ← w && i LSBi = i & j; movf j+1,w ; w ← j (MSB)andwf i+1,f ; i MSB← w && i MSBUnsigned <strong>vs</strong>. Signed <strong>Operations</strong>These modifiers determine if thevariables are treated as unsignedor signed values (signed <strong>is</strong>assumed if no modifier <strong>is</strong> present).Signed values use two’scomplement representation.unsigned char i,j;signed char i,j;Bitw<strong>is</strong>e logical operations on multi-byte values areeasy; just perform the same operation on each byte. Theorder in which the bytes are computed does not matter.<strong>Operations</strong> that work differentlyfor signed, unsignedcompar<strong>is</strong>on, right shift (>>),multiplication, div<strong>is</strong>ion<strong>Operations</strong> that work the samefor unsigned,unsignedBitw<strong>is</strong>e logical, addition,subtraction, left shift (


Signed Right Shift (>>)Signed Right Shift in PIC16 AssemblyThe value 0x80 = -128 in two’s complement.Recall that right shift <strong>is</strong> same as divide by two. Then-128 >> 1 == -128 / 2 == -64 == 0xC0If unsigned right shift <strong>is</strong> performed (0 shifted into MSB), then0x80 >> 1 == 0x40 == +64 , the wrong answer!!When doing a signed right shift, the MSB must be kept thesame value (th<strong>is</strong> <strong>is</strong> also known as an arithmetic right shift).Makes sense, dividing a negative number by 2 should notchange the sign!!b7 b6 b5 b4 b3 b2 b1 b0C codesigned char i;i >> 1;Set carry to besame as sign bitbefore shift.PIC16 assembly;; i >> 1bcf STATUS,C ;clear carrybtfsc i,7 ;sign bit =1?bsf STATUS,C ;set carryrrf i ;i >> 1V 0.1 19V 0.1 20Signed Left Shift ( j true if i <strong>is</strong> to left of j,need for -i and +j to swap places if want to useunsigned compar<strong>is</strong>on0xFF2560x80128-1280x7f127+127-1 -i +jSwap sides of -i, +j by complementing sign bits0x00000x0000V 0.1 23PIC16: Signed Compar<strong>is</strong>on Algorithm #1Steps for i > j signed compar<strong>is</strong>onoperand adjustment1. Load j into w reg.2. Complement w sign bit by XOR’ing with 0x80.3. Store in temporary location (you pick), call th<strong>is</strong> temp.4. Load i into w reg.5. Complement w sign bit by XOR’ing with 0x80.6. Subtract w from temp (new j -newi).Unsigned compare7. If carry = 0, then i > jV 0.1 244


C codesigned char i,j;if (i > j) {i = i + j;}/* do stuff */Must use temporarylocation because donot want to modifyj!!!! Requires 8instructions, notSigned Compare (>, Alg #1)PIC16movf j,w ; w ← jxorlw 0x80 ; toggle j sign bitmovwf temp ; save new jmovf i,w ; w ← ixorlw 0x80 ; toggle i sign bitsubwf temp,w ; w← new j-new ibtfsc STATUS,Cgoto skip ;C=1, j>= iifbodymovf j,waddwf i,f ;i = i + jskip;; do stuff...PIC16: Signed Compar<strong>is</strong>on, Algorithm #2Steps for i > j signed compar<strong>is</strong>oncheck if signs equal1. Load j into w reg.2. XOR w with i, store in w3. If bit 7 = 0, then both signs the same. Goto 5, dounsigned compare.4. If bit 7 of j = 1, then j <strong>is</strong> negative, so i > j!5. Load i into w reg6. Subtract w from j (j - i).Unsigned compare7. If carry = 0, then i > jcounting if body.V 0.1 25V 0.1 26signed char i,j;if (i > j) {i = i + j;}/* do stuff */Signed Compare (>, Alg. #2)PIC16C code movf j,w ; w ← jDoes not needtemporary location,but requires moreinstructions (11 <strong>vs</strong>. 8)xorwf i,w ; w ← j ^ ibtfss WREG,7goto docmp ; unsigned cmpbtfss j,7 ; check j signgoto skip ; j pos, so i < jgoto ifbody ; j neg, so i > jdocmpmovf i,w ; w ← <strong>is</strong>ubwf j,w ; w← j- ibtfsc STATUS,Cgoto skip ;C=1, j>= iifbodymovf j,waddwf i,f ;i = i + jskip;; V do 0.1 stuff...27PIC18 Signed CompareThe PIC18 added two additional flagsV (overflow flag), set on two’s complement overflowN (negative flag), set if MSB = 1 after operationAlso added branches based on single flag conditions:bc (branch if carry), bnc (branch if not carry)bov (branch on overflow), bnov (branch if no overflow)bn (branch if negative), bnn (branch if not negative)bz (branch if zero), bnz (branch if not zero)bra (branch always)A branch functions as a conditional goto, will d<strong>is</strong>cuss exactoperation later.V 0.1 28Using N,V flags for Signed CompareTo compare i > j, perform j – iAfter j-i, if V = 0 (correct result, no overflow)if N=1 (result negative) then i > jelse N=0 (answer positive) so j >= iAfter j-i, if V = 1 (incorrect result)if N=0 (result positive) then i > jelse N=1 (result negative) so j >= iMost processors have unsigned compare instructions (operatefrom Z, C flags) and signed compare instructions (operate from Z,N, V flags). The PIC18 only has unsigned compare instructions(cpfsgt, cpfslt) but does have the V,N and branches based on theseflags. The PIC16 only has Z,C flags and no dedicated comparePIC18 Signed Compare (Assembly)signed char i,j;if (i > j) {i = i + j;}/* do stuff */instructions. V 0.1 29V 0.1 30C codeDoes not need atemporary location,requires 6 instructionsoutside of if body.PIC18movf i,w ; w ← <strong>is</strong>ubwf j,w ; w ← j - ib<strong>vs</strong> v_1bnn skip ; V=0,N=0 j>=ibra ifbody; V=0,N=1v_1bn skip ; V=1, N=1 j>=iifbody ;V=1,N=0movf j,waddwf i,f ;i = i + jskip;; do stuff...5


What do you need to know?• PIC16 extended prec<strong>is</strong>ion operations for logical,addition/subtraction, increment/decrement, shiftleft, shift right• PIC18 add with carry, subtract with borrow• PIC16 methods for signed compar<strong>is</strong>on• How to use N,V flags of PIC18 for signedcompar<strong>is</strong>onV 0.1 316

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!