I'm trying to implement logical NOT and logical AND in assembly, I did logical NOT already using x < 1
but I can't think of how to implement AND, I can use binary and but that's broken for negative numbers (it assumes -1 is true
) and it doesn't make any sense when NOT obviously works because -1 < 1
would return 1
So I'm confused, how could I do it, any known implementation which I could use? I can't find it, been looking for a while
The standard solution is to implement a && b
as
if (a)
return (b);
else
return (0);
ie use a conditional jump. On sufficiently new x86, you can also use a cmov
instruction like this:
; assumes input in eax and ebx
mov ecx, eax ; ecx = A
test ebx, ebx ; B?
cmovnz ecx, ebx ; ecx = A ? B : A
This is an answer without jumps: Assume you want to compute logical AND of RCX
and RDX
and store it in RAX
and you may use RBX
(note that called functions usually must preserve RBX
!).
xorl %eax, %eax # RAX = 0
xorl %ebx, %ebx # RBX = 0
cmpq %rcx, %rbx # CF = (RCX != 0)
adcb %bl, %al # RAX = (RCX != 0)
cmpq %rdx, %rbx # CF = (RDX != 0)
adcb %bl, %al # RAX = (RCX != 0) + (RDX != 0)
shrb $1, %al # RAX = ((RCX != 0) + (RDX != 0)) >> 1
Now RAX == 1
if and only if RCX != 0
and RDX != 0
; otherwise, RAX == 0
一种方法是在每个输入上执行逻辑而不是两次,然后执行按位与( AND
指令)。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.