纯粹为了短期保存而推送寄存器是否可以? [英] Is it okay to push registers purely for preservation for short periods of time?

查看:20
本文介绍了纯粹为了短期保存而推送寄存器是否可以?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经学习 NASM 几个星期了.一切顺利 - 我目前一直专注于 64 位.

I have been learning NASM for a few weeks now. All is going well - I have been focusing on 64-bit for now.

之前我注意到当我执行 mul 指令时 rdx 寄存器被弄乱了.

Earlier I noticed that the rdx register was being messed with when I execute a mul instruction.

基本上,代码是这样的:

Basically, the code was similar to this:

; Code using RDX is up here

mov rax,2
mov rbx,10

mul rbx

; Code using RDX again is down here .. but the value is now zero

所以,显然RDX与mul有关.为了解决这个问题,我将代码更改为:

So, obviously RDX has something to do with mul. To fix this, I changed the code to this:

push rdx
mov rax,2
mov rbx,10

mul rbx

pop rdx

基本上,我在 mul 指令之前将寄存器保留在堆栈上,该指令会弄乱它......然后将其弹出(因为我不关心 中的值>rdx .. 我希望它成为我想要的样子).

Basically, I am preserving the register on the stack prior to the mul instruction that messes with it ... then popping it back afterwards (because I don't care about the value in rdx .. I want it to be what I want it to be).

我的问题是:这样做可以吗?常见吗?好久没做这个了..不过感觉有点奇怪.

My question is: Is this okay to do? Is it common? I haven't been doing this for very long .. but it feels a bit strange.

我可以改用局部变量进行某些计算而不是寄存器..这可能会解决问题,而无需在这么短的时间内将其推入堆栈..但问题仍然存在.

I could instead resort to using local variables for some of my computations instead of registers .. that would probably solve the issue without resorting to pushing it onto the stack for this short period of time .. but the question still stands.

额外问题:是否有什么地方可以全面介绍这些指令在 64 位上下文中的作用?我似乎找不到一个参考,它实际上用简单的英语告诉我 mul 指令之后 rdx 寄存器发生了什么.

Bonus question: Is there somewhere that has full coverage of what these instructions do in a 64-bit context? I can't seem to find a reference that actually tells me in plain English what is happening to the rdx register after the mul instruction.

推荐答案

有没有什么地方可以全面介绍这些指令在 64 位上下文中的作用?我似乎找不到一个参考,它实际上用简单的英语告诉我 mul 指令后 rdx 寄存器发生了什么

Is there somewhere that has full coverage of what these instructions do in a 64-bit context? I can't seem to find a reference that actually tells me in plain English what is happening to the rdx register after the mul instruction

英特尔手册说得很清楚:

MUL r/m64   Unsigned multiply (RDX:RAX ← RAX ∗ r/m64).

也就是说,RDX 将保存产品的最高 64 位.

That is, RDX will hold the most significant 64 bits of the product.

至于你的主要问题;如果可以,请使用不同的寄存器.如果不能,则需要以某种方式备份 RDX 的值,而使用堆栈是一种简单的方法.

As for your main question; use a different register if you can. If you can't, then you'll need to back up RDX's value somehow, and using the stack is a simple way of doing so.

这篇关于纯粹为了短期保存而推送寄存器是否可以?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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