News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Creating a circular queue

Started by nickc86, May 06, 2017, 11:19:51 AM

Previous topic - Next topic

jj2007

MasmBasic uses internally a circular buffer for the more complex Print, Let and Cat$ tasks (examples). The MbBuffer* functions are not documented, but here is a working example:
include \masm32\MasmBasic\MasmBasic.inc
  Init
  invoke MbBufferGet
  xchg eax, edi ; swap with a permanent reg
  invoke lstrcpy, edi, chr$("This is the first test")
  push 1 ; flag increase slot
  add len(edi), edi
  push eax ; end address
  call MbBufferFix ; sets eax as retval and registers slot

  invoke MbBufferGet
  xchg eax, edi
  invoke MbCopy, edi, chr$("The second test"), -1 ; copy and add a zero delimiter
  push 1 ; flag increase slot
  push eax ; end address
  call MbBufferFix ; sets eax as retval and registers slot

  print "now we print the buffers in any order:", 13, 10

  push 0 ; current slot
  call MbGetSlotPointer
  deb 4, "slot 0: string, len incl zero delimiter", $[edx], [edx+4] ; [edx] is the address of slot 0, [edx+4] the len of the string
  print [edx], 13, 10
  push -1 ; previous slot
  call MbGetSlotPointer
  print [edx], 13, 10
  push 1 ; back to current slot
  call MbGetSlotPointer
  print [edx], 13, 10
  push -1 ; previous
  call MbGetSlotPointer
  print [edx], 13, 10
EndOfCode


MbBufferGet does what the name says, same for MbBufferFix, which expects two parameters: 0=overwrite current slot, 1=use new slot, and the end position of the copied string.

MbGetSlotPointer retrieves a specific slot, i.e. pointer in [edx] and len in [edx+4]. The argument is 0 for last slot, -1 for 2nd last, -2 for 3rd last etc.

Each single string can be 160k long, total size is 640k. 50 slots are available, which means that one could concatenate up to 50 strings in a single "print" line. Simple example:
Print Str$("ecx is %i", ecx), Str$(", while ebx=%i", ebx), Str$(" and esi=%i. The hex of ecx is ", esi), Hex$(ecx), CrLf$