C - what is the return value of a semicolon?

im just curious about 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 with 'gcc -Wall -o semicolon semicolon.c' to create an executable and 'gcc -Wall -S semicolon.c' to get the assembler code which is:

.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 not such an assembler pro, i only know that printf prints what is in eax but i dont fully understand what 'movl %eax, 4(%esp)' means which i assume fills eax before calling test but what is the value then? what means 4(%esp) and what does the value of esp mean?

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

-------------Problems Reply------------

Your assembly language annotated:

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

Note that nothing in test touches eax.

.size test, .-test
.section .rodata
.string "%u\n"
.globl main
.type main, @function
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.

Note that at this point, nothing has modified eax. Whatever came into main is still here.

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,

So, what gets printed out is whatever came in to main. As others have pointed out it is undefined: It depends on what the startup code (or the OS) has done to eax previously.

The semicolon has no return value, what you have there is an "empty return", like the one used to return from void functions - so the function doesn't return anything.

This actually causes a warning when compiling:

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

And I don't see anything placed in eax before calling test.

About 4(%esp), this means taking the value from the stack pointer (esp) + 4. I.e. the one-before-last word on the stack.

The return value of an int function is passed in the EAX register. The test function does not set the EAX register because no return value is given. The result is therefore undefined.

A semicolon indeed has no value.

I think the correct answer is that a return <nothing> for an int function is an error, or at least has undefined behavor. That's why compiling this with -Wall yields

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

As for what the %4,esp holds... it's a location on the stack where nothing was (intentionally) stored, so it will likely return whatever junk is found at that location. This could be the last expression evaluated to variables in the function (as in your example) or something completely different. This is what "undefined" is all about. :)

Category:c# Views:0 Time:2010-01-15
Tags: c#

Related post

  • How can I use js eval to return a value? 2011-09-13

    I need to evaluate a custom function passed from the server as a string. It's all part of a complicated json I get, but anyway, I seem to be needing something along the lines: var customJSfromServer = "return 2+2+2;" var evalValue = eval(customJSfrom

  • Mathematica Module Not Returning Value 2011-11-15

    I am new to Mathematica, and using a Module to perform a procedure, then return a value. However, Mathematica seems to be evaluating and returning symbolic values instead of the numerical value I want. Questions I have are: When don't you use semicol

  • How do you print inside a case statement in SML? 2009-04-12

    I'm just starting out with SML, and I'm trying to modify some code so I understand what it's doing (I can't find a decent SML/NJ debugger, but that's a separate question). fun type_check e theta env non_gens = case e of constant_int _ => (integer,

  • mySQL query fails when read from file or web 2009-08-27

    This is driving me nuts! I have created a query. The query is written to Windows file for future connection to the remote database. Upon the connection using cURL, my local script uses cURL to send the query. I assemble variables using build_http_que

  • XCode Semicolon after Autocomplete 2010-02-20

    When using autocomplete to fill in method arguments, most of the time there should be a semicolon at the end. It seems like there must be some keyboard shortcut to put in the semicolon (and maybe a newline) at the end but I can't find it! Thanks. edi

  • Why not put all braces inline in C++, C#, Java, javascript, etc.? 2010-06-14

    Of all the conventions out there for positioning braces in C++, C#, Java, etc., I don't think I've ever seen anyone try to propose something like this: public void SomeMethod(int someInput, string someOtherInput) { if (someInput > 5) { var addedNu

  • JavaScript braces on new line or not? 2010-07-10

    At work, we place braces on the next line, but at home, I do the opposite. Which one do you prefer? (K&R vs OTBS) function something() { // ... } function something() { // ... } A lot of JavaScript libraries seem to use the OTBS (one true brace s

  • Why does a results vary based on curly brace placement? 2010-09-04

    I have read this article, where an example is shown. Please explain why the code snippets below return different results due to changes in the placement of curly the braces. Example with an opening curly brace { on new line. function test() { return

  • Unknown type: METHOD_DEF when running a groovy script 2010-10-20

    The following groovy script doesn't compile import java.util.concurrent.Callable println "b"; Callable<String> callable = new Callable<String>() { String call() { println("C"); return null; } }; This is the error: org.codehaus.groovy.cont

  • How can I assign a variable to content attr of dojo.xhrPost dynamically? 2010-10-21

    How can I assign a variable to content attr of dojo.xhrPost dynamically ? Below is the code and some explanation for my scenario: Code with hard coded values for content dojo.xhrPost({ url: /esturl/, handleAs : "text", content : {INST1 : '{"0001" : "

  • C++ compilation error "expected constructor, destructor, or type conversion before 'class'" 2011-07-29

    I received this error when compiling a large program parts of which I had changed, but it showed up in a part I hadn't changed. I was using gcc on unix. I've seen discussion online about similar errors, but those involve templates, which I'm not usin

  • Returning Large Results Via a Webservice 2008-08-15

    I'm working on a web service at the moment and there is the potential that the returned results could be quite large ( > 5mb). It's perfectly valid for this set of data to be this large and the web service can be called either sync or async, but I

  • Is it possible to return objects from a WebService? 2008-08-15

    Instead of returning a common string, is there a way to return classic objects? If not: what are the best practices? Do you transpose your object to xml and rebuild the object on the other side? What are the other possibilities? --------------Solutio

  • Why doesn't my Perl map return anything? 2008-08-15

    When I am running the following statement: @filtered = map {s/&nbsp;//g} @outdata; it is returning an empty list instead of the filtered list that I expected. What I am trying to do is remove every occurrence of &nbsp; from an array of string

  • How can I stop MATLAB from returning until after a command-line script completes? 2008-08-18

    I see in the MATLAB help (matlab -h) that I can use the -r flag to specify an m-file to run. I notice when I do this, MATLAB seems to start the script, but immediately return. The script processes fine, but the main app has already returned. Is there

  • DCOM: CoCreateInstanceEx returns E_ACCESSDENIED 2008-08-21

    I'm working on a DCOM application with the server and client on two machines, both of which are running WinXP with Service Pack 2. On both machines, I'm logged in with the same username and password. When the client on one machine calls CoCreateInsta

  • XmlHttpRequest return values 2008-08-22

    For an application I'm building in my spare time I am looking for (arguably) the correct way to return data from an XmlHttpRequest. Options I see are: Plain HTML. Let the request format the data and return it in a usable format. Advantage: easy to co

  • Is it better to structure an SQL table to have a match, or return no result 2008-08-22

    I've got an interesting design question. I'm designing the security side of our project, to allow us to have different versions of the program for different costs and also to allow Manager-type users to grant or deny access to parts of the program to

  • Java return copy to hide future changes 2008-08-27

    In Java, say you have a class that wraps an ArrayList (or any collection) of objects. How would you return one of those objects such that the caller will not see any future changes to the object made in the ArrayList? i.e. you want to return a deep c

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.158 (s). 11 q(s)