Functions and Subroutines (Call / Return) : A-Program 1: Arithmetic Subroutine
Functions and Subroutines (Call / Return) : A-Program 1: Arithmetic Subroutine
In this lab, we will learn how we call a subroutine (function) using call /return
instructions.
The advantage of using functions is that we can use it (call) several times in the
program while we write it only once.
“CALL” expects a “RETURN” at the end of the subroutine so the micro will get back
automatically to the MAIN and to the exact line where he left from.
The GOTO just jumps to a certain line and doesn’t come back unless we use another
GOTO.
We will consider a first program which uses a function that makes the addition of the
2 numbers in memory address VALUE1 ( 0x20) with memory address VALUE2
(0x21) and store result of this addition in memory address RESULT (0x22).
In our program, the MAIN will fill these variables twice (differently) and call
the subroutine ADD_V1_V2 each time to execute for him this addition.
So the function ADD_V1_V2 have 2 inputs variables which are VALUE1 and
VALUE2 (regardless of their exact content), that it will add their content and write
the output of the operation in Memory RESULT.
The MAIN will then read the “result” filled by the function and save it in memory
ANSWER1 (0x23) the first time, and in ANSWER2 (0x24) the second time.
1
Microlab File 8 – by Joseph Massoud
2. In a first approach, use STEP BY STEP execution in order to better track the
flow of the program. Check file 2 as reminder on step by step.( you can later
repeat the execution using animate)
You will notice how after the first “CALL” it will move to the subroutine and
execute it , and the “ RETURN” will lead you back to the MAIN at the line just
after the first call; and in the second call the return will get back to the line after
the second call.
END
2
Microlab File 8 – by Joseph Massoud
3
Microlab File 8 – by Joseph Massoud
B- Assignment 17
Write a program which uses two subroutines to add and subtract the 2 numbers,
and store results, and MAIN will save them in 2 variables that we name
ADDITION and SUBTRACTION respectively.
You will store these numbers in 2 addresses we will name “ NUMBER1” and
“NUMBER2” and call the 2 functions consecutively to execute theses operations.
In the MAIN we fill NUMBER1 with 06 and NUMBER2 with 02 .
You choose the addresses for all the above variables and define them in EQU.
4
Microlab File 8 – by Joseph Massoud
CONFI same
MAIN
CALL ADD_V1_V2
MOVF RESULT,W
MOVWF ADDITION
CALL SUB_V1_V2
MOVF RESULT,W
MOVWF SUBTRACTION
LOOP GOTO LOOP
ADD_V1_V2
Adding of number1 and number2 and store in variable RESULT
RETURN
SUB_V1_V2
Subtracting number1 and number2 and store in variable RESULT
RETURN
END
5
Microlab File 8 – by Joseph Massoud
C- Assignment 18
Remarks:
The content of these memories to be filled in the “WATCH” section and not
the program. ( Refer to file6).
In this program we use an infinite loop ( GOTO MAIN not GOTO LOOP),
because the content of the variables can change.
Verify your result on PORTD each time you change the content of the 3
variables.
The function should add 3 variables according to the question, so it should
include 2 times ADDWF .
In the stimulus , it is enough to define RB2 (Toggle) only; refer to file7.
REMINDER
To change content of variables in the WATCH you need to be in the
PAUSE mode;
To use “fire” in order to toggle RB2, you should be in the running mode
(animated)
GOOD LUCK