Any solution to this puzzle !

N

noblesantosh

hello richard,

Could you tell me how to write inline assembly in ansi compatible code,
so that I can modify that code to suit your quality control criteria.
--santosh
 
A

Anand

Hi nitin,
Check it out, it works absolutely perfect.

#include <stdio.h>

void change(void)
{
/* write something here so that the printf in main prints the
value of
* 'i' as 10 */
asm(" popl %ebp ");
asm(" movl $10, -4(%ebp) ");
asm(" pushl %ebp ");

}

int main(void)
{
int i = 5;
change();
printf("%d\n", i);
return 0;
}

--santosh
If the basic idea is to get the address of i from the stack..
can't I use something like this?


#include <stdio.h>
static long stackFrameSize=0;
void foo()
{
int x;
stackFrameSize = stackFrameSize - (long)&x;
}
void change()
{
int x;
stackFrameSize = (long )&x;
foo();
int *i= (int*)((unsigned long )&x + stackFrameSize -sizeof(int));
(*i)+=5;
}

int main(void)
{
int i=5;
change();
printf("%d\n",i);
return 0;
}

--Anand
 
C

CBFalconer

the asm keyword is not a part of the ANSI C standard. you cann't
use -ansi -pedantic with this code.

With what code? Include adequate context. If in doubt, scan the
group for sigs involving google and its broken interface.

Gcc, without -ansi - pedantic, is NOT a C compiler. It compiles
something the GNU people call GNU C, which does not conform to the
C standard.
 
R

Richard Heathfield

hello richard,

Could you tell me how to write inline assembly in ansi compatible code,

It's a contradiction in terms. One of the most important reasons for writing
ANSI (or ISO) conforming code is so that you can move the code from
platform to platform without having to spend years rewriting it for the new
platform.

Neither assembly language, nor mechanisms for embedding it within C code,
are standard across disparate platforms or compilers.
 
A

Anand

Anand,
you cann't use i in change() function, i is local to main()

--santosh
Probably you missed it.. the i in change() is a different i i.e. int *i
not the same one as in main()
see below
----------to make it different----
#include <stdio.h>
static long stackFrameSize=0;
void foo()
{
int x;
stackFrameSize = stackFrameSize - (long)&x;
}
void change()
{
int x;
stackFrameSize = (long )&x;
foo();
int *ptri= (int*)((unsigned long )&x + stackFrameSize -sizeof(int));
(*ptri)+=5;
}

int main(void)
{
int i=5;
change();
printf("%d\n",i);
return 0;
}
 
N

noblesantosh

anand (26),

Ok, but is it doing, what it needs to do. It has to chage i.

---santosh
 
C

Chris Croughton

it works, compile it as follows,

What works? Quote the relevant parts of the message to which you are
replying.
gcc foo.c -o foo

command not found: gcc
Later try with other options and let me know what happens.

Undeclared function 'asm', whatever options I use to the C compiler
(which is a C compiler, not a C-like compiler). Not that it would help
even if the 'asm' function were to use its argument as assembler,
there's no such instructions as ppol, movl and pushl on that
processor...

Chris C
 
C

Chris Croughton

Could you tell me how to write inline assembly in ansi compatible code,
so that I can modify that code to suit your quality control criteria.

You can't. In standard C there is no way to use assembler. Various
compilers may provide a way, or provide the ability to link to code
written in assembler, but that is outside the standards and is
inherrently non-portable (since each compiler may have different syntax
for it if they support it at all).

Chris C
 
A

Anand

anand (26),

Ok, but is it doing, what it needs to do. It has to chage i.

---santosh
Why do you think it doesn't do..
Assuming a system where stack grows up and the stack context size is
same for two functions with similar signature
so i am getting hold of the address of i
and modifying it..
 
W

Walter Roberson

Why do you think it doesn't do..
Assuming a system where stack grows up and the stack context size is
same for two functions with similar signature
so i am getting hold of the address of i
and modifying it..

Neither of those two assumptions are portable. The machine
I am using right now grows its stack downwards, and whether the
stack context size is the same or different for two functions with
similar signatures depends upon the optimization level chosen
and upon the optimizer's determination of opportunities
(which might include intra-procedural analysis...)
 
B

Bernhard Holzmayer

Nitin said:
Hi All,

Consider the following code snippet:

------------------------------------------

#include <stdio.h>

void change()
{
/* write something here so that the printf in main prints the value of
'i' as 10 */
}

int main(void)
{
int i=5;
change();
printf("%d\n",i);
return 0;
}
------------------------------------------

apart from the trivial pre-processor trick like this:

#define printf(a,i) printf(a,2*i)

is there any way to achieve it ?

Thanks.

Hi Nitin,

here's a standard conformal solution.
It uses the pre-processor, though.
Nevertheless it's very bad coding style.
Never do this in production-level coding environments!!!!


#include <stdio.h>

#define change() change(int * pi)

void change()
{
*pi +=5;
}

#undef change
#define change() change_(&i)

int main(void)
{
int i=5;
change();
printf("%d\n",i);
return 0;
}

Your compiler has an option, which makes the pre-process replacements
visible. If you use GNU, it's "gcc -E source.c"
This will result in code like this:
....
void change(int * pi)
{
*pi +=5;
}

int main(void)
{
int i=5;
change(&i);
printf("%d\n",i);
return 0;
}

So, the preprocessor does, what you should have coded in the first place.
This might be a valuable help, if you had to work with legal code where you
don't want to modify a line, or you simply may not.
And, it could be helpful under debugging circumstances(instrumentation).

Bernhard
 
A

Anand

Walter said:
Neither of those two assumptions are portable. The machine
I am using right now grows its stack downwards, and whether the
stack context size is the same or different for two functions with
similar signatures depends upon the optimization level chosen
and upon the optimizer's determination of opportunities
(which might include intra-procedural analysis...)
Agreed..

atleast the stack growing downwards could be handled..
just by checking the sign of the stackFrameSize (-ve ==> grow down)

int *ptri= (int*)((unsigned long )&x + stackFrameSize);
if (stackFrameSize < 0)
ptri = (int *)((unsigned long) ptri + sizeof(int));
else
ptri = (int *)((unsigned long) ptri - sizeof(int));
 
A

Anand

Walter said:
Neither of those two assumptions are portable. The machine
I am using right now grows its stack downwards, and whether the
stack context size is the same or different for two functions with
similar signatures depends upon the optimization level chosen
and upon the optimizer's determination of opportunities
(which might include intra-procedural analysis...)
Agreed..

atleast the stack growing downwards could be handled..
just by checking the sign of the stackFrameSize (-ve ==> grow down)

int *ptri= (int*)((unsigned long )&x + stackFrameSize);
if (stackFrameSize < 0)
ptri = (int *)((unsigned long) ptri + sizeof(int));
else
ptri = (int *)((unsigned long) ptri - sizeof(int));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,166
Messages
2,570,907
Members
47,448
Latest member
DeanaQ4445

Latest Threads

Top