Assembly 汇编中逐位减去两个整数
我正试图一点一点地减去2个整数,我得到了这个算法Assembly 汇编中逐位减去两个整数,assembly,x86,bitwise-operators,masm,irvine32,Assembly,X86,Bitwise Operators,Masm,Irvine32,我正试图一点一点地减去2个整数,我得到了这个算法 b = 0 difference = 0 for i = 0 to (n-1) x = bit i of X y = bit i of Y bit i of difference = x xor y xor b b = ((not x) and y) or ((not x) and b) or (y and b) end for loop 我已经实现了这一行b=((不是x)和y)或((不是x)和b)或(y和b
b = 0
difference = 0
for i = 0 to (n-1)
x = bit i of X
y = bit i of Y
bit i of difference = x xor y xor b
b = ((not x) and y) or ((not x) and b) or (y and b)
end for loop
我已经实现了这一行b=((不是x)和y)或((不是x)和b)或(y和b)
。我应该如何在代码中实现算法的最后一行
这就是我到目前为止所做的:
INCLUDE Irvine32.inc
.data
prompt1 BYTE "Enter the first integer: ",0dh,0ah,0
prompt2 BYTE "Enter the second integer: ",0dh,0ah,0
prompt3 BYTE "The first integer entered is not valid ",0dh,0ah,0
prompt4 BYTE "The second integer entered is not valid ",0dh,0ah,0
X byte 0
Y byte 0
diff byte 0
.code
main PROC
L1:
mov edx, OFFSET prompt1
call writeString
xor edx, edx
call readInt
js printError1
cmp eax, 0ffh
jg printError1
mov X, al
xor eax, eax
L2:
mov edx, OFFSET prompt2
call writeString
xor edx, edx
call readInt
js printError2
cmp eax, 0ffh
jg printError2
mov Y, al
xor eax, eax
jmp calculation
printError1:
mov edx, OFFSET prompt3
call writeString
xor edx, edx
jmp L1
printError2:
mov edx, OFFSET prompt4
call writeString
xor edx, edx
jmp L2
calculation:
mov ebx, 0
mov diff, 0
mov ecx, 7
subtract:
mov al, X
and al, 1h
mov dl, Y
and dl, 1h
xor al, dl
xor al, bl
mov diff, al
rol X, 1
rol Y, 1
loop subtract
exit
main ENDP
END main
该算法从计算循环标签开始。为了实现算法的最后一行,我需要保存存储在al
寄存器中的值,但是由于dl
和bl
在使用哪个通用寄存器来存储al
的值
INCLUDE Irvine32.inc
.data
prompt1 BYTE "Enter the first integer: ",0dh,0ah,0
prompt2 BYTE "Enter the second integer: ",0dh,0ah,0
prompt3 BYTE "The first integer entered is not valid ",0dh,0ah,0
prompt4 BYTE "The second integer entered is not valid ",0dh,0ah,0
prompt5 BYTE "The result is: ",0dh,0ah,0
X byte 0
Y byte 0
sum byte 0
.code
main PROC
L1:
mov edx, OFFSET prompt1
call writeString
xor edx, edx
call readInt
js printError1
cmp eax, 0ffh
jg printError1
mov X, al
xor eax, eax
L2:
mov edx, OFFSET prompt2
call writeString
xor edx, edx
call readInt
js printError2
cmp eax, 0ffh
jg printError2
mov Y, al
xor eax, eax
jmp calculation
printError1:
mov edx, OFFSET prompt3
call writeString
xor edx, edx
jmp L1
printError2:
mov edx, OFFSET prompt4
call writeString
xor edx, edx
jmp L2
calculation:
mov ebx, 0
mov bh, 0
mov ecx, 8
subtract:
mov al, X
and al, 1h
mov dl, Y
and dl, 1h
mov ah, al
mov dh, al
xor al, dl
xor al, bl
mov bh, al
add sum, bh
not ah
and ah, dl
not dh
and dh, dl
and dl, bl
or ah, dh
or ah, dl
mov bl, ah
ror X, 1
ror Y, 1
loop subtract
xor eax, eax
mov al, sum
js printError1
cmp ebx, 0ffh
jg printError1
jmp printResult
printResult:
xor edx, edx
mov edx, OFFSET prompt1
call writeString
call writeInt
exit
main ENDP
END main
好的,我知道了。不,你的代码还是错的。下面是一段代码,展示了如何在堆栈中存储寄存器。(但它还远未优化) 通常,如果寄存器不足,请使用堆栈。 如果在代码中的其他位置使用了寄存器并且需要持久化,请使用堆栈存储它们,然后在完成后重置它们
calculation:
mov ebx, 0
mov ecx, 7
subtract:
; init
mov eax, 0
mov edx, 0
; al = bit i of x
mov al, X
and al, 1h
; dl = bit i of y
mov dl, Y
and dl, 1h
; save data for later (technique 1 the stack)
push eax
push edx
; bit i of difference = x xor y xor b
xor al, dl
xor al, bl
or diff, al ; or instead of mov
; restore data (technique 1 the stack)
pop edx
pop eax
; b = ((not x) and y) or ((not x) and b) or (y and b)
not al
mov dh, al ; copy not al in dh (technique 2)
and al, dl ; ((not x) and y)
and dh, bl ; ((not x) and b)
and dl, bl ; (y and b)
or al, dh ; ((not x) and y) or ((not x) and b)
or al, dl ; ((not x) and y) or ((not x) and b) or (y and b)
mov bl, al
ror diff, 1
ror X, 1
ror Y, 1
loop subtract
ror diff, 1
如果寄存器用完,则查找哪个值可以是临时值,然后使用堆栈。push ax,使用ax来处理临时数据,然后将值返回到ax之类的东西是的,但是push和pop指令应该在后面的部分中,我不应该知道这个Lab中的那些指令,我只需要存储al的值,因为这是我想要修改的唯一寄存器。你也可以将值存储到临时变量中。但是您仍然有很多寄存器,例如
AH
,BH
,DH
,SI
,DI
,BP
(后三个是16位的,但可以很好地存储8位的值)。此外,您还可以进行如下计算:MOV AL,X;XOR-AL,Y;和AL,1
为您保存一个寄存器。