<p>Initialise $x \gets v$ and $y \gets -v$. Then increment both $x$ and $y$ in parallel until one of them is equal to 0.</p>

CC BY-SA 2.5
2010-09-21T11:24:05.897Z
2010-09-21T11:24:05.897Z
Jukka Suomela
<p>So I'm learning to program in an assembly language for an abstract machine wich is very similar to an <a href="http://www.proofwiki.org/wiki/Definition%3aUnlimited_Register_Machine" rel="nofollow">URM Machine</a>.
An URM machine has 3 basic instructions (or 4 in some literature):<br>
        To zero a register: Z(r)<br>
        To increment a register: S(r)<br>
        To Jump to a line or label if registers r1 and r2 contain the same value: J(r1,r2,l)<br>
Now, my abstract machine is even weaker because, for jumping it only alows a comparison between a register and the literal 0.<br>
To compensate it allows to assign any value to a register (not just zero as in the URM) and basic arithmetic operations.<br>
Both machines allow an infinite number of registers.</p>

<p>I was able to write a program that successfully compares two positive numbers and returns the maximum.<br>
Now I would like to make my program able to receive also negative numbers.  </p>

<p>My question: How can I check if a number is negative? Is it even possible with only these instructions? </p>

<p>I confess that I'm not very clever with this kind of low level languages...  </p>

<p>My maximum program follows: (input goes on r1 and r2 and output comes on r3)</p>

<pre><code>maximo(){
    r5 := r1 - r3    
    jump (r5,0,maxr2)

    r5 := r2 - r4
    jump (r5,0,maxr1)


    r3 := r3 + 1
    r4 := r4 + 1
    jump (r1,r1,maximo)
}

maxr1(){
    r3 := r1 
}

maxr2(){
    r3 := r2 
}
</code></pre>

<p>Thank you!</p>

CC BY-SA 2.5
2010-09-21T11:47:56.123Z
By the way, if you can subtract, you can reduce a comparison $a = b$ to a subtraction $x \gets a - b$ and comparison $x = 0$. Hence it should be obvious that your machine can easily do anything that a URM can do.
Jukka Suomela
<ds.algorithms><machine-models>
Decide if number is negative (in URM machine like language)
Sérgio Nunes
# Decide if number is negative (in URM machine like language)

(Asked by: username_0 on 2010-09-21)

So I'm learning to program in an assembly language for an abstract machine wich is very similar to an URM Machine.
An URM machine has 3 basic instructions (or 4 in some literature):
        To zero a register: Z(r)
        To increment a register: S(r)
        To Jump to a line or label if registers r1 and r2 contain the same value: J(r1,r2,l)
Now, my abstract machine is even weaker because, for jumping it only alows a comparison between a register and the literal 0.
To compensate it allows to assign any value to a register (not just zero as in the URM) and basic arithmetic operations.
Both machines allow an infinite number of registers.
I was able to write a program that successfully compares two positive numbers and returns the maximum.
Now I would like to make my program able to receive also negative numbers.  
My question: How can I check if a number is negative? Is it even possible with only these instructions? 
I confess that I'm not very clever with this kind of low level languages...  
My maximum program follows: (input goes on r1 and r2 and output comes on r3)
maximo(){
    r5 := r1 - r3    
    jump (r5,0,maxr2)

    r5 := r2 - r4
    jump (r5,0,maxr1)


    r3 := r3 + 1
    r4 := r4 + 1
    jump (r1,r1,maximo)
}

maxr1(){
    r3 := r1 
}

maxr2(){
    r3 := r2 
}

Thank you!

**Tags:** ds.algorithms, machine-models

> **Comments:**
> **username_1**: By the way, if you can subtract, you can reduce a comparison $a = b$ to a subtraction $x \gets a - b$ and comparison $x = 0$. Hence it should be obvious that your machine can easily do anything that a URM can do.  

---

## 1 answers

### Answer by username_1 on 2010-09-21. Score: 4 | Accepted answer

Initialise $x \gets v$ and $y \gets -v$. Then increment both $x$ and $y$ in parallel until one of them is equal to 0.


---

