views:

44

answers:

1

Hello!

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 instructons?

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!

A: 

Go this answer on CSTheory:

Initialise x←v and y←−v. Then increment both x and y in parallel until one of them is equal to 0.

By Jukka Suomela

Sérgio Nunes