装配中的MUL功能 [英] MUL function in assembly

查看:321
本文介绍了装配中的MUL功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在汇编中进行简单的乘法运算,但是由于某些原因,当标记了MUL函数时,我看不到寄存器会发生变化.

I am trying to make a simple multiply action in assembly but for some reason i do not see the registers change when the MUL function is marked.

mov bx, 5    
mov cx, 10

mul cx

推荐答案

这些称为指令,它们指定要由处理器执行的操作. mov mov e的助记符,而mul mul 的助记符.其他常见指令包括addsubdiv.我相信您可以弄清楚这些操作指定了什么!

These are called instructions, and they specify operations that are to be performed by the processor. mov is a mnemonic for move, while mul is a mnemonic for multiply. Other common instructions include add, sub, and div. I trust you can figure out what operation these specify!

大多数指令采用两个参数.用技术术语来说,这些通常称为 operands .第一个(左侧)是目的地,第二个(右侧)是 source .因此,在mov bx, 5的情况下,这会将字面值5移动到目标寄存器bx中.当然,这些参数的顺序很重要,因为您不能将寄存器bx的内容移到文字值5

Most instructions take two parameters. In the technical jargon, these are often known as operands. The first (on the left) is the destination, and the second (on the right) is the source. So, in the case of mov bx, 5, this moves the literal value 5 into the destination register bx. The order of these parameters matters, of course, because you could not move the contents of the register bx into the literal value 5!

mul指令有点奇怪,因为它的某些操作数是隐式的.也就是说,它们没有明确指定为参数.对于mul指令,目标操作数被硬编码为ax寄存器.源操作数是您作为参数传递的一个:它可以是寄存器或内存位置.

The mul instruction is a little bit strange because some of its operands are implicit. That is, they are not explicitly specified as parameters. For the mul instruction, the destination operand is hard-coded as the ax register. The source operand is the one that you pass as a parameter: it can be either a register or a memory location.

因此,您可以想象mul cx表示mul ax, cx,但是由于ax目标寄存器是隐式的,所以您不会这样写.

Therefore, you could imagine that mul cx means mul ax, cx, but you don't write it that way because the ax destination register is implicit.

现在,mul指令命令处理器将目标操作数与源操作数相乘,并将结果存储在目标中.在代码中,您可以想象mul cx将转换为ax = ax * cx.现在,您应该看到问题了:您尚未初始化ax寄存器的内容,因此您要乘以10(这是您在cx中放置的值)乘以ax中剩下的任何垃圾.因此,结果毫无意义!

Now, a mul instruction commands the processor to multiply the destination operand by the source operand, and store the result in the destination. In code, you can imagine that mul cx would translate into ax = ax * cx. And now you should see the problem: you have not initialized the contents of the ax register, so you are multiplying 10 (which is the value you placed in cx) by whatever garbage was left in ax. As such, the result is meaningless!

如果您实际上想做5 * 10,那么您只需要在代码中更改一个字符即可:

If you in fact want to do 5 * 10, then you just have to change one character in your code:

mov  ax, 5     ; ax = 5
mov  cx, 10    ; cx = 10
mul  cx        ; ax = ax * cx   ; actually dx:ax = ax * cx

结果将存储在隐式目标寄存器ax中.

The result will be stored in ax, which is the implicit destination register.

从技术上讲,结果将存储在dx:ax中.这是一个寄存器对,表示结果的高位部分将存储在dx中,而结果的低位部分将存储在ax中.为什么会有这种额外的并发症?因为将两个16位值相乘可能会导致一个大于16位的值!在16位寄存器的中返回全乘法结果,允许mul指令返回32位结果.但是,当您只是在学习时,就不必为此担心.您可以忽略溢出的可能性,并从ax中提取结果的较低部分. (但是请记住,无论是否要使用16位的mul都会覆盖dx.在386及更高版本上,您可以使用imul ax, cx来真正执行ax *= cx,而不会浪费时间编写dx.)

Well, technically, the result will be stored in dx:ax. This is a register pair, and means that the high portion of the result will be stored in dx, while the low portion of the result will be stored in ax. Why this extra complication? Because multiplying two 16-bit values may result in a value that is larger than 16 bits! Returning the full multiply result in a pair of 16-bit registers allows the mul instruction to return a 32-bit result. When you're just learning, though, you don't need to worry about this. You can just ignore the possibility of overflow, and extract the low portion of the result from ax. (But remember that 16-bit mul overwrites dx whether you want it or not. On 386 and later you can use imul ax, cx to really do ax *= cx without wasting time writing dx.)

虽然我确定这只是一个玩具示例,但实际上没有理由编写将两个常数相乘的 code .这可以在构建时完成,要么使用计算器并对值进行硬编码,要么以符号形式写出常数的乘法,然后让您的汇编器进行计算.即mov ax, 50.或者让您的汇编器使用mov ax, 5 * 10为您完成此操作.但是就像我说的那样,我确定您已经知道这一点!

And while I'm sure that this is just a toy example, there is really no reason to write code that multiplies two constants together. This can be done at build time, either using a calculator and hard-coding the value, or writing out the multiplication of the constants symbolically and letting your assembler do the computation. That is, mov ax, 50. Or let your assembler do it for you with mov ax, 5 * 10. But like I said, I'm sure you knew this already!

如果其他所有方法均失败,请查阅文档以获取给您带来麻烦的说明.您几乎可以始终通过谷歌搜索指令名称和"x86"在网上找到它.例如,可以在此处以及其他几个站点中找到mul文档. .这些信息可能很复杂,但是只要稍作努力,您就可以提取所需的信息.您还可以在标记 wiki .

If all else fails, do consult the documentation for the instruction that is giving you trouble. You can almost always find this online by Googling the name of the instruction and "x86". For example, the mul documentation can be found here, as well as several other sites. This information can be kind of complicated, but with a bit of effort, you should be able to extract the information you need. You will also find lots of other great information and links in the x86 tag wiki.

但是由于某些原因,当标记了MUL功能时,我看不到寄存器更改.

but for some reason i do not see the registers change when the MUL function is marked.

我还应该指出,如果您使用调试器单步执行代码,则当前标记/突出显示的行是要执行的 about 行.它尚未执行,因此它对寄存器,存储器等的影响尚不可见.您必须单步执行该指令,以便标记/突出显示在下一个行上,然后您将看到上一条(刚刚执行的)指令的效果.

I should also point out that, if you are using a debugger to step through your code, the currently marked/highlighted line is the line that is about to execute. It has not executed yet, so its effects on registers, memory, etc. will not be visible yet. You have to step over the instruction, so that the mark/highlight is on the next line, and then you will see the effects of the previous (just-executed) instruction.

如果您理解了我上面的解释,则在mul指令之后,您应该会看到axdx寄存器的内容发生了变化.如果调试器显示了两个标志,那么您还将看到标志和指令指针发生变化.没有别的改变! (英特尔指令参考手册 mul 的条目未列出任何对体系结构状态的影响的机器.)

If you understood my explanation above, after a mul instruction, you should see the contents of the ax and dx registers change. You will also see flags and the instruction pointer change, if your debugger shows either of them. Nothing else should change! (Intel's instruction reference manual entry for mul doesn't list any other effects on the architectural state of the machine.)

这篇关于装配中的MUL功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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