本文介绍了在汇编中一点一点地减去两个整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试逐位减去2个整数,并为此算法提供了

I'm trying to subtract 2 integers bit-by-bit and was given this algorithm

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)或((not x)and b)或(y and b).我应该如何在代码中实现算法的最后一行

I have implemented up to this line b = ((not x) and y) or ((not x) and b) or (y and b). How should I implement that last line of the algorithm in my code

这是我到目前为止所拥有的:

This is what I have so far:

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 的值?

The algorithm start from the calculation loop label. I needed to save the value stored in al register, in order to implement the last line of the algorithm, but since dl and bl is in used which general purpose register should I use to store value of al?

推荐答案

您的代码仍然没有错.以下是一段代码,显示了如何在堆栈中存储寄存器.(但是,距离优化还很远)通常,如果您的寄存器不足,请使用堆栈.如果寄存器在代码的其他位置使用过并且需要保留,请使用堆栈存储它们,然后在完成后将其重置.

No your code is still wrong. Below is a piece of code that shows how to store registers in the stack. (It is however far from optimized)In general if you’re out of registers, use the stack.If registers are used in other places in your code and need to persist, use the stack to store them then reset them when you’re done.

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

这篇关于在汇编中一点一点地减去两个整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 16:16
查看更多