Assembly Language for Beginners

(nextflipdebug2) #1

1.11. MORE ABOUT RESULTS RETURNING


push ebp
mov ebp, esp
and esp, -16
sub esp, 16
mov DWORD PTR [esp], OFFSET FLAT:.LC0
call puts
leave
ret

Let’ s write a bash script that shows the exit status:


Listing 1.97: tst.sh

#!/bin/sh
./hello_world
echo $?


And run it:


$ tst.sh
Hello, world!
14


14 is the number of characters printed. The number of characters printed isslipsfromprintf()through
EAX/RAXinto “exit code”.


By the way, when we decompile C++ in Hex-Rays, we can often encounter a function which terminated
with destructor of some class:


call ??1CString@@QAE@XZ ; CString::~CString(void)
mov ecx, [esp+30h+var_C]
pop edi
pop ebx
mov large fs:0, ecx
add esp, 28h
retn


By C++ standard, destructor doesn’t return anything, but when Hex-Rays don’t know about it, and thinks
that both destructor and this function returnsint, we can see something like that in output:


return CString::~CString(&Str);
}


1.11.2 What if we do not use the function result?


printf()returns the count of characters successfully output, but the result of this function is rarely used
in practice.


It is also possible to call a function whose essence is in returning a value, and not use it:


int f()
{
// skip first 3 random values:
rand();
rand();
rand();
// and use 4th:
return rand();
};


The result of the rand() function is left inEAX, in all four cases.


But in the first 3 cases, the value inEAXis just not used.

Free download pdf