Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Assembly - checking and writing bits to a register

Status
Not open for further replies.

Spadez

New Member
Hi.

This seems like quite a basic question but im not sure how to do it and a basic google search didn't turn up anything.

Firstly, if I have a register called colour with the content:

0000 0101

I want to check the 2nd bit in the register, and see if it is a 0 or a 1. How do i do this in assembly language?

Furthermore, after checking the result is 0 and I then want to write a 1 to the 2nd bit in the register. How do I do this?

Thanks!

James
 
Hi,

To test the state of a bit in a register use the BTFSS or BTFSC instructions.
See the Instruction Set Summary towards the end of the Datasheet for the chip your are using.

To change one or more bits of a register use the BSF or BCF instructions

Code:
                movlw b'00001010'
		movwf	d1
		
		btfss	d1,1
		goto	iszero
notzero         bcf	d1,1
		result is b'00001000'
 
Thank you for the reply. I didnt expect another English user to be replying this late!

So if d1 is my register, and I want to change bit 2 to 0 but leave all the other bits untouched, I can do this:

bcf d1,1

As another example, if I had this:


movlw b'10110011'
movwf d1

and I want to change d1 to 11110011 (without affecting any other bits) then I do this:


I hope ive got that right, if so, I understand :)
I knew BSF could be used on the port and tris registers but I wasnt sure if they could also be used on user defined registers.

-----------

EDIT:

If i used this in my header:

BLUE EQU 6

Could I then do:

bsf d1,BLUE
 
Last edited:
You should use #define,
Code:
#define Blue 6

;and

    bsf d1,Blue

or,
Code:
#define Blue d1,6 

;and

    bsf Blue

Mike.
 
Hi,

Yes you have the idea, thats the easy way, though there are other ways of doing the same thing.

Do you use Mplabs Sim ? - its very helpful for single stepping through your code one instruction at a time and viewing W and the registers values changing in the Watch windows
 

Attachments

  • ScreenShot001.jpg
    ScreenShot001.jpg
    134.1 KB · Views: 141
Status
Not open for further replies.

Latest threads

Back
Top