title: Assembly Language: JE/JNE as an if-else
body: <p>So I'm trying to use <code>JE</code>/<code>JNE</code> to do an if-else statement. The problem is, it does not jump to where I want it to go.</p>
  <p>What I'm trying to do here is to multiply 2 user-input numbers by using the <code>ADD</code> instruction and only <strong>compare</strong> and <strong>jump</strong> commands.</p>
  <p>I was thinking of doing an if-else style but I couldn't continue because it could not jump to other labels.</p>
  <p>Here is my code:</p>
  <pre><code>.model small
  .stack
  .data
      str1 db &quot;Enter first number: &quot;,&quot;$&quot; 
      
      str2 db 13,10,&quot;Enter second number: &quot;,&quot;$&quot;
      
      str3 db 13,10,&quot;The product of &quot;
      num1 db ?, &quot; &amp; &quot;
      num2 db ?, &quot; is &quot;
      prod db ? ,&quot;$&quot; 
      
  .code
      mov ax,@data
      mov ds,ax
  
      mov ah,9
      lea dx, str1
      int 21h
      
      mov ah,1
      int 21h ; takes user input for num1
      mov num1,al ; stores user input to variable input
  
      mov ah,9
      lea dx, str2
      int 21h
      
      mov ah,1
      int 21h ; takes user input for num2
      mov num2,al ; stores user input to variable input
      
      cmp num1,1 ; compare num1 to 1
      jne equals2 ; jump to equals2 if num1 != 1, otherwise, it will just continue
          mov al, num2
          mov prod, al
          
          mov ah,9
          lea dx, str3
          int 21h
          jmp endCode
  
  equals2:        
          mov al, num2
          ADD al, al
          mov prod, al
          sub prod, 49 ; convert hex to decimal
          
          mov ah,9
          lea dx, str3
          int 21h
  
  endCode:
      mov ah,4ch
      int 21h
  
      mov ah,4ch
      int 21h
  END
  
  </code></pre>
  <p>So if <em>num1</em> == 1, it's fine.</p>
  <p>But when <em>num1</em> == 2 or more, it does not jump to <em>equals2</em>, instead, it just continues.</p>
  <p>Here is the sample output:</p>
  <blockquote>
  <pre><code>Enter first number: 2
  Enter second number: 4
  The product of 2 &amp; 4 is 4
  </code></pre>
  </blockquote>
  <p>Expected output:</p>
  <blockquote>
  <pre><code>Enter first number: 2
  Enter second number: 4
  The product of 2 &amp; 4 is 8
  </code></pre>
  </blockquote>
  <p>BTW, the user-input must be from 1 to 9.</p>