# how to write basic if statement in ASM

Status
Not open for further replies.

#### shifteh

##### Newbie level 6
ok so basically after reading a fair few things on the net regarding IF statements in asm i understand now that i have NFI lol

can somebody write a quick == for example look at portb , ok so we now have 00011 at portb , subtract 00010 if not left with 00000 then subtract 00001 if not left with etc etc , even a few lines just to pint me in the right direction would be all i need, thanks guys :

\

MPLAB
16F84A

#### wp100

Hi,

This might help...

Code:
DISPLAY = .16           ; Display .128 = 128X84 graphic  or  Display .16  = LCD 16X2 character

IF DISPLAY == .16

call	lcd_init

ENDIF

IF DISPLAY == .128

call	glcd_init

ENDIF

#### shifteh

##### Newbie level 6
with
Display = .16

would i be able to use it like

variable = b'00011'

then change the variable later in the program. ?

#### Tahmid

No, I don't think so. IF is an instruction to the assembler at assemble time, and has no effect at run time.

The 'IF' statement is an assembler directive, not a program instruction.

Depending on whether the condition is true or not, it will assemble the block of code within the IF statement. You use it for example when you want to skip hardware checking while debugging software or is there are similar variants of a program and you want to selectively add or omit blocks of code.

Hope this helps.
Tahmid.

shifteh

### shifteh

Points: 2

#### wp100

Hi,

Might be missing your point but think this is all your need, or the basis for expanding the point.

btfss PORTB,2 ; TEST BIT 2 OF PORT B
goto isnotset ; if zero go here
goto isset ; if one go here

or

movlw b'00000011'
andwf PORTB,W
ACT ON RESULT

shifteh

### shifteh

Points: 2

#### shifteh

##### Newbie level 6
ok so i need to test more than one bit at a time my guess is use btfss and if it finds a 1 in the 0 bit then go to anouther btfss to check if the second bit is there but it would make the program quite long

whats this andwf and how would i act on the response or is it like btfss with 2 following lines like true, false

#### wp100

shifteh
ok so i need to test more than one bit at a time my guess is use btfss and if it finds a 1 in the 0 bit then go to anouther btfss to check if the second bit is there but it would make the program quite long

Hi,

Welcome to Assembler ! - as you can see its generally a 'long' hand way of coding compared to the high level languages.
There are usually a few ways of coding that you can achieve the same function you are after, some folk can do some very clever stuff with assembler, but the btfss/sc is probably the simplest way to comare Bits.

Although not perhaps something you are currently considering, but the 18F range of chips use an enhanced assembler, not dramatically different but with a few more instructions to make things a lot easier, plus paging and banking are virtually elimated.

whats this andwf and how would i act on the response or is it like btfss with 2 following lines like true, false

The AND is just one of several logical arithmetic operations where the data you load is acted on by AND, OR, XOR etc and the result is held in the STATUS register by either the C or Z bits which you again use the BTFSS/SC to check the result.

So for example you wanted to check if bits 0 was high on port B
movlw b'00000001;
andwf PORTB,W
btfsc STATUS,Z ; if the result of the AND is Zero, the Z bit of the STATUS register is set to ONE
; you then check with btfsc/ss to see it the Z bit is 0 or 1

Assume you have got the list if Instructions from the 16F84A Datasheet.

Another possible option for you, though do not know all of what you are trying to achieve, is to use the Interrupts on PortB.
You have 2 choices, for RB0 to Interrupt on the leading or trailing edge of a signal, and RB4-7 to Interrupt when any input changes state.

Also have a look at the Assembler Tutorial sites like Gooligum - they show all the basic methods.

shifteh

### shifteh

Points: 2

#### btbass

Try this program. It generates pic asm code for common c constructs such as if, for, while etc.
No install needed, just extract the files to a directory and run 'PicAsm12.exe'.

#### Attachments

• PicHelp.rar
197.9 KB · Views: 15
vinodstanur

### vinodstanur

Points: 2

#### shifteh

##### Newbie level 6
movlw b'00000001;
andwf PORTB,W
btfsc STATUS,Z ; if the result of the AND is Zero, the Z bit of the STATUS register is set to ONE
; you then check with btfsc/ss to see it the Z bit is 0 or 1

this is exactally what i need , thanks so much

so basically what im doing is watching porta for a high on bits 0 and 1 (up and down) then once detected it then goes and finds out which gear its is already in to know the next one to change to weither it be up or down

first gear 00011
second gear 00001
Third gear 00000
Forth gear 00010

thanks everyone for there help will test the andwf when i get back to my compiling pc

#### wp100

this is exactally what i need , thanks so much

so basically what im doing is watching porta for a high on bits 0 and 1 (up and down) then once detected it then goes and finds out which gear its is already in to know the next one to change to weither it be up or down

first gear 00011
second gear 00001
Third gear 00000
Forth gear 00010

thanks everyone for there help will test the andwf when i get back to my compiling pc

Hi,

Afraid I did not fully check the logic of using the AND with your needs.

You need to use the SUB instead ,sample code attached which I have checked runs on all four logic states.
The code is only enough for simulation, you will need to add the Config statement for your hardware.

#### Attachments

• gear.rar
73.2 KB · Views: 10
shifteh

### shifteh

Points: 2

#### shifteh

##### Newbie level 6
ok maby i did not describe it well enough

Porta is used for watching the gear shifter which is up and down , 00001 for up 00010 for down

Portb is used for connecting to the transmission

first gear 00011
second gear 00001
Third gear 00000
Forth gear 00010

thease are the pins that need to be high to change the gears via relays to control the hydrolic solenoids

so
Watch portA
If input is detected use btfsc to then goto either UP or DOWN subroutine

in that subroutine it must check the portb to see which gear its in, so if portb = 00011 we now know we are in first gear( thats what i need this code for) then we goto ok we are in 00011 now goto 00001(which is second gear) so set portb to 00001

and so forth hope this makes it a tad clearer

#### sahu

Try this program. It generates pic asm code for common c constructs such as if, for, while etc.
No install needed, just extract the files to a directory and run 'PicAsm12.exe'.

nice...very nice
where i found for PIC16

#### wp100

Hi,

Was just showing you how you could do your gear select sensing using the Sub instruction as against the And instruction I incorrectly said would do it.

Port A can be similarly interrogated by either reading the full port or by testing just the one bit as mentioned earlier.

Hopefully you can now see how you can construct the whole routine, though its often worth while doing a flowchart before you start coding to check your logic is good.

happy coding...

shifteh

### shifteh

Points: 2

#### shifteh

##### Newbie level 6
i have read your code and well thanks a heap, i this done in MPLAB???

cblock 0x0C ; specify user variables ( registers)
GEAR_INPUT
SELECTOR
endc

org 0x00
goto start

in this im guessing i could add for example "VARIABLE" and move things into it??? like W????
or am i missing the point?

#### wp100

Hi,

Once installed you need to set up a 'project' for the code - if stuck I can do one for you and post it.

The Variables / User Resgisters are 1 byte blocks of data in RAM memory that you can assign a Name to rather than a complicated hex location.
Its used for storing any 8 bits of data you want, it is normally done by W receiving the data and then moving that data into the file.
movlw b'11110000' ; load data 0xF0 into W
movwf VARIABLE1 ; move data to VARIABLE1 and retain till power off or another move instruction.

Sounds like you really need to start with a basic Pic tutorial like this Gooligum Electronics

shifteh

### shifteh

Points: 2

#### shifteh

##### Newbie level 6
ok so i have it working

in the gear selections subroutine part, you will see how ive used BTFSC and BTFSS to see which gear its in by checking each bit

due to a comunications problem the gears are actually as follows
First 11
Second 01
third 00
Forth 10

im in the process now of incorporating an lcd into the design
Thanks everybody you have been an awsome help

list P=16F84A,
include <P16f84A.inc>
ERRORLEVEL -302

cblock 0x0C ; specify user variables ( registers)

endc
org 0x00
COUNT1 equ 08h
bsf 03h,5 ;Go to Bank 1
movlw b'00000' ;Put 00000 into W
movwf TRISA ;Move 00000 onto TRISA
movlw b'0000000'
movwf TRISB ; move 00000000 into trisb
bcf 03h,5 ;Come back to Bank 0

movlw b'00011' ;sets output as first gear
movwf PORTB ;portb

;----------------------------
; MAIN PROGRAMM LOOP
;----------------------------
main

Loop1 decfsz COUNT1,1 ; GEAR CHANGE DELAY , Count to 255 twice, 1 Second Delay Between Reading for a gear selection
goto Loop1 ;
Loop2 decfsz COUNT1,1 ;
goto Loop2

btfsc PORTA, 0 ;Is "UP" button pressed?
CALL GEARUP ;Higher gear selection has been made

btfsc PORTA, 1 ;Is "DOWN" button pressed?
CALL GEARDOWN ;Lower gear selection has been made

loop goto main
;----------------------------------------------------------------
; Gear selection subroutines
;-----------------------------------------------------------------

GEARUP
bcf PORTB, 3
btfsc PORTB, 0
goto FSU ;first and second
goto TFU ;Third and forth

FSU btfsc PORTB, 1
goto second_gear
goto third_gear

TFU btfss PORTB, 1
goto fourth_gear
bsf PORTB, 3
return

GEARDOWN
bcf PORTB, 3
btfsc PORTB, 0
goto FSD
goto TFD

FSD
btfss PORTB, 1
goto first_gear
bsf PORTB, 3 ;
return
TFD
btfss PORTB, 1
goto second_gear ; if 0
goto third_gear ; if 1
return
;------------------------------------------------------------
;GEAR Communication to Transmission
;------------------------------------------------------------
first_gear
movlw b'00011'
movwf PORTB
return
second_gear
movlw b'00001'
movwf PORTB
return
third_gear
movlw b'00'
movwf PORTB
return
fourth_gear
movlw b'00010'
movwf PORTB
return

end

#### wp100

Hi,

Glad you have got it running ok - a few things I would do to make the code easier to use particularly when your code becomes more complex.

When changing memory Banks, simply use the ' Banksel ' instruction as previoulsy shown

You will need to use a Config statement at the beginning of your code, unless the defaults are what you need.
You need to look into this, you can find details in the 16F84A.TEMP Template file and 16F84A.INC Include file , both found in directories within MPASM.

The lcd routine is a difficult one to start coding for yourself, you will find good examples here -

PIC Tutorial* Three - LCD Modules
LCD Drivers
EPE FAQs & Resources

#### wp100

Hi,

Just had chance to look at your current code in a bit more detail - there are a few things you need to address.
They are all the typical beginners mistakes so you are not alone.
This is where a good tutorial will help.

They all relate to the Set Up phase of your code.

First the Delay routine, perhaps nothing wrong with it, it states a 1 second delay, but without knowing the oscillators frequency its impossible to check - always notate your delays as '1sec at 4meghz' so if you change the oscillator its easy to see you must change the delay routine to match it.

You have specified the variable Count1 by using the EQUates statement COUNT1 equ 08h.
Look at the datasheet section DATA MEMORY and the REGISTER FILE MAP.
You can see the RAM memory is split into two 'banks' and that the first 12 locations in both banks are what the pic system uses for its 'work' files.
You can now see that location 0x08 is used by the system so your COUNT1 could be overwritten at any time.
You can only use Bank 0 from location 0x0C for your USER variables.
As you can see entering EQU 0x0C etc for each variable is a long way of doing things.
If you put COUNT1 in the Cblock section the system automatically gives it the next free address.

Your routine uses several switch INPUTS and some OUTPUT to your tranny.
The Pics Ports have to be set up accordingly,
First the data direction, Input or Output has to be set by means of the TRIS instruction.
1 for Input and 0 for output.
When specifing the bits of anything its always better to show all 8 bits, not just the odd ones you think you are changing eg b'11110001'
At the moment you have all the portA and B pins set as ' 0 ' Outputs

Status
Not open for further replies.