In a unit-cost random access machine, the value of a register $$r_i$$ can be set to the sum of other two registers $$r_j$$ and $$r_n$$ using basic $$L O O P$$ commands as well as the algorithm for $$r_i:=r_j\pm c$$ and the algorithm for the NOP operation.

### Implementing a unit-cost random access machine capable to represent negative numbers and to subtract registers

Although the unit-cost random access machine is not able to store negative numbers in its registers, it is not much more complicated to calculate the difference of two registers $$r_j$$ and $$r_n$$, because negative integers can be represented by pairs of natural numbers, as shown in the definition of integers. Thus, it is possible to build a unit-cost random access machine, which is able to store negative numbers using more auxiliary registers representing integers as pairs of natural numbers and implementing an appropriate subtraction operation on these pairs.

# Algorithm: Addition (or Subtraction) of Two Registers

class UCRAM(): # unit-cost random access machine with 3 registers r_i = 0 r_j = 0 r_n = 0

  def set_ri_to_0(self):
while True:
if self.r_i &gt; 0:
self.r_i = self.r_i - 1 #  LOOP register i
self.NOP()  #  DO nothing
else:
break

def LOOP_rj_DO_increment_r_i(self):
while True:
if self.r_j &gt; 0:
self.r_j = self.r_j - 1 #  LOOP register j
self.r_i = self.r_i + 1 #  DO increment register i
else:
break

def LOOP_rn_DO_increment_ri(self):
while True:
if self.r_n &gt; 0:
self.r_n = self.r_n - 1 #  LOOP register n
self.r_i = self.r_i + 1 #  DO increment register i
else:
break

self.set_ri_to_0()
self.LOOP_rj_DO_increment_r_i()
self.LOOP_rn_DO_increment_ri()

def NOP(self):
self.r_i = self.r_i + 1 #  LOOP register i
self.r_i = self.r_i - 1  # LOOP register i

1. Usage for adding the r_i = r_j + r_n
2. creating a unit-cost access machine with registers initially set ucram = UCRAM() ucram.r_j = 7 # assumed initial value for r_j ucram.r_n = 123 # assumed initial value for r_n

print(ucram.r_i, ucram.r_j, ucram.r_n)

# setting r_i:=r_j

print(ucram.r_i, ucram.r_j, ucram.r_n)

1. will output
2. 0 7 123
3. 0 7 123

Algorithms: 1 2
Examples: 3

Thank you to the contributors under CC BY-SA 4.0!

Github:

### References

#### Bibliography

1. Erk, Katrin; Priese, Lutz: "Theoretische Informatik", Springer Verlag, 2000, 2nd Edition