asmBB Forum : Learning the AsmBB source code
<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:Thread162022-12-03T05:08:23Zadmin on Learning the AsmBB source codetag:asmbb.org,2022-11-19:Post232022-12-03T05:08:23Z
<p>Well, I can help by shortly describing the structure of the project. But notice that because of using FastCGI protocol, <strong>AsmBB</strong> is a little bit more complex than usual for beginner assembly programmer.
</p>
<p>The simple CGI web application (for example my CMS <a href="http://chiselapp.com/user/johnfound/repository/MiniMagAsm/index">MiniMagAsm</a>) works by short living CGI processes. They are started by the web server, receive the HTTP request by reading STDIN, write the response (e.g. HTML) to STDOUT and terminate.
</p>
<p>The FastCGI application works a little bit different. At first it works all the time and listens for connections on some socket. The web server connects to this socket and sends the request. Then the FastCGI application sends back the response using the same connection. Serving multiply requests on the same connection and multiply connections are possible. Also, the web server can start several instances of the FastCGI application. It is all about a performance.
So, the structure of AsmBB:
</p>
<p>Everything starts in <a href="https://asm32.info/fossil/asmbb/artifact?name=82c71f1f23&ln=123">engine.asm, line:123 - the label start</a>:. Here the engine initializes its environments and opens the SQLite database with the forum data. Then it calls the procedure <a href="https://asm32.info/fossil/asmbb/artifact?name=82c71f1f23&ln=163">Listen (line: 163)</a> that is the main loop of the program. It returns only when the engine terminates. After retuning from Listen, the program closes the database, finalizes its works and exits.
</p>
<p>The procedure Listen is defined in the source file <a href="https://asm32.info/fossil/asmbb/artifact?name=81f60b545f&ln=166">fcgi.asm:166</a>. This is the code, that handles <a href="https://en.wikipedia.org/wiki/FastCGI">FastCGI</a> protocol. It simply listens for connections from the web server and on connection creates a thread that to serve the request and continues to listen. The thread is started in the procedure procServeRequest in <a href="https://asm32.info/fossil/asmbb/artifact?name=81f60b545f&ln=347">fcgi.asm:347</a>.
</p>
<p>This thread, receives the request information on the socket and collects it for future processing.
</p>
<p>Once the whole information is collected - i.e. the HTTP reques, the POST data (if any) and the environment parameters, the procedure <a href="https://asm32.info/fossil/asmbb/artifact?name=cf7738eb98&ln=127">ServeOneRequest</a> is called. It is actually the "business logic" code that makes the application to act as a web forum.
</p>
<p><strong>ServeOneRequest</strong> returns the response (e.g. HTML code, images, etc.) that is returned to the web server, according to FastCGI protocol. After completing the request, the thread terminates or stays waiting for another request. It depends on how the web server can multiplex the requests on the FastCGI connection.
</p>
<p><strong>ServeOneRequest</strong> is located in the file <a href="https://asm32.info/fossil/asmbb/artifact?name=cf7738eb98&ln=127">commands.asm:127</a>. It analyzes the request URL and the request type (GET, POST) and decides how to serve it. For example, it can return some file directly, or read the information from the database, or store some information in the database.
</p>
<p>The URL analyze is important and located on <a href="https://asm32.info/fossil/asmbb/artifact?name=cf7738eb98&ln=427">commands.asm:427</a> - it dispatches the control depending on the URL elements. The addresses of the different procedures are loaded to <strong>ECX</strong> and later called (label <a href="https://asm32.info/fossil/asmbb/artifact?name=cf7738eb98&ln=598">.exec_command</a>, line: 598).
</p>
<p>Later you can browse these procedures. They are located in different files, serving different aspects of forum engine. For example, the <a href="https://asm32.info/fossil/asmbb/artifact?name=fd6dc9e977&ln=7">procedure ListThreads</a> <strong>threadlist.asm</strong> creates the list of threads on the front page of the forum.
</p>
<p>The <a href="https://asm32.info/fossil/asmbb/file?ci=tip&name=source/showthread.asm&ln=46">procedure ShowThread</a> <strong>showthread.asm</strong> displays one thread. And so on.
</p>
<p>Notice, that AsmBB widely uses the library FreshLib. You can read more in <a href="https://fresh.flatassembler.net/index.cgi?page=content/articles/2_FreshLibDoc.txt">FreshLib reference</a> and <a href="https://fresh.flatassembler.net/index.cgi?page=content/articles/3_howto.txt">FreshLib user guide</a>, but unfortunately the documentation is far from perfect.
</p>
<p>In order to better browse the big code, scattered across multiply files, I would suggest using <a href="https://fresh.flatassembler.net/">Fresh IDE</a> code browsing features. Read more in the following article: Tips and tricks. Especially useful if the feature "Goto definition" (Ctrl+D) that will jump you at the line where some label is defined. The cross reference is also useful (Ctrl+R).
</p>
<p><a href="https://fresh.flatassembler.net/index.cgi?page=content/articles/3_howto.txt">FreshLib user guide</a> </p>
admin