Q:

subtract two number bit by bit

I'm having problem trying subtract 2 number bit by bit in assembly(MASM)
this is my algorithm code
_Calculation:

    mov ecx , 7
    mov ebx, 0

_Subtract:
    ;clear out register for easy to check 
    xor eax, eax
    xor edx, edx

    ; al = bit i of first number
    mov al, first_num
    and al, 1h 

    ; dl = bit i of second number
    mov dl, second_num
    and dl, 1h

    ; save data for later use
    mov ah, al
    mov bh, dl

    ; bit i of difference = x xor y xor b
    xor al, dl
    xor al, bl
    or difference, al 

    ; b = ((not x) and y) or ((not x) and b) or (y and b)
    mov al, ah
    mov dl, bh
    not al
    mov dh, al ; copy not al in dh 
    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 difference, 1
    ror first_num, 1
    ror second_num, 1
    loop _Subtract
    ror difference, 1

when i did test run most of the input work fine but for certain large number the result came out wrong 
for example, when i take 255 -1 the difference come out to be 126 instead of 254 
what is wrong with the code that I wrote, can anyone give a suggestion? 
also subtract any smaller number to a greater number does not give negative result but the number 127 I don't know what wrong with the code.  
thank you in advance

A:

You don't need to do like that.  According to the truth table:
A │ B │ A-B │ Borrow
──┼───┼─────┼───────
0 │ 0 │  0  │ 0
0 │ 1 │  1  │ 1
1 │ 0 │  1  │ 0
1 │ 1 │  0  │ 0

The result is just an exclusive OR of A and B
So you can do xor ax, bx, xor eax, ebx... depending on the size of the operands

