Compact implementation of logical AND in x86 assembly -
hi, x86 assembler geeks!
i have interesting problem test assembler programming skills.
i'm author of problem, know correct answer.
your task implement logical and in x86 assembly , satisfy following 5 conditions:
condition #1
boolean values encoded in 16-bit words in standard way:
0x0000 = false 0x0001..0xffff = true
condition #2
operation "logical and" 16-bit values looks following:
logical_and(value1,value2) == 0 if (value1 == 0) or (value2 == 0) logical_and(value1,value2) != 0 if (value1 != 0) , (value2 != 0)
you must give correct result 16-bit value1
, value2
.
please note free choose nonzero value "true" result, not 0x0001
or 0xffff
.
example, allowed have logical_and(0xdead,0xbeef) == 42
condition #3
should write 16-bit code x86 real mode.
input parameters in ax
, bx
, result 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 , 32-bit extensions may contain garbage on exit
obviously, single instruction and ax,bx
not enough: when ax=1
, bx=2
, result must nonzero.
condition #4
x86 instructions allowed (even sse).
can use stack.
neither external code (call externalproc
, int xx
) nor external lookup tables permitted.
initialized data should inside chunk of code.
example solution (12 bytes of code)
; **** entry: ax, bx test ax,ax setnz al test bx,bx setnz bl , ax,bx ; **** exit: ax
example solution (6 bytes of code)
; **** entry: ax, bx neg ax sbb ax,ax , 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 task using 4 bytes of code.
probably, have found short solution input parameters in ax , cx.
nice try!
unfortunately, solution not correct answer (because of using cx input).
probably, there more 1 correct answer exist.
anyway, first correct answer (which satisfies 5 requirements) awarded 500 rep points bounty.
my own 4-byte-long code quite unexpected , has remarkable property.
please not brute-force. use brains.
to moderators:
not code-golf. first correct answer accepted.
mul bx or ax, dx
on older machines might not fast compared longer answers.
Comments
Post a Comment