Is there a way to determine whether or not a number is even or odd using marie assembly language?

1k Views Asked by At

Normally you would just use modulus to determine this but since that is not an option I tried to use a repeated subtraction loop and utilized skipcond400 to determine if the value was equal to 0. This was perfectly fine if every time i were to input a number it was even. The problem im having is if the number is odd, that condition would never be met resulting in an infinite loop. I could use skipcond000 as an alternative but it would not be able to tell me if a number is even because odd numbers would never equal 0 as they cannot be divided exactly into pairs. Im stuck on how to even determine this because my ultimate goal is to use this to add all the even numbers leading up to a certain value. In order to do this though I would first need to determine whether user input is even or odd so that way I can have a different set of instructions to do. So far I had something like this in mind but i legitimately have no idea where to go from here. Am i approaching this totally wrong?

ORG 100

Input
Store y //store input in a variable thats not messed with

Load y

Store x //store a duplicate of the input so i can mess with it 


loop, Load x  // loop that does repeated subtraction  

Subt two

Store x

Skipcond 400

Skipcond 000

Jump loop


x, DEC 0

counter, DEC 0

two, DEC 2
2

There are 2 best solutions below

0
On BEST ANSWER

The original code is using a valid approach to solving the task. The pseudo-code below is constructed to show how one might use the limited skip-branching in MARIE. Using label names a comments can help to illustrate expectations at various statements.

  load X into accumulator
detect_even:
  substract 2 from accumulator
  skip if accumulator is positive
    goto zero_or_neg
  # 'accumulator is positive'
  goto detect_even
zero_or_neg:
  # accumulator is -1 or 0
  skip if accumulator is zero
    goto not_even
  # 'accumulator is zero'
  # no op, goto even, or omitted instruction
even:
  # here X is even as accumulator is 0
  # use X, perhaps add to running total?
not_even:

Note that the accumulator is reused for the primary detection loop, much as though X - Y - Y - Y - ... YMMV with negative numbers.

2
On

There's no AND instruction on MARIE, which on other machines would be used to easily isolate the lowest bit.

There are several techniques you could use.  Combining Skipcond is super confusing but can be made to work.  However, you should be aware that Skipcond actually cannot do all the conditions we would like it to do so, we do end up resorting to clever sequences like what you're suggesting.

Your sequence will continue when the current value is zero, which I think is an error.

I believe what you want to do is continue the modulus loop only if the value is > 0.  See this answer (table near the end) to identify sequences for that: https://stackoverflow.com/a/66725608/471129.

You should also be able to use multiple Skipconds in a sequence, but to be repetitive, super confusing though should be workable.


An alternative is to shift the bit of interest.  Each time you double a number, which can be done by adding to itself, that will perform a left shift.  15 of those will move the LSB (which tells us if the number is even/odd) to the MSB position, which can be tested using Skipcond.