home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 December
/
simtel1292_SIMTEL_1292_Walnut_Creek.iso
/
msdos
/
ddjmag
/
ddj8811.arc
/
TONKIN.ASC
< prev
next >
Wrap
Text File
|
1988-10-21
|
3KB
|
94 lines
_INSERTING ELEMENTS INTO A BASIC INTEGER ARRAY_
by
Bruce Tonkin
[LISTING ONE}
The Test Program
defint a-z
rem $dynamic
dim a(10000)
for i = 1 to 10000: a(i) = i: next i
t! = timer
for i = 1 to 10000
b = 9999
call iinsert(seg a(1), b)
a(1) = -i
next i
t1! = timer - t!
for i = 1 to 10000: print a(i); : next i
print
t! = timer
for i = 1 to 10
for j = 10000 to 2 step -1: a(i) = a(i - 1): next j
a(1) = i
next i
t2! = timer - t!
print t1!; "seconds for 10,000 assembler insertions."
print t2!; "seconds for 10 QB4 insertions."
END
[LISTING TWO]
Iinsert Program Listing
.MODEL MEDIUM
.CODE
PUBLIC IINSERT
;Iinsert by Bruce W. Tonkin on 8-12-88 for QB 4.0 & MASM 5.0
;Iinsert will move, in descending order, elements of any
;1-dimensional integer array to allow a new element to be
;inserted. It is called with:
;call iinsert (seg arg1,count)
;where arg1 is the element where the new value is to be inserted,
;and count is the integer number of elements to move. e.g.:
;call iinsert(seg x%(5),y%)
;will move y% elements up one within the array, and you can then
;assign a new value to x%(5). The value of x%(5) will be in
;x%(6) and x%(6) in x%(7), and so on for a count of y% elements.
;If the last element were x%(1000), then y% should be 1000-5=995,
;since the 999th element will go to the 1000th and elements 5
;through 999 will move up to elements 6 through 1000. This
;routine works with either static or dynamic arrays, regardless
;of the array's location in memory-- the DS register that
;indicates the current BASIC data segment is irrelevant.
Iinsert PROC
push bp ;save old BP
mov bp,sp ;Set framepointer to old stack
push ds ;save data segment--altered by routine
push es ;and extra segment--altered by routine
push di ;and destination index--altered by routine
push si ;and source index--altered by routine
push ss ;and stack segment--just to be safe
mov bx,[bp+6] ;address of the count parameter
mov cx,[bx] ;count is now in CX
shl cx,1 ;multiply that by two for the moment
les di,[bp+8] ;segmented address of destination parameter
lds si,[bp+8] ;segmented address of source parameter, too
add di,cx ;es:di points to end of destination
add si,cx ;and ds:si points to end of source
inc di ;move up one element for destination for an
inc di ;integer insert--that's two bytes
shr cx,1 ;restore original cx value
;default destination is es:di, default source is ds:si for movsw
std ;set direction flag for decremented copy
rep movsw ;move word at a time, since elements are words
movsw ;and move the original element up one, too
pop ss ;restore saved registers
pop si
pop di
pop es
pop ds
cld ;clear direction flag--a well-mannered routine
pop bp ;restore old base pointer
ret 0ah ;clear 10 bytes of parameters on return
Iinsert ENDP
END