I need help with my MASM code. When I'm using dynamic allocation for arrays my other variables change values after adding few elements to array.
.686
.387
.model flat, stdcall
.xmm
include include\kernel32.inc
includelib lib\kernel32.lib
.data
szyfr DB 9, 15, 19, 2
;tab DD 4 dup (?) - it works fine
;wynik DD 4 dup (?) - it works fine
tmp DD 0
j DD 0
t DD 0
x DD 2
tmpa DD 0
tmpb DD 0
dlText DD 0
tab DD ?
wynik DD ?
.code
invoke GetProcessHeap
mov ebx, eax
INVOKE HeapAlloc, ebx, 0, 4 - it doesn't work
mov tab, eax
INVOKE HeapAlloc, ebx, 0, 4 - it doesn't work
mov wynik, eax
Writing to array:
xor edx, edx
mov eax, esi ; esi = iterator = i
mul x ; x = 2
mov edx, eax
add edx, j
add edx, offset tab
mov eax, t ; t = number <0, 26>
mov dword ptr [edx], eax ; tab[i][j] = number <0, 26>
Your method of accessing the memory you've allocated with HeapAlloc
is incorrect. When you do add edx, offset tab
you don't get the address of the memory you allocated; you get the address of tab
, and tab
is just a DWORD
. To add the address of your allocated memory you should use add edx,tab
.
By the way, these lines:
xor edx, edx
mov eax, esi ; esi = iterator = i
mul x ; x = 2
mov edx, eax
could be simplified into just:
lea edx,[esi*2]