Today we will learn how to call subroutine in assembly.
Click here to see the previous part.
According to many comments and my colleagues, it seems to me that many people are bored or difficult to program in the assembly. Perhaps many have given up hope of learning programming. I thought about them and decided that I will write two more tutorials in assembly, in the next tutorials I will do programming in C because programming in C is much easier. I started with assembly because it is better to understand the micro-controller. It is known what micro-controller is working in each machine cycle. I will try to discuss as much as possible in the next two tutorials so that most of the instruction is used.
Let's remember the last programming of the previous day.
CountA equ 0Ch; Define a Variable
CountB equ 0Dh; Define a Variable
BSF STATUS, RP0; Select Bank1
MOVLW b’00000 ’; put 00000 into W register
MOVWF TRISA; Move 00000 into TRISA
BCF STATUS, RP0; Select Bank
START MOVLW b’11111 ’; put 11111 into W register
MOVWF PORTA; turn ON all LEDs
Movlw d’200 ’; Count to 200 times up to 255
Movwf countB
Movlw d’255 ’; Count to 255
Movwf countA
DECFSZ countA, 1
Goto $ - 1; One line will go back.
DECFSZ countB, 1
Goto - 5; Program counter will go back five lines.
MOVLW b’00000 ’; put 00000 into W register
MOVWF PORTA; turn OFF all LEDs
Movlw d’200 ’; Count to 200 times up to 255
Movwf countB
Movlw d’255 ’; Count to 255
Movwf countA
DECFSZ countA, 1
Goto $ - 1; One line will go back.
DECFSZ countB, 1
Goto - 5; Program counter will go back five lines.
GOTO START; Go back to START line
The blue part of the code that you see in the program above has added two delays to our program. Writing a full Delay section separately from the above program stands for,
CountA equ 0Ch; Define a Variable
CountB equ 0Dh; Define a Variable
Movlw d’200 ’; Count to 200 times up to 255
Movwf countB
Movlw d’255 ’; Count to 255
Movwf countA
DECFSZ countA, 1
Goto $ - 1; One line will go back.
DECFSZ countB, 1
Goto - 5; Program counter will go back five lines.
We can fill this delay within a routine and call him as many times as we like. That means today we will learn to call Subroutine. The advantage is that there is no need to write the same code repeatedly, it takes less time and space.
The part of the code that we will use as a subroutine will first take a name and once all the lines of the subroutine are executed, so that he goes back to where he was called from, I will finish the subroutine by writing the return instruction. I give the name of Subroutine to Delay.
CountA equ 0Ch; Define a Variable
CountB equ 0Dh; Define a Variable
Delay Movlw d’200 ’; Count to 200 times up to 255
Movwf countB
Movlw d’255 ’; Count to 255
Movwf countA
DECFSZ countA, 1
Goto $ - 1; One line will go back.
DECFSZ countB, 1
Goto - 5; Program counter will go back five lines.
Return
The first two lines of the above code have been left out of the Subroutine called Delay because these two lines have to be written at the beginning of the Main Program. Let's use the Delay Subroutine in the main program.
COUNTA EQU 0CH; Define a Variable
COUNTB EQU 0DH; Define a Variable
BSF STATUS, RP0; Select Bank1
MOVLW b’00000 ’; put 00000 into W register
MOVWF TRISA; Move 00000 into TRISA
BCF STATUS, RP0; Select Bank0
START MOVLW b’11111 ’; put 11111 into W register
MOVWF PORTA; turn ON all LEDs
CALL DELAY
MOVLW b’00000 ’; put 00000 into W register
MOVWF PORTA; turn OFF all LEDs
CALL DELAY
GOTO START; Go back to START line
; Delay Subroutine is starting from here.
DELAY MOVLW d’200 ’; Count to 200 times up to 255
MOVWF COUNTB
MOVLW d’255 ’; Count to 255
MOVWF COUNTA
DECFSZ COUNTA, 1
GOTO - 1; One line will go back.
DECFSZ COUNTB, 1
GOTO $ - 5; Program counter will go back five lines.
RETURN
In the above program, the micro controller will go down from the first line to complete one instruction after another and will return to the START line after GOTO comes to the START line. This way the inside of the loop will continue to rotate. I have written Delay Subroutine at the bottom of the program, so the Micro controller will never DELAY until you call DELAY.
Whenever a subroutine is called, the micro controller saves the address from where the call was made, that is, where to return after completing the subroutine. This business is called STACK. While one Subroutine is inside another Subroutine, another Subroutine inside it can be called a maximum of 8 Subroutines. To put it simply, you can put another packet inside one packet, another inside it, thus you can keep a maximum of 8 packets. If 9 nested subroutine calls are made, then the STACK of the micro controller will overflow. Again, if you execute the RETURN instruction by mistake without making a subroutine call, then the STACK will be underflow.
I hope everyone will run the program and let me know if there is a problem. It was just like today. In the next episode we will discuss the INPUT of Micro controller. At the same time we will make a programmer to program our Micro controller.
0 Comments