The MASM Forum

General => The Campus => Topic started by: Konai on June 15, 2013, 08:52:01 AM

Title: Learning PTR pointers with dwords
Post by: Konai on June 15, 2013, 08:52:01 AM
Ok I have used pointers in the past but usually with bytes

display_hex proc
; esi has the offset of the (BYTE) to be printed in (HEX).
  xor eax,eax
  mov al,[esi]
  and al,0f0h
  shr al,4
  mov bl,LUT[eax]
  mov [msg],bl
  mov al,[esi]
  and al,0fh
  mov bl,LUT[eax]
  mov [msg+1],bl
  invoke StdOut,ADDR msg
  ret
display_hex endp


In the example above I am using a pointer when I move a byte value into the variable [msg+1]
I understand how this works and what it is doing.

When it comes to dwords though I am somewhat confused.
I am trying to insert into an "array" of dword values. (cnt_array dd 256 dup 0)

What I don't understand is how to do that.

If I do something like ...
dword ptr [count_array+1]
Am I pointing at count_array + 1 byte??? or +1 dword i.e. +4 bytes from the beginning of the array.

Also, when I LUT[eax] to get an offset within that array to return the ascii value of that hex symbol. (LUT is look up table with ascii values of each hex digit in it )
LUT db "0123456789ABCDEF",0

Is there a way to do dword prt LUT[eax+1] ... not really sure if that is a clear question or not, but this question is not as important as the first one.

Ultimately I am trying to get file statistics and count the number of times each byte appears within a file using a pseudo array of dword values to hold the count of each byte that was in the file.

I have tried to make this as clear as possible so please let me know if something I said is confusing or more information is required.

Thank you for your help.
Title: Re: Learning PTR pointers with dwords
Post by: MichaelW on June 15, 2013, 09:45:46 AM
To access a DWORD array with an index that advances by 1, you use a scale factor on the index register. The possible values for the scale factor are 1, 2, 4, or 8 (for indexing a BYTE, WORD, DWORD, or QWORD).

;==============================================================================
include \masm32\include\masm32rt.inc
;==============================================================================
.data
    cnt_array dd 256 dup(0)
.code
;==============================================================================
;==============================================================================
start:
;==============================================================================
    mov ebx, OFFSET cnt_array
    xor edx, edx
    mov ecx, 256
  @@:
    mov [ebx+edx*4], edx
    inc edx
    dec ecx
    jnz @B

    xor edx, edx
    mov ecx, 256
  @@:
    mov eax, [ebx+edx*4]
    pushad
    printf("%d\t", eax)
    popad
    inc edx
    dec ecx
    jnz @B
    printf("\n\n")

    inkey
    exit
;==============================================================================
end start

Title: Re: Learning PTR pointers with dwords
Post by: Konai on June 15, 2013, 09:57:15 AM
Ok, the scaling factor is what I was missing. Thank you very much for your help!
Title: Re: Learning PTR pointers with dwords
Post by: MichaelW on June 15, 2013, 10:20:06 AM
I'm not sure about your second question. You could do a DWORD LUT like this:

LUT dd "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"

But I see no need for a table of ASCII character codes for counting the occurrences of the BYTE values 0-255.
Title: Re: Learning PTR pointers with dwords
Post by: FORTRANS on June 15, 2013, 10:55:26 PM
Hi,

   If you want to count characters, just use the character
shifted to the counter size to access the counter array.


Counts  DD      256 DUP ( 0 )

...

        XOR     EAX,EAX ; Zero entire register.
        MOV     AL,Char ; Get your character.
        SHL     EAX,2   ; Byte count to DWORD pointer.
        INC     [Counts+EAX]


HTH,

Steve N.