我一直在为我的汇编类进行硬件分配,我们必须从 F 转换为 C,然后计算“更正”值和“四舍五入”值的差异。但是,当我尝试对第二个数组重用 eax 时,我无法让程序正常运行。我一直试图找出为什么这会给我的程序带来问题,但到目前为止我还没有成功。谁能向我解释为什么会发生这种情况?
.586
.MODEL FLAT
EXTRN _printf:PROC
.STACK 4096 ; reserve 4096-byte stack
.DATA ; reserve storage for data
nbrArray DWORD -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
nbrC DWORD -23,-23,-22,-22,-21,-21,-20,-19,-19,-18,-18,-17,-17,-16,-16,-15,-14,-14,-13,-13,-12
nbrElts DWORD 21
cTemp DWORD ?
mult DWORD 5
div DWORD 9
diff DWORD ?
corr DWORD ?
format1 BYTE "Rounded C: %d", 0
format2 BYTE " Corrected C: %d", 0
format3 BYTE " Difference: %d", 10, 0
.CODE ; start of main program code
main PROC
lea ebx, nbrArray ; get address of nbrArray
lea edx, nbrC ; get address of nbrCorrect
mov ecx, nbrElts ; count := nbrElts
forLoop:
mov eax, [ebx] ; mov value to eax
add ebx, 4 ; iterate thru array
sub eax, 32 ; F-32
imul mul ; (F-32)*5
add eax, 4 ; ((F-32)*5)+4
cdq
idiv div ; (((F-32)*5)+4)/9
mov cTemp, eax ; Store celcius result
mov eax, [edx] ; mov value to eax
add edx, 4 ; iterate thru array
mov corr, eax ; store correct result
sub eax, cTemp ; Calculate difference
;output: use printf("Rounded Up C: %d", cTemp)
pusha
push cTemp
push offset format1
call _printf
add esp, 8
popa
;output: use printf(" Corrected C: %d", corr)
pusha
push corr
push offset format2
call _printf
add esp, 8
popa
loop forLoop
mov eax, 0
ret
main ENDP
END
这就是造成问题的部分。特别是第一行,每当我运行我的程序时:
mov eax, [edx] ; mov value to eax
add edx, 4 ; iterate thru array
mov corr, eax ; store correct result
最佳答案
在 for 循环中,您必须存储和恢复 edx
,因为所有指令 mul
、div
和 cdq
使用 edx 作为隐式辅助寄存器。因此您没有访问您想要访问的数组。
重用edx的两种策略是:
1) 使用堆栈
push edx;
;; do something else, like
cdq
mul ebx
idiv ebp
...
pop edx
2) 为指针指定一个变量(我在这里使用索引)
mov dword ptr [myIndex], 0 ;// Optionally initialize the variable
...
cdq
mov edx, [myIndex] ;// Use which ever register that is available
inc dword ptr [myIndex], 1 ;// Post increment the index -- directly in memory
mov eax, [myArray + 4*edx] ;// use 'SIB' addressing mode (Scale, Index, Base)
关于assembly - 对两个不同的数组重复使用 EAX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22368436/