News:

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

Sorting?

Started by Farabi, July 28, 2008, 01:57:53 AM

Previous topic - Next topic

Farabi

A have a list of name and I want it to be sorted by alphabetical orders, Is windows have any library for this task?
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

hutch--

No but the masm32 library does. Use "ltok" and the sort algos documented in the help file.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Farabi

Quote from: hutch-- on July 28, 2008, 02:42:50 AM
No but the masm32 library does. Use "ltok" and the sort algos documented in the help file.

I cant found "ltok" function on the masm32 help file, where I can find it?
Do you have any example for this?
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

ragdog


hutch--

Farabi,

Just make sure you have a late version of masm32, get the version 10 beta as it has the most current library and reference material.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Farabi

I make the sorting algo working, but please check if there any mistake, I hope I free all the memory I used.


Quote

mAlloc proc nSize:dword
   
   add nSize,4
   invoke GlobalAlloc,GMEM_ZEROINIT or GMEM_FIXED,nSize
   .if eax==0
      invoke MessageBox,NULL,addr mem_error,NULL,MB_OK
   .endif
   
   ret
mAlloc endp

ltok proc pTxt:DWORD,pArray:DWORD
   LOCAL lmem:dword
  ; ---------------------------------------------------------------
  ; tokenise lines in a text source writing an array of pointers
  ; to the address of "pArray" and returning the line count in EAX.
  ; ---------------------------------------------------------------

    .data
    align 4
    actbl \
      db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
      db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
      db 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
      db 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
      db 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
      db 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
      db 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
      db 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
      db 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
      db 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
      db 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
      db 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
      db 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
      db 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
      db 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
      db 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
    .code

    push ebx
    push esi
    push edi

    mov edi, 1                      ; set counter to 1 in case of no trailing CRLF

    mov esi, pTxt
    sub esi, 1
  ; ----------------
  ; count line feeds
  ; ----------------
  @@:
    add esi, 1
    movzx eax, BYTE PTR [esi]
    test eax, eax                   ; test for terminator
    jz @F
    cmp eax, 10                     ; test for line feed
    jne @B
    add edi, 1                      ; lf count in EDI
    jmp @B
  @@:
  ; --------------------
  ; multiply result by 4
  ; --------------------
    add edi, edi
    add edi, edi
    pushad
    invoke mAlloc,edi

    mov lmem, eax                ; allocate pointer array memory
   popad                           ; I modify this part, I hope it only allocate 1 memory, its too complicated to read how this function work
   mov ecx,lmem
    mov edi, ecx                    ; copy allocated memory address into EDI
    mov esi, pTxt
    xor ebx, ebx                    ; zero arg counter
    sub esi, 1
    jmp ftrim

  ; ---------------------------------

  terminate:
    mov BYTE PTR [esi], 0           ; terminate end of current line

  ftrim:                            ; scan to find next acceptable character
    add esi, 1
    movzx eax, BYTE PTR [esi]       ; zero extend byte
    test eax, eax                   ; test for zero terminator
    jz lout
    cmp BYTE PTR [actbl+eax], 0     ; check it against acceptable character table
    je ftrim

  ; ¤=÷=¤=÷=¤=÷=¤=÷=¤
    mov [edi], esi                  ; write current location to pointer
    add edi, 4                      ; set next pointer location
    add ebx, 1                      ; increment arg count
  ; ¤=÷=¤=÷=¤=÷=¤=÷=¤

  ttrim:                            ; scan to find the next CR or LF
    add esi, 1
    movzx eax, BYTE PTR [esi]       ; zero extend byte
    test eax, eax                   ; test for zero terminator
    jz lout
    sub eax, 13                     ; test for CR
    jz terminate
    add eax, 3                      ; test for LF
    jz terminate
    jmp ttrim

  ; ---------------------------------

  lout:
    mov esi, pArray                 ; load passed handle address into ESI
    mov DWORD PTR [esi], ecx        ; store local array handle at address of passed handle

    mov eax, ebx                    ; return array count in EAX
    pop edi
    pop esi
    pop ebx

    ret

ltok endp

FSort proc uses esi edi pMem:dword,nFileSize:dword
   LOCAL buff[256]:dword
   LOCAL lcnt:Dword
   LOCAL pArry:dword
   LOCAL tmp_mem:Dword
   
   invoke ltok,pMem,addr pArry
   mov lcnt,eax
   invoke assort,pArry,lcnt,0
   
   mov edx,nFileSize
   add edx,2
   invoke mAlloc,edx
   mov tmp_mem,eax
   
   push tmp_mem
   
   mov esi,pArry
   mov ecx,lcnt
   @@:
   push esi
   push ecx
      invoke lstrcpy,tmp_mem,[esi]
      invoke lstrlen,[esi]
      add tmp_mem,eax
      mov edx,tmp_mem
      mov word ptr[edx],0d0ah
      add tmp_mem,2
   pop ecx
   pop esi
   add esi,4
   dec ecx
   jnz @b
   
   pop tmp_mem
   
   invoke MemCopy,tmp_mem,pMem,nFileSize  ; Copy back the data that have line separator data.
   
   invoke GlobalFree,pArry
   .if eax!=0
      invoke MessageBox,0,CADD("Memory deletion error"),0,0
   .endif
   invoke GlobalFree,tmp_mem
   .if eax!=0
      invoke MessageBox,0,CADD("Memory deletion error"),0,0
   .endif
   
   
   ret
FSort endp
Quote
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

hutch--

Farabi,

The "ltok" algo calculates that memory requirement for the array of pointers it creates by counting the line feeds to get the line count, adds 1 in case there is no trailing CRLF and then writes the starting address of each line to consecutive pointer array members. All you need to do is pass the address of the source data then pass the adress of a single DWORD variable to it and when it returns the DWORD variable contains the adress of the pointer array.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php