title: Adding two words sometimes misbehaves because of CBW instruction
body: <p><strong>What I need</strong></p>
  
  <p>Part of the program I am requested to write is an addition. The thing is that we are requested to define <em>bytes</em> and the convert them to <em>words</em> (<strong>signed extension</strong>) and only afterwards add them.</p>
  
  <p><strong>The problem</strong></p>
  
  <p>The addition sometimes does not give the desired output. For example, I am using the following:</p>
  
  <pre><code>data segment
      first     DB    183
      second    DB    94
  data ends
  
  code segment
  start:
  
      mov ax,data
      mov ds,ax
  
      mov AX, 0
      mov AL, first
      cbw
  
      mov BX, AX
      mov AX, 0
  
      mov AL, second
      cbw
  
      add AX, BX
  
      mov ax, 4c00h
      int 21h
  
  code ends
  end start
  </code></pre>
  
  <p>94 (<em>5E in hex</em>) + 183 (<em>B7 in hex</em>) = 277 (<em>115 in hex</em>), but because of the first <code>cbw</code>, <em>AX = FFB7</em> instead of <em>B7</em>. However, the second number remains <em>AX = 5E</em> as expected, so adding the two results in <em>AX = 15</em> with <em>CF = 1</em>.</p>
  
  <p>I found <a href="http://www.ousob.com/ng/masm/ng64ce.php" rel="nofollow">this page about CBW</a> where it states that "This instruction will set AH to 0FFh if the sign bit (bit 7) of AL is set", which is my case, because <em>B7</em> is <em>1011 0111</em> in binary.</p>
  
  <p>Am I missing something? Should I interpret the carry flag in some way? Why am I not obtaining <em>115 (hex)</em> ?</p>
  
  <p>Thanks in advance.</p>