home *** CD-ROM | disk | FTP | other *** search
/ terabyteunlimited.com / 2014.06.terabyteunlimited.com.tar / terabyteunlimited.com / pushenve.txt < prev    next >
Text File  |  2010-09-22  |  3KB  |  102 lines

  1. Back in the good ole days there was a great magazine called PC Tech Journal 
  2. then later PC TECHNIQUES.  One of my favorite columns was 
  3. "Pushing the Envelope" by Michael Abrash.
  4.  
  5. I entered his first challenge (word count) and improved it by roughly 2
  6. times.  I was frustrated to learn that the winning entry (3x) was based
  7. on the first idea I had of using a 64K lookup table.  I abandoned it because
  8. I figured it would break the rules of being more than 200 lines.
  9. (This oddly had a profound effect on shaping my entire life with regards
  10. to rules)
  11.  
  12. Future challenges were issued:
  13.  
  14. "Short Sorts"
  15. Beat David Stafford's 25-byte C-callable small model routine that sorts an
  16. array of integers in ascending order.  So I did! (see code below)
  17.  
  18. "Hi/Lo"
  19. Beat David Stafford's 24-byte C-callable small model routine that finds the
  20. greatest or smallest unsigned int in 24 bytes or less".  So I did!
  21. (see code below)
  22.  
  23.  
  24. ;------------------------------------------------------------------------
  25. ; c small model
  26. ;
  27. ; Sorts an array of ints.  23 bytes
  28. ;
  29. ; void sort( int num, int a[] );
  30. ;
  31. ; David Flicek
  32. ;------------------------------------------------------------------------
  33.  
  34.          .model small
  35.          .code
  36.                    public _sort
  37.  
  38. top:     lodsw
  39.          cmp       ax,[si]                  ; are these two word in order?
  40.          jl        cont                     ; yes - continue
  41.          xchg      ax,[si]                  ; else swap words
  42.          mov       [si-2],ax
  43. cont:    loop      top                      ; next
  44.  
  45. _sort:   cld                                ; foward direction
  46.          pop       dx                       ; ip
  47.          pop       cx                       ; num
  48.          pop       si                       ; a[]
  49.          dec       cx                       ; num--
  50.          push      si                       ; put regs back on stack
  51.          push      cx
  52.          push      dx
  53.          jg        top                      ; cx>0 then jmp
  54.          ret
  55.  
  56. end
  57.  
  58. ;------------------------------------------------------------------------
  59. ; c small model (23 bytes)
  60. ; c tiny model  (22 bytes with ds overide)
  61. ;
  62. ; Find the greatest or smallest unsigned int.
  63. ;
  64. ; #define hi 0x72 (jb)
  65. ; #define lo 0x77 (ja)
  66. ; void findint(char hilo, int num, unsigned a[] );
  67. ;
  68. ; David Flicek
  69. ;------------------------------------------------------------------------
  70.  
  71.          .model small
  72.          .code
  73.                    public _findint
  74.  
  75. _findint:
  76.          pop       dx                       ; ret address
  77.          pop       ax                       ; opcode ja or jb
  78.          pop       cx                       ; num
  79.          pop       bx                       ; a[]
  80.          push       bx                       ; put back on stack
  81.          push       cx
  82.          push      ax
  83.          push       dx
  84.          mov       byte ptr [$+8],al        ; patch in opcode
  85. get:     mov       ax,[bx]                  ; get int
  86. check:   cmp       ax,[bx]                  ; check saved with next
  87.          jmp       short get                ; place holder for opcode
  88.          inc       bx                       ; point to next int
  89.          inc       bx
  90.          loop       check                    ; loop until done
  91.          ret
  92.  
  93. end
  94.  
  95.  
  96. jmpfar macro jseg, joff
  97.   db 0EAh
  98.   dw joff
  99.   dw jseg
  100. endm
  101.  
  102.