assemblyx86stack-frame

The x86 assembly code run successfully but vscode show error without more information


I am new to assmbly, and I have some problem. I compiled the following code with GCC:

.intel_syntax noprefix

.section .data
number:
    .int 65

message:
    .ascii "The number is %d %d.\n\0"

.section .text
    .globl  _main

_main:
    push ebp

    lea eax, [message]
    mov ebx, number
    mov [esp + 8], ebx
    add ebx, 1
    mov [esp + 4], ebx
    mov [esp], eax

    call _printf

    pop ebp
    xor eax, eax
    ret

It can display the message The number is 66 65. on the console, but the vscode show some errors without more information.

vscode output

I tried to remove the line mov [esp + 4], ebx, and the error that vscode showed fixed.

.intel_syntax noprefix

.section .data
number:
    .int 65

message:
    .ascii "The number is %d %d.\n\0"

.section .text
    .globl  _main

_main:
    push ebp

    lea eax, [message]
    mov ebx, number
    mov [esp + 8], ebx
    add ebx, 1
    mov [esp], eax

    call _printf

    pop ebp
    xor eax, eax
    ret

vscode output after fixing

Can anyone help me to solve the problem and explan it? Thank you!


Solution

  • Thanks for Mr./Ms. Peter's answer (and also thanks for Mr./Ms. rpatel3001's answer), I solve this problem by reserving the stack space.

    Here is the code after modified:

    .intel_syntax noprefix
    
    .section .data
    number:
        .int 65
    
    message:
        .ascii "The number is %d %d.\n\0"
    
    .section .text
        .globl  _main
    
    _main:
        push ebp
        sub esp, 8 # <-----
    
        lea eax, [message]
        mov ebx, number
        mov [esp + 8], ebx
        add ebx, 1
        mov [esp + 4], ebx
        mov [esp], eax
    
        call _printf
    
        add esp, 8 # <-----
    
        pop ebp
        xor eax, eax
        ret