How create an Little Mans Computer (LMC) code that will get a number. Display 1 if the number is odd, display 0 if the number is even

后端 未结 3 1567
夕颜
夕颜 2021-01-29 15:19

I need help in my study\'s where it can display 1 if the number is even and 0 if the number is odd. for example if the input is 99 it will display the output 1 which means odd.

3条回答
  •  既然无缘
    2021-01-29 16:07

    Margaret's answer expects the accumulator to have a determined value when the negative flag is set by a previous subtraction of 2. Or at least that adding 2 to that would bring the accumulator back in its previous state.

    This may be true in some implementations, but there is no guarantee. In theory the LMC only deals with non-negative numbers, and the only notion of negative is in the "negative" flag. Wikipedia warns that the accumulator's value is undefined after a subtraction leads to overflow on the negative end.

    There is for instance this implementation of an LMC where the accumulator will be set to -1 whatever the magnitude of the overflow is, and so the distinction between odd and even is lost at that moment. Many will consider this a very odd implementation, but it is compliant with the LMC specification.

    Here is a version that does not make assumptions beyond the LMC specification:

    #input: 9
           INP
      loop STA result
           SUB two
           BRP loop
           LDA result ; restore value from before subtraction
           OUT
           HLT
       two DAT 2
    result DAT
      
    

提交回复
热议问题