Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

If Statement in assembly

Status
Not open for further replies.

redhat

Advanced Member level 4
Joined
Aug 1, 2004
Messages
116
Helped
2
Reputation
4
Reaction score
0
Trophy points
1,296
Location
Egypt
Activity points
979
how can i write an if statement in assembly for example :

if porta(pin0)==0 then
execute this code
else
execute this code
endif

i can't use btss because i want to execute more than one instruction if the condition is true.
thanks in advance
 

Willl only provide pseudocode since it's not stated which platform is used.

Code:
bit_test(test_boolean), skip on clear;
goto state_true;

state_false:
   blah blah blah
   goto state_done;

state_true:
  blah blah blah

state_done:
  bleah blah blah
 

hi,
for 8051 it would look something like
JNZ P1.0 labelelse ;
code ;write code for then here
JMP endlabelelse
labelelse:
code ;write code for else
endlabelelse:
code ;further code

hope this helps!
pimr
 

if you use avr, this an alternative:

ldi r16,0b00000000 ;load register r16 with all 0's
out DDRA, r16 ;configure PORTA for all input
in r18, PINDA ; read PINA buffer
cpi r18, 0b00000001 ; check PINA.0 =1?
brne not_1 ; if PINA.0 not 1 (null) do subroutine not_1
breq is_1 ; if PINA.0 = 1 do subroutine is_1

not_1:
........

is_1:
.........
 

if you are using 8051 , then :

mov c,p1.0
jc notzero
the first code
notzero:
the second code
 

If you are using a Pic, try my assembler code generator. It writes code in Pic assembler for common constructs in programming like if, while, for etc.
 

i am using pic16
thank you all for your help
 

Hey RedHat,
You can include more than one instruction if the condition is true using BTFSS by jumping to some subprogram



BTFSS PORTB,0
call code1
call code2


BTFSS will test bit 0 of PORTB, call code1 if bit0==0,but the return instruction in the end of code1 will load the program counter with the address of the "call code2" insturction.The previous code is modified now by adding a user defined variable TEST.code1 will set bit0 of TEST before return.The program can skip "call code2" now like this :



BCF TEST,0
BTFSS PORTB,0
call code1
BTFSS TEST,0 ; was code1 executed? skip if yes,
call code2 ; execute code2 if not
BCF TEST,0
.
.
.
;-----------------------------------
code1
.
.
.
BSF TEST,0
return
;------------------------------------
code2
.
.

return








Regards
 

Assembler is too low level to have logical sematics ,
INSTEAD these have to be done "by hand" .And basically is done with READING variables (registers or ports) compairing the read value to expected and depending of the compairaison execute a jump to some other address.

So basically :

READ is a mov or a load
COMPAIRE is cmp or another arithmetic
JUMP is jmp
 

on the pic you dont have other choice than btfsc/s
you can use jmp instead of calls to skip the instructions for one condition or the other:

BTFSS PORTB,0
jmp PortIs0
PortIs1 (this label is not really needed)
; execution goes here when portb,0 is 1
: here goes your code for this condition
;
jmp finish
PortIs0
; execution goes here when portb,0 is 0
; your code for this condition

finish
 

A lot of really nice replies here but I've always used this:
CJNE P0.0,#0,ELSE
;code when equal
ELSE:
;code when not equal
RET
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top