Hi, x86 assembler geeks!
I have an interesting problem to test your assembler programming skills.
I'm the author of this problem, so I know the correct answer.
Your task is to implement logical AND in x86 assembly and satisfy the following 5 conditions:
Condition #1
Boolean values are encoded in 16-bit words in the most standard way:
0x0000 = False
0x0001..0xFFFF = True
Condition #2
Operation "logical AND" for 16-bit values looks like the following:
logical_AND(value1,value2) == 0 if (value1 == 0) or (value2 == 0)
logical_AND(value1,value2) != 0 if (value1 != 0) and (value2 != 0)
You must give correct result for any 16-bit value1
and value2
.
Please note that you are free to choose any nonzero value for "True" result, not only 0x0001
or 0xFFFF
.
For example, it is allowed to have logical_AND(0xDEAD,0xBEEF) == 42
Condition #3
You should write 16-bit code for x86 real mode.
Input parameters are in AX
and BX
, result is in AX
:
; Registers on entry:
; AX = value1
; BX = value2
(your code goes here)
; Registers on exit:
; AX = logical_AND(value1,value2)
; BX,CX,DX,SI,DI,BP and their 32-bit extensions may contain garbage on exit
Obviously, single instruction and AX,BX
is not enough: when AX=1
and BX=2
, result must be nonzero.
Condition #4
Any x86 instructions are allowed (even SSE).
You can use the stack.
Neither external code (call ExternalProc
, int XX
) nor external lookup tables are permitted.
All initialized data should be inside your chunk of code.
Example solution (12 bytes of code)
; **** Entry: AX, BX
test AX,AX
setnz AL
test BX,BX
setnz BL
and AX,BX
; **** Exit: AX
Example solution (6 bytes of code)
; **** Entry: AX, BX
neg AX
sbb AX,AX
and AX,BX
; **** Exit: AX
Example solution (5 bytes of code)
; **** Entry: AX, BX
cmp AX,BX
jb @Done
xchg AX,BX
@Done:
; **** Exit: AX
Condition #5
You must perform the task using only 4 bytes of code.
Probably, you have already found very short solution with input parameters in AX and CX.
Nice try!
Unfortunately, this solution is not a correct answer (because of using CX as input).
Probably, there are more than one correct answer exist.
Anyway, first correct answer (which satisfies all 5 requirements) will be awarded 500 rep points bounty.
My own 4-byte-long code is quite unexpected and has very remarkable property.
Please do not brute-force. Use your brains.
To moderators:
This is not a code-golf. First correct answer will be accepted.
On older machines this might not be very fast compared to longer answers.