1
votes

So my excercise is to change order of bits so:

7 6 5 4 3 2 1 0 

will be

3 2 7 6 1 0 5 4

I'm struggling with figuring this out. I'm trying to use rol method to rotate them so I would get 3 2 1 0 7 6 5 4 which would be at least a little bit closer. But the first bit that goes out of scope isn't carried through the second one is my example that I use is 0000 1111

code:

  ldi  r16,$0F   - 0000 1111
       rol  r16  - 0001 1110
       rol  r16  - 0011 1100
       rol  r16  - 0111 1000
       rol  r16  - 1111 0000
       rol  r16  - 1110 000(0 - should be 1)
       rol  r16  - 1100 0001

UPDATE I've read in the excercise that we should flip bits 5 4 3 2 and then use shifts and rotate to get that order so here is my code

       ldi  r16,$0F
       ldi  r17,$3C
       eor  r16,r17
       lsl  r16
       lsl  r16

so r16 is 0000 1111 r17 is 0011 1100 then i use XOR gate to get in r16 0011 0011 and then i want to shift twice to the left with lsl so i get 0110 0110 but after I do the second shift I get: 0011 0001 instead of 1100 1100 and I'm not really sure why is that happening.

UPDATE 2 The previous code didn't work because the program didn't know what to do after executing last function so it went back to the beggining giving me bad result the solution is end: rjmp end which forces program to infinitely jump to this line over and over again

       ldi  r16,$0F
       ldi  r17,$3C
       eor  r16,r17
       lsl  r16
       lsl  r16
       end:  rjmp  end 
3
This is not merely changing order of bits ... why doesn't an array work? - WhatsUp
@WhatsUp This is changing order of bits. And array would be an overkill. I believe the OP is intended to solve this with logical operators. - Margaret Bloom
@MargaretBloom Aha I see, he wants that bit 7 goes to bit 5, etc. is that right? - WhatsUp
@WhatsUp Yes, that's it. However the OP has not specified any architecture for us to answer (assembly looks like AVR). - Margaret Bloom
Yes it is AVR ATmega 8535 and could you please take a look at the update? - Higeath

3 Answers

3
votes

A shorter version, making use of the T flags can be this:

;Input: r16
;Output: r16

ror r16        ;r16 = 0 7 6 5   4 3 2 1
ror r16        ;r16 = 1 0 7 6   5 4 3 2
mov r17, r16   ;r17 = 1 0 7 6   5 4 3 2

ror r16        ;r16 = 2 1 0 7   6 5 4 3
ror r16        ;r16 = 3 2 1 0   7 6 5 4

bst r17, 7     ;T = 1
bld r17, 3     ;r17 = 1 0 7 6   1 4 3 2
bst r17, 6     ;T = 0
bld r17, 2     ;r17 = 1 0 7 6   1 0 3 2

andi r17, $3c  ;r17 = - - 7 6   1 0 - -
andi r16, $c3  ;r16 = 3 2 - -   - - 5 4

or r16, r17    ;r16 = 3 2 7 6   1 0 5 4

Nota Bene
Your attempts at solving this exercise show that you haven't really understood (low level) programming yet.
I suggest you taking some times to internalize the concepts you are studying.

0
votes

I would do it by brute force:

ldi   r16, $0F (or any number, your input)
eor   r18, r18
mov   r17, r16
andi  r17, $C0
lsr   r17
lsr   r17
or    r18, r17
mov   r17, r16
andi  r17, $30
lsr   r17
lsr   r17
lsr   r17
lsr   r17
or    r18, r17
mov   r17, r16
andi  r17, $0C
lsl   r17
lsl   r17
lsl   r17
lsl   r17
or    r18, r17
mov   r17, r16
andi  r17, $03
lsl   r17
lsl   r17
or    r18, r17 (the output is `r18`)
0
votes

Pseudo assembly (byte stored in r1):

mov r2, r1
mov r3, r2
mov r4, r3

shr r1, 4
shl r2, 2
shr r3, 2
shl r4, 4

and r1, 0x03
and r2, 0x0C
and r3, 0x30
and r4, 0xC0

xor r4, r3
xor r3, r2
xor r2, r1