Solved: simple bit shifting problem

Go To Last Post
13 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello Everyone,

I have 8 bytes of data I need to condense into one byte.

Each byte is either an 0xFF or a 0x00.

If the byte is 0, then the bit in temp1 would be 0, if the byte is 0xFF
then the corresponding bit in temp1 would be 1.

 

The 8 bytes of data are saved LSB first.
I want to correct that so the output byte is correct.

 

I can't figure out why I am getting 0x26 as the output when it should be
0x46.

 

I am using avrstudio to simulate. Any ideas where I am going wrong?

I can't see the problem however it may be obvious to someone.

 

Any help is greatly appreciated.

 

Here is the code:

;
.nolist
.include "m8def.inc"
.list
;
.def    temp    = r16
.def    temp1    = r17
.def    count    = r18
.cseg
.org    0
;-------------------------------------------------------
Reset:
    rjmp    Start            ;
;-------------------------------------------------------
Start:
    ldi    temp,LOW(RAMEND)    ;
    out    SPL, temp             ;
    ldi    temp,HIGH(RAMEND)    ;
    out    SPH, temp             ;
    
    ldi    ZH,HIGH(HexData*2)
    ldi    ZL, LOW(HexData*2)
    ldi    count,8
    clr    temp1
Loop:
    lpm    temp,Z+        ;get one byte
    lsr    temp        ;Shift bit 0 to C
    ror    temp1        ;C to bit 7
    dec    count        ;8 bytes to make one byte
    brne    Loop
;-------------------------------------------------------
stop:
    rjmp    stop
HexData:
;LSB                                   ;MSB
;byte = 64 (output must be 46, MSB corrected)
;      0    1    1    0    0    1    0    0
.db 0x00,0xFF,0xFF,0x00,0x00,0xFF,0x00,0x00
.exit

 

This topic has a solution.
Last Edited: Wed. Jan 22, 2020 - 03:15 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your code looks fine to me.   But it makes my head hurt when you have the MSB on the right of the page.

 

I ran it in the Simulator.   R17 contained 0x26 when it got to stop:

 

Hint. Put breakpoints on the LSR and RJMP lines.   Then walk through with F5.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:

I ran it in the Simulator.   R17 contained 0x26 when it got to stop:

 

Hint. Put breakpoints on the LSR and RJMP lines.   Then walk through with F5.

David.

Thank you David, I was using R5 in the simulator and I came up with 0x26 as the result,

however I am wondering why it it not 0x46.

 

This code works but I have to swap the nibbles at the end.

I was hoping someone would see why the code in the first post doesn't produce the same result.

 

Is there something I am not seeing?

 


;
.nolist
.include "m8def.inc"
.list
;
.def	temp	= r16
.def	temp1	= r17
.def	count	= r18

.cseg
.org	0
;-------------------------------------------------------
Reset:
	rjmp	Start			;
;-------------------------------------------------------
Start:
	ldi	temp,LOW(RAMEND)	;
	out	SPL, temp     		;
	ldi	temp,HIGH(RAMEND)	;
	out	SPH, temp     		;

	ldi	ZH,HIGH(HexData*2)
	ldi	ZL, LOW(HexData*2)

	ldi	count,8
	clr	temp1

Loop:
	lpm	temp,Z+
	ror	temp		;Shift bit 0 (MSB) to C
	rol temp1		;C to bit 0
	dec	count		;8 bytes to make one byte
	brne	Loop

	swap	temp1
;-------------------------------------------------------
stop:
	rjmp	stop

HexData:
;LSB								   ;MSB
;byte = 64 (output must be 46, MSB corrected)
;      0    1    1    0    0    1    0    0
.db 0x00,0xFF,0xFF,0x00,0x00,0xFF,0x00,0x00
.exit

 

Last Edited: Tue. Jan 21, 2020 - 10:25 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just to stop my head hurting:

;LSB								   ;MSB
;byte = 64 (output must be 46, MSB corrected)
;      0    1    1    0    0    1    0    0 
;MSB   0    0    1    0    0    1    1    0  LSB for human people

 

That is easy to read now.   i.e. 0x26

Why do you think it should be 0x46 ?

 

If your hardware requires the first byte of your array to be the LSB in the output,   translate to a human-readable form.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well that is how the data is stored in the memory. LSB first.

I think it should be 0x46 because that is what the data shows

 

0b01000110 = 0x46


MSB                                              LSB
0       1      0      0      0      1      1      0
00     FF     00     00     00     FF     FF     00

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If the data is stored lsb first, surely you want to do a right rather than left shift?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I can't figure out why I am getting 0x26 as the output when it should be 0x46.

how? you get: 

LSB                                                                       MSB

;      0    1    1    0    0    1    0    0
.db 0x00,0xFF,0xFF,0x00,0x00,0xFF,0x00,0x00

in other words you should have this BYTE:

00100110   or  0010 0110  which is 0x26 

 

 

since you are shifting in 8 times, you can scrap  

clr	temp1

 

 

 

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

this:

HexData:
;LSB				       ;MSB
;byte = 64 (output must be 46, MSB corrected)
;      0    1    1    0    0    1    0    0
.db 0x00,0xFF,0xFF,0x00,0x00,0xFF,0x00,0x00

should have been this:

HexData:
;LSB			  	      ;MSB
;byte = 64 (output must be 46, MSB corrected)
;      0    1    1    0    0    0    1    0
.db 0x00,0xFF,0xFF,0x00,0x00,0x00,0xFF,0x00

Duh? I was sitting in front of a screen too long, I couldn't see what was right in front of me.

It works now, thanks Guys.

Last Edited: Wed. Jan 22, 2020 - 02:19 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

PLease mark the post with the solution.

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It works now, thanks Guys.

What do you mean?  When was it not working?  Did you change the calculation?  You give it some data & it gives you some result, as a byte.

You changed the data, thus will have a different result.

Some other data will give some other result too.

 

Be careful you don't fool yourself!   

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Wed. Jan 22, 2020 - 03:02 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:
When was it not working?
As he said, he reordered the data:

.db 0x00,0xFF,0xFF,0x00,0x00,0xFF,0x00,0x00

(which I think we can all agree is 0x26) to be:

.db 0x00,0xFF,0xFF,0x00,0x00,0x00,0xFF,0x00

which is the actual 0x46 he was hoping for.

 

Thus proving the old adage: "garbage in, garbage out" ;-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It sure is difficult to read bit patterns right to left.

I suspect that this is the reason for the OP wanting 0x46.    And contortions using SWAP.

 

The 6 nybble reads the same right to left or left to right.

The 2 nybble reads as 2 or 4 depending on how much your head hurts.

 

The real mystery is:    the code originally posted in #1 was both correct and efficient.   Neatly formatted too !!

 

If the OP can write such elegant code,   I would expect her to translate binary, hex, ... accurately.

 

The code that is now in #3 is still efficient.   But the loop builds a MSB-first result.    And the neat formatting is broken.

 

I am sure that these "problems" can be resolved easily:   Invest in a pencil.

 

However neatly you write on a screen it is not as reliable as writing on paper.

 

David.

Last Edited: Wed. Jan 22, 2020 - 12:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:

The 2 nybble reads as 2 or 4 depending on how much your head hurts.

That was it! I was reading it from left to right. 

 

Thanks Everyone!