本文介绍了使用SHL由奇数乘汇编语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要37乘以EAX,我知道我可以使用 SHL EAX,5 SHL寄存器,2 然后添加寄存器EAX 38倍增,但我不知道code。通过奇数的繁殖和我必须使用SHL添加,只MOV。谢谢!

I need to multiply EAX by 37, I know I can use shl eax,5 and shl register,2 then add the register to eax to multiply by 38, but I'm not sure the code to multiply by an odd number and I have to use shl add and mov only. Thanks!

推荐答案

使用LEA,可以直接乘小了很多,奇常数(2,4,8,3,5,9),以及添加两个注册和答案移动到不同的地方。这是令人惊讶的是有用的。撰写这些,你可以通过pretty乘用的LEA指令序列的很多小恒,常pretty短。明智地使用其他ADD,SHL和SUB(包括NEG则ADD)指令可以缩短这些序列。这些这样的短序列几乎总是比使用乘法快,部分原因是因为许多指令在执行由目前的处理器重叠:

Using LEA, you can directly multiply by a lot of small, odd constants (2,4,8,3,5,9) as well as add two registers and move the answer to a different place. This is astonishingly useful. Composing these, you can multiply by pretty much any small constant using a sequence of LEA instructions, often pretty short. Judicious use of other ADD, SHL, and SUB (including NEG then ADD) instructions can shorten those sequences. Such short sequences of these are almost always faster than using multiply, partly because many of the instructions are overlapped in execution by current processors:

乘EAX 37:

 lea ecx, [eax+8*eax] ; 9 * eax
 lea eax, [ecx*4+eax] ; 37 * eax

乘EAX 38:

Multiply eax by 38:

 lea ecx, [8*eax]
 neg eax
 lea ecx, [5*ecx] 
 lea eax, [ecx+2*eax]

更好的:

 lea ecx, [8*eax+eax] ; 9 * eax
 lea ecx, [4*ecx] ; 36 * eax
 lea eax, [eax*2+ecx] ; 38 * eax

只是为了好玩,用103乘以:

Just for fun, multiply by 103:

 lea ecx, [8*eax] ; 8 * eax
 lea ecx, [ecx*4] ; 32 * eax
 lea ecx, [ecx*2+ecx] ; 96 * eax
 lea ecx, [ecx+8*eax]; ; 104 * eax
 sub ecx, eax ; 103 * eax

这篇关于使用SHL由奇数乘汇编语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 05:23