What is the return value of a semicolon?

im just interested in the following example

#include<stdio.h>
int test();
int test(){
     //    int a = 5;
     //    int b = a+1;
     return ;
}
int main(){
     printf("%u\n",test());
     return 0;
}

i compiled it using 'gcc -Wall -o semicolon semicolon.c' to create an executable file and 'gcc -Wall -S semicolon.c' to get the assembler code, which:

    .file   "semicolon.c"
    .text
.globl test
    .type   test, @function
test:
    pushl   %ebp
    movl    %esp, %ebp
    subl    $4, %esp
    leave
    ret
    .size   test, .-test
    .section        .rodata
 .LC0:
    .string "%u\n"
    .text
 .globl main
    .type   main, @function
 main:
    leal    4(%esp), %ecx
    andl    $-16, %esp
    pushl   -4(%ecx)
    pushl   %ebp
    movl    %esp, %ebp
    pushl   %ecx
    subl    $20, %esp
    call    test
    movl    %eax, 4(%esp)
    movl    $.LC0, (%esp)
    call    printf
    movl    $0, %eax
    addl    $20, %esp
    popl    %ecx
    popl    %ebp
    leal    -4(%ecx), %esp
    ret
    .size   main, .-main
    .ident  "GCC: (Ubuntu 4.3.3-5ubuntu4) 4.3.3"
    .section        .note.GNU-stack,"",@progbits

since im is not such an assembler pro, I only know that printf prints what is in eax but I don’t fully understand what “movl% eax, 4 (% esp)” means, I assume filling eax before calling the test but what then is the value? what does 4 (% esp) mean and what does esp mean?

if I uncomment the lines in test () printf prints 6 - which is written in eax ^^

+3
source share
4 answers

Your build language is annotated:

test:
    pushl   %ebp        # Save the frame pointer
    movl    %esp, %ebp  # Get the new frame pointer.
    subl    $4, %esp    # Allocate some local space on the stack.
    leave               # Restore the old frame pointer/stack
    ret

, eax.

.size   test, .-test
.section        .rodata
 .LC0:
.string "%u\n"
.text
 .globl main
.type   main, @function
main:
leal    4(%esp), %ecx      # Point past the return address.
andl    $-16, %esp         # Align the stack.
pushl   -4(%ecx)           # Push the return address.
pushl   %ebp               # Save the frame pointer
movl    %esp, %ebp         # Get the new frame pointer.
pushl   %ecx               # save the old top of stack.
subl    $20, %esp          # Allocate some local space (for printf parameters and ?).
call    test               # Call test.

, . , , .

movl    %eax, 4(%esp)      # Save eax as a printf argument.
movl    $.LC0, (%esp)      # Send the format string.
call    printf             # Duh.
movl    $0, %eax           # Return zero from main.
addl    $20, %esp          # Deallocate local space.
popl    %ecx               # Restore the old top of stack.
popl    %ebp               # And the old frame pointer.
leal    -4(%ecx), %esp     # Fix the stack pointer,
ret

, , , - , . , undefined: , ( ) eax.

+9

, , , " ", , void, .

:

warning: `return' with no value, in function returning non-void

eax test.

4 (% esp), (esp) + 4. I.e. - .

+7

int EAX. EAX, . , undefined.

+2

.

, return <nothing> int , , undefined. -Wall

semi.c: In function ‘test’:
semi.c:6: warning: ‘return’ with no value, in function returning non-void

%4,esp, , (), , , . , ( ) - . , "undefined".:)

0

Source: https://habr.com/ru/post/1728600/


All Articles