FARM
ASSEMBLY

Hello stranger!

Your grandfather owns a small countryside farm. He is eagerly waiting for the springtime when fields are plowed and crops planted. Grandfather has asked you to check if his old trusty, but rusty tractor still works and if it needs repairs.

You encounter the old tractor inside your grandfather's barn and you realize that it doesn’t seem to ignite. You wonder what might be the problem?

Luckily your frugal grandfather has saved the old manual. You wipe out the dust on its cover and find out that the microcontroller inside the tractor might be toasted. To start the tractor, you need to calculate the ignition verification code. Luckily the manual contains the source code for the calculation.

Assembly reference

Farm assembly operates on four general purpose registers r1, r2, r3 and r4. These registers have signed integer value and are initialiazed to value 0.

Following instructions are supported:

mov dest[reg] src[reg|imm]

Move value src to register dest

add dest[reg] src[reg|imm]

Add value src to register dest

neg dest[reg]

Negate the value of register dest

jgz op1[imm|reg] op2[imm|reg]

Jump by op2 instructions (relative to current instruction) if op1 is greater than zero

jmp op1[imm|reg]

Jump by op1 instructions unconditionally relative to current instruction

In above reference, [reg] is any of the general purpose registers and [imm] is an immediate value (signed integer).

Example program:

mov r2 10
add r1 r2
add r2 -1
jgz r2 -2
neg r1

Program sets r2 to 10, and decrements it to zero while adding each value to r1, effectively calculating sum of numbers from 1 to 10. The result is then negated.

Your task is to simulate the ignition calculation linked below. Run the program and find what the value of r1 register is (in above example, the answer would be -55).

Ignition.fasm

Your answer: