Quantcast
Channel: x86 Assembly - Programmers Heaven
Viewing all articles
Browse latest Browse all 152

trying to understand the stack

$
0
0
Well hello there.

I am a relatively new in Assembly programming. I have an extensive background in C++ programming, but I haven't been coding in about four years. Now I've started coding once again and I decided to move closer to the machine and learn the Assembly language.

That's all for background. Now as for my problem. I am reading the book "Guide To Assembly Language Programming In Linux" (by Sivarama Dandamudi) and I'm at the moment learning how to use stack. Well, I came across a rather confusing error (, is it error or is it just me, I'm not completely sure) in the book. Here is the thing:

I page 243 there is a piece of code like this:

push number1
push number2
call sum

(yes, very basic procedure call routine)

a little later, in the same page, the author tells that:

"... Since the stack is a sequence of memory locations, ESP + 4 points to number2 and ESP + 6 points to number1. Note that both number1 and number2 are 16-bit values. For instance,

mov EBX, [ESP+4]

can be used to access number2 ..."

now, as for the problem... in page 244 there is a descriptive diagram of the stack, after the 'sum' procedure has been called, and after the previous EBP value has been pushed into the stack and the EBP has been set to point: move EBP, ESP

But the problem is, in the diagram it says that the position of number2 in the stack is EBP + 8, and the position of number1 is EBP + 12 , not EBP + 8 and EBP + 10 as it clearly should if number1 and number2 that were pushed in the stack were 16-bit values. (remember at first the book told - that is before the function call - that ESP+4 points to number2 and ESP+6 points to number1)

I just don't get it. Is it an error in the book or is it just me who doesn't understand it...

Well, as for the next problem I have with the way the stack has been explained in the book is this:

(going back to the paragraph in page 243
"... Since the stack is a sequence of memory locations, ESP + 4 points to number2 and ESP + 6 points to number1. Note that both number1 and number2 are 16-bit values. For instance,

mov EBX, [ESP+4]

can be used to access number2 ...")

if the number1 in the stack is 16-bit value, how on earth can we move number1 into EBX, a 32-bit register. Doesn't that completely mes up things, since how does the instruction mov EBX, [ESP+4] know that we are only reading the first 16 bits from the stack, and not the whole 32-bits, into EBX? Because clearly if we read 32-bits from the stack, wouldn't we then read both the number1 and number2 into the EBX register? I'm a bit confused as to how on earth we can use a 32-bit register to read a 16-bit value from the stack. Shouldn't we use "mov BX, [ESP+4]" instead?

I hope you understand the meaning of the problem that I'm trying to explain. I do understand that maybe my choice of words are not the best possible, but my excuse would be that english is not my first language. But in you understood my problem, I would really appreciate it if you could help me out with it. I know I'm still a very much beginner when it comes to Assembly, but I really want to learn the language, and I want to learn to understand how the computer works (and that includes learning to understand how the stack works). Thank you in advance.

Viewing all articles
Browse latest Browse all 152

Trending Articles