本文介绍了如何从程序集(x86)更改指针值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的C代码,用于更改指针值

This is my C code that is used to change pointer value

     #include<stdio.h>
     #include<stdlib.h>
     typedef struct tcb tcb_t;

     struct tcb {
     void* sp;
     int id;
     };


    void* changePointer(struct tcb*);
    int main(){
         struct tcb* s;
         s=malloc(sizeof(struct tcb*));
        printf("%p\n",changePointer(s));
        printf("%p\n",s);
   return 0;
  }

这是我的汇编函数(x86)

This is my assembly function (x86)

    .text
    .globl changePointer

   changePointer:
   push %ebp
   movl %esp, %ebp
   movl 0x8(%ebp),%eax//original pointer value
   movl %ebp,%esp
   movl %ebp,0x8(%ebp) //it is to be the value  after changing
   popl %ebp
   ret

但是它并没有改变汇编函数中的指针值.请解释哪里出错了?

But it didn't change pointer value inside the assembly function .Please explain where went wrong?

推荐答案

尝试一下,它仅使用一条汇编程序指令,并将样板交给编译器:

Try this, which only uses a single assembler instruction and leaves the boilerplate to the compiler:

void changePointer(struct tcb *s)
{
    asm("movl %%esp, %0"
            : /* No output */
            : "m" (s->sp);
            : /* Nothing clobbered */
       );
}

这篇关于如何从程序集(x86)更改指针值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-05 07:50