Jump to content

ASM Stack


Jonnycake

Recommended Posts

Okay, so I was just messing around with some ASM (linux x86) and using the stack. I have a basic understanding of the stack, but can't figure out one thing. So, let's say I have this:

.section .text
        .globl _start

_start:
         pushl $0x0a414141
         pushl $0x41
         movl %esp,%ecx
         movl $8,%edx
         movl $4,%eax
         movl $1,%ebx
         int $0x80
         movl $1,%eax
         movl $0,%ebx
         int $0x80

My question is why do i put the value 8 into edx instead of 5 since the actual string length is 5. I understand that each part of the stack holds 4 bytes (hence esp+4, esp+8, etc.), but I think it would just make more sense to have 5. I assume it's because with that the stack looks like:

+8 0x0a414141
+4 0x00000041
+0 esp

So you go from +4 and go forward 8 bytes ignoring the null bytes. Am I right in assuming this or is there another reason?

Link to comment
Share on other sites

Its word aligned, for a number of reasons, including simplicity and speed. If you were to allow mutiple pieces of data to be stored in the same word how would you tell where the first ended and the second began? You can't without more data to signify so, given this is small bits of data much cleaner to keep things word aligned.

Link to comment
Share on other sites

Ahhh, okay, I understand now. So, basically, you have to have a specific amount of bytes in a certain position on the stack or else you'd have to have another piece of data that tells where the string ends and begins which would be completely inefficient because it uses memory?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...