ASCII调整和十进制调整指令如何工作? [英] How do ASCII Adjust and Decimal Adjust instructions work?

查看:180
本文介绍了ASCII调整和十进制调整指令如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力理解来自x86汇编语言的ASCII调整指令.

I've been struggling with understanding the ASCII adjust instructions from x86 assembly language.

我在互联网上看到的所有信息都在告诉我不同​​的东西,但是我猜想这就是同一件事,只是我以不同的方式解释了这一点.

I see all over the internet information telling me different things, but I guess it's just the same thing explained in a different form that I still don't get.

谁能用 AAA AAS 我们必须相加,从AL的低位半字节中减去6?

Can anyone explain why in the pseudo-code of AAA, AAS we have to add, subtract 6 from the low-order nibble in AL?

并且有人可以在其中解释AAMAAD和十进制调整指令伪代码英特尔指令集手册也是如此,为什么如此,其背后的逻辑是什么?

And can someone explain AAM, AAD and the Decimal adjust instructions pseudo-code in the Intel instruction set manuals too, why are they like that, what's the logic behind them?

最后,有人可以举例说明这些说明何时有用,或者至少在过去的应用中有用.

And at last, can someone give examples when these instructions can be useful, or at least in what applications they have been useful in the past.

我知道现在这些指令不被使用,但是我仍然想知道这些指令是如何工作的,很高兴知道.

I know that nowadays these instructions aren't used, but I still want to know how these instructions work, it's good to know.

推荐答案

为什么要在AAA伪代码AAA中添加AAS,从AL中的低位半字节中减去6

why in the pseudo-code of AAA, AAS we have to add, subtract 6 from the low-order nibble in AL

因为以十六进制表示的每个字符都有16个不同的值,而BCD仅具有10个值.当您以十进制进行数学运算时,如果数字大于10,则需要取10的模数并继续到下一行.类似地,在BCD数学中,当加法的结果大于9时,您将加6以跳过剩余的6个无效"值并进位到下一位.相反,您将减法减去6.

Because in hexadecimal each character has 16 distinct values and BCD has only 10. When you do math in decimal, if a number is larger than 10 you need to take the modulus of 10 and carry to the next row. Similarly, in BCD math, when the result of the addition is larger than 9 you add 6 to skip the 6 remaining "invalid" values and carry to the next digit. Conversely you subtract 6 in subtractions.

例如:27 + 36

For example: 27 + 36

  27: 0010 0111
+ 36: 0011 0110
───────────────
5_13: 0101 1101 (13 >= 10)
+  6:      0110
───────────────
  63: 0110 0011 (13 + 6 = 19 = 0x13, where 0x3 is the units digit and 0x10 is the carry)

进行解压加法是一样的,只是您直接从单位数字携带到十位数,而丢弃每个字节的前半字节

Doing unpacked addition is the same except that you carry directly from the units digit to the tens digit, discarding the top nibbles of each byte

有关更多信息,请阅读

  • BCD Addition assembly program logic
  • Why must six be added to a BCD addition if it is an invalid BCD code?

有人还能在英特尔指令集手册中解释AAM,AAD和十进制调整指令伪代码,为什么会这样,其背后的逻辑是什么?

and can someone explain AAM, AAD and the Decimal adjust instructions pseudo-code in the Intel instruction set manuals too, why are they like that, what's the logic behind them?

AAM只是从二进制到BCD的转换.您通常以二进制形式进行乘法运算,然后调用AAM将结果除以10,并将商-余数对存储在两个未打包的BCD字符中

AAM is just a conversion from binary to BCD. You do the multiplication normally in binary, then calling AAM divides the result by 10 and store the quotient-remainder pair in two unpacked BCD characters

例如:

13*6 = 78 = 0100 1110
78/10 = 7 remains 8 => result = 0x78

AAD是相反的:除法之前,您调用AAD将其从BCD转换为二进制,然后像其他二进制除法一样进行除法

AAD is the reverse: before the division, you call AAD to convert it from BCD to binary and do the division just like other binary divisions

例如:87/5

0x8*10 + 0x7 = 0x57
0x57/5 = 0x11 remains 0x7

执行这些指令的原因是因为在过去,内存非常昂贵,因此您必须尽可能减少内存使用量.因此,在那个时代, CISC CPU非常普遍.他们使用大量复杂的指令来最大程度地减少用于执行任务的指令.如今,内存便宜得多,现代架构几乎要冒风险,需要权衡CPU复杂度和代码密度

The reason for those instruction is because in the past, memories are expensive and you must reduce the memory usage as much as possible. Hence in that era CISC CPUs are very common. They use lots of complex instructions to minimize the instructions used to do a task. Nowadays memory is much cheaper and modern architectures are almost RISCy, with the trade off of CPU complexity and code density

这篇关于ASCII调整和十进制调整指令如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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