Jeffpowrs
Jeffpowrs

Reputation: 4540

Stack Parameter Offset Issue MASM

I'm new to MASM and I'm having a bit of trouble with using indirect offsets and passing arguments on the stack.

I have a sorted array and it's size that I am passing to a procedure via the stack. I want to print the first and last element of the array. I push the two arguments the the stack, the offset of the first element of the array and the number of elements in the array.

The number of elements in the array is correct but when I try and access the next position on the stack [the arraysize pos + 4] I get a huge number, the array elements never exceed 900. If someone can point out the issue, probably obvious, to me I would be very grateful. Thank you in advance.

push OFFSET array
push arraySize
call displayRange

...

;------------------------------------
displayRange PROC 
;------------------------------------

push    ebp
mov ebp, esp
pushad              ;save the registers

mov eax, [ebp + 8]
call    WriteDec

mov edx, OFFSET range1_str
call    WriteString

mov esi, [ebp + 12]
mov eax, [esi]
call    WriteDec

mov edx, OFFSET range2_str
call    WriteString

mov edx, TYPE array
mov eax, [ebp + 8]
mul edx
add eax, 12

mov esi, [ebp + eax]
    mov     eax, [esi]
call    writeDec

mov edx, OFFSET range3_str
call    WriteString
call    Crlf

popad
pop ebp

ret 8
displayRange ENDP
#
          UPDATE
#

To answer my own question, the reason I'm getting such a large number is because I'm passing the ADDRESS not the actual dereferenced value. Here is the correct code:

;------------------------------------
displayRange PROC 
;------------------------------------

push    ebp
mov ebp, esp
pushad              ;save the registers

mov eax, [ebp + 8]
call    WriteDec

mov edx, OFFSET range1_str
call    WriteString

mov esi, [ebp + 12]
mov eax, [esi]
call    WriteDec

mov edx, OFFSET range2_str
call    WriteString

mov edx, TYPE array
mov eax, [ebp + 8]
mul edx
add eax, 12

mov esi, [ebp + eax]

call    writeDec

mov     edx, OFFSET range3_str
call    WriteString
call    Crlf

popad
pop ebp

ret 8
displayRange ENDP

Upvotes: 0

Views: 775

Answers (1)

rkhb
rkhb

Reputation: 14399

Instead of:

mov     edx, TYPE array
mov     eax, [ebp + 8]
mul     edx
add     eax, 12
mov     eax, [ebp + eax]

write:

mov     edx, TYPE array
mov     eax, [ebp + 8]
mul     edx
add     eax, [ebp+12]
mov     eax, [eax]

Upvotes: 1

Related Questions