简体   繁体   中英

(32-bit assembly) pass pointer to proc

i dont know why this program cannot output

+1 +2 +3 +4

the output is

+4214784 +1967600538 +2130567168 +1638356

i guess it is address, but why? how to correct it?

here is my code:

include irvine32.inc

.data
  matrix dword 1, 2, 3, 4

.code
  print proto, m:ptr dword

  main proc
    invoke print, addr matrix

    exit
  main endp

  print proc, m:ptr dword
    mov eax, m[0 * type m]
    call writeint

    mov eax, m[1 * type m]
    call writeint

    mov eax, m[2 * type m]
    call writeint

    mov eax, m[3 * type m]
    call writeint

    ret
  print endp

  end main

thanks for your answer <(__)>

m is a pointer passed on the stack. The assembler will turn m into something like [ebp+8] . Indexing will access items on the stack, starting from that position and that's not what you want. You need to dereference the m pointer which you can only do if you load it into a register.

mov ecx, m  ; this will be mov ecx, [ebp+8] or similar
mov eax, [ecx + 0*4] ; first item
call WriteInt
mov eax, [ecx + 1*4] ; second item
call WriteInt
...

I do not recommend beginners use fancy features of their assembler without understanding what code gets generated exactly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM