vendredi 4 juin 2021

Write this loop in assembly x86

int arr[]= {5,8,7,4,1,9};

    int n =6;    //array size



    for (int i = 0; i < n-1; i++)
    {

        // Last i elements are already in place

        for (int j = 0; j < n-i-1; j++)
        {

            if (arr[j] > arr[j+1])
            {

                int x=arr[j];

                arr[j] = arr[j+1];

                arr[j+1] = x;

            }


        }

    }
for (int i=0; i < n ; i++)
    {

        cout << " " <<arr[i]<< " " ;

    }

use it to sort numbers from{5,8,7,4,1,9} to {1,4,5,7,8,9} and print them looks like bubble sort the code i tried is

format pe; console
 entry start
 include 'win32ax.inc'
 section '.code' code readable executable

start:

mov eax,[i]
Firstloop:
        cmp eax,[size-1]
        jg startloop
        jl Secondloop
;the second loop
mov ebx,0
Secondloop:
        mov ebp,[size,i]
       ; mov ebp,sub
        cmp ebx,[size-esi-1]
        jl ifcondition
        jg EndOfFirstLoop
        cmp edx,esp
        jg swap
        jl EndOfSeconLoop



ifcondition:
        mov edx,[array+ebx*4]
        mov esp,[array+ebx*8]
        cmp edx,esp
        jg swap
        jl EndOfSeconLoop

swap:
        mov edi,edx
        mov edx,esp
        mov esp,edi
        jmp EndOfSeconLoop
EndOfSeconLoop:
        inc ebx
        jmp Secondloop
EndOfFirstLoop:
        inc esi
        jmp Firstloop





mov ecx,[size]
mov eax , 0
startloop:

        push ecx
        push eax
        cinvoke printf, f6 , [array+eax*4]
        pop eax
        pop ecx
        add eax,1
       loop startloop
       cinvoke scanf, f5, vard
       cinvoke printf, newline

exit:

   invoke ExitProcess, 0;

section '.data' data readable writeable

 f5 db '%d',0
 f6 db ' %d ',0
 newline db '',10,0
 vard dd ?

array dd 5,8,7,4,1,9
size  dd 6

i dd 0




section '.idata' import data readable writeable
library msvcrt, 'msvcrt.dll', kernel32, 'kernel32.dll'
import msvcrt, printf, 'printf', getchar,'getchar', scanf,'scanf'
import kernel32, ExitProcess,'ExitProcess'

it gives me error and don't run the error in part (n-i-1) ....................................................................................................................................................................................................................................................................................................

Aucun commentaire:

Enregistrer un commentaire