LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-09-2009, 12:21 PM   #16
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: washington U.S.
Distribution: Damn Small Linux, KateOs, M$ Ickdows Vista, My own OS
Posts: 2,094

Rep: Reputation: 104Reputation: 104

AFAIK you do not need leave on any processor, also 195 is fact and 8088 instruction not petium LOL.
 
Old 11-09-2009, 05:42 PM   #17
orgcandman
Member
 
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 534

Rep: Reputation: 86
Quote:
Originally Posted by aagajaba View Post
I came 2 know 1 important thing.
195 is opcode for return in Pentium processors.
So it works with pentium.
But in core 2 duo, (as is mine), before returning from a function, it seems it is necessary to have an instruction called leave. I am not usre about this though.
So it gives segfault in core 2 duo.
Works fine on mine.
Code:
[05:36:05][aconole@ssh:~]
$ cat /proc/cpuinfo | grep Dual
model name	: Intel(R) Pentium(R) Dual  CPU  E2200  @ 2.20GHz
model name	: Intel(R) Pentium(R) Dual  CPU  E2200  @ 2.20GHz
[05:36:10][aconole@ssh:~]
$ cat test.cpp
#include <iostream>
using namespace std;
int main = ( cout << "Hello world!\n", 195 );
[05:36:13][aconole@ssh:~]
$ g++ -o tst test.cpp
[05:36:18][aconole@ssh:~]
$ ./tst
Hello world!
[05:36:19][aconole@ssh:~]
$
Just as an fyi.

A good way to see what's happening is to break at the cout print. The way I did this was make the code looks as follows:
Code:
#include <iostream>
using namespace std;
void pwn(const char *p){cout << p;}
int main = ( pwn("hello world\n"), 195 );
Then break on pwn.
Following is the backtrace:
Code:
#0  0x00000000004007d0 in pwn(char*) ()
#1  0x000000000040082d in __static_initialization_and_destruction_0(int, int)
    ()
#2  0x000000000040084c in global constructors keyed to _Z3pwnPc ()
#3  0x0000000000400916 in __do_global_ctors_aux ()
#4  0x000000000040066b in _init ()
#5  0x00007fffffffde18 in ?? ()
#6  0x00000000004008a5 in __libc_csu_init (argc=4196668, argv=0x1, envp=0x0)
    at elf-init.c:79
#7  0x00007ffff732b512 in __libc_start_main () from /lib64/libc.so.6
#8  0x0000000000400709 in _start () at ../sysdeps/x86_64/elf/start.S:113
That ?? () function call looks a little suspicious can be found in the memory map at:
Code:
7ffffffea000-7ffffffff000 rw-p 7ffffffea000 00:00 0                      [stack]
My guess is that it's related to the way the linker sets up the initialization process. I don't know that there's any "rule" that a program needs a main function, and this would certainly be a creative use of the main variable as a way to satisfy a braindead link check (omg - no main symbol?!) while not requiring main to actually do anything. Try setting a breakpoint on the address of main - it's never actually hit. However, my guess is that this is compiler dependent behavior. Not something to rely on, but certainly funny. The 195, if it's truly a return op code, is probably there for those versions of the compiler which will execute &main.

All in all, neat piece of code.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
small syntax problem with C code (implemented in Code Composer Studio) illiniguy3043 Programming 6 01-07-2008 03:14 AM
XAWTV works, videodog works, motion works but how to code my own? rylan76 Linux - Hardware 0 01-06-2006 07:30 AM
What's difference between the two small blocks of c++ code clinux_rulz Programming 9 12-04-2005 09:39 AM
what's wrong with this small code ? indian Programming 2 08-18-2004 12:12 PM
small errors in kernel code (2.4.22) Robert0380 Linux - General 2 10-06-2003 10:15 AM


All times are GMT -5. The time now is 05:10 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration