myRegister[myOtherRegister] = 1 in assembly?

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

Hello,

I want to use a register to let me specify a bit to set in another register. In a high level, I would like to do something like

myRegister[myOtherRegister] = 1;

Is there something like SBR Rr,K that I can use?

Thank you,
Sam

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

Don't quite understand what you want to do, but you can OR myRegister,myOtherRegister.
So you can use SBR to set the bits in myOtherRegister and then do the oring. Remember that SBR only works with registers 16-31.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

kingmu wrote:
Hello,

I want to use a register to let me specify a bit to set in another register. In a high level, I would like to do something like

myRegister[myOtherRegister] = 1;

Is there something like SBR Rr,K that I can use?

Thank you,
Sam

do you mean you want to set bit number "myRegister" in register "myOtherRegister"?
any reason why you want to do that?
usually i use OR mnemonic.

	ldi R1, bitnumber
	ldi	R0, 1
repeat:	
	SHL	R0
	DEC	R1
	BRNE	repeat
	OR	register, R0

KISS - Keep It Simple Stupid!

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

Thanks guys,

The problem is I don't know which bits I want to set until runtime, so I don't think I can use SBR.

I used a variation of what m23402027 posted

; myOtherRegister contains the index (1-8) of the bit to set
; in myRegister

LDI mask,%01

Loop:
  LSL mask ; This takes us one past our target index
  DEC myOtherRegister
  BRNE Loop

LSR mask ; To correct overstep caused by loop

OR myRegister,mask

I don't know if it's the best way though..

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

Quote:
I don't know if it's the best way though..

That depends on your requirements. If speed is what you need then a lookup table would probably be the fastest.

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

Quote:
I used a variation of what m23402027 posted

oops, looks like i've mistyped SHL with LSL. Nowadays i'm more convenient with C rather assembly. Already forgotten some assembly instructions...

KISS - Keep It Simple Stupid!

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

Quote:
I used a variation of what m23402027 posted

oops, looks like i've mistyped SHL with LSL. Nowadays i'm more convenient with C rather assembly. Already forgotten some assembly instructions...

KISS - Keep It Simple Stupid!

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

This is from an older thread that was a glitch / zbaird creation

;
; Convert any 3 bit r16 value into the corresponding weight single binary bit in r17
;
;	i.e. r16 = 5 would give the result r17 = 1 << 5
;
ldi r17, 0x01 
sbrc r16, 1 
	ldi r17, 0x04 
sbrc r16, 0 
	lsl r17 
sbrc r16, 2 
	swap r17

This method is even faster than doing a table lookup.

Binary values:
------------------------------------
r16 = 000, r17 = 0000 0001
r16 = 001, r17 = 0000 0010
r16 = 010, r17 = 0000 0100
r16 = 011, r17 = 0000 1000
r16 = 100, r17 = 0001 0000
r16 = 101, r17 = 0010 0000
r16 = 110, r17 = 0100 0000
r16 = 111, r17 = 1000 0000

All of the more significant r16 bits are ignored and have no effect on r17.