asmBB Forum : FreshLib reference
<img src="Wh ÎÿsèÿD://asmbb.org/images/title.svg" alt="Title img">
<h1>AsmBB is fast and lightweight web forum engine</h1>
tag:asmbb.org,2022-11-19:Thread192023-01-23T13:29:51Zadmin on FreshLib referencetag:asmbb.org,2022-11-19:Post332023-01-23T13:29:51Z
<h1>15. FreshLib directory "data/"
</h1>
<p>This directory contains several libraries that handles different data structures. The libraries are mostly OS independent. Actually the only OS dependent part is one small routine in Win32 section, that converts strings from UTF-8 to UTF-16 because Windows can't handle UTF-8 strings directly.
</p>
<h2>15.1. "arrays.asm" library
</h2>
<p>This library handles dynamic arrays, containing elements of arbitrary size. All elements of the array have the same size.
</p>
<p>TArray structure have following definition:
</p><pre><code class=""> struct TArray
.count dd ?
.capacity dd ?
.itemsize dd ?
.lparam dd ?
label .array dword
ends
</code></pre>
<p>The above structure represents the header of the array. The actual array will have arbitrary size, depending on the element count and size.
</p>
<p>The first element of the array begins on offset <strong>TArray.array</strong> from the begining of the memory block.
</p>
<p>The field <strong>TArray.count</strong> contains the current element count of the array.
</p>
<p>The field <strong>TArray.capacity</strong> contains the current capacity of the array. It is because the library usually allocates more memory than is needed for the array element count. This approach reduces memory allocations and reallocations and thus increases the speed of inserting and deleting elements in the array. How many memory will be allocated depends on the user setting of the variable ResizeIt (defined in <strong>memory.asm</strong>). This variable contains a pointer to the procedure that simply increases the value of ECX to the next suitable value.
</p>
<p>The field <strong>TArray.itemsize</strong> contains the size in bytes of one array element. Changing of this value is not recommended.
</p>
<p>The field <strong>TArray.lparam</strong> is for user defined parameter, associated with the array.
</p>
<h3>proc CreateArray, .itemSize
</h3>
<p>This procedure creates new array with item size <code>[.ItemSize]</code>
</p>
<p>The procedure returns CF=0 if the array is properly created and pointer to the array is placed in EAX.
</p>
<p>In case the memory cannot be allocated, the procedure returns CF=1. To free the allocated array, use <strong>FreeMem</strong> procedure.
</p>
<h3>proc AddArrayItems, .ptrArray, .count
</h3>
<p>This procedure adds new array items at the end of the array pointed by <code>[.ptrArray]</code>
</p>
<p>The procedure returns two values:
</p>
<ul><p><strong>EAX</strong> contains pointer to the first of the new appended elements.
</p>
<p><strong>EDX</strong> contains pointer to the array (in the process of appending of the new element, it is possible the whole array to be moved to the new address in memory, so the programmer should store the value of EDX for the future reference to the array.
</p></ul>
<p>In case, the new memory can not be allocated, the procedure returns CF=1 and EDX contains the proper pointer to the original array.
</p>
<h3>proc InsertArrayItems, .ptrArray, .iElement, .count
</h3>
<p>This procedure inserts <code>[.count]</code> new elements at the <code>[.iElement]</code> position of the array pointed by <code>[.ptrArray]</code>
</p>
<p>If <code>[.iElement]</code> is larger or equal to <code>[TArray.count]</code> the elements are appended at the end of the array. (Just like <strong>AddArrayItems</strong>) Otherwise, all elements are moved to make room for the new elements.
</p>
<p>The procedure returns exactly the same results as <strong>AddArrayItems</strong> procedure — EDX points to the array and EAX points to the first of the new inserted elements.
</p>
<p>CF is an error flag.
</p>
<h3>proc GetArrayItem, .array, .item
</h3>
<p>CF=0; Returns in EAX pointer to the array item with index <code>[.item]</code>.
</p>
<p>CF=1; The requested item does not exists ( <code>[.item]</code> >= <code>[.array.count]</code> ). In this case, EAX contains pointer to the end of the array the byte next after the last array element.
</p>
<h3>proc DeleteArrayItems, .ptrArray, .iElement, .count
</h3>
<p>This procedure deletes <code>[.count]</code> items with begin index <code>[.iElement]</code> the <code>[.ptrArray]</code> dynamic array. If the capacity of the array is bigger than the recommended for the new count, then the array is resized. The recommended size is calculated using ResizeIt procedure from memory library.
</p>
<p>Returns EDX - pointer to the TArray. In the most cases this pointer will not be changed, but this also depends on the current OS memory allocation API, so it is safer to store the pointer for future use, instead of one passed to the procedure.
</p>
<p>This procedure can not fail, because deleting element is always possible. In some rare cases it can fail to reallocate smaller memory block, but this is not a problem for the array consistency.
</p>
<h3>proc VacuumArray, .ptrArray
</h3>
<p>This procedure removes the reserved memory from the array in order to make it as small as possible. Note, that the first insert/append operation after the vacuum operation will be very slow. The memory economized this way depends on reallocation strategy and can be from 25 to 100% in some cases.
</p>
<h3>proc ListIndexOf, .ptrList, .Item
</h3>
<p>The list is a special case of array with item size equal to 4 bytes (dword). This procedure searches the list <code>[.ptrList]</code> for item equal to <code>[.Item]</code> and returns the index of the element in EAX. In case of error CF=1.
</p>
<h3>proc ListFree, .ptrList, .FreeProc
</h3>
<p>Frees all elements of the list <code>[.ptrList]</code>, calling <code>[.FreeProc]</code> for every element of the list.
</p>
<p>FreeProc callback procedure have one argument of type dword that is the value of the current list element. The definition of the callback procedure is similar to following:
</p><pre><code class=""> proc FreeMyListItem, .ptrItem
begin
;do something with the item being freed
return
endp
</code></pre>
<h2>15.2. "strlib.asm" library
</h2>
<p>Using strings in assembler was often problematic for programmers - static strings can't be resized, so they had to reserve as many bytes as they thought user could need, and it still could be not enough. For that reason we created Dynamic String Library - a library that operates on dynamic strings, that are automatically resized when needed. Also, StrLib contains many functions that perform different string operations — comparison, inserting one string into another, etc. In StrLib, the strings are represented by handles, not by pointers. This way, the string can be freely resized and moved in memory. StrLib can distinguish the handle values from pointer and will process properly both of them. When the processing is possible, the procedures will operate on static strings in memory.
</p>
<p>The strings in the StrLib are stored in specific format. The format is defined following way:
</p>
<pre><code class=""> struc string {
.capacity dd ?
.len dd ?
label .data byte
}
virtual at -(sizeof.string)
string string
sizeof.string = $-string
end virtual
</code></pre>
<p>The structure string have variable length and is dynamically allocated.
</p>
<p>The field <strong>string.capacity</strong> on offset <strong><del>8* contains the allocated memory size in bytes.
</del></strong></p>
<p>The field <strong>string.len</strong> on offset <strong><del>4* contains the current length of the string in bytes.
</del></strong></p>
<p>The string content begins on offset 0. The content of the string always ends with at least one zero byte, so the string format is compatible with ASCIIZ format used in the most OS API.
</p>
<p>All procedures in StrLib will set <code>[string.len]</code> to the proper value and will use it in the string processing.
</p>
<p>If the programmer manipulates the string data directly, he should set <code>[string.len]</code> himself, or call <strong>StrFixLen</strong> in order to let StrLib to scan the string and to fix the length field.
</p>
<p>StrLib procedure reference:
</p>
<pre><code class=""> proc StrNew
</code></pre>
<p>Creates new dynamic string and returns its handle.
</p>
<pre><code class=""> proc StrDel, .hstring
</code></pre>
<p>Frees the memory occupied by given string. If the .hstring is pointer to memory, StrDel will try to find it in the table of created strings and if found, will free it as well. If the string passed by pointer is not found in the list - StrDel exits without error. Note, that passing pointer can degrade the performance of the procedure.
</p>
<pre><code class=""> proc StrPtr, .hstring
</code></pre>
<p>Returns in EAX the pointer of the string with handle <code>[.hstring]</code> If .hstring is pointer EAX will be equal to <code>[.hstring]</code> If the handle is invalid, StrPtr returns CF=1 and EAX=0.
</p>
<pre><code class=""> proc StrLen, .hstring
</code></pre>
<p>Returns the length of the string in bytes, excluding the terminating zero. If <code>[.hstring]</code> is handle, the procedure returns directly the stored length of the string. If <code>[.hstring]</code> is pointer, the functions scans the string and computes the length. Thus, passing handle is much faster, especially for long strings.
</p>
<pre><code class=""> proc StrDup, .hstring
</code></pre>
<p>Creates new string and copy the content of <code>[.hstring]</code> to it. Returns in EAX — handle of the new string. <code>[.hstring]</code> can be memory pointer or string handle.
</p>
<pre><code class=""> proc StrFixLen, .hstring
</code></pre>
<p>StrFixLen scans the string in order to compute its actual length and then writes this length in the field <code>[string.len]</code>. StrFixLen should be call only with handle of the string as argument. It will process pointer as well, but will assume there is a field <code>[string.len]</code> at offset <code>-4</code>. The user should call StrFixLen only if the <code>[string.len]</code> field does not contains the proper value, because of some reason - for example the user made some string processing that changes the length of the string or the string data is returned by some OS function that does not care about string length.
</p>
<pre><code class=""> proc StrSetCapacity, .hString, .capacity
</code></pre>
<p>This function ensures that the allocated for the string memory is enough to hold at least <code>[.capacity]</code> bytes. If needed the string memory is reallocated. Returns pointer to the string data in EAX. If the reallocation is impossible, returns CF=1;
</p>
<pre><code class=""> proc StrCopy, .dest, .source
</code></pre>
<p>Copies the content of <code>[.source]</code> string to <code>[.dest]</code> string. <code>[.dest]</code> must be a handle. <code>[.source]</code> can be handle or pointer. Returns nothing.
</p>
<pre><code class=""> proc StrCompCase, .str1, .str2
proc StrCompNoCase, .str1, .str2
</code></pre>
<p>Compares two strings - case <em>sensitive</em> (StrCompCase) or string <em>not sensitive</em> (StrCompNoCase).
</p>
<p>Returns CF = 1 if the strings are equal.
</p>
<p>Returns CF = 0 if the strings are different.
</p>
<p>The speed of this procedure varies depending on passed strings and its content. The worst case is when the strings are passed as pointers and have equal lengths. The best case is when the strings are passed as handles and have different lengths.
</p>
<pre><code class=""> proc StrCat, .dest, .source
</code></pre>
<p>Concatenates the string <code>[.source]</code> to the end of the string <code>[.dest]</code>.
</p>
<pre><code class=""> proc StrCharPos, .hString, .char
</code></pre>
<p>StrCharPos returns a pointer to the first occurence of a given char in specified string.
</p>
<pre><code class="">
proc StrPos, .hstring, .hpattern
</code></pre>
<p>StrPos returns in EAX a pointer to the first occurence of a <code>[.hpattern]</code> string in <code>[.hstring]</code> or NULL if the pattern was not found.
</p>
<pre><code class=""> proc StrCopyPart, .dest, .source, .pos, .len
</code></pre>
<p>Extracts <code>[.len]</code> bytes on position <code>[.pos]</code> from the string <code>[.source]</code> and copies them to the string <code>[.dest]</code>. Returns CF=1 if the needed memory can not be allocated.
</p>
<pre><code class=""> proc StrExtract, .string, .pos, .len
</code></pre>
<p>Extracts <code>[.len]</code> bytes on position <code>[.pos]</code> from the string <code>[.source]</code> and copies them to the new created string. Returns the created string in EAX or CF=1 if the needed memory can not be allocated.
</p>
<pre><code class=""> proc StrSplit, .hString, .pos
</code></pre>
<p>Splits the string <code>[.hstring]</code> into two parts on byte possition <code>[.pos]</code>. The left part remains in <code>[.hstring]</code>; The right part is returned as new created string in EAX. Note that the memory of the <code>[.hstring]</code> will not be reallocated, only the length of the string will be set accordingly.
</p>
<pre><code class=""> proc StrInsert, .dest, .source, .pos
</code></pre>
<p>Inserts the <code>[.source]</code> string on possition <code>[.pos]</code> into the <code>[.dest]</code> string. Returns nothing.
</p>
<pre><code class=""> proc NumToStr, .num, .flags
</code></pre>
<p>Converts number <code>[.num]</code> in any radix to string. Returns the new created string in EAX.
</p>
<p><code>[.flags]</code> controls the way number have to be converted. <code>.flags</code> is dword value that contains following values:
</p>
<p>byte0 - number of digits if ntsFixedWidth is specified.
</p>
<p>byte1 - radix for the convertion. Some radixes have predefined constants:
</p><pre><code class=""> ntsBin = $0200
ntsQuad = $0400
ntsOct = $0800
ntsDec = $0a00
ntsHex = $1000
</code></pre>
<p>byte2, byte3 - flags:
</p><pre><code class=""> ntsSigned = $00000
ntsUnsigned = $10000
ntsFixedWidth = $20000
</code></pre>
<pre><code class=""> proc StrToNumEx, .hstring
</code></pre>
<p>Converts <code>[.hstring]</code> to number. Returns CF=0 and value in EAX if the conversion was successful, or CF=1 and EAX = 0 if not.
</p>
<p>The procedure supports the FASM numbers format:
</p>
<p><strong>0x1111</strong>, <strong>$1111</strong> or <strong>1111h</strong> will be converted as HEX;
</p>
<p><strong>1111</strong> as decimal;
</p>
<p><strong>1111b</strong> as binary;
</p>
<p><strong>1111o</strong> as octal.
</p>
<pre><code class=""> proc StrCharCat, .hString, .char
</code></pre>
<p>Appends up to 4 bytes from <code>[.char]</code> at the end of <code>[.hString]</code>
</p>
<pre><code class=""> proc StrCharInsert, .hString, .char, .pos
</code></pre>
<p>Inserts up to 4 characters from <code>[.char]</code> into the <code>[.pos]</code> position of the <code>[.hString]</code>
</p>
<pre><code class=""> proc StrClipSpacesR, .hString
proc StrClipSpacesL, .hString
</code></pre>
<p>Removes the spaces from the start (StrClipSpacesL) or from the end (StrClipSpacesR) of the string <code>[.hString]</code>.
</p>
<pre><code class=""> proc StrCleanDupSpaces, .hString
</code></pre>
<p>Removes duplicating spaces from the string <code>[.hStrin]</code> and leaves only single spaces. For example the string ("." represents the space char) "123.....456" will be processed to "123.456".
</p>
<pre><code class=""> proc StrHash, .hString
</code></pre>
<p>Computes the hash value from the string <code>[.hString]</code>. This procedure implements <code>FNV-1b</code> algorithm.
</p>
<p>Returns 32 bit hash value in EAX.
</p>
<pre><code class=""> proc DataHash, .ptrData, .len
</code></pre>
<p>Computes the hash value from the memory array at address <code>[.ptrData]</code> with byte length <code>[.len]</code>. This procedure also uses <code>FNV-1b</code> algorithm.
</p>
<p>Returns 32 bit hash value in EAX.
</p>
<pre><code class=""> proc StrLenUtf8, .hString, .len
</code></pre>
<p>Computes the length in chars of the first <code>[.len]</code> bytes of the UTF-8 encoded string <code>[.hString]</code> The scan of the string ends on one of the two conditions — terminating zero byte is reached or <code>[.len]</code> bytes are processed. So, if <code>[.len]</code> == <code>-1</code> the scan will always end on the end of the string.
</p>
<pre><code class=""> proc StrOffsUtf8, .hString, .pos
</code></pre>
<p>Returns in EAX the address of the <code>[.pos]</code> character of the UTF-8 encoded string <code>[.hString]</code> If the length of the string is less than <code>[.pos]</code> — returns NULL.
</p>
<pre><code class=""> proc DecodeUtf8, .chars
</code></pre>
<p>Decodes 4 bytes in <code>[.chars]</code> to UNICODE dword value.
</p>
<p>Returns: CF=0 — no error; eax — unicode value; edx — byte count of the char. <code>[1..4]</code>
</p>
<p>CF=1 — invalid utf-8 char; if eax = edx = 0; the character can not be decoded; if edx <> 0 — eax = the overlong encoded character. edx = byte count of the char.
</p>
<p><strong>Note:</strong> When CF=1 and <code>[.chars]</code> is overlong encoded char, eax contains the proper value and edx contains the proper length. But it is still invalid character, according to the standards.
</p>
<pre><code class=""> proc ExpandTabs, .hstring, .tabstop
</code></pre>
<p>Converts the tab characters in <code>[.hstring]</code> into space characters, according to <code>[.tabstop]</code> length. Returns in EAX a value, that indicates by how many chars the length of the string increased.</p>
adminadmin on FreshLib referencetag:asmbb.org,2022-11-19:Post322023-01-23T12:35:57Z
<h1>13. FreshLib directory "timers/"
</h1>
<h2>13.1. "timers.asm" library
</h2>
<p>library deals with user created timers and also contains some procedures for work with the system time and date.
</p>
<h3>TTimer structure.
</h3>
<p>The timers in FreshLib are represented with the following memory structure:
</p>
<pre><code class=""> struct TTimer
.next dd ?
.interval dd ?
.value dd ?
.flags dd ?
.Callback dd ?
.Expired dd ?
.tag dd ?
ends
</code></pre>
<p>The fields are:
</p>
<ul><p><code>.next</code> — Don't change this. It is a pointer to the next timer in the timers chain. It is for internal use only.
</p>
<p><code>.interval</code> — the interval of the time in ms
</p>
<p><code>.value</code> — The current value of the timer in ms. When this value becomes higher than <code>[.interval]</code> an event is fired and the value becomes 0. This value is incremented by the system dependent time step - probably something like 1..100ms
</p>
<p><code>.flags</code> — contains a set of tmfXXXX flag values. Determines the behavior of the timer. See below for description of the flags.
</p>
<p><code>.Callback</code> — pointer to the callback procedure of the timer. The callback procedure should accept one argument with the pointer to the timer that fired the event: <code>proc OnTimer, .ptrTimer</code>
</p>
<p><code>.Expired</code> — count of the timer expirations, if the callback procedure was not called.
</p>
<p><code>.tag</code> — user defined value associated with the timer.
</p></ul>
<p>The <code>.flags</code> field can have one or more of the following values:
</p>
<ul><p><code>tmfDoNothing</code> — when the timer expires no action should be performed. .Expired field of the timer will be incremented.
</p>
<p><code>tmfCallProc</code> — <code>[TTimer.Callback]</code> contains pointer to the procedure that to be executed once per timer expiration.
</p>
<p><code>tmfSyncDestroy</code> — If this flag is set, the timer will be destroyed on the next timer expiration. In this case, the configured event is fired and then the timer is destroyed. The flag is checked after the event returns, so the event handler can reset this flag and thus to prevent destruction.
</p>
<p><code>tmfRunning</code> — If this flag is set, the timer runs. If the event handler resets this flag, the timer will fire only once and will be suspended.
</p></ul>
<h3>proc TimerCreate
</h3>
<p>Creates a new timer.
</p>
<p>Returns:
</p>
<ul><p><code>CF=0</code>; EAX= pointer to the TTimer structure. The timer is created suspended. The user can set or reset tmfRunning in <code>[.flags]</code> in order to start or stop the timer. Also, the user have to enter proper values in the remaining fields.
</p>
<p><code>CF=1</code>; Error allocating memory.
</p></ul>
<h3>proc TimerDestroy, .ptrTimer
</h3>
<p>Destroys the timer <code>[.ptrTimer]</code>
</p>
<h1>14. FreshLib directory "simpledebug/"
</h1>
<h2>14.1. "debug.asm" library
</h2>
<p>This library includes number or macros and procedures aimed to assist the debugging process of the application. These macros display different data values on the debugging console.
</p>
<p>The library contains its own output procedures, so it does not depend on the other used libraries.
</p>
<p>All the macros from this library generate code only when options.DebugMode = 1, so the programmer can include as many debug statements as needed and leave them in the source. They will not be included in the final binary. The debug macros will always preserve all registers, except the EFLAGS register.
</p>
<h3>macro DebugMsg msg
</h3>
<p>Displays the text message msg to the debug console. Example:
</p>
<pre><code class=""> DebugMsg 'The program executes here!'
</code></pre>
<h3>macro OutputRegister reg, radix
</h3>
<p>Outputs the content of some register in the given radix. Example:
</p><pre><code class=""> OutputRegister regEAX, 10
</code></pre>
<p>The possible register values are: regEDI, regESI, regEBP, regESP, regEBX, regEDX, regECX, regEAX
</p>
<h3>macro OutputMemory pointer, size
</h3>
<p>OutputMemory will dump <code>[size]</code> bytes of memory at address <code>[pointer]</code>; Example:
</p><pre><code class=""> OutputMemory esi, 128
</code></pre>
<h3>macro OutputNumber number, radix, digits
</h3>
<p>Outputs digits digits of the number in radix radix. Example:
</p><pre><code class=""> OutputNumber 12345, 16, 8
</code></pre>
<h3>proc GetTimestamp
</h3>
<p>Returns in eax timestamp measured in milliseconds (ms). </p>
adminadmin on FreshLib referencetag:asmbb.org,2022-11-19:Post312023-01-23T12:29:46Z
<h1>12. FreshLib directory "system/"
</h1>
<h2>12.1. "memory.asm" library
</h2>
<p>This library provides OS independent way of allocating, reallocating and freeing dynamic memory blocks. All other libraries in FreshLib that needs dynamic memory, use this library.
</p>
<p>The user who needs such memory blocks should use it as well.
</p>
<h3>proc GetMem, .size
</h3>
<p>Allocates <a href="https://.size/">.size</a> byte of dynamic memory.
</p>
<p>Returns:
</p>
<ul><p><code>CF=0</code>; EAX = pointer to the allocated memory;
<code>CF=1</code>; EAX=0 if the memory can not be allocated.
</p></ul><p>The memory is filled with NULL.
</p>
<h3>proc FreeMem, .ptr
</h3>
<p>Frees the specified in <code>[.ptr]</code> dynamically allocated memory. Returns nothing.
</p>
<h3>proc ResizeMem, .ptr, .newsize
</h3>
<p>Reallocates memory on address <code>[.ptr]</code> to the new size in <code>[.newsize]</code>
</p>
<p>Returns:
</p>
<ul><p><code>CF=0</code>; EAX = pointer to the allocated memory;
<code>CF=1</code>; EAX=.ptr if the memory can not be reallocated. In this case, the memory block is not changed
</p></ul>
<p>The increased part of the memory block is <strong>not</strong> zeroed.
</p>
<h2>12.2. "files.asm" library
</h2>
<h3>proc FileOpen, .filename
</h3>
<p>The procedure opens the file with filename in <code>[.filename]</code> for reading.
</p>
<p>Returns:
</p>
<ul><p><code>CF=0</code>; EAX = Handle to the file.
</p>
<p><code>CF=1</code>; EAX = Error code.
</p></ul>
<h3>proc FileCreate, .filename
</h3>
<p>Creates a file or opens the existing one and truncates its size to 0. The file is opened for writing.
</p>
<p>Returns:
</p>
<ul><p><code>CF=0</code>; EAX = Handle to the file.
</p>
<p><code>CF=1</code>; EAX = Error code.
</p></ul>
<h3>proc FileClose, .handle
</h3>
<p>Closes the previously opened file.
</p>
<p>Returns:
</p>
<ul><p><code>CF=0</code>; EAX = Handle to the file.
<code>CF=1</code>; EAX = Error code.
</p></ul>
<h3>proc FileRead, .handle, .buffer, .count
</h3>
<p>Reads <code>[.count]</code> bytes from the file <code>[.handle]</code> in the buffer at <code>[.buffer]</code>.
</p>
<p>Returns:
</p>
<ul><p><code>CF=0</code>; EAX = The count of actually read bytes.
</p>
<p><code>CF=1</code>; EAX = Error code.
</p></ul>
<h3>proc FileWrite, .handle, .buffer, .count
</h3>
<p>Writes <code>[.count]</code> bytes from the buffer <code>[.buffer]</code> to the file with handle <code>[.handle]</code>.
</p>
<p>Returns:
</p>
<ul><p><code>CF=0</code>; EAX = The count of actually written bytes.
</p>
<p><code>CF=1</code>; EAX = Error code.
</p></ul>
<h3>proc FileSeek, .handle, .dist, .direction
</h3>
<p>Moves the file pointer of the file <code>[.handle]</code> on <code>[.dist]</code> distance (in bytes) relative to <code>[.direction]</code>.
</p>
<p>Direction is one of the following values:
</p>
<ul><p>fsFromBegin — relative to the file begin.
</p>
<p>fsFromEnd — relative to the file end (then <code>[.dist]</code> should be negative).
</p>
<p>fsFromCurrent — relative to the current position.
</p></ul>
<h3>proc FileDelete, .filename
</h3>
<p>Deletes the file with filename in <code>[.filename]</code>
</p>
<p>Returns:
</p>
<ul><p><code>CF=0</code>; The file was deleted.
</p>
<p><code>CF=1</code>; EAX = Error code.
</p></ul>
<h3>proc GetErrorString, .code
</h3>
<p>Returns in EAX, pointer to the human readable error message, corresponding to the error code passed in <code>[.code]</code>
</p>
<p>The message string have to be passed to <strong>FreeErrorString</strong>, when not needed.
</p>
<h3>proc FreeErrorString, .ptrString
</h3>
<p>Frees the error string <code>[.ptrString]</code>, previously returned by <strong>GetErrorString</strong>. As long as the error strings are allocated by the OS, they have to be free by OS as well. Returns nothing.
</p>
<h3>proc LoadBinaryFile, .ptrFileName
</h3>
<p>Loads the whole file <code>[.ptrFileName]</code> to the dynamically allocated memory block.
</p>
<p>Returns:
</p>
<ul><p><code>CF=0</code>; EAX = pointer to the allocated memory; ECX = the size of the loaded file.
</p>
<p><code>CF=1</code>; EAX = Error code. ECX = 0; The memory is not allocated.
</p></ul>
<p>The allocated memory have to be free after use with <strong>FreeMem</strong>.
</p>
<h3>proc SaveBinaryFile, .ptrFileName, .aptr, .size
</h3>
<p>Creates or overwrites the file <code>[.ptrFileName]</code> with the <code>[.size]</code> bytes from the buffer <code>[.aptr]</code>;
</p>
<p>Returns:
</p>
<ul><p><code>CF=0</code>; EAX = count of the bytes actually write;
</p>
<p><code>CF=1</code>; EAX = error code;
</p></ul>
<h3>proc FileExists, .ptrFileName
</h3>
<p>Check the existence of the file with name in <code>[.ptrFileName]</code>.
</p>
<p>Returnds:
</p>
<ul><p><code>CF=1</code> — the file does not exists.
</p>
<p><code>CF=0</code> — the file exists.
</p></ul>
<p>The existence of the file is checked using <strong>FileOpen</strong> procedure. If the file can be opened, it is considered existing.
</p>
<h2>12.3. "process.asm" library
</h2>
<h3>proc Terminate, .exit_code
</h3>
<p>Terminates the application and all of its threads. Returns <code>[.exit_code]</code> to the OS.
</p>
<p>This procedure simply does not returns, because the application stops.
</p>
<h3>proc ThreadCreate, .ptr_to_function, .ptr_to_args
</h3>
<p>Creates new thread. <code>[.ptr_to_function]</code> points to the thread procedure. The thread procedure should have one argument. When the thread starts, <code>[.ptr_to_args]</code> is passed as a thread argument.
</p>
<p>Returns:
</p>
<ul><p><code>CF=0</code>; EAX = is a handle to the new thread. In the different OSes this value can have different meaning. But it identifies the thread anyway.
</p>
<p><code>CF=1</code>; EAX = error code;
</p></ul>
<h3>proc MutexCreate, .ptrName, .ptrMutex
</h3>
<p>Creates new mutex with name <code>[.ptrName]</code> and save its handle to <code>[.ptrMutex]</code> variable.
</p>
<p>The calling thread takes the owneship of the mutex.
</p>
<p>If <code>[.ptrName]</code> = 0, unnamed mutex will be created.
</p>
<h3>proc WaitForMutex, .ptrMutex, .timeout
</h3>
<p>Waits until the mutex is released and takes the ownership.
</p>
<p>Returns:
</p>
<ul><p><code>CF=0</code> — the mutex ownership is successfuly obtained.
<code>CF=1</code> — the timeout was expired.
</p></ul>
<h3>proc MutexRelease, .ptrMutex
</h3>
<p>Releases the ownership of the specified mutex.
</p>
<h3>proc MutexDestroy, .ptrMutex
</h3>
<p>Destroys the mutex <code>[.ptrMutex]</code>
</p>
<h2>12.4. "clipboard.asm" library
</h2>
<p><strong>clipboard.asm</strong> library contains very simple clipboard functions that works only on text data.
</p>
<h3>proc ClipboardRead
</h3>
<p>Returns in EAX handle to the string with the current clipboard content. If the clipboard is empty or contains not textual information, EAX=0; The user should delete the string when not needed by passing it to <strong>StrDel</strong>.
</p>
<h3>proc ClipboardWrite, .hstring
</h3>
<p>Writes the string <code>[.hstring]</code> to the clipboard. Returns nothing. </p>
adminadmin on FreshLib referencetag:asmbb.org,2022-11-19:Post302023-01-23T12:13:54Z
<h2>11.2. "_globals.inc" library
</h2>
<p>This library defines several macros intended to deal with data definitions.
</p>
<p>Usually all data definitions have to be placed in special section of the executable file. This is not very convenient, because the code that process this data and the data definitions must reside in separate places of the source code, and in most cases even in different files.
</p>
<p>The idea of <code>"globals.inc"</code> macro library is to allow the data definition to be described anywhere in the source code, but these definitions to be defined at once, at the place the programmer wants - usually in the data section of the program.
</p>
<ul><p><code>macro uglobal</code>
<code>macro iglobal</code>
<code>macro endg</code>
<code>macro IncludeAllGlobals</code>
</p></ul>
<p><strong>uglobal</strong> begins block for undefined data definition. The block ends with <strong>endg</strong> macro. Between "uglobal" and "endg" macro any count of data definitions can be inserted.
</p>
<p>Note that because uglobal block defines undefined data, it is only the labels and the size of data that have meaning inside this block. Any data, defined with data definition directive will not increase the size of the executable file, but will be allocated when the executable is loaded in the memory.
</p>
<p>The undefined data will be defined later at the place where "IncludeAllGlobals" macro resides. In order to not increase the size of the executable file, the undefined data is always placed at the end of all data definitions.
</p>
<p>"iglobal" macro, again combined with "endg" defines initialized data. The data defined in the block will be created at "IncludeAllGlobals" statement.
</p>
<p>This block increases the size of the executable file, because it contains sensible data, that have to be included in the file.
</p>
<p>Actually, neither <strong>uglobal</strong>, nor <strong>iglobal</strong> blocks defines any data immediately. Instead, the data definitions are stored in a list. The real definition occurs later, when <strong>IncludeAllGlobals</strong> macro is invoked. For this reason, <strong>IncludeAllGlobals</strong> must be placed in the source after all used global data blocks.
</p>
<p>The programmer <span class="underlined">should not</span> use explicitly IncludeAllGlobals, unless the memory model <code>"manual"</code> is set in the <strong>@BinaryType</strong> statement.
</p>
<h3><code>struc text [val]</code>
</h3>
<p>The macro <code>"text"</code> is actually a structure. It needs to be preceded by some label name.
</p>
<p>This macro accepts string or number arguments. When it is invoked with string arguments, it defines a zero terminated string constant, and also a local constant <code>.length</code> equal to the length of the string without terminating zero. When invoked with number as argument, <code>"text"</code> defines label at the address <code>val</code> and does not defines .length constant.
</p>
<p>The <code>"text"</code> macro, the same way as <strong>iglobal</strong> and <strong>uglobal</strong>, simply stores string data for defer definition. This definition, occurs in IncludeAllGlobals invocation. Note that the real definition will be made only if the string is used in the program. Not used strings will not be defined.
</p>
<p>Look at the following example:
</p>
<pre><code class=""> myName text 'John',$20,'Smith'
</code></pre>
<p>This code will define the data and constant labels following way:
</p>
<pre><code class=""> if used myName
myName db 'John Smith'
.length = $-myName
db 0
end if
</code></pre>
<p>Why to define separate macro for the strings and not to use the normal iglobal block? At first, <strong>text</strong> macro defines a real data only if this data is used somewhere in the source. This way is prevented bloating of the code with unneeded data definitions.
</p>
<p>Also <code>( IMPORTANT )</code> the macro <strong>text</strong> defines only one instance of given string. For example, following code will define the string "TestTextMacro" only once and will set String2 to point to the same address as String1:
</p>
<pre><code class=""> String1 text "TestTextMacro"
String2 text "TestTextMacro"
</code></pre>
<p>If the program changes the text on <strong>String1</strong> the text on <strong>String2</strong> will be changed as well, because they actually share the same memory.
</p>
<p>This behavior of <strong>text</strong> macro made possible to FreshLib to allow using string constants directly as a procedure call arguments. As long as the duplication of the strings is not possible, using string constants as a procedure arguments will not cause duplication of data in the program memory.
</p>
<h3>macro var expr
</h3>
<p>The macro var defines dword variable with a given value. The use is following:
</p>
<pre><code class=""> var MyVar = 1234
</code></pre>
<p>The only differens from the usual use of dd directive is that the variable will be defined only if used in the source.
</p>
<p>Note that the variable is created at the place where <strong>var</strong> is used, so you need to place it inside a <strong>iglobal</strong> block if you want it to be defined in the global data place.
</p>
<h2>11.3. "_struct.inc" library
</h2>
<p>This library contains only three simple macros:
</p>
<ul><p><code>macro struct name</code>
<code>macro rstruct name</code>
<code>ends</code>
</p></ul>
<p><strong>struct</strong> macro is aimed to provide easy creation of data structures. The "struc" directive in FASM is known to not create actual labels, but only the template for the label definitions. So, we need to create an instance of the data structure in order to have addresses and offsets of its fields.
</p>
<p>But very often we don't have static data structure of the given type, but data structure, pointed by some of the registers. In this case in order to use offsets to the fields of the data structure, we need to define at least one virtual instance of the structure at address 0. Then we can use the values of the fields as an offsets in the instructions - for example:
</p>
<pre><code class=""> mov eax, [esi+RECT.right].
</code></pre>
<p>So, this is exactly what "struct" macro does. Besides it defines the "struc" structure with the given fields, it creates a single virtual instance of this structure, in order to be used later for register addressing. Also, the macro defines local constant of <strong>sizeof</strong>. global label equal to the byte size of the structure. In all remaining functionality it behaves exactly as the struc directive.
</p>
<p>The syntax of struct macro is the following:
</p>
<pre><code class=""> struct StructureName
.field1 dd ?
.field2 RECT
.fieldN:
ends
</code></pre>
<p>The definition begins with "struct" followed by the structure name. The definition ends with "ends" directive. Between both, any local label definition becomes a member of the structure. The above definition, results in following code:
</p>
<pre><code class="">struc StructureName {
.field1 dd ?
.field2 RECT
.fieldN:
}
virtual at 0
StructureName StructureName
sizeof.StructureName = $
end virtual
</code></pre>
<p>The <strong>rstruct</strong> macro works exactly as <strong>struct</strong> but places the fields in reverse direction. I.e. the last field will have offset 0 and the first field will have smallest negative offset.
</p>
<p>For example:
</p>
<pre><code class="">rstruct RevStruct
.first dd ? ; RevStruct.first = -8
.second dd ? ; RevStruct.second = -4
label .last dword ; RevStruct.last = 0
ends
</code></pre>
<p>This macro is useful for some variable data structures where the data is placed at the positive offsets and the header is located on the negative addresses.
</p>
<p>FreshLib uses such structures in StrLib and some other libraries.
</p>
<h2>11.4. "_display.inc" library
</h2>
<p>This library contains macros that enhance the functionality of standard FASM "display" directive.
</p>
<h3><code>macro disp [arg]</code>
</h3>
<p>The macro "disp" displays the strings given in the arguments, just as "display" FASM directive does. Additionally it can display numbers in any given radix:
</p>
<pre><code class=""> disp <number, radix>
</code></pre>
<h3>macro DispSize Text, Sz
</h3>
<p>"DispSize" is very specialized macro, that displays the text and number in the following form:
</p>
<pre><code class="">Size of [Text] is: Sz bytes
</code></pre>
<p>The size number is automatically scaled to bytes or kbytes, depending on the value of Sz.
</p>
<p>This macro allows easy display and control of the sizes of particular areas of the program - data structures, subroutines etc.
</p>
<p>DispSize macro behavior is controlled by <strong>options.ShowSizes</strong> option.
</p>
<h2>How Fresh implements "display" directive
</h2>
<p>There are some specifics in Fresh IDE, concerning message displaying. The "display" directive in Fresh works in a slightly different way than the original FASM directive.
</p>
<p>It outputs text in Fresh message window. Each message can have one of six icons, or it can have no icon at all. And because message window is implemented as a TreeView control, you can organize your messages into groups (directories).
</p>
<p>Implementation is a bit "tricky" - when you display a character whose code is less than 16, it is interpreted in a special way. Characters from 1 to 6 set an icon of current message. It sounds complicated, but it is quite simple. Try:
</p>
<pre><code class=""> display 2, "some message"
</code></pre>
<p>It will display "some message" with an error icon. Another codes are used for controlling directory structure. Try to type following lines and see what would happen:
</p>
<pre><code class=""> display 3, "message at root", 0x09
display 3, "child message1", 0x0a
display 3, "child message2", 0x0d
display 3, "again at root", 0x0a
</code></pre>
<p>Of course you don't have to put each message in separate display directive, you can, with the same result write:
</p>
<pre><code class="">display 3, "at root",$09,3,"child1",$0a,3,"child2", $0d,3,"again at root",$0a
</code></pre>
<p>Here is the complete list of all special characters and their meanings:
</p>
<pre><code class="">char meaning
$01 set current icon to "warning"
$02 set current icon to "error"
$03 set current icon to "info"
$04 set current icon to "find"
$05 set current icon to "none"
$06 set current icon to "debug"
$08 end current row and set one level back.
$09 end current row and set it as new directory.
$0a end current row and keep current level.
$0d end current row and set current level to root level.
</code></pre>
adminadmin on FreshLib referencetag:asmbb.org,2022-11-19:Post292023-01-23T12:07:02Z
<h1>11. FreshLib directory "macros/"
</h1>
<p>This directory contains several libraries that provides common convenience functions to be used with big assembly projects.
</p>
<p>All these libraries will be included automatically in <code>"%lib%/freshlib.inc"</code> file.
</p>
<p>There is no overhead including all these libraries, because there is no code to be generated, just macro definitions. There is a little delay in compile time but thanks to fasm's speed, it is barely noticeable.
</p>
<p>Lets examine each one of these libraries.
</p>
<h2>11.1. "_stdcall.inc" library
</h2>
<p>In general this library provides ways of definition and invoking of the procedures with argument passing through the stack. It supports STDCALL and CCALL calling conventions.
</p>
<h3>11.1.1. Procedure definition macros
</h3>
<ul><p><code>macro proc name, [arg]</code>
</p>
<p><code>macro begin</code>
</p>
<p><code>macro endp</code>
</p>
<p><code>macro return</code>
</p>
<p><code>macro cret</code>
</p>
<p><code>macro locals</code>
</p>
<p><code>macro endl</code>
</p></ul>
<p>These macros define a procedure, create a stack frame for the local variables and define symbolic names for the arguments. The macro "proc" defines the global label "name" as a name for the procedure. All arguments and local variables are defined as a local labels with regard to the name of the procedure. That is why all arguments and local variables must have names beginning with dot.
</p>
<p>Between the line with <strong>proc</strong> and <strong>begin</strong>, any number of local variables can be defined. The macro <strong>begin</strong> marks the begining of the procedural code.
</p>
<p>The macro <strong>endp</strong> marks the end of the procedural code.
</p>
<p>The return from procedure instruction is provided by macros <strong>return</strong> or <strong>cret</strong> depending on the calling convention we want to use: <strong>return</strong> clears the arguments from the stack and <strong>cret</strong> does not.
</p>
<p>Inside the procedure, a secondary stack frame can be allocated with the pair <strong>locals</strong> and <strong>endl</strong>. All data definitions, enclosed between these macros will define a secondary stack frame that is a continuation of the stack frame defined between <strong>proc</strong> and <strong>begin</strong>.
</p>
<p>Any number of <strong>locals</strong> and <strong>endl</strong> pairs can be used, but all of these secondary stack frames will overlap between each other. This feature is specially intended to provide savings of stack space and at the same time, to provide talking variable names for the different parts of more complex procedures.
</p>
<p>For example (in Win32) let we have complex window procedure that have to process multiple messages.
</p>
<p>One of the message handlers may need one variable <strong>.rect</strong>.
</p>
<p>Another message handler may need two variables called <strong>.point1</strong> and <strong>.point2</strong>.
</p>
<p>But the procedure as a whole is never going to need all those variables at the same time, because it process only one message at a time. On the other hand it may need the variable <strong>.ctrldata</strong> for every message processed. The optimal solution is to define the variables as shown in the following example:
</p>
<pre><code class="">proc CtrlWinProc, .hwnd, .wmsg, .wparam, .lparam
.ctrldata dd ?
begin
invoke GetWindowLong, [.hwnd], GWL_USERDATA
mov [.ctrldata], eax
cmp [.wmsg], WM_MESSAGE1
je .message1
cmp [.wmsg], WM_MESSAGE2
je .message2
return
.message1:
locals
.rect RECT
endl
; do something.
return
.message2:
locals
.point1 POINT
.point2 POINT
endl
; do something.
return
endp
</code></pre>
<p>The assignment of the stack memory for the above example is shown in the table:
</p>
<pre><code class="">Address Stack frames
Common Locals 1 Locals 2
EBP-20 .rect.left .point1.x
EBP-16 .rect.top .point1.y
EBP-12 .rect.right .point2.x
EBP-8 .rect.bottom .point2.y
EBP-4 .ctrldata
</code></pre>
<p>As you can see, <strong>.rect</strong> occupies the same memory as <strong>.point1</strong> and <strong>.point2</strong>, but <strong>.ctrldata</strong> is never overlapped and exists independently.
</p>
<p>As a general rule, you have to use the definitions between "proc" and "begin" for local variables that are used in every call of the procedure and separate locals/endl definitions for variables needed for the particular branches inside the procedure. This approach will always provide the optimal size for the locals stack frame.
</p>
<h3>macro <code>interface name, [arg]</code>
</h3>
<h3>macro <strong>body name</strong>
</h3>
<p>Sometimes, especially when portable code is to be created, one needs to define the interface part of the procedure in some place and the actual body in another place of the source code (even in separate file).
</p>
<p>In this case the macro <strong>interface</strong> and <strong>body</strong> are to be used.
</p>
<ul><p><strong>interface</strong> has the same syntax as <strong>proc</strong>, but does not need other elements ( begin, endp, etc.)
</p>
<p><strong>body</strong> needs only the name of the procedure (without the arguments).
</p></ul>
<p>Here is small example:
</p>
<pre><code class=""> interface SomeProc, .arg1, .arg2
....
....
body SomeProc
begin
mov eax, [.arg1]
add eax, [.arg2
return
endp
</code></pre>
<h3>macro <strong>initialize</strong>
</h3>
<h3>macro <strong>finalize</strong>
</h3>
<p>The macros "initialize" and "finalize" defines one special type of procedures that, during compilation are registered in a two separate lists - one for "initialize" and one for "finalize" procedures. Procedures defined with "initialize" and "finalize" must have no any arguments.
</p>
<p>After that, using the macros "InitializeAll" and "FinalizeAll", all these procedures can be call at once. "initialize" procedures are call in the order of their definition and "finalize" procedures in reverse order.
</p>
<p>These macros provides standard and consistent way for initialization and the finalization of the libraries and modules of the application.
</p>
<p>FreshLib uses this mechanism and the user is free to use it as well.
</p>
<h3>11.1.2. Procedure call macros.
</h3>
<ul><p><code>macro stdcall proc, [arg]</code>
<code>macro ccall proc, [arg]</code>
<code>macro invoke proc, [arg]</code>
<code>macro cinvoke proc, [arg]</code>
</p></ul>
<p>These macros call the procedures with <strong>STDCALL</strong> and <strong>CCALL</strong> calling convention.
</p>
<p><strong>stdcall</strong> macro pushes the arguments to the stack in reverse order and then call the procedure with label proc. As long as the macro "stdcall" does not provide any stack cleanup (this duty is assigned to the procedure) the arguments can be pushed in free manner using, for example, separate push instructions for part of the arguments and arguments in the stdcall line for the remaining arguments.
</p>
<p>This approach can be very convenient in some cases. For example see the following source:
</p><pre><code class=""> stdcall CreateSomeObject
push eax
stdcall DoSomething
stdcall DeleteSomeObject
</code></pre>
<p>Here, the procedure DoSomething changes the value of eax, so the handle is saved in the stack. The procedure DeleteSomeObject needs one argument — a handle of the object. But as long as the proper value is already in the stack, it is mindless to pop the value and then to push it again. So the source calls DeleteSomeObject without any arguments. The procedure knows the proper number of arguments (one in this example) and clean the stack properly.
</p>
<p>The standard (and wrong) approach is to pop the argument from the stack and then to pass it to the procedure explicitly is the stdcall statement:
</p><pre><code class=""> stdcall CreateSomeObject
push eax ; save the handle.
stdcall DoSomething
pop eax ; ??? Why ???
stdcall DeleteSomeObject, eax
</code></pre>
<p>This source will generate the meaningless instructions sequence:
</p><pre><code class=""> pop eax
push eax
</code></pre>
<p>invoke macro is the same as "stdcall" with the only difference - it calls the procedure indirectly ( <code>call [proc]</code> instead of <code>call proc</code> ). This mechanism usualy is used to call the functions imported from external dynamic linked libraries. Of course, the imported functions can be call with <code>stdcall [someproc]</code> but the <strong>invoke</strong> macro helps to better distinguish what procedures are imported and what are internal for the program.
</p>
<p><strong>NOTE:</strong> The user should never use <strong>invoke</strong> in the portable programs, because such programs never use directly OS dependent import functions.
</p>
<p><strong>ccall</strong> macro calls a procedure with CCALL convention. This means that the procedure returns with simple "retn", without cleaning the parameters from the stack. Then "ccall" macro provides instructions that remove the arguments from the stack.
</p>
<p>Because ccall have to know the exact count of passed arguments, all arguments have to be passed explicitly as a values in the ccall statement. Tricks as described above will not work properly and leads to stack not properly cleaned after the call.
</p>
<p>"cinvoke" is the same as ccall, but using indirect call. The reason for existing of "cinvoke" macro is the same as with "invoke" macro — better legibility of the source.
</p>
<p><strong>About the calling conventions:</strong> While all Win32 dynamic linked libraries uses STDCALL convention, most (if not all) of Linux libraries uses CCALL convention.
<strong>All code libraries of Fresh use STDCALL calling convention and it is platform independient.</strong>
</p>
<h3>11.1.3. Procedure call arguments
</h3>
<p>The arguments of the procedure call macros can be of two types:
</p><ul><p><strong>32bit number</strong> - it can be numeric constant, register or memory location that is directly pushed to the stack.
<strong>String</strong> - if the argument is string constant longer than 4 characters (the shorter strings are treated as a number) a string constant is created in the memory and the address of this constant is pushed to the stack. The string constant can contains more than one element enclosed in <strong><</strong> and <strong>></strong> brackets. See how <strong>text</strong> macro works.
</p></ul>
<p>Several examples of how some cases are compiled:
</p><pre><code class=""> stdcall MyProc, 1234h
> pushd 1234h
</code></pre>
<pre><code class="">stdcall MyProc, 'shrt' ; push it as a dword const.
> pushd 'shrt'
</code></pre>
<pre><code class="">stdcall MyProc, <'Some long text with control symbols', 13, 10>
> local lbl
> lbl text 'Some long text with control symbols', 13, 10
> pushd lbl
</code></pre>
<pre><code class="">stdcall MyProc, <'sh', 'rt'> ; separate the short string on several pieces
> local lbl
> lbl text 'sh', 'rt'
> pushd lbl
</code></pre>
<pre><code class="">stdcall MyProc, txt 'shrt' ; using "txt" prefix on short strings.
> local lbl
> lbl text 'shrt'
> pushd lbl
</code></pre>
<pre><code class="">stdcall MyProc, 'Long string'
> local lbl1
> lbl1 text 'Long string'
> pushd lbl1
</code></pre>
<p>If the same string is used several times, only one copy of the string is defined in the memory.
</p>
<pre><code class="">stdcall MyProc, 'Long string'
> local lbl2
> lbl2 text 'Long string' ; lbl2 = lbl1
> pushd lbl2
</code></pre>
<p>This functionality is provided by the macro <strong>pushx</strong>. It can be used by the user as well.</p>
adminadmin on FreshLib referencetag:asmbb.org,2022-11-19:Post282023-01-23T11:16:19Z
<h1>8. FreshLib directory "compiler/"
</h1>
<p>This directory contains only one macro library: <code>"executable.inc"</code>
</p>
<h2>8.1. "executable.inc" library
</h2>
<p>This library defines the macros that are going to be used for creating the main structure of the program. The implementation of these macros is OS dependent, as long as the type of the executable file is OS dependent: PE for Win32 and ELF for Linux. The use of the library, however is OS independent and is common for all supported OSes. Depending on the value of <code>TargetOS</code> alias, the library will create PE executable or DLL <code>(%TargetOS%='Win32')</code>, or ELF executable or shared library <code>(%TargetOS%='Linux')</code>
</p>
<p><strong>NOTE: </strong> Every of the macros from this library must used only once in the program.
</p>
<h3>macro <strong>@BinaryType type, model</strong>
</h3>
<p>This macro sets the type of the executable. The argument <strong>type</strong> can accept one of the following values: <strong>GUI</strong>, <strong>console</strong> or <strong>DLL</strong>.
</p>
<p>The argument <strong>model</strong> is optional and determines the memory model the compiler will follow for the data sections of the program. The possible values are in general OS dependent, but some are in common use:
</p><ul><p><strong>default</strong> - use the default model for the given binary type. The data sections are usually separated from the code section.
</p>
<p><strong>compact</strong> - try to make the binary as small as possible, merging data, import, export and code sections if possible.
</p>
<p><strong>manual</strong> - does not automatically create data or import sections. The programmer is responsible for creating these sections.
</p></ul><p>This macro also begins the main code section of the executable and defines the entry label of the program. The entry label is fixed to <strong>start:</strong> if the binary type is console or GUI and <strong>EntryPoint</strong> if DLL.
</p>
<p>For example, following code will tell the compiler to compile the program as a GUI application with default memory model:
</p><blockquote><header></header><p>include '%lib%/freshlib.inc'
@BinaryType GUI
</p></blockquote>
<h3>macro <strong><span class="underlined"></span>create_all_imports</strong>
</h3>
<p>This macro is for internal use only. The user <span class="underlined">must not call it</span>, unless the memory model in <strong>@BinaryType</strong> is set to <strong>manual</strong>.
</p>
<p>This macro creates a data structure for all imported from dynamic libraries functions. The data is placed where the macro is called.
</p>
<h1>9. FreshLib directory "equates/"
</h1>
<h3>"allequates.inc" library.
</h3>
<p>This library defines all constants and structures needed for OS dependent parts of FreshLib.
</p>
<p>Actually, the user should never use these constants and structures in the portable program.
</p>
<p>The constants and structures that the user should use are defined in the respective libraries, not in <code>"allequates.inc"</code>.
</p>
<p>This library will be included automatically by <code>"%lib%/freshlib.inc"</code> file, so the user should not care about this library at all.
</p>
<h1>10. FreshLib directory "imports/"
</h1>
<p>This directory contains include files with import definitions for different DLLs. As long as FreshLib handles the import sections automatically, the user should never use these files explicitly.
</p>
<p>FreshLib contains very big catalog of shared libraries for Windows and decent set of Linux shared libraries. The import macros used by FreshLib includes in the import section of the program, only the functions used by the program, so it will never define redundant import items.</p>
adminadmin on FreshLib referencetag:asmbb.org,2022-11-19:Post272023-01-23T11:05:16Z
<h1>6. FreshLib code conventions
</h1>
<h2>6.1. Naming conventions
</h2>
<p>1. The names prefixed with one or more underscores ("_") are not recomended for use by the user. These are internaly used labels that can be changed later. More underscores in the prefix - more "internal" is the given identifier.
</p>
<p>For example one underscode ( like this: <code>_AlmostPrivate</code>) means - "use it carefully".
</p>
<p>Three underscores (for example <code>___SomeVeryPrivateLabel</code>) means - don't use it at all. It is for internal use only and will be changed later!
</p>
<p>2. The names are considered to be used with code completion editor - i.e. there is no long equal prefixes of the names, but there are short "class" prefixes. For example most of the procedures in StrLib begin with "Str" prefix.
</p>
<p>3. In general, FreshLib uses <a href="http://en.wikipedia.org/wiki/CamelCase">CamelCase</a> naming convention, with constants in lowerCamelCase and procedures in HigherCamelCase.
</p>
<p>4. All local labels and procedure arguments are prefixed with dot — ".";
</p>
<p>5. Almost all of the struct and all of object definitions are prefixed with "T" prefix — for example <strong>TTimer</strong> or <strong>TButton</strong>
</p>
<p>6. The file names convention. All file names with extension <code>.inc</code> doesn't contain any code or data definition. Only macro and constants definitions are permitted.
</p>
<p>The files with '.asm' extension can define code and data. Although, the code and data in FreshLib can exists in the compiled binary, only if they are used. Not used data or code, included in the binary should be considered a bug.
</p>
<h2>6.2. Register preserving convention
</h2>
<p>1. The rule is - preserve all you are using. All procedures in FreshLib preserves all registers, except these used for returning result values.
</p>
<blockquote><header></header><p>Note: There is some small retreat from this rule - in the object handling procedures, some register are preserved internaly, so the user may not preserve them in its code. See <strong>object.asm</strong> library for details.
</p></blockquote>
<p>2. CF is widely used for returning error status or boolean result. As a rule <code>CF=0</code> means no error; <code>CF=1</code> means error.
</p>
<p>The use of CF is described always in the description of the respective procedures.
</p>
<p>3. The procedures can return result in more than one register. As a rule, EAX is the result register for the most procedures, but sometimes other registers are used - in order to provide better interface for assembly programming.
</p>
<p>For example number of procedures return X,Y values in EAX, EDX registers.
</p>
<p><strong>EAX</strong> — commonly used for returning 32bit values;
</p>
<p><strong>EDX</strong> — second choise - used together with EAX for 64 bit values, or as a second returned value.
</p>
<p><strong>ECX</strong> — usually some count. For example if EAX returns pointer to some memory, ECX will contains the data size. See <strong>LoadBinaryFile</strong> for example.
</p>
<h1>7. Using FreshLib
</h1>
<p>There are only two files, the user should include in order to use FreshLib. They are both located in the "freshlib" directory, usually referred by %lib% directory alias.
</p>
<p>These files are:
</p><ul><p><code>%lib%/freshlib.inc</code> - contains all macro and OS dependent equates definitions.
</p>
<p><code>%lib%/freshlib.asm</code> - contains all code of FreshLib. Only the used code will actually be included in the result binary.
</p></ul><p>The minimal application with FreshLib have following code:
</p>
<pre><code class="">include "%lib%/freshlib.inc"
@BinaryType GUI
include "%lib%/freshlib.asm"
start:
InitializeAll
; Place your code here
FinalizeAll
stdcall TerminateAll, 0
</code></pre><p>The macro <code>@BinaryType</code>, will be explained later in the next chapter.</p>
adminadmin on FreshLib referencetag:asmbb.org,2022-11-19:Post262023-01-23T05:51:05Z
<h1>1. Overview
</h1>
<p><a href="https://fresh.flatassembler.net/index.cgi?page=content/articles/2_FreshLibDoc.txt">FreshLib</a> is an assembly library aimed to ease the development of assembly language applications, freely portable between different platforms, such as Win32 or Linux.
</p>
<p>The library is coded in <a href="http://flatassembler.net/">flat assembler</a> syntax and is intended to be easily used within Fresh IDE, although it could be used for plain FASM development.
</p>
<p>The library consists of two layers: one, that is OS dependent and a second one that is OS independent. The OS dependent layer is very small, in order to help porting it for different OSes. This layer only makes interface to the core OS functions, such as memory allocations, file access, drawing functions, simple window management etc.
</p>
<p>The OS independent layer is responsible for the main application functionality allowing creation of different kind of windows and controls, although it could be used for plain FASM development.
</p>
<p>FreshLib is mainly intended for developing GUI applications, as they are the most challenging to be ported across different platforms. FreshLib is also created with visual programming in mind, so it contains a flexible, event driven and OS independent template engine allowing visual creation of application user interfaces.
</p>
<p>FreshLib is in early development stage and probably will be changed many times in order to reach their objectives: to be small, fast and easy to use.
</p>
<p>The main intention is to keep the bloat off the library, but containing all necessary accessories for comfortable programming of a very wide range of applications.
</p>
<p>The architecture of FreshLib is open and it can be freely expanded with other libraries without increasing the size of applications. In other words, only those parts of the library that are effectively used will be compiled on the final executable.
</p>
<h1>2. About this manual
</h1>
<p>This manual is a "work in progress". Any part of it can be changed at any time.
</p>
<p>Of course, some of the libraries described in this document are more stable and finished like the macro, system and data libraries. Therefore, the chapters about these libraries are less likely to be changed. Other libraries (like graphics and GUI), will be heavily modified so the manual will be changed accordingly.
</p>
<h1>3. Structure of the library.
</h1>
<p>FreshLib contains many code and macros libraries, structured hierarchically and depending on each other. Here is shown a part of the library directory tree:
</p><pre><code class=""> freshlib/
compiler/
Linux/
Win32/
data/
Linux/
Win32/
dialogs/
Linux/
Win32/
...
</code></pre>
<p>The library is structured to support different platforms transparently. You can see, that the tree consists of main sub-directories, that contains OS independent libraries, separated by topics. For example system subdirectory contains libraries for accessing system resources such as memory, files, etc. data contains libraries for data handling and so on. Every topic directory have also several sub-directories, that contains OS dependent code these directories are named after the platform they serve. (In this moment only Linux and Win32 OSes are supported).
</p>
<h1>4. Compiler setup for FreshLib use.
</h1>
<p>You can use any FASM compiler to compile applications that uses FreshLib. In order to be compiled properly, FreshLib needs environment variables named <strong>lib</strong> and <strong>TargetOS</strong> to be defined.
</p>
<p>The variable <strong>lib</strong> contains the path to the main directory of FreshLib and the variable <strong>TargetOS</strong> contains the target platform, the application will be compiled for. The value of <strong>TargerOS</strong> is identical to the name of OS dependent directories in FreshLib.
</p>
<p>Currently, there are 3 of them: <strong>Win32</strong>, <strong>Linux</strong> and <strong>KolibriOS</strong>. Setting <strong>TargetOS</strong> to some of these values will make the program to be compiled for this OS.
</p>
<p>There are several ways these variables to be defined, depending on the compiler you use <a href="http://flatassembler.net/">FASM</a>, <a href="http://flatassembler.net/">FASMW</a> or <a href="http://fresh.flatassembler.net/">Fresh IDE</a>:
</p><ul><li><p>These variable can be defined in the OS environment - see your OS documentation for details. this approach is more universal - it works for all kind of FASM compilers. The main drawback is that you have to use OS specific commands and probably will have to edit some system files.
</p></li>
<li><p>Definition in the section <code>[environment]</code> of <code>"fasm.ini"</code> or <code>"Fresh.ini"</code> file, depending on the IDE you are using. This approach works for both FASMW and Fresh IDE, but in Fresh, the same effect can be done from inside the IDE. Besides, defined this way, the environment variables becomes "global" - active for all projects compiled with FASMW or Fresh.
</p></li>
<li><p>From inside Fresh IDE.
</p></li></ul>
<p>In Fresh IDE, the environment variables are named <code>"alias"</code>, because they serve to provide short alias for the file paths. Two types of alias (environment variables) lists are supported by Fresh: <code>global aliases</code> and <code>project aliases</code>. Global aliases are defined in the IDE options dialog: <code>Options|IDE options|Aliases</code>. Here is the screenshot of this dialog:
</p>
<p><img class="block" src="https://asmbb.org/!attached/1" alt="IDE options dialog, section "Aliases"" />
</p>
<p>The global aliases are active for every project compiled with Fresh and are stored in the <code>Fresh.ini</code> file, inside the Fresh program directory.
</p>
<p>Project aliases are defined in the Project options dialog: <code>Project|Project options</code> or from the project manager, click on the button <strong>Settings</strong> at the top and select <strong>Project options</strong>. The project options dialog is shown on the following screenshot:
</p>
<p><img class="block" src="https://asmbb.org/!attached/2" alt="Project based aliases can be edited in the project options dialog." />
</p>
<p>The project aliases are stored inside the project file <code>(.fpr)</code> and they are project specific.
</p>
<p>For FreshLib it is not important what list will be used, but it is more convenient for <strong>lib</strong> variable to be defined in the global list and for <strong>TargetOS</strong> variable to be defined in the project aliases. In such way the common parameter (the place of the library) will be set once for all projects, and the particular parameter (the target OS) will be set separately for every project.
</p>
<p>Also, there is very convenient way of changing the value of project aliases — if several values are specified in the project alias, separated with | char (for example: <strong>Win32|Linux</strong>), Fresh will provide fast switching between these values from the project manager options menu, as shown on the picture:
</p>
<p><img class="block" src="https://asmbb.org/!attached/3" alt="The aliases with more than one value will appear in the popup menu for fast changing." />
</p>
<p>When Fresh searches for needed alias names, during the compilation, it searches first in the project aliase list, then the global aliases and at the end, the OS environment variables. Of course, if the alias is not found on these places, the compilation fails with error.
</p>
<h1>5. FreshLib compiling options
</h1>
<p>FreshLib uses some options in order to set the behavior of the compiler and the different macro libraries. These options are defined as a local constants of the label "options." Here is a list:
</p><ul><p><code>options.FastEnter</code> controls the behavior of the proc macro.
</p>
<p>When options.FastEnter = 1 the procedure entry/leave code will be created with faster, but bigger push ebp/pop ebp instructions.
</p>
<p>When options.FastEnter = 0 — enter/leave instructions are used.
</p>
<p><code>options.ShowSkipped</code> controls the information displayed during compilation.
</p>
<p>When options.ShowSkipped = 1 the compiler will display in the output window the procedures that are not compiled because they are not used in the program.
</p>
<p><code>options.ShowSizes</code> controls the behaviour of the DispSize macro.
</p>
<p>When options.ShowSizes = 0 the macro DispSize will be disabled.
</p>
<p><code>options.DebugMode</code> controls the behaviour of the debug macros.
</p>
<p>When options.DebugMode = 1 the macros from simpledebug library will generate debug code and debug console will be created on running the application.
</p>
<p>When options.DebugMode = 0 these macros will not generate code and the debug console will not be created.
</p></ul>
admin