0

I have the following c assembly code which sorts an array in descending order, I have tested it using 8086emu and it worked 100% but at visual studio it gave me wrong results and an error. Any ideas or how to solve this problem.

My Code:-

#include "stdafx.h"
#include <iostream>
using namespace std;


void main(void)
{
short *arr;
arr = new short[10];
cout << "please enter the array elements" << endl;
for (int i = 0; i < 10; i++)
{
    cin >> arr[i];
}

short *p;
p = arr;
_asm{

START:
    mov cx, 9
        mov esi, p

    LABEL2 :
    MOV ax, [esi]
        CMP ax, [esi + 2]
        JGE LABEL1
        MOV bx, [esi + 2]
        MOV word ptr[esi], bx
        MOV word ptr[esi + 2], ax
        JMP START

    LABEL1 :
    inc esi
        inc esi
        LOOP LABEL2

}

for (int i = 0; i < 10; i++)
{
    cout << arr[i] << endl;
}

}

The result

pmg
  • 106,608
  • 13
  • 126
  • 198
Jamal Alkelani
  • 606
  • 7
  • 19
  • 1
    how far through the assembly does it make? What have you done to debug this? – Paul Bentley May 05 '17 at 10:27
  • @PaulBentley I haven't debugged that,it appears directly after entering the array elements, but the the same code which is inside the ASM block works with Masm and emu. – Jamal Alkelani May 05 '17 at 10:41
  • `loop` probably decrements and checks `ecx`, not just the lower word (`cx`). What if you do `mov ecx,9` instead of `mov cx,9`? – Michael May 05 '17 at 11:37
  • @Michael Ohhhhhhhh thank you a loooot that worked ,, thanks <3 I appreciate that god bless you – Jamal Alkelani May 05 '17 at 11:48

1 Answers1

0

The problem has been solved by Michael in a comment.

The problem is that I was using the LOOP instruction, which implicitly decrements and tests the ECX register. However, when I initialized the register at the top of the loop, I only initialized its low-order word (mov cx, 9), which left garbage in the upper word.

The solution is to initialize the full ECX register: mov ecx, 9

Community
  • 1
  • 1
Jamal Alkelani
  • 606
  • 7
  • 19
  • By the way, `LOOP` is a very slow and obsolete instruction. You really shouldn't be using it at all. It is equivalent to `dec ecx`+`jnz ???`, and that's what you should use instead. If you'd written it out that way, not only would your code be faster and more idiomatic, but you'd also have caught this bug easily. (But more generally, you shouldn't be writing inline assembly here at all. This is code that a C compiler can generate and optimize easily. Your assembly code is just slower and more error-prone. If you're trying to *learn* assembly-language programming, don't use a C compiler.) – Cody Gray - on strike May 05 '17 at 13:52
  • @CodyGray oh thanks a lot I'll put that in mind, but why I shouldn't use a C compiler ?! – Jamal Alkelani May 05 '17 at 14:12
  • If you want to learn assembly language, use an assembler, like MASM or NASM, and write *all* the code in assembly. Inline assembly is a horrible, complicated beast, and it is *not* a good way to learn assembly. – Cody Gray - on strike May 05 '17 at 14:23
  • @CodyGray Of course , I use them but this's a H.W ,so I must use C assembly, by the way this's the hardest programming language I had ever used it's complicated ! – Jamal Alkelani May 05 '17 at 14:33