-2

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.

Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64
  • 5
    How is this not code-golf? – Ross Ridge Jun 10 '15 at 15:57
  • This is typical Q&A question (give an answer on the problem). Code golf implies lengthy competition. – Egor Skriptunoff Jun 10 '15 at 15:59
  • 2
    I don't see how code-golf implies "lengthy", and you're clearly making this a competition. – Ross Ridge Jun 10 '15 at 16:02
  • 5
    I'm voting to close this question as off-topic because this an off-topic code-golf competition. – Ross Ridge Jun 10 '15 at 16:03
  • 1
    In code-golf all answers are correct, and the competition does not finish with first correct answer posted. For Q&A questions, first correct answer is accepted. Visit code-golf.SE and you will see the difference. – Egor Skriptunoff Jun 10 '15 at 16:09
  • 1
    Even if that were true it wouldn't make your contest on-topic here. However the code-golf site sets a different standard: http://codegolf.stackexchange.com/help/on-topic – Ross Ridge Jun 10 '15 at 16:18
  • 2
    @EgorSkriptunoff: You should have posed this question as, "I have to AND two values and I am very space constrained. I tried these sequences but I'm looking for something shorter". Then the naysayers wouldn't claim it was "code golf", because it would look like every other SO question. – Ira Baxter Jun 10 '15 at 17:11
  • 1
    I don't like the first paragraph, but when reading past it, the second paragraph does not make it any better. SO is not for "challenges". – Jongware Jun 10 '15 at 21:50
  • @IraBaxter No, the question is fundamentally a puzzle. The constraints are artificial and ignore all practical considerations so that there can only be one answer. If has was actually trying to reduce the code size of an actual program the context would be different. Once practical considerations are added, like the fact these values have to come from someplace and go somewhere, you get a different question and a different answer. – Ross Ridge Jun 11 '15 at 01:07
  • @RossRidge: I think we disagree. Much of my career has been spent (perhaps pointlessly from your perspective) in tuning low-level routines (schedulers, interrupt handlers, mutexes, queues, ...) where a few cycles saving in one execution added up to serious savings or realtime response. On older machines size mattered; not so much anymore, but the basic issue is "How do I accomplish computation X in very few instructions". His particular constraints may be artificial, but they look a lot like things I used to do a lot (and still do, but somewhat less). Much of SO Q/A is also pretty trivial. – Ira Baxter Jun 11 '15 at 02:26
  • 1
    @IraBaxter As I said, a real question about reducing code size in a actual program would be different. It would be a useful question with a useful answer, not a pointless question with a pointless answer. I've done a lot low level optimization myself, so I don't consider saving a few bytes necessarily pointless or trivial. But I've also seen a lot foolish code-size optimizations like this one. They ended up increasing the overall code size because the rest of the code had to be adjusted to fit the limited constraints. If this were a real question, a real answer could address that problem. – Ross Ridge Jun 11 '15 at 02:48
  • @EgorSkriptunoff: Thank you for the bounty. – Ira Baxter Jun 12 '15 at 11:49

1 Answers1

3
 MUL BX 
 OR  AX, DX

On older machines this might not be very fast compared to longer answers.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • 1
    @JoseManuelAbarcaRodríguez is right, MUL doesn't take two operands. Fortunately, it does take one and the other is AX. I adjusted the MUL instruction. – Ira Baxter Jun 10 '15 at 16:23
  • 1
    This is the correct answer –  Jun 10 '15 at 16:23
  • It's still not a good answer. – Ross Ridge Jun 10 '15 at 16:33
  • Why so fast? ))) Yes, after correcting to `MUL BX` the answer has become correct. And what about second instruction? Can it be substituted with something else? ;-) – Egor Skriptunoff Jun 10 '15 at 16:34
  • 1
    @Egor: This meets all your requirements. It seems hard to avoid a 2nd instruction :-{ Given that you have to have one, it is hard to find a shorter one that does the job, or one that is faster than the OR. What's not to like? – Ira Baxter Jun 10 '15 at 16:37
  • 1
    @JoseManuelAbarcaRodríguez: if AX=2^15 and BX=2 ==> product is 2^16. This leaves AX as zero. Without the OR, you lose the nonzeroness of the answer. – Ira Baxter Jun 10 '15 at 16:41
  • @IraBaxter - I'm not about optimization. Can you find another instructions which give another correct answers (with the same first instruction)? – Egor Skriptunoff Jun 10 '15 at 16:42
  • I can't start a bounty right now. 2 days must pass. – Egor Skriptunoff Jun 10 '15 at 16:45
  • @EgorSkriptunoff: You suggested an alternative instruction for the second one. You need something that combines the nonzeroness of AX and DX and OR does that pretty well. I was unable to convince myself that any of the other arithmetic instructions (ADD, SUB, XOR) would properly preserve that in all cases. I'm curious as to whether you actually had an alternative, as you seem to hint. [The OR is a pretty good choice: it works, it clearly does the the right thing, and it fits.] – Ira Baxter Jun 12 '15 at 11:52
  • @IraBaxter - `ADD` is not a correct answer because of `0xFFFE*0xFFFE` leads to zero sum of product's words. What about the others two? This is rather amazing fact, it can be simply proved. ;-) – Egor Skriptunoff Jun 12 '15 at 13:13