Jin000000
Jin000000

Reputation: 29

Is there any way to know the memory address differernce between %esp and %ebp?

(32bit assembly, so I can't use the register like r14)

Is there any way to know the memory address differernce between %esp and %ebp?

For example, if there is a less then two element in the stack, I want to go back to the read_token, which goes back and waits for another letter.

So if the difference is less then two blocks(8), then I want to go back to read_token.

But I found out that code like below:

cmpl $4, (%ebp)-(%esp)
jle read_token

is actually impossible.

I know that

"pop one elemet/check if stack top is empty/if empty, put it back/if nonempty, goto read_token "

is possible, but I think it is so complicated and might have more simpler way... can anyone help me?

Upvotes: 0

Views: 111

Answers (1)

Peter Cordes
Peter Cordes

Reputation: 365277

To implement do { } while(esp <= ebp -4); you'll want a tmp register, for example

.loop:                          # do{
   ...

   lea -4(%ebp), %eax                  # eax = ebp-4
   cmp %eax, %esp                      # AT&T means compare mnemonics are backwards
   jbe .Lloop                   # }while(esp <= ebp-4)

Use unsigned compares for addresses; your stack could span the signed-overflow boundary.


To actually compute the distance between two registers, simply subtract tmp = ebp-esp:

   mov   %ebp, %eax
   sub   %esp, %eax

  # cmp   $4, %eax
  # j??  somewhere

But obviously this is less efficient if you just want to branch on the result, costing an extra mov instruction vs. the LEA version where we could copy-and-add a negative constant all in one instruction.

Upvotes: 2

Related Questions