我一直在为我的汇编类进行hw赋值,我们必须从F转换为C,然后计算“Corrected”值和“Rounded”值的差值。但是,当我尝试为第二个数组重用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
答案 0 :(得分:2)
在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)