Writing to stack as local variable in _start function (x86 ASM)

Christophe De Troyer

I'm writing a compiler - or something that looks like it - and I'm at the part where I generate simple, unoptimized assembly code.

The code works, except for one thing.

I understand that %esb and %esp work as follows in a function call:

Parameter #N       -> N*4+4(%ebp)
Parameter 2        -> 12(%ebp)
Parameter 1        -> 8(%ebp)
Return Address     -> 4(%ebp)
Old %ebp           -> (%ebp)
Local Variable 1   -> -4(%ebp)
Local Variable 2   -> -8(%ebp) and (%esp)

However, the code below, which is output from my compiler, segfaults on the first line where I try to assign to -8(%esp) in _start. Doing the same thing in the twice function does not segfault.

It seems to be that in the _start function I can't store 'local variables' on the stack, and I wonder how I should fix this?

The program:

.section .data
.section .text
.globl _start
.globl twice
.type twice, @function
twice:
    pushl %ebp
    movl %esp, %ebp

    movl 8(%ebp), %eax
    movl 8(%ebp), %ebx
    addl %eax, %ebx
    movl %ebx, -4(%ebp)
    movl %ebx, %eax
    movl %eax, -8(%ebp)
    movl %eax, -8(%ebp)
end_twice:
    leave
    ret
_start:
    movl $10, %eax
    movl %eax, -8(%ebp)
    movl $10, %eax
    pushl %eax
    call twice
    addl $4, %esp
    movl %eax, -8(%ebp)
    movl %eax, %ebx
    # Exit stuff.
    movl $1, %eax
    int $0x80

Original input:

int twice(int x)
{
    return x+x;
}

int main()
{
    int x;
    x = 10;
    x = multiply(x);
}
Deleted User

You'd typically, if using EBP as pointer to local stack frame, not write to where it's pointing to before calling the sub. instead, you'd simply push to ESP, and leave it to the sub to set up EBP accordingly, so you can read from where you pushed to ESP before. Your use of EBP, if using "traditional" stack frames, is therefore incorrect.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how the local variable organized in stack when function call?

From Dev

Setting value stored at address using x86 ASM

From Dev

Local variable in global function

From Dev

ASM x86 Function call : best method to save EBP and common registers (EAX, EBX, ...)

From Dev

x86 Assembly , stack push instruction

From Dev

[x86 ASM]Running a graphical debugger with arguments

From Dev

Why function parameter occupy at least 4 bytes stack on x86?

From Dev

Does the stack automatically get popped when leaving a function in x86 NASM assembly language?

From Dev

ASM x86 Push and pop

From Dev

Overriding a Local Variable name in Java Bytecode using the ASM library

From Dev

Local variable position on stack not changing

From Dev

x86 assembly: Pass parameter to a function through stack

From Dev

gcc x86 Windows stack alignment

From Dev

how the local variable organized in stack when function call?

From Dev

ASM x86 Function call : best method to save EBP and common registers (EAX, EBX, ...)

From Dev

x86 Assembly , stack push instruction

From Dev

Latency semantics of read registers on x86 ASM

From Dev

Inline x86 asm for dividing by 2 in C

From Dev

x86 assembly: Compare DWORD variable

From Dev

writing response value to local variable in Volley

From Dev

ASM x86 Push and pop

From Dev

x86 ASM - Read line by line

From Dev

Writing NetCDF time variable from start of year

From Dev

ROL in x86 ASM in PROC parameter

From Dev

Why x86 places arguments on stack?

From Dev

asm X86 - segmentation fault?

From Dev

Local function - assigning variable

From Dev

Assembly x86 FPU - Stack Confusion

From Dev

Update function with local variable

Related Related

HotTag

Archive