saad shan
saad shan

Reputation: 21

How to find minimum signed value in array in assembly

i am trying to make selection sort program in assembly but i stuck how to find minimum signed number

i am beginner in assembly i learned just conditional and unconditional jumps and subroutines

i have assignment of selection sort of array of signed numbers i write code it finds minimum number but after further comparison it store greater number

[org 0x0100] 
          jmp  start 

data:          dw   -1,-7,0,-4
temp:          dw 0

start:
    mov bx,0
    mov cx,4
    mov ax,[data+bx]
h1;
    cmp ax,[data+bx+2]
    jnle l1 

    add bx,2
    cmp bx,6
    jne h1




l1:
    mov dx,[data+bx+2]
    mov [temp],dx
    add bx,2
    cmp bx,6
    jne h1

    mov  ax, 0x4c00         
        int  0x21

i expect after termination of my program array will be sorted according to following the selection sort algorithm.

Upvotes: 2

Views: 798

Answers (1)

Michael
Michael

Reputation: 58467

Your algorithm doesn't make much sense. Your current code would correspond to something like this in C:

int min = 0;
for (int i = 0; i < 3; i++) {
    if (data[i] > data[i+1]) {
        min = data[i+1];
    }
}

Note how your comparison doesn't take the current min value into account. It only cares about the difference between adjacent elements in the array (and those elements do not move around in the loop).


What you should be doing is something like this:

int min = data[0];
for (int i = 1; i < 4; i++) {
    if (data[i] < min) {
        min = data[i];
    }
}

It should be simple enough for you to translate that into assembly.

Upvotes: 3

Related Questions