2

Suppose I have a to compare a data register and i have to compare it to equalling one of 2 numbers. how would i go about that?

I know how to do it for just comparing with one number not 2.

CMP #0, D3
BNE ELSE
REST OF THE CODE HERE

How do i compare for when I want to compare it with either 0 or some other number like 7. In c++ you would say

if(x == 0 || x == 7)
{code here}
else
{code here}
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
romano
  • 23
  • 2
  • Not really part of your problem, but instead of `cmp #0,d3` you should of course use `tst d3`. Also, if you don't need the value of `d3` afterwards, you can `subq #7,d3` instead of `cmp #7,d3` (saving two bytes each and executing slightly faster) – chtz Mar 23 '19 at 10:30

1 Answers1

4

In assembler, there are no braced blocks, only gotos. So think about it, if x == 0 you already know you need the "then" code, but if x != 0, you have to test x == 7 to find out whether to go to the "then" code or the "else" code.

Since C is capable of expressing this structure, I'll use that to illustrate:

Your code

if(x == 0 || x == 7)
    {code T here}
else
    {code E here}

is equivalent to:

    if (x == 0) goto then;
    if (x == 7) goto then;
else: /* label is not actually needed */
    code E here
    goto after_it_all;
then:
    code T here
after_it_all:
    ;
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Thank you. This helped understand it perfectly. – romano Mar 23 '19 at 03:52
  • 2
    @romano: And notice how naturally the short-circuiting behavior of `||` appeared -- if you make the `goto then` decision from the first condition, the second condition (and any complex calculations or function calls) never gets reached. – Ben Voigt Mar 23 '19 at 03:54