Question

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 <(__)>

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top