; ======================= source-file: _length_2.asm ============================
;
; Purpose:
; Fast string, binary and number-list element-count, combined into _one_
; Global predicate
;
; ----------------------------------------------------------------------------
IDEAL
P586
MODEL   FLAT
CODESEG
align   4

    public  _length_2

PROC _length_2  near
ARG list:dword
    mov ecx,[esp+4]
    xor edx,edx    ; initialize EDX as number-of-elements counter
    mov ah,2        ; prepare AH = 2 (end-element flag)
@@p1:   
    mov al,[byte ptr ecx] ; put the "element-type indicator" in AL
    cmp al,ah       ; compare with 2 (end-of-list indicator)
    jz  @@xx        ; if end of list, then exit!   
    inc edx        ; else, increment element_counter EDX
    mov ecx,[ecx+8] ; make ECX = next list-element-pointer
    jmp short @@p1      ; repeat the loop
@@xx:
    mov eax,edx     ; return the number of elements, in EAX
    retn
ENDP    _length_2

    END

; ============================= VISUAL PROLOG TEST PROGRAM: ===============================
CONSTANTS
 test_length = 1

GLOBAL DOMAINS
   ILIST = INTEGER*
   RLIST = REAL*
   ULIST = UNSIGNED*
   SLIST = STRING*
   BLIST = BINARY*
   CLIST = CHAR*

GLOBAL PREDICATES
   UNSIGNED length(STRING) -(i) language c
   UNSIGNED length(BINARY) -(i) language c
   UNSIGNED length(ILIST) -(i) language c
   UNSIGNED length(RLIST) -(i) language c
   UNSIGNED length(ULIST) -(i) language c as "_length_2"
   UNSIGNED length(SLIST) -(i) language c as "_length_2"
   UNSIGNED length(BLIST) -(i) language c as "_length_2"
   UNSIGNED length(CLIST) -(i) language c as "_length_2"

ifdef test_length
GOAL
 getbacktrack(LOOP1),
 repeat, write("\nGive a list of integers: "), readln(S1), term_str(ilist,ILx,S1),
   N1 = length(ILx), write("Listlength = ",N1),
   write("\nPress ESC for the next test; any other key to continue:\n"),
   readchar(C1x), C1x='\27',
 cutbacktrack(LOOP1),
 getbacktrack(LOOP2),
 repeat, write("\nGive a list of strings: "), readln(S2), term_str(slist,SLx,S2),
   N2 = length(SLx), write("Listlength = ",N2),
   write("\nPress ESC for the next test; any other key to continue:\n"),
   readchar(C2x), C2x='\27',
 cutbacktrack(LOOP2),
 getbacktrack(LOOP3),
 repeat, write("\nGive a list of Characters: "), readln(S3), term_str(clist,CLx,S3),
   N3 = length(CLx), write("Listlength = ",N3),
   write("\nPress ESC for the next test; any other key to continue:\n"),
   readchar(C3x), C3x='\27',
 cutbacktrack(LOOP3),
 !.
enddef

; ***************  "32-bit Assembly Language Extensions for Visual Prolog" ****************
; Written by George A. Stathis (c) 2005,         E-mail: omadeon@yahoo.com, gstathis@enm.gr
; URL: http://www.omadeon.com/asm     Company: ENB Ltd.     Company site: http://www.enb.gr
; **** Use of this source-code is unrestricted, provided that the author is mentioned. ****