We are currently experiencing issues with email delivery; this is being resolved as quickly as possible.

stationeers.de Mod workshop · Dominik1199
DE Sign in
  1. Home
  2. IC10 academy
  3. Subroutines with jal and ra

Chapter 6 · Memory & structure

Subroutines with jal and ra

Write code once, use it many times: a jump with a return ticket.

Lesson 16 of 18 10 min

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.

Warning

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.

Tip

If a subroutine calls another one, ra gets overwritten. Then do push ra first and pop ra before returning.

Diagram

Calling and returning from a subroutine Main loop 2 l r0 d0 Temperature 3 jal celsius 4 s d2 Setting r1 5 l r0 d1 Temperature 6 jal celsius 7 s d3 Setting r1 8 j start celsius: sub r1 r0 273.15 j ra ra = 4 back to line ra
jal jumps into the subroutine and remembers the following line in ra. j ra returns exactly there.

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.

Esc
↑ ↓ selectEnter open/ opens search anywhere