Search code examples
assemblyx86gnu-assembler

Why does initialising from stack yeld weird result in GNU GAS (AT&T)


I am kind of a noob in assembler. I am trying to initialise variables from the stack (ie. in a practical case their values are pushed to the stack by a third party). The code is similar to this:

.data
b:
        .long   0
a:
        .long   0

        .global main
        .text
main:
        push $2
        pop b
        push $1
        pop a

Anyway, what I would expect would be to have 1 in a and 2 in b. Instead, I have 1 in a and cruft in b (though it is static between executions). What am I doing wrong?

Thank you very much in advance.


Solution

  • You are doing wrong 2 things:

    1. Not showing the exact code, just something "similar".
    2. Not showing how you checked the value.

    The code is working as-is:

    (gdb) p a
    $1 = 1
    (gdb) p b
    $2 = 2