It is meant to count the first two numbers in the array and then place them in ascending order, but sth wrong. Any idea?

Code segment



           jmp main

           add1 dw 9,3,2,7,1,4,2,5,8,1



main:      mov si,0

           call orderab

exit:    mov ax,04c00h

           int 21h





orderab:   cmp add1[si],add1[si+2] 

                              

                                           

           jle finish

           mov ax,add1[si]

           mov bx,add1[si+2]

           mov add1[si],bx

           mov add1[si+2],ax

finish:    ret

Recommended Answers

All 7 Replies

yes i changed +2 to +1, but it still doesn't work, it has a small bug.

; because "cmp" instruction doesnt accept two memory operands
; CMP r , r/i
; CMP m , r/i
; instead of "cmp add1[si],add1[si+2]"
; try:
mov dx, add1[si]
cmp dx, add1[si + 2]

I managed to make it work but it only compares the first two elements of the array, i want it to keep comparing until it gets to the end of the array...

Code segment

           jmp main
           add1 dw 9,3,2,7,1,4,2,5,8,1

main:      mov si,0
           call orderab
           
exit:      mov ax,04c00h
           int 21h


orderab:   mov ax,add1[si]
           cmp ax,add1[si+2]               
                                           
           jle finish
           mov bx,add1[si+2]
           mov add1[si],bx
           mov add1[si+2],ax
finish:    ret

then you have to "inc si" and "jmp orderab" before "finish"

ode segment

jmp main
add1 dw 9,3,2,7,1,4,2,5,8,1


main: call order

exit: mov ax,04c00h
int 21h


order: mov cx,10
loop: mov si,0
dec cx
cmp cx,0
jz exit
call orderab
jmp loop


orderab: mov dx,9
loop1: dec dx
mov ax,add1[si]
cmp ax,add1[si+2]
jle finish
mov bx,add1[si+2]
mov add1[si],bx
mov add1[si+2],ax
finish: inc si
inc si
cmp dx,0
jnz loop1
ret

Any idea why it doesn't work?

Try declaring the array as a byte array instead of word.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.