marie

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


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

Solution

  • 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.