紧凑的实现逻辑和x86汇编 [英] Compact implementation of logical AND in x86 assembly

查看:165
本文介绍了紧凑的实现逻辑和x86汇编的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好 x86汇编怪才!

我有一个有趣的问题来测试你的汇编编程技能。结果
我这个问题的作者,所以我知道正确的答案。

你的任务是贯彻落实逻辑和x86汇编并满足以下5个条件:

条件#1 结果
布尔值均设有codeD以16位字的最标准的方式:

  0×0000 =假
0x0001..0xFFFF = TRUE

条件#2 结果
操作逻辑与16位值如下所示:

 逻辑与(值1,值2)== 0,如果(值== 0)或(值2 == 0)
  逻辑与(值1,值2)!= 0,如果(值!= 0)和(数值2!= 0)

您必须给出正确的结果对于任何16位值1 值2 。结果
请注意,您可以自由选择真的​​结果任何非零值,不仅 0×0001 0xFFFF的。< BR>
例如,它允许有逻辑与(0xDEAD,0xBEEF)== 42

条件#3 结果
你应该写16位code用于x86实模式。结果
输入参数在 AX BX ,结果是 AX

 ;对注册条目:
  ; AX =值
  ; BX =值2
  (您code到这里)
  ;注册退出:
  ; AX =逻辑与(值1,值2)
  ; BX,CX,DX,SI,DI,BP和32位扩展可能包含退出垃圾

显然,单指令和AX,BX 是不够的:当 AX = 1 BX = 2 ,结果必然是非零。

条件#4 结果
任何x86指令都可以使用(甚至SSE)。结果
您可以使用堆栈。结果
无论是外部code(呼叫ExternalProc INT XX ),也不是外部查找表是允许的。结果
所有的初始化数据应该是你的code的块里面。

例解(12字节code的)

 ; ****入口:AX,BX
   测试AX,AX
   setnz AL
   测试BX,BX
   setnz BL
   与AX,BX
   ; ****退出:AX

例解(6字节code的)

 ; ****入口:AX,BX
   NEG AX
   SBB AX,AX
   与AX,BX
   ; ****退出:AX

例溶液(5个字节code的)

 ; ****入口:AX,BX
   CMP AX,BX
   JB @Done
   XCHG AX,BX
@Done:
   ; ****退出:AX

条件#5 结果
您必须使用只有4个字节code的执行任务。

或许,你已经找到了在AX和CX输入参数很短的解决方案。结果
不错的尝试!结果
不幸的是,这种解决方案是不是一个正确的答案(因为使用CX作为输入)。

也许,有一个以上的正确答案存在。结果
不管怎么说,先正确答案(满足所有要求5)将获得500代表处点赏金。

我自己的4字节长的code是挺意外的,具有非常卓越的性能。

请不要蛮力。使用你的大脑。

要主持人:结果
这不是一个code高尔夫球场。一是正确答案将被接受。


解决方案

  MUL BX
 或AX,DX

在旧机器,这可能不是非常快相比,更长的答案。

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.

解决方案

 MUL BX 
 OR  AX, DX

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

这篇关于紧凑的实现逻辑和x86汇编的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆