Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The Ultimate Method for Writing Memory to a File

The Ultimate Method for Writing Memory to a File

by  Alt255  Posted    (Edited  )
With all of the questions about accessing memory with QB I decided to show everyone the ultimate method for saving large blocks of RAM to a file.

A conventional loop setting memory segments and then PEEKing at each byte in each 16 byte segment could require many many minutes to read and write the RAM that this program accesses in a fraction of a second. It appears to be several times faster (and more flexible) than the BSAVE command.

How fast? When this program is timed, using Interrupt 21 function 2c, running on a K62/350, it comes in at .05 seconds. It's hard to get a more accurate time with DOS since the system timer resolution is .05 seconds. In any case, this little routine gets and saves one megabyte of memory in very short order.

You will need QB45 or better and must load the QB library (or equivalent) with the command line "QB /L QB.QLB".
[tt]
' $DYNAMIC
DEFINT A-Z

'Declare the registers. Don't use ' $INCLUDE: QB.BI to load
'them. This one requires that DS is a long integer.
'Don't try to modify the other registers or use this
'declaration in a different program.

TYPE RegTypeX
AX AS INTEGER
BX AS INTEGER
CX AS INTEGER
DX AS INTEGER
BP AS INTEGER
SI AS INTEGER
DI AS INTEGER
FLAGS AS INTEGER
DS AS LONG
ES AS INTEGER
END TYPE
DIM InRegs AS RegTypeX, OutRegs AS RegTypeX

'MemFile$ will contain the name of the file we want to write.
MemFile$ = "1STMEG" + CHR$(0)
InRegs.AX = &H3C00
InRegs.CX = &H20
InRegs.DS = VARSEG(MemFile$)
InRegs.DX = SADD(MemFile$)
CALL InterruptX(&H21, InRegs, OutRegs)
'Create an empty file
InRegs.AX = &H3D01
'01 = open the file for write access
InRegs.DS = VARSEG(MemFile$)
InRegs.DX = SADD(MemFile$)
CALL InterruptX(&H21, InRegs, OutRegs)
'Open the file
hFile = OutRegs.AX 'hFile is the handle for the file

FOR Segment& = 0 TO 65535 STEP 2048
'Skip segs already read
InRegs.AX = &H4000
InRegs.BX = hFile

InRegs.CX = 32767 'Read this many bytes
InRegs.DS = Segment& 'Begin with this segment
InRegs.DX = 0 'Start at offset 0
[/b]'Write the bytes to the file
CALL InterruptX(&H21, InRegs, OutRegs
NEXT
InRegs.AX = &H3E00
InRegs.BX = hFile
CALL InterruptX(&H21, InRegs, OutRegs)
'Close the file [/b]
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top