Stack in Assembly
Stack in Assembly
I need help with a code in Assembly.
I'm coworking on this and my group just did this Assembly code where it supposed to do the same as what I did in #c.
Could someone help me to understand what happens with de stack at least on the first steps so I can go on and conclude the rest?
I'm a begginer in Assembly, but I know that these lines just save the value for the calling function, takes a frame for the called function and save space for local variables, but I can't figure out the next first steps.
mov ebp
mov ebp,esp
sub esp, 16
Here is what I did in #c:
void mult_integer(int X[A_Linhas][A_Colunas], int number)
{
int c, l;
for (l = 0; l < A_Linhas; l++)
{
for (c = 0; c < A_Colunas; c++)
{
X[l][c] = number * X[l][c];
}
}
}
And here is the code in Assembly:
mul_integer:
push ebp
mov ebp, esp
sub esp, 16
mov dword [ebp-4H], 0
jmp L_020
L_017: mov dword [ebp-8H], 0
jmp L_019
L_018: mov edx, dword [ebp-4H]
mov eax, edx
add eax, eax
add eax, edx
shl eax, 2
mov edx, eax
mov eax, dword [ebp+8H]
lea ecx, [edx+eax]
mov edx, dword [ebp-4H]
mov eax, edx
add eax, eax
add eax, edx
shl eax, 2
mov edx, eax
mov eax, dword [ebp+8H]
add edx, eax
mov eax, dword [ebp-8H]
mov eax, dword [edx+eax*4]
imul eax, dword [ebp+0CH]
mov edx, eax
mov eax, dword [ebp-8H]
mov dword [ecx+eax*4], edx
add dword [ebp-8H], 1
L_019: cmp dword [ebp-8H], 2
jle L_018
add dword [ebp-4H], 1
L_020: cmp dword [ebp-4H], 3
jle L_017
nop
leave
ret
What's unclear? I want to know what happens to the stack in "mov dword [ebp-4H], 0 " for example... It's not clear to me what does this manipulation of ebp-4H or ebp -8H means... I know it's somehow running the matrix, but still not clear. I just need to know the beggining so I can figure out the rest.
– Léo Eduardo Silva
Jul 1 at 21:19
It isn't.
[ebp-4] is l and [ebp-8] is c (or the other way around, can't be bothered to check). Clearly, that corresponds to l = 0.– Jester
Jul 1 at 21:20
[ebp-4]
l
[ebp-8]
c
l = 0
Assuming you have first copied the stack pointer into
ebp, you are accessing various parts of it. Your locals are typically at negative offsets, arguments at positive.– Jester
Jul 1 at 21:46
ebp
stack is just ordinary memory, like any other, the only "special" thing about it, that stack is where the
esp register points to, and several instruction use this memory area implicitly (call, ret, push, pop, ...). So you can run this piece in debugger, break on the first instruction of the function, and use the debugger to view memory content around esp address (after the call function instruction it will point at the return address value stored in stack by call), and watch out how ebp is set relatively to that, and how the other area of memory around is used for local variables.– Ped7g
Jul 2 at 6:51
esp
call, ret, push, pop, ...
esp
call function
call
ebp
1 Answer
1
When I write mov edx, [ebp-4] or +4, or -4, +8, -8 what am I doing with the stack, exactly?
mov edx, [ebp-4]
Take a look at the stack (diagram from Wikipedia's Call Stack article). Note that low memory addresses are at the top of this diagram, while higher memory address are at the bottom.

The frame pointer is stored in register ebp (on x86). It contains the address of the Return Address.
ebp
Your local variables are stored before the Return Address. Variable c is 4 bytes in size. By subtracting 4 from the ebp address, you are now pointing to your first local variable, c. Subtract another 4 (making it -8), and you're now pointing to your second local variable l.
c
ebp
c
l
Excellent! Just what I need to conclude. Thanks a lot!
– Léo Eduardo Silva
Jul 1 at 22:38
Another one: if, instead of subtracting 4 I sum 4 or 8, what I am expecting to get? @Krii
– Léo Eduardo Silva
Jul 1 at 22:43
@LéoEduardoSilva Instead of moving "up" the stack, you will move "down" it. If you look at the image, you can see that "below" the frame pointer are the function parameters. :)
– Krii
Jul 2 at 3:35
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
"but I can't figure out the next first steps" - it's unclear what exactly you are asking. PS: that's very inefficient asm code.
– Jester
Jul 1 at 21:14