问题描述
作为ASM编程的入门者,我需要将2的结果转换为Assembly的38的幂,并且我需要您的帮助以了解为什么我的程序未产生我需要的结果(它打印4十进制):
as a starter in ASM programming i am need to get the result of 2 to the power of 38 in Assembly , and i need your help in understanding why is my program doesn't produce the result i am need (it prints 4 decimal):
.386
.model flat, stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\msvcrt.inc
includelib \masm32\lib\msvcrt.lib
.data
formatstr db "%d",0
.code
start:
mov eax , 2
mov ecx , 38
mov esi , eax
mov edx , 0
.while TRUE
mul esi
mov esi, edx
add esi, eax
mov edx, 0
mov eax, 2
dec ecx
.break .if (!ecx)
.endw
invoke crt__cprintf, addr formatstr, esi
end start
如您所见,我正在使用masm32编写代码(如果在这种情况下有问题的话).
as you can see i am writing it using masm32 (if that has any matter in that case).
THX.
推荐答案
2^38
显然不适合一个32位寄存器,例如eax
.
2^38
obviously does not fit in one 32-bit register such as eax
.
要存储值2^38
(274877906944
),您需要39位.在32位代码中,您可以使用例如.两个32位寄存器,例如edx:eax
.但是,在32位代码中,mul
仅接受32位因数(例如,寄存器,其他始终为eax
),因此在循环中使用32位mul
可能会获胜不起作用,因为即使mul
将64位结果存储在 的32位寄存器中>.
To store the value 2^38
(274877906944
) you need 39 bits. In 32-bit code you can use eg. two 32-bit registers such as edx:eax
. However, in 32-bit code mul
only accepts 32-bit factors (eg. registers, other of them is always eax
), so using 32-bit mul
in a loop won't work, because you cannot store your intermediate results in a 32-bit register to be multiplied again, even if mul
stores the 64-bit result in edx:eax
.
但是您可以使用 rcl
来计算2^38
以32位代码显示:
But you can use rcl
to compute eg. 2^38
in 32-bit code:
xor edx,edx
mov eax,2 ; now you have 2 in edx:eax
mov ecx,38 ; 2^n, in this case 2^38 (any value x, 1 <= x <= 63, is valid).
x1: dec ecx ; decrease ecx by 1
jz ready ; if it's 2^1, we are ready.
shl eax,1 ; shift eax left through carry flag (CF) (overflow makes edx:eax zero)
rcl edx,1 ; rotate edx through carry flag (CF) left
jmp x1
ready: ; edx:eax contains now 2^38.
编辑:一个受@Jagged O'Neill的答案启发的非循环实现.对于指数> = 32,这一次没有跳跃,对于指数<<是1,没有跳跃. 32,也适用于ecx
0,大于63的ecx
设置edx:eax
至0
.
a non-loop implementation inspired by @Jagged O'Neill's answer. This one is without jumps for exponent >= 32, one jump for exponent < 32, works also for ecx
0, for ecx
greater than 63 sets edx:eax
to 0
.
mov ecx,38 ; input (exponent) in ecx. 2^n, in this case 2^38.
; (any value x, 0 <= x <= 63, is valid).
; the code begins here.
xor eax,eax
xor edx,edx ; edx:eax is now prepared.
cmp cl,64 ; if (cl >= 64),
setb al ; then set eax to 0, else set eax to 1.
jae ready ; this is to handle cl >= 64.
; now we have 0 <= cl <= 63
sub ecx,1
setnc al ; if (count == 0) then eax = 0, else eax = 1.
lea eax,[eax+1] ; eax = eax + 1. does not modify any flags.
jna ready ; 2^0 is 1, 2^1 = 2, those are ready now.
mov ebx,ecx ; copy ecx to ebx
cmp cl,32 ; if (cl >= 32)
jb low_5_bits
mov cl,31 ; then shift first 31 bits to the left.
shld edx,eax,cl
shl eax,cl ; now shifted 31 bits to the left.
lea ecx,[ebx-31] ; cl = bl - 31
low_5_bits:
shld edx,eax,cl
shl eax,cl
ready:
这篇关于x86组装中的力量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!