Question

I'm supposed to create a frequency table containing 256 DWORDs and count the number of occurrences of a character in a string using the ASCII code as the index. I'm getting stuck on how to use the stored ASCII value as the index and increment that index. Here is what I have so far.

    TITLE String Find

    INCLUDE Irvine32.inc
    Get_freqs PROTO, 
        ptrTarget:PTR BYTE, 
        ptrFreqTable:PTR BYTE

    .data
    targetStr BYTE "AAEBDCFBBC",0
    freqTable DWORD 256 DUP(0)
    .code

    main PROC

    INVOKE Get_freqs,  ADDR targetStr, ADDR freqTable
    mov ecx,LENGTHOF freqTable
    mov edx,LENGTHOF freqTable
    L1:
        push    edx
        mov eax,edx
        call    writeInt
        mov al,' '
        call writeChar
        mov eax,OFFSET freqTable
        add eax,ecx
        call writeInt
        pop edx
        dec edx
        loop L1

    exit
    main ENDP

    Get_freqs PROC, 
        ptrTarget:PTR BYTE,
        ptrFreqTable:PTR BYTE

    INVOKE Str_length,ptrTarget         ; EAX = length source
    mov ecx,eax
L1:
    mov eax,ptrTarget[ecx]
    add eax,ptrFreqTable
    loop L1
    ret

    Get_freqs   ENDP 

    END main

This is the part I'm having trouble with.

L1:
    mov eax,ptrTarget[ecx]
    add ptrFreqTable[eax],1
    loop L1
Était-ce utile?

La solution 2

I fixed it by using some directives that I had not been taught when I posted this.

L1:mov eax,0                    ; clear upper bits of EAX
    lodsb                       ; AL = [ESI], inc ESI
    cmp al,0                    ; end of string?
    je done                     ; yes: exit
    shl eax,2                   ; multiply by 4
    inc DWORD PTR[edi+eax]      ; add to table entry
    jmp L1
done:

My display technique was flawed as well, but since that wasn't part of my question I'll leave it be here so that I may smack myself on the forehead when I come back here.

Autres conseils

You'll only want to read bytes from the string, and then scale them for access to your DWORD array. So replace:

L1:
    mov eax,ptrTarget[ecx]
    add ptrFreqTable[eax],1
    loop L1

With something like:

L1:
    movzx eax, byte ptr ptrTarget[ecx - 1]
    add ptrFreqTable[eax*4 - 4],1
    loop L1

The -1 and -4 is because you're really looping from length(string) to 1 (LOOP decrements CX and stops looping when CX equals 0).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top