How to display a hex dump of memory contents
by Jon Ripley, May 2011
It is useful sometimes to be able to list the contents of a block of memory to examine it. This may be to help debug a problem, examine binary data in memory or one of many other uses.
The easiest way to achieve that is probably to use the supplied Memory Monitor add-in utility, which optionally displays the contents of a block of memory in hexadecimal and ANSI (commonly known as a hex dump).
However if you want to achieve a similar effect in your own program you can use PROCm_Dump below; it accepts four parameters:
PROCm_dump(block%, size%, items%, verbose%)
Here block% should point to the memory to list, size% is the size in bytes, items% is the number of bytes to display on each line – typically 16 – and verbose controls whether a header is printed.
In the example below we reserve 16 bytes of memory, write a message into the memory and call PROCm_dump to display a hex dump of the memory:
DIM a 15 $$a="Hello world!" PROCm_dump(a, 16, 8, TRUE)
The ouput of the code is as follows:
Address : 00 01 02 03 04 05 06 07 :
04421AA1 : 48 65 6C 6C 6F 20 77 6F : Hello wo 04421AA9 : 72 6C 64 21 00 00 00 00 : rld!....
Here is the code for PROCm_dump:
DEF PROCm_dump(P%,L%,S%,F%) LOCAL I%,J%,K% FOR I%=0 TO L%-1 STEP S% IF I%MOD(S%*16)=0 IF F% THEN PRINT '"Address :"; FOR J%=0 TO S%-1 PRINT " ";FNh0(J%,2); NEXT J% PRINT " :"' ENDIF PRINT FNh0(P%+I%,8)" :"; FOR J%=0 TO S%-1 K%=?(P%+I%+J%) IF P%+I%+J%<P%+L% THEN PRINT " ";FNh0(K%,2); ELSE PRINT " "; ENDIF NEXT J% PRINT " : "; FOR J%=0 TO S%-1 K%=?(P%+I%+J%) IF K%<32 OR K%>126 K%=ASC"." IF P%+I%+J%>=P%+L% K%=ASC" " VDU K% NEXT J% PRINT NEXT I% ENDPROC DEF FNh0(N%,L%) =RIGHT$(STRING$(L%,"0")+STR$~N%,L%)