与JMP指令混淆 [英] confusing with JMP instruction

查看:215
本文介绍了与JMP指令混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个内联汇编程序以取消链接/home/httpd 中的"grades.txt" ,下面是代码:

I write an inline assembly program to unlink "grades.txt" in /home/httpd, here is the code:

void main()
{
   __asm__(
   "jmp L\n"\
   "sub1:\n"\
   "movl 0x4(%esp), %ebx\n"\
   "movb $0xa, %al\n"\
   "int $0x80\n"\
   "L:\n"\
   "call sub1\n"\
   ".string \"//home//httpd//grades.txt\" "
   );
}

我认为代码将按照我想要的方式工作,以取消"/home/httpd"中的grades.txt的链接,但是当我执行它时,出现了段错误.

I think the code shall do what I want, to unlink the grades.txt in "/home/httpd", yet when I execute it, there is a segment fault.

然后我使用gdb来解决此错误,我发现它无法执行"jmp L"行,该程序将在第5行["__asm __("]时停止,直到我输入"ctrl + c"中断它.

And I use gdb to tackle this fault, I found that it can't execute the line "jmp L", the program will stop when in line 5 ["__asm__("] until I enter "ctrl + c" to interrupt it.

如果我添加"exit(0)"的程序集以使程序干净退出,然后继续执行它,则程序将不执行任何操作而退出.

If I add the assembly of "exit(0)" to let the program exit cleanly , and continue execute it, the program will just exit without doing anything.

这很令人困惑,为什么程序不执行jmp指令?是否有任何错误?

so this is quite confusing, why the program doesn't execute the jmp instruction? Is there any errors?

非常感谢您的帮助!

推荐答案

几件事:

  • 您应该使用%eax而不是%al,因为3个最高有效字节不能为000000
  • movl 0x4(%esp), %ebx行应为movl (%esp), %ebx,因为未将%ebp压入堆栈,因此返回地址位于%esp+0
  • 在执行doinig int 80h之后,代码将掉落并一遍又一遍地调用sub1,因此您需要额外的跳转:

  • You should use %eax instead of %al because the 3 most significant bytes can be not 000000
  • The movl 0x4(%esp), %ebx line should be movl (%esp), %ebx because %ebp is not pushed onto the stack hence return address is at %esp+0
  • After doinig int 80h the code will fall through and call sub1 over and over again, so you need an extra jump:

int80h
add $4, %esp # pop the return address
jmp exit     # jump over the call
call sub1
....
exit:

  • 无需使用\-字符串将为您串联

  • No need to use \ - the strings will be concatenated for you

    总结以上提示:

    int main()
    {
       __asm__(
       "jmp L\n"
       "sub1:\n"
       "movl (%esp), %ebx\n"
       "movl $0xa, %eax\n"
       "int $0x80\n"
       "add $4, %esp\n"
       "jmp exit\n"
       "L:\n"
       "call sub1\n"
       ".string \"//home//httpd//grades.txt\"\n"
       "exit:\n"
       );
    }
    

    这篇关于与JMP指令混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆