0

Given to write a program to find in how many ways can a 32 bit number be rotated to give the same number.

  mov R0, 1431655765   
  mov R6,R0   
  mov R5,31
  mov R4,0   
  .loop :  
     cmp R5,0 
     beq .exit  
     lsl R2,R0,31 
     lsr R0,R0,1 
     sub R5,R5,1 
     or R0,R0,R2 
     cmp R6,R0 
     beq .inc 
     b .loop 
  .inc : 
     add R4,R4,1 
     b  .loop 
  .exit: 
       .print R4 

Though I get 0 as the answer always.Is there some error in my logic?

Here the .print instruction prints the value in a register. lsl is logical left shift lsr is logical right shift b is unconditional branching beq is branch if equal to

Sahay
  • 39
  • 1
  • 4
  • 1
    You should comment your code. Also, explain your logic. If available, use a debugger. If not, walk it through on paper. – Jester Jun 12 '16 at 12:27
  • Yes, there is an error, though not in your idea, just in your code. Has to do with `R2` and `R3`. – Solarflare Jun 12 '16 at 13:01
  • Apparently, opcode r1,r2,r3 means r1 = r2 opcode r3? – aghast Jun 12 '16 at 13:47
  • @AustinHastings Yes. – Sahay Jun 12 '16 at 13:50
  • Looks better now, but it should be very easy for you to just print all intermediate values and check if they make sense (or post them here). And you might want to check the meaning of your operators, `cmp` can be something different than `tst`. – Solarflare Jun 12 '16 at 14:12
  • Also, for optimization purposes you probably don't need to AND your low bit, since you are shifting the whole thing up 31 (all the higher bits will drop off). – aghast Jun 12 '16 at 14:23
  • @AustinHastings And is being used to extract the LSB of R0 and store it in R2 every time the loop is executed. – Sahay Jun 12 '16 at 14:26
  • @Sahay Yes, and then you shift it up 31 places and OR it back into R0. I would suggest you just do something like LSL R2, R0, 31. – aghast Jun 12 '16 at 14:28
  • @AustinHastings edited it, Sir. Still the output shows as zero for every number. – Sahay Jun 12 '16 at 18:00
  • Is `.print` an actual opcode, or a macro? If it's a macro, does it modify registers? – aghast Jun 12 '16 at 20:48
  • It just prints the register content without modifying it. @AustinHastings – Sahay Jun 13 '16 at 04:05

0 Answers0