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.

[SOLVED] How to do if-else in assembly language?

Status
Not open for further replies.

skyassasin16

Junior Member level 2
Joined
Jan 10, 2011
Messages
24
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,283
Activity points
1,453
hi everyone, im new here..
im using MPLAB IDE and working on PIC16F877A.
how can i translate this algoritm into an assembly language?

if W = 1
goto sub_1
if W = 2
goto sub_2
if W = 3
goto sub_3

thanks
 

wp100

Advanced Member level 5
Joined
May 15, 2009
Messages
3,056
Helped
883
Reputation
1,781
Reaction score
736
Trophy points
113
Location
Prime Meridian
Activity points
0
Hi,

You can use the Assembler Directives - see the full list in the Microchip Assembler Users Guilde.

Code:
DISPLAY = 204  ;Display 162 = 16X2 character, 204 = 20x4



       IF DISPLAY == 204
       
       ; user code
       
        ENDIF

       IF DISPLAY == 162
        
       ; user code
        
       ENDIF
 

krishna1234

Member level 2
Joined
Dec 18, 2010
Messages
49
Helped
9
Reputation
18
Reaction score
9
Trophy points
1,298
Location
mumbai,india
Activity points
1,616
DECFSZ W, 1
goto L1
goto sub_1
L1:DECFSZ W, 1
goto L2
goto sub_2
L2:DECFSZ W, 1
goto L3
goto sub_3
L3 : ;non 123

---------- Post added at 15:22 ---------- Previous post was at 15:09 ----------

this only for positive values, if there -ve values exists first check the borrow then use this code.
 

keith1200rs

Super Moderator
Staff member
Joined
Oct 9, 2009
Messages
10,877
Helped
2,065
Reputation
4,130
Reaction score
1,597
Trophy points
1,403
Location
Yorkshire, UK
Activity points
57,269
BE CAREFUL. Assembler directives are totally different to conditional assembly code. As assembler directive such as IF ENDIF will include the code in your final assembly code or not based on a value when you assemble it - NOT AT RUNTIME.

Keith.
 

bobsanjose

Advanced Member level 1
Joined
Jan 13, 2006
Messages
416
Helped
56
Reputation
112
Reaction score
21
Trophy points
1,298
Location
Silicon Valley
Activity points
5,228
skyassasin16,
Your style looks a lot like Basic. My recommendation would be to write this small code in "C" and hand it over to the MPLAP C-Compiler. Then look into the list-file how the C-Compiler does it. Might be a bit cryptic in the beginning but if you read through it a few times you should get the hang of it.

hth, Bob
 

PaulHolland

Advanced Member level 2
Joined
Jan 15, 2003
Messages
664
Helped
95
Reputation
190
Reaction score
34
Trophy points
1,308
Location
Holland
Activity points
4,841
The following example is a working example of how a switch statement can be made, NOTE only works if your index value is like in your example 0,1,2,3,4,5,6,7 etc..

Code:
SWITCH:		        movwf	tmp 			; Save the input value
				movlw	HIGH (table + 1)  ; Calculate the start of the table
				movwf	PCLATH		;
				movf	        tmp,w	        ;
				addlw 	LOW  (table + 1)  ; Add input value to Table Pointer				
				btfsc	        STATUS,C	        ;
				incf	        PCLATH,f	        ; Adjust table start if it crosses a boundary of 256.

table:			        movwf	 PCL
	                        goto           L0
				goto           L1
				goto           L2		   
				goto           L3

L0:                                                   ; Put your code here for W = 0
                                return
L1:                                                   ; Put your code here for W = 1
                                return
L2:                                                   ; Put your code here for W = 2
                                return
L3                                                    ; Put your code here for W = 3
                                return
 

skyassasin16

Junior Member level 2
Joined
Jan 10, 2011
Messages
24
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,283
Activity points
1,453
I'm new to this forum, how can I thank you guys? are there are any reputation scoring in here?
 

btbass

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,897
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,863
Some time ago I wrote a utility to help write assembler code for Pic16 series micros and posted it on eda board here.



It can generate code for delays of various lengths, loops conditionals, etc.

No install is needed, just extract all the files to a directory. Call the directory PicHelp or whatever you like.
Generates code for common constructs in programming.

Please give it a go and see if it helps.
 

keith1200rs

Super Moderator
Staff member
Joined
Oct 9, 2009
Messages
10,877
Helped
2,065
Reputation
4,130
Reaction score
1,597
Trophy points
1,403
Location
Yorkshire, UK
Activity points
57,269
I'm new to this forum, how can I thank you guys? are there are any reputation scoring in here?

You should see a "Helpful? Please click" button at the bottom of most messages if you are logged in. Click that for the helpful posts.

Keith
 

skyassasin16

Junior Member level 2
Joined
Jan 10, 2011
Messages
24
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,283
Activity points
1,453
The following example is a working example of how a switch statement can be made, NOTE only works if your index value is like in your example 0,1,2,3,4,5,6,7 etc..

Code:
SWITCH:		        movwf	tmp 			; Save the input value
				movlw	HIGH (table + 1)  ; Calculate the start of the table
				movwf	PCLATH		;
				movf	        tmp,w	        ;
				addlw 	LOW  (table + 1)  ; Add input value to Table Pointer				
				btfsc	        STATUS,C	        ;
				incf	        PCLATH,f	        ; Adjust table start if it crosses a boundary of 256.

table:			        movwf	 PCL
	                        goto           L0
				goto           L1
				goto           L2		   
				goto           L3

L0:                                                   ; Put your code here for W = 0
                                return
L1:                                                   ; Put your code here for W = 1
                                return
L2:                                                   ; Put your code here for W = 2
                                return
L3                                                    ; Put your code here for W = 3
                                return

When I try this sir, when W value goes higher than 3, the whole program crashes. I try to add more goto in the table subroutine but still crashes. anyways, thanks everyone.
 
  • Like
Reactions: vishy71

    vishy71

    Points: 2
    Helpful Answer Positive Rating

PaulHolland

Advanced Member level 2
Joined
Jan 15, 2003
Messages
664
Helped
95
Reputation
190
Reaction score
34
Trophy points
1,308
Location
Holland
Activity points
4,841
Do not forget to put a return behind every part of code !. I use this upto 256 and they all work. Also call this part of code with a call SWITCH !!

Code:
SWITCH:		        movwf	tmp 			; Save the input value
				movlw	HIGH (table + 1)  ; Calculate the start of the table
				movwf	PCLATH		;
				movf	        tmp,w	        ;
				addlw 	LOW  (table + 1)  ; Add input value to Table Pointer				
				btfsc	        STATUS,C	        ;
				incf	        PCLATH,f	        ; Adjust table start if it crosses a boundary of 256.

table:			        movwf	 PCL
	                        goto           L0
				goto           L1
				goto           L2		   
				goto           L3
	                        goto           L4
				goto           L5
				goto           L6		   
				goto           L7


L0:                                                   ; Put your code here for W = 0
                                nop etc..
                                nop etc..
                                return
L1:                                                   ; Put your code here for W = 1
                                return
L2:                                                   ; Put your code here for W = 2
                                return
L3                                                    ; Put your code here for W = 3
                                return
L4:                                                   ; Put your code here for W = 4
                                return
L5:                                                   ; Put your code here for W = 5
                                return
L6:                                                   ; Put your code here for W = 6
                                return
L7                                                    ; Put your code here for W = 7
                                return
 

skyassasin16

Junior Member level 2
Joined
Jan 10, 2011
Messages
24
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,283
Activity points
1,453
Yes, I tried the PicHelp also but unfortunately, even if the value is correct e.g., value == 1, is does not jump into the _TRUE subroutine. dunno what im doing wrong,. =(
 
  • Like
Reactions: vishy71

    vishy71

    Points: 2
    Helpful Answer Positive Rating

btbass

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,897
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,863
I don't understand?
If I ask it if x == 1, it generates this code: which is correct.

Code:
;------ if routine

_IF	movf	x,w
	sublw	.1
	bnz	_FALSE

_TRUE	;CODE BODY HERE!

_FALSE	;FAILED IF TEST
 

vishy71

Full Member level 2
Joined
Dec 16, 2010
Messages
126
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
Iran
Activity points
2,295
Hi

movf (your register name),0
sublw 0x01

btfsc 0x03,2 ;if Z bit in STATUS register is 1 then goto line1
goto (or call) line1
goto (or call) line2 ;Z bit is 0 (your register value <=> 0x01 (1)) so got line2

line1
;your rutin

line2
;your rutin
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top