News:

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

Character count algo.

Started by hutch--, February 28, 2011, 12:45:59 PM

Previous topic - Next topic

hutch--


IF 0  ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                      Build this template with "CONSOLE ASSEMBLE AND LINK"
ENDIF ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm32\include\masm32rt.inc

    char_count PROTO :DWORD,:DWORD,:DWORD

  ; -----------------------------------------
  ; register with array address and immediate
  ; -----------------------------------------
    min_arr MACRO reg, imm
      EXITM <[reg+imm*4]>
    ENDM

  ; -------------------------------------------
  ; takes either a memory operand or a register
  ; as 1st arg, immediate as second
  ; -------------------------------------------
    arr_item MACRO array,imm_member
      mov eax, array
      mov eax, [eax+imm_member*4]
      EXITM <eax>
    ENDM

  ; ----------------------------------------------------------
  ; longer but takes memory operands, registers and immediates
  ; preserves ECX and returns in EAX
  ; ----------------------------------------------------------
    get_item MACRO arr,member
      mov eax, arr
      push ecx
      mov ecx, member
      mov eax, [eax+ecx*4]
      pop ecx
      EXITM <eax>
    ENDM

    .data
      lfcntx dd 0

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    LOCAL parr  :DWORD
    LOCAL buffer[1024]:BYTE
    LOCAL hMem  :DWORD
    LOCAL flen  :DWORD

    LOCAL cnt13 :DWORD
    LOCAL cnt10 :DWORD

    push esi
    push ebx

    mov hMem, InputFile("\masm32\include\windows.inc")
    mov flen, ecx

    lea eax, buffer
    mov parr, eax
    invoke memfill,parr,1024,0          ; zero fill array

    invoke char_count,hMem,flen,parr

  ; ------------------------------------
    mov esi, parr
    sub esi, 4
    or ebx, -1
  @@:
    add esi, 4
    add ebx, 1
    print str$(ebx)," = "
    print str$([esi]),13,10
    cmp ebx, 255
    jl @B
  ; ------------------------------------

    mov cnt13, arr_item(parr,13)            ; medium len macro
    mov cnt10, get_item(parr,10)            ; long macro

    print str$(cnt13),13,10
    print str$(cnt10),13,10

    mov esi, parr

    mov eax, min_arr(esi,13)                ; short macro
    print str$(eax),13,10

    invoke GlobalFree,hMem

    pop ebx
    pop esi

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

char_count proc ptxt:DWORD,ltxt:DWORD,parray:DWORD

  ; --------------------------------------------------
  ; INPUT
  ; ptxt   = text to count chars in
  ; ltxt   = length of ptxt in bytes
  ; parray = 1024 byte zero filled array address

  ; OUTPUT
  ; parray address contain 256 x 4 byte values.
  ; OFFSET 0 = ASCII zero, OFFSET 4 = ASCII 1 etc ....
  ; --------------------------------------------------
    mov ecx, [esp+4]            ; ptxt
    mov edx, [esp+12]           ; parray
    sub ecx, 1

  lpst:
  REPEAT 3
    add ecx, 1
    movzx eax, BYTE PTR [ecx]
    add DWORD PTR [edx+eax*4], 1
    sub DWORD PTR [esp+8], 1
    jz lpout
  ENDM

    add ecx, 1
    movzx eax, BYTE PTR [ecx]
    add DWORD PTR [edx+eax*4], 1
    sub DWORD PTR [esp+8], 1
    jnz lpst

  lpout:

    ret 12

char_count endp

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php