Jump to content
Sign in to follow this  

Shifting By Multiples Of 8

Recommended Posts

The compiler does not recognise that right shifting by 24 is a multiple of 8 bits.


#include <system.h>

unsigned long	mylong;
unsigned char	mychars[5];

void main(void) {

mylong = 0x12345678;
0003  3078	  MOVLW 0x78
0004  1283	  BCF STATUS, RP0
0005  1303	  BCF STATUS, RP1
0006  00A5	  MOVWF gbl_mylong
0007  3056	  MOVLW 0x56
0008  00A6	  MOVWF gbl_mylong+D'1'
0009  3034	  MOVLW 0x34
000A  00A7	  MOVWF gbl_mylong+D'2'
000B  3012	  MOVLW 0x12
000C  00A8	  MOVWF gbl_mylong+D'3'

mychars[0] = (unsigned char)(mylong >> 0);
000D  0825	  MOVF gbl_mylong, W
000E  00A0	  MOVWF gbl_mychars

mychars[1] = (unsigned char)(mylong >> 8);
000F  0826	  MOVF gbl_mylong+D'1', W
0010  00A1	  MOVWF gbl_mychars+D'1'

mychars[2] = (unsigned char)(mylong >> 16);
0011  0827	  MOVF gbl_mylong+D'2', W
0012  00A2	  MOVWF gbl_mychars+D'2'

mychars[2] = (unsigned char)(mylong >> 24);
0013  0825	  MOVF gbl_mylong, W
0014  00A9	  MOVWF CompTempVar499
0015  0826	  MOVF gbl_mylong+D'1', W
0016  00AA	  MOVWF CompTempVar500
0017  0827	  MOVF gbl_mylong+D'2', W
0018  00AB	  MOVWF CompTempVar501
0019  0828	  MOVF gbl_mylong+D'3', W
001A  00AC	  MOVWF CompTempVar502
001B  1003	  BCF STATUS,C
001C  0CAC	  RRF CompTempVar502, F
001D  0CAB	  RRF CompTempVar501, F
001E  0CAA	  RRF CompTempVar500, F
001F  0CA9	  RRF CompTempVar499, F
0020  1003	  BCF STATUS,C
0021  0CAC	  RRF CompTempVar502, F
0022  0CAB	  RRF CompTempVar501, F
0023  0CAA	  RRF CompTempVar500, F
0024  0CA9	  RRF CompTempVar499, F
0025  1003	  BCF STATUS,C
0026  0CAC	  RRF CompTempVar502, F
0027  0CAB	  RRF CompTempVar501, F
0028  0CAA	  RRF CompTempVar500, F
0029  0CA9	  RRF CompTempVar499, F
002A  1003	  BCF STATUS,C
002B  0CAC	  RRF CompTempVar502, F
002C  0CAB	  RRF CompTempVar501, F
002D  0CAA	  RRF CompTempVar500, F
002E  0CA9	  RRF CompTempVar499, F
002F  1003	  BCF STATUS,C
0030  0CAC	  RRF CompTempVar502, F
0031  0CAB	  RRF CompTempVar501, F
0032  0CAA	  RRF CompTempVar500, F
0033  0CA9	  RRF CompTempVar499, F
0034  1003	  BCF STATUS,C
0035  0CAC	  RRF CompTempVar502, F
0036  0CAB	  RRF CompTempVar501, F
0037  0CAA	  RRF CompTempVar500, F
0038  0CA9	  RRF CompTempVar499, F
0039  1003	  BCF STATUS,C
003A  0CAC	  RRF CompTempVar502, F
003B  0CAB	  RRF CompTempVar501, F
003C  0CAA	  RRF CompTempVar500, F
003D  0CA9	  RRF CompTempVar499, F
003E  1003	  BCF STATUS,C
003F  0CAC	  RRF CompTempVar502, F
0040  0CAB	  RRF CompTempVar501, F
0041  0CAA	  RRF CompTempVar500, F
0042  0CA9	  RRF CompTempVar499, F
0043  1003	  BCF STATUS,C
0044  0CAC	  RRF CompTempVar502, F
0045  0CAB	  RRF CompTempVar501, F
0046  0CAA	  RRF CompTempVar500, F
0047  0CA9	  RRF CompTempVar499, F
0048  1003	  BCF STATUS,C
0049  0CAC	  RRF CompTempVar502, F
004A  0CAB	  RRF CompTempVar501, F
004B  0CAA	  RRF CompTempVar500, F
004C  0CA9	  RRF CompTempVar499, F
004D  1003	  BCF STATUS,C
004E  0CAC	  RRF CompTempVar502, F
004F  0CAB	  RRF CompTempVar501, F
0050  0CAA	  RRF CompTempVar500, F
0051  0CA9	  RRF CompTempVar499, F
0052  1003	  BCF STATUS,C
0053  0CAC	  RRF CompTempVar502, F
0054  0CAB	  RRF CompTempVar501, F
0055  0CAA	  RRF CompTempVar500, F
0056  0CA9	  RRF CompTempVar499, F
0057  1003	  BCF STATUS,C
0058  0CAC	  RRF CompTempVar502, F
0059  0CAB	  RRF CompTempVar501, F
005A  0CAA	  RRF CompTempVar500, F
005B  0CA9	  RRF CompTempVar499, F
005C  1003	  BCF STATUS,C
005D  0CAC	  RRF CompTempVar502, F
005E  0CAB	  RRF CompTempVar501, F
005F  0CAA	  RRF CompTempVar500, F
0060  0CA9	  RRF CompTempVar499, F
0061  1003	  BCF STATUS,C
0062  0CAC	  RRF CompTempVar502, F
0063  0CAB	  RRF CompTempVar501, F
0064  0CAA	  RRF CompTempVar500, F
0065  0CA9	  RRF CompTempVar499, F
0066  1003	  BCF STATUS,C
0067  0CAC	  RRF CompTempVar502, F
0068  0CAB	  RRF CompTempVar501, F
0069  0CAA	  RRF CompTempVar500, F
006A  0CA9	  RRF CompTempVar499, F
006B  1003	  BCF STATUS,C
006C  0CAC	  RRF CompTempVar502, F
006D  0CAB	  RRF CompTempVar501, F
006E  0CAA	  RRF CompTempVar500, F
006F  0CA9	  RRF CompTempVar499, F
0070  1003	  BCF STATUS,C
0071  0CAC	  RRF CompTempVar502, F
0072  0CAB	  RRF CompTempVar501, F
0073  0CAA	  RRF CompTempVar500, F
0074  0CA9	  RRF CompTempVar499, F
0075  1003	  BCF STATUS,C
0076  0CAC	  RRF CompTempVar502, F
0077  0CAB	  RRF CompTempVar501, F
0078  0CAA	  RRF CompTempVar500, F
0079  0CA9	  RRF CompTempVar499, F
007A  1003	  BCF STATUS,C
007B  0CAC	  RRF CompTempVar502, F
007C  0CAB	  RRF CompTempVar501, F
007D  0CAA	  RRF CompTempVar500, F
007E  0CA9	  RRF CompTempVar499, F
007F  1003	  BCF STATUS,C
0080  0CAC	  RRF CompTempVar502, F
0081  0CAB	  RRF CompTempVar501, F
0082  0CAA	  RRF CompTempVar500, F
0083  0CA9	  RRF CompTempVar499, F
0084  1003	  BCF STATUS,C
0085  0CAC	  RRF CompTempVar502, F
0086  0CAB	  RRF CompTempVar501, F
0087  0CAA	  RRF CompTempVar500, F
0088  0CA9	  RRF CompTempVar499, F
0089  1003	  BCF STATUS,C
008A  0CAC	  RRF CompTempVar502, F
008B  0CAB	  RRF CompTempVar501, F
008C  0CAA	  RRF CompTempVar500, F
008D  0CA9	  RRF CompTempVar499, F
008E  1003	  BCF STATUS,C
008F  0CAC	  RRF CompTempVar502, F
0090  0CAB	  RRF CompTempVar501, F
0091  0CAA	  RRF CompTempVar500, F
0092  0C29	  RRF CompTempVar499, W
0093  00A2	  MOVWF gbl_mychars+D'2'

mychars[5] = (unsigned char)(mylong >> 32);
0094  3000	  MOVLW 0x00
0095  01A5	  CLRF gbl_mychars+D'5'

0096  0008	  RETURN


The above code snippet shows shifting by 8, 16 and 32 is handles OK (32 has a redundant instruction though).

Perhaps a loop could be considered for shifts greater than say 8 to produce more compact code with a little loss of speed.


Creating a union of a long and char[4] is a workaround to all those shifts to get access to the individual bytes of the long.





Share this post

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...