title: MASM Why doesn't decrementing a register find the next value in an array?
body: <p>I'm testing to see if an entered string is a palindrome by taking the string, moving it into a character array and comparing first and last elements of the char array to each other to test. I can get the first element of the array to find the second character easily, but to find the last acceptable value and decrement that, it doesn't find the next character in the array. So if the corrected/cleaned char array looks like: </p>
  
  <p>['A']['B']['C']['D']['A']</p>
  
  <p>ebx will go from 'A' -> 'B' but edi will not change from 'A' -> 'D'</p>
  
  <p>Why will ebx change characters but edi only subtracts 1 from it's register value? What can I do to have edi change character value? Thanks!</p>
  
  <p>C++ code: (just in case)</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstring&gt;
  #include &lt;sstream&gt;
  
  using namespace std;
  
  extern"C"
  {
  char stringClean(char *, int);
  char isPalindrome(char *, int);
  }
  
  int main()
  {
  int pal = 0;
  const int SIZE = 30;
  string candidate = "";
  char strArray[SIZE] = { '\0' };
  
  cout &lt;&lt; "enter a string to be tested: ";
  getline(cin, candidate);
  
  
  int j = 0;
  for (int i = 0; i &lt; candidate.length(); i++)        //getting rid of garbage before entering into array
  {
      if (candidate[i] &lt;= 'Z' &amp;&amp; candidate[i] &gt;= 'A' || candidate[i] &lt;= 'z' &amp;&amp; candidate[i] &gt;= 'a')
      {
          strArray[j] = candidate[i];
          j++;
      }
  }
  
  if (int pleaseWork = stringClean(strArray, SIZE) == 0)
      pal = isPalindrome(strArray, SIZE);
  
  if (pal == 1)
      cout &lt;&lt; "Your string is a palindrome!" &lt;&lt; endl;
  else
      cout &lt;&lt; "Your string is NOT a palindrome!" &lt;&lt; endl;
  
  system("pause");
  return 0;
  }
  </code></pre>
  
  <p>masm code: </p>
  
  <pre><code>.686
  .model flat
  .code
  
  _isPalindrome PROC ; named _test because C automatically prepends an underscode, it is needed to interoperate
      push ebp
      mov ebp,esp ; stack pointer to ebp
  
      mov ebx,[ebp+8] ; address of first array element
      mov ecx,[ebp+12] ; number of elements in array
      mov ebp,0
      mov edx,0
      mov eax,0
      push edi    ;save this
      push ebx    ;save this
  
      mov edi, ebx    ;make a copy of first element in array
      add edi, 29     ;move SIZE-1 (30 - 1 = 29) elements down to, HOPEFULLY, the last element in array
  
      mov bl, [ebx]
      mov dl, [edi]
  
      cmp dl, 0           ;checks if last element is null
      je nextElement      ;if null, find next
      jne Comparison      ;else, start comparing at Comparison:
  
  nextElement:
      dec edi             ;finds next element
      mov dl, [edi]       ;move next element into lower edx
      cmp dl, 0           ;checks if new element is mull
      je nextElement      ;if null, find next
      jne Comparison      ;else, start comparing at Comparison:
  
  Comparison:
      cmp bl,dl           ;compares first element and last REAL element
      je testNext         ;jump to textNext: for further testing
  
      mov eax,1           ;returns 1 (false) because the test failed
      jne allDone         ;jump to allDoneNo because it's not a palindrome
  
  testNext:
      dec edi     ;finds last good element -1 --------THIS ISN'T DOING the right thing
      inc ebx             ;finds second element
  
      cmp ebx, edi        ;checks if elements are equal because that has tested all elements
      je allDone          
  
      ;mov bl,[ebx]       ;move incremented ebx into bl
      ;mov dl,[edi]       ;move decremented edi into dl
      jmp Comparison      ;compare newly acquired elements
  
  
  
  allDone:
      xor eax, eax
      mov ebp, eax
  
      pop edi
      pop edx
      pop ebp
      ret
  _isPalindrome ENDP
  
  END 
  </code></pre>