2
$\begingroup$

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!

$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – Jukka Suomela Sep 21 '10 at 11:47
4
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.