??? 05/28/07 07:21 Read: times |
#139913 - commented Responding to: ???'s previous message |
;the following routine returns number of ones in accumulator ;the purpose is purely educational (to point out existence of parity in PSW) ;- this is NOT the way how such algorithm has to be implemented ; ;input and output in acc ; Quiz: mov b,#16 ;first we take apart the input into nibbles ; the algorithm counts number of ones in each ; nibble then adds them up at the end mul ab ;upper nibble in b.0-b.3, lower in a.4-a.7 rlc a ;processing one nibble - store one bit in C ; and process the remaining 3 bits jb p,Quiz1 ;parity is the lowest bit of count of ones ; in the remaining 3 bits ;parity=0 means, we have 0 or 2 ones jz Quiz2 ;0 ones - there is already 0 in acc, which is the result ; for the 3 bits mov a,#2 ;else the result is 2 sjmp Quiz2 Quiz1: ;parity=1 means, we have 1 or 3 ones addc a,#22h ;if 3 ones, it is 0E0h, and that is the only ; value which overflows (and leaves upper nibble ; all zero) when 02xh added ; we add also the carry (the 4th bit of nibble) ; here and also a magic number 2, which together ; with the C (added in addc a,#0 below) yields ; the required result of 3 (or 4) ; this all is certainly dirty enough... :-) jc Quiz2 anl a,#03h ; if 1 (or 2) ones, clear the upper nibble dec a ; and correct the result (and carry is here 0) Quiz2: addc a,#0 ;add up the 4th bit stored in C so far xch a,b ;and store the result for 1st nibble swap a ;simultaneously get 2nd nibble and prepare it to high bits rlc a ;repeat the same process as above jb p,Quiz3 jz Quiz4 mov a,#2 sjmp Quiz4 Quiz3: addc a,#22h jc Quiz4 anl a,#03h dec a Quiz4: addc a,b ;finally, add up both counts (plus the 4th bit) ret Nasty enough, huh? :-) JW |
Topic | Author | Date |
weekend-end quiz | 01/01/70 00:00 | |
How about this? | 01/01/70 00:00 | |
@#$%^& | 01/01/70 00:00 | |
Two equivalent functions | 01/01/70 00:00 | |
OK, I admit, 2:1 | 01/01/70 00:00 | |
Nice variation! | 01/01/70 00:00 | |
shortest? | 01/01/70 00:00 | |
Eleven bytes. No tricks. | 01/01/70 00:00 | |
The wayward path followed... | 01/01/70 00:00 | |
Deterministic Way | 01/01/70 00:00 | |
Yet another tweak | 01/01/70 00:00 | |
OMG.... | 01/01/70 00:00 | |
You are forgiven | 01/01/70 00:00 | |
commented | 01/01/70 00:00 | |
Nastiness density increased | 01/01/70 00:00 | |
That looks like my assembly code. | 01/01/70 00:00 |