我有一个程序,我正在调用Visual Studio 2010中的x86汇编(MASM)。 它所做的只是将存储在ax寄存器中的基数10数字转换为一个二进制字符串(例如10100b)。我遇到的问题是,只要斧头等于1,它就会覆盖并等于一些大数字。div操作换行(x86汇编)
.code
main proc
xor eax, eax
xor ebx, ebx
xor ecx, ecx
xor edx, edx
lea esi, binResult ; convert result to string (binary notation)
mov ax, [result]
mov bx, 2
call Convert2Bin
lea esi, binResult ; test
call PrintString
EndofProgram:
invoke ExitProcess, 0
main endp
Convert2Bin proc ; Define procedure
pushad ; save registers
pushfd ; save flags
divide_Convert2Bin:
cmp eax, 1
je addOne_ThenExit
cmp eax, 0
je addZero_ThenExit
div ebx
cmp edx, 1
je addOne_ThenLoop
cmp edx, 0
je addZero_ThenLoop
addOne_ThenLoop:
mov byte ptr [esi], '1'
inc esi
jmp divide_Convert2Bin
addZero_ThenLoop:
mov byte ptr [esi], '0'
inc esi
jmp divide_Convert2Bin
addOne_ThenExit:
mov byte ptr [esi], '1'
inc esi
jmp done_Convert2Bin
addZero_ThenExit:
mov byte ptr [esi], '0'
inc esi
jmp done_Convert2Bin
done_Convert2Bin:
mov byte ptr [esi], 'b'
popfd ; restore flags
popad ; restore registers
ret ; return to caller
'pusha/popa'和'和'pushf/popf'都很慢。在asm中写入时,您可以*在每个函数的基础上构建自己的ABI,但是保存/恢复*所有内容*都是不好的选择。标准ABI不保留标志,并且有一些调用者保存的寄存器,可以在不保存的情况下在函数中使用。您只需要保存/恢复ABI指定为被调用者保存的寄存器。 –