News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Sort the buffer in descending order

Started by Valente, October 20, 2008, 08:01:19 AM

Previous topic - Next topic

Valente

Hi people,

I want to order the buffer in descending order but I'm having some problems with errors and even with the algorithm i thought. I think i need some guidance in this small problem so here is the code I have already written:


    .486                                    ; create 32 bit code
    .model flat, stdcall                    ; 32 bit memory model
    option casemap :none                    ; case sensitive

    include \masm32\include\windows.inc     ; always first
    include \masm32\macros\macros.asm       ; MASM support macros

  ; -----------------------------------------------------------------
  ; include files that have MASM format prototypes for function calls
  ; -----------------------------------------------------------------
    include \masm32\include\masm32.inc
    include \masm32\include\gdi32.inc
    include \masm32\include\user32.inc
    include \masm32\include\kernel32.inc

  ; ------------------------------------------------
  ; Library files that have definitions for function
  ; exports and tested reliable prebuilt code.
  ; ------------------------------------------------
    includelib \masm32\lib\masm32.lib
    includelib \masm32\lib\gdi32.lib
    includelib \masm32\lib\user32.lib
    includelib \masm32\lib\kernel32.lib
   
    .data

    buff word 1,2,3,4,5, 251 dup(1)
    newbuff word 0
     
    .code                       ; Tell MASM where the code starts

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

start:                          ; The CODE entry point to the program

    mov ecx, 0   ; indexador
    mov ax, LENGTHOF buff ; decrementador (contador)
    mov dh, 0 ; maior numero   
    mov dl, 0 ; menor numero

    ciclo:
        .if ax != 0
            add cx, 2
            mov dh, [offset buff]
            mov dl, [offset buff+ecx]
            cmp dh, dl
            ja ordena
            jb ciclo
            dec ax
        .endif

    ordena:
        mov bh, [offset newbuff]
        mov bh, dh
        jmp ciclo



I'm trying to compare each two positions of the buffer and if the first one is bigger than the second one, the program allocates the first position in a new buffer. Is there anything i can do to improve the algorithm ?

hutch--

if you have masm32 version 10, look in the example code, "exampl08\bubble" for a simple example of sorting a sequence of integers. I notice that you code looks like 16 bit code which neither has the range of 32 bit code  nor is it as fast so unless you have good reason to work in 16 bit code you are better off working in 32 bit code.

The bubble sort is very simple but not really fast enough to do any serious sorting work, if you need to sort integers fast, there are much better algorithms like a quick sort, shell sort, comb sort etc ..... but they are more advanced algorithms to code.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

MichaelW

Valente,

Before you can improve the algorithm, you first need to fix the problems that are preventing your code from assembling. Note that in 32-bit code an offset address is 32 bits.
eschew obfuscation

Valente

Quote from: MichaelW on October 20, 2008, 08:15:30 AM
Valente,

Before you can improve the algorithm, you first need to fix the problems that are preventing your code from assembling. Note that in 32-bit code an offset address is 32 bits.


Yeah, sure. I couldn't assemble and run the app because there are errors with the the instructions:

mov dh, [offset buff]   ; line 50
mov dl, [offset buff+ecx] ; line 51

and

mov bh, [offset newbuff]  ; line 60


It says the instruction operands are invalid. Is that because the offset size ? How can I fix it ?

hutch--

All addresses in 32 bit are 32 bit in size, that is why it will not assembler. Use a full 32 bit register.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

RuiLoureiro

Quote from: Valente on October 20, 2008, 08:23:29 AM
mov dh, [offset buff]   ; line 50
mov dl, [offset buff+ecx] ; line 51
and
mov bh, [offset newbuff]  ; line 60

It says the instruction operands are invalid. Is that because the offset size ? How can I fix it ?


;**************************************************************
Hi,
        If you want the the byte at the buff address
        then use  «byte ptr [buff]», «byte ptr [buff + ecx]».
        But there is some logic problems with your code (the numbers are words, you call bytes ...).
        See this sample. Does this help you ?

; ********************
; Console Assemble & Link
;********************
include \masm32\include\masm32rt.inc
; ««««««««««««««««««««««««««««««««««««««««««««««««
.data
    buff    dd  10, 2, 23, 40, 15, 19, 1, 10, 9, 3             ; the numbers are dwords

.code
; ««««««««««««««««««««««««««««««««««««««««««««««««
start:
            mov     ecx, 10                 ; nº de dwords no buffer
            mov     esi, offset buff        ; endereço inicial do buffer

ini:
            sub     ecx, 1                  ; 9 comparações iniciais, depois 8, ...
            jz      fim

            push    ecx
           
                mov     ebx, 0
                mov     edx, dword ptr [esi]
           
ciclo:
                add     ebx, 4
                mov     eax, dword ptr [esi+ebx]
                cmp     eax, edx
                ja      short @F
           
                ; »»»»»»»»»»»»»»»»
                ;     troca
                ; »»»»»»»»»»»»»»»»
                mov     dword ptr [esi], eax
                mov     dword ptr [esi+ebx], edx
                mov     edx, eax
             
@@:
                dec     ecx
                jnz     ciclo

            pop     ecx
           
            add     esi, 4                  ; avança o 1º a comparar
            jmp     ini

fim:

    inkey   
    exit
; ««««««««««««««««««««««««««««««««««««
end start

Rui