home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1988 / 11 / tonkin.asc < prev    next >
Text File  |  1988-10-21  |  3KB  |  94 lines

  1. _INSERTING ELEMENTS INTO A BASIC INTEGER ARRAY_
  2. by
  3. Bruce Tonkin
  4.  
  5.  
  6. [LISTING ONE}
  7.                         The Test Program 
  8.  
  9. defint a-z 
  10. rem $dynamic 
  11. dim a(10000) 
  12. for i = 1 to 10000: a(i) = i: next i 
  13. t! = timer 
  14. for i = 1 to 10000 
  15.    b = 9999 
  16.    call iinsert(seg a(1), b) 
  17.    a(1) = -i 
  18. next i 
  19. t1! = timer - t! 
  20. for i = 1 to 10000: print a(i); : next i 
  21. print 
  22. t! = timer 
  23. for i = 1 to 10 
  24.    for j = 10000 to 2 step -1: a(i) = a(i - 1): next j 
  25.    a(1) = i 
  26. next i 
  27. t2! = timer - t! 
  28. print t1!; "seconds for 10,000 assembler insertions." 
  29. print t2!; "seconds for 10 QB4 insertions." 
  30. END 
  31.  
  32.  
  33.  
  34.  
  35. [LISTING TWO]
  36.  
  37.                      Iinsert Program Listing 
  38.  
  39.  .MODEL MEDIUM 
  40.  .CODE 
  41.  PUBLIC IINSERT 
  42. ;Iinsert by Bruce W. Tonkin on 8-12-88 for QB 4.0 & MASM 5.0 
  43. ;Iinsert will move, in descending order, elements of any 
  44. ;1-dimensional integer array to allow a new element to be 
  45. ;inserted.  It is called with: 
  46. ;call iinsert (seg arg1,count) 
  47. ;where arg1 is the element where the new value is to be inserted, 
  48. ;and count is the integer number of elements to move.  e.g.: 
  49. ;call iinsert(seg x%(5),y%) 
  50. ;will move y% elements up one within the array, and you can then 
  51. ;assign a new value to x%(5).  The value of x%(5) will be in 
  52. ;x%(6) and x%(6) in x%(7), and so on for a count of y% elements. 
  53. ;If the last element were x%(1000), then y% should be 1000-5=995, 
  54. ;since the 999th element will go to the 1000th and elements 5 
  55. ;through 999 will move up to elements 6 through 1000.  This 
  56. ;routine works with either static or dynamic arrays, regardless 
  57. ;of the array's location in memory-- the DS register that 
  58. ;indicates the current BASIC data segment is irrelevant. 
  59.  
  60. Iinsert PROC 
  61.     push bp        ;save old BP 
  62.     mov  bp,sp     ;Set framepointer to old stack 
  63.     push ds        ;save data segment--altered by routine 
  64.     push es        ;and extra segment--altered by routine 
  65.     push di        ;and destination index--altered by routine 
  66.     push si        ;and source index--altered by routine 
  67.     push ss        ;and stack segment--just to be safe 
  68.     mov  bx,[bp+6] ;address of the count parameter 
  69.     mov  cx,[bx]   ;count is now in CX 
  70.     shl  cx,1      ;multiply that by two for the moment 
  71.     les  di,[bp+8] ;segmented address of destination parameter 
  72.     lds  si,[bp+8] ;segmented address of source parameter, too 
  73.     add  di,cx     ;es:di points to end of destination 
  74.     add  si,cx     ;and ds:si points to end of source 
  75.     inc  di        ;move up one element for destination for an 
  76.     inc  di        ;integer insert--that's two bytes 
  77.     shr  cx,1      ;restore original cx value 
  78. ;default destination is es:di, default source is ds:si for movsw 
  79.     std            ;set direction flag for decremented copy 
  80.     rep  movsw     ;move word at a time, since elements are words 
  81.     movsw          ;and move the original element up one, too 
  82.     pop  ss        ;restore saved registers 
  83.     pop  si 
  84.     pop  di 
  85.     pop  es 
  86.     pop  ds 
  87.     cld            ;clear direction flag--a well-mannered routine 
  88.     pop  bp        ;restore old base pointer 
  89.     ret  0ah       ;clear 10 bytes of parameters on return 
  90. Iinsert ENDP 
  91.     END 
  92.  
  93.  
  94.