Functions and Subroutines (Call / Return) : A-Program 1: Arithmetic Subroutine

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Microlab File 8 – by Joseph Massoud

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.

A subroutine (function) is a secondary program that can be called by the main


program to execute a certain function.

The advantage of using functions is that we can use it (call) several times in the
program while we write it only once.

We usually write all our functions below the MAIN loop.

Difference between “CALL” and ‘GOTO” instructions:

“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

1. Create a program lab81.asm . Write the program, quickbuild, WATCH.

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.

VALUE1 EQU 0X20


VALUE2 EQU 0X21
RESULT EQU 0X22
ANSWER1 EQU 0X23
ANSWER2 EQU 0X24

CONFI SAME as before

INIT CLRF VALUE1


CLRF VALUE2
CLRF RESULT
CLRF ANSWER1
CLRF ANSWER2

MAIN MOVLW H’05’


MOVWF VALUE1
MOVLW H’08’
MOVWF VALUE2
CALL ADD_V1_V2
MOVF RESULT,W
MOVWF ANSWER1
MOVLW H’06’
MOVWF VALUE1
MOVLW H’02’
MOVWF VALUE2
CALL ADD_V1_V2
MOVF RESULT,W
MOVWF ANSWER2
LOOP GOTO LOOP

ADD_V1_V2 MOVF VALUE1,W


ADDWF VALUE2,W
MOVWF RESULT
RETURN

END

2
Microlab File 8 – by Joseph Massoud

3. After execution ( step by step or animate) , you will see that


ANSWER1 contains 13 D ( H’0D’) which is 8 + 5
and ANSWER2 contains 08 which is 5+3

3
Microlab File 8 – by Joseph Massoud

B- Assignment 17

4. Create a program LAB82.ASM .

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 .

The result in the variable ADDITION will be : 08 ( 6+2)

The result in variable SUBTRACTION will be : 04 ( 6-2)

You choose the addresses for all the above variables and define them in EQU.

5. QUICKBUILD and ANIMATE ( see structure below)

4
Microlab File 8 – by Joseph Massoud

The structure of the program will be:

All the EQU

CONFI same

INIT clear all variables

MAIN

You load NUMBER1 variable with value 06


You load NUMBER2 variable with value 02

CALL ADD_V1_V2
MOVF RESULT,W
MOVWF ADDITION

You load NUMBER1 variable with value 06


You load NUMBER2 variable with value 02

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

6. Create a program LAB83.ASM .

Write a program which keeps testing input switch B2 ;


If B2 pressed (On) the program calls a function which adds the content of the
three memory addresses 0x27, 0x28 and 0x29 and store the result on PORTD.
If B2 not pressed the program will write 00 on PORTD.

7. Quickbuild- WATCH- STIMULUS- ANIMATE

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

You might also like