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.

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

Status
Not open for further replies.

skyassasin16

Junior Member level 2
Junior Member level 2
Joined
Jan 10, 2011
Messages
24
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,283
Visit site
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
 

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
 
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.
 
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.
 
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
 
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
 
I'm new to this forum, how can I thank you guys? are there are any reputation scoring in here?
 

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.
 
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
 
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
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
 
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
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
 
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

Back
Top