0

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 = ((not x) and y) or ((not x) and b) or (y and 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ますか?

4

2 に答える 2

1

いいえ、あなたのコードはまだ間違っています。以下は、レジスタをスタックに格納する方法を示すコードです。(ただし、最適化にはほど遠いです)一般に、レジスタが不足している場合は、スタックを使用してください。レジスタがコード内の他の場所で使用されており、保持する必要がある場合は、スタックを使用してそれらを保存し、完了したらリセットします。

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
于 2013-06-26T00:04:00.763 に答える
0
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

はい、分かりました

于 2013-06-25T23:47:41.387 に答える