Two sensors, two displays, the same conversion twice – copying costs lines and characters. Better: write the calculation once as a subroutine.
jal: a jump with a return ticket
jal celsius # jumps to celsius, ra = next line
jal jumps like j, but first stores the number of the next line in the register ra. At the end of the subroutine, j ra brings you right back there.
start:
yield
l r0 d0 Temperature
jal celsius # r1 = r0 in degrees C
s d2 Setting r1
l r0 d1 Temperature
jal celsius
s d3 Setting r1
j start
celsius:
sub r1 r0 273.15
j ra # back to the caller
Input and output travel through agreed registers: here celsius expects the value in r0 and returns the result in r1.
The subroutine belongs after the j start. Otherwise the chip runs into it at the end of the main loop, and j ra jumps to wherever the last call left it.
If a subroutine calls another one, ra gets overwritten. Then do push ra first and pop ra before returning.
Diagram
Exercise
Task
Two gas sensors on d0 and d1, two LED displays on d2 and d3. Each display shows the temperature of “its” sensor in °C (d0 → d2, d1 → d3).
Write the conversion only once as a subroutine and call it twice with jal.
Hints
Hint 1
One line is enough inside the subroutine: sub r1 r0 273.15.
Hint 2
In the main loop: read the sensor into r0, jal celsius, then write r1 to the display – twice.