News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Dereferencing

Started by Alek, December 09, 2018, 12:24:41 PM

Previous topic - Next topic

Alek

For some reason dereferencing doesn't seem to be "working"

LOCAL buffer[128]:BYTE
LOCAL pBuffer:DWORD   
lea eax, buffer                 
mov pBuffer, eax

INVOKE ReadConsoleA, consoleHandleInput, pBuffer, BUFFER_SIZE, offset charsWritten, 0

mov eax, [pBuffer]


This move into eax is putting in the address and not the value. Having the same results as:

mov eax, pBuffer  (obviously)     AND
mov eax, dword ptr[buffer]

Not sure why I can't deref

hutch--

See if this is useful to you. Its 64 bit MASM code but it is pretty simple stuff. With MASM, putting square bractets around a named variable is simply ignored and you are just loading the variable into the register. Use LEA to get the address and dereference the register works fine.

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

    include \masm32\include64\masm64rt.inc

    .data
      text1 db "  Cannons to right of them",13,10,0
      text2 db "  Cannons to left of them",13,10,0
      text3 db "  Cannons in front of them",13,10,0
      text4 db "  Volleyed and thundered",13,10,0

      tarr dq text1,text2,text3,text4           ; create an array of pointers

    .code

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

entry_point proc

    LOCAL .r12 :QWORD

    mov .r12, r12                               ; preserve r12 (a non volatile register)

    lea r12, tarr                               ; load the text array address

    conout QWORD PTR [r12+0]                    ; output dereferenced array member to the console
    conout QWORD PTR [r12+8]
    conout QWORD PTR [r12+16]
    conout QWORD PTR [r12+24]

    mov r12, .r12                               ; restore r12

    waitkey cfm$("\n  Thats all folks ....\n")  ; C style escapes

    .exit                                       ; run the exit macro

entry_point endp

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

    end

Alek

Cool, works now! Made a simple game that Ill post up here in a few - I'm sure you guys can critique it a lot ;)

hutch--

Alek,

Here is a variation that uses loop code instead of inlined 4 calls.

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

    include \masm32\include64\masm64rt.inc

    .data
      text1 db "  Cannons to right of them",13,10,0
      text2 db "  Cannons to left of them",13,10,0
      text3 db "  Cannons in front of them",13,10,0
      text4 db "  Volleyed and thundered",13,10,0

      tarr dq text1,text2,text3,text4           ; create an array of pointers

    .code

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

entry_point proc

    USING r12,r13                               ; specify registers to be preserved

    SaveRegs                                    ; save the specified regs

    lea r12, tarr                               ; load the text array address
    mov r13, LENGTHOF tarr                      ; array member count as loop counter

  ; loop code -----------------------------------

  @@:                                           ; MASM anonymous label
    conout QWORD PTR [r12]                      ; display array member to the console
    add r12, 8                                  ; set address in r12 to next array member
    sub r13, 1                                  ; decrement the counter
    jnz @B                                      ; loop back if its not zero

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

    waitkey cfm$("\n  Seeya round like a rissole ...\n")    ; C style escapes

    RestoreRegs                                 ; restore the specified regs
    .exit                                       ; run the exit macro

entry_point endp

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

    end