如果函数没有明确使用“ret",为什么没有返回值 [英] Why is no value returned if a function does not explicity use 'ret'

查看:38
本文介绍了如果函数没有明确使用“ret",为什么没有返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下程序:

SECTION .text
main:
     mov ebx, 10
     mov ecx, 50

repeat:
     inc ebx
     loop repeat

     mov eax, ebx
     ret

当这个程序运行时,它按预期返回 60.但是,如果删除最后的 ret 语句,程序运行良好,但返回 0.这是为什么呢?

When this program runs, it returns 60 as expected. However, if you drop the final ret statement, the program runs fine, but then returns 0. Why is that?

推荐答案

当您离开ret"时,计算机会执行最后一个move eax, ebx",然后执行计算机内存中接下来发生的任何事情.

When you leave off the "ret", the computer executes the last "move eax, ebx" and then executes whatever happens to be next in computer memory.

我很惊讶您没有收到非法指令/访问;这将是最常见的反应.在清除寄存器之后,垃圾指令的作用就像一个返回.

I'm surprised you don't get an illegal instruction/access; that would be the most common response. Somehow the garbage instruction acts like a return, after trashing the registers.

您所说的返回 60"是什么意思也有点不清楚.您的意思是作为命令提示符的值?很明显,您的程序没有防御非法指令陷阱的能力.当你在没有防御的情况下遇到这样的陷阱时,Windows 会做什么我不清楚;我从经验中知道,当我这样做时,Windows 往往只会终止我的进程,并且我会得到一些随机退出状态.0"可能就是这样的状态.

Its also a little unclear what you mean by "returns 60". You mean as a value to the command prompt? It is clear that your program has no defense against illegal instruction traps. What Windows does when you get such a trap without a defense is unclear to me; I know from experience when I do that Windows tends to just terminate my process and I get some random exit status. "0" might be such a status.

尝试添加:

      mov   byte ptr[eax], 0

在ret"指令之前;这将导致非法内存引用.您报告您获得的状态.如果你在这种情况下得到零状态结果,我不会感到惊讶.

before the "ret" instruction; this will cause an illegal memory reference. You report what status you get. It wouldn't suprise me if you got the zero status result in this case.

这篇关于如果函数没有明确使用“ret",为什么没有返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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