tag:blogger.com,1999:blog-9811203376875912192024-03-05T13:06:40.447-08:00Beneschtech BlogVarious blog articles about computer science and programmingChris Beneschhttp://www.blogger.com/profile/10552098270462277588noreply@blogger.comBlogger9125tag:blogger.com,1999:blog-981120337687591219.post-30332634043950578852020-06-12T22:37:00.000-07:002020-06-12T22:37:48.088-07:00Disassembling C++ Part 2 -- Objects<h1 dir="ltr" id="docs-internal-guid-9ecda802-7fff-2a7f-1002-6f639b6b68fa" style="line-height: 1.38; margin-bottom: 6pt; margin-top: 20pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 20pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">What is an object?</span></h1>
<br /><div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">This article is part two of my Disassembling C++ series. The first one was </span><a href="https://medium.com/swlh/disassembling-c-part-1-overloads-57c138d63edb" style="text-decoration: none;"><span style="-webkit-text-decoration-skip: none; background-color: transparent; color: #1155cc; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration-skip-ink: none; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">here</span></a><span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> about overloaded functions, and mangling. Some of what we are going to discuss is built on that. An object programmatically is basically a collection of data and functions that serve a similar purpose. For example, you can have a bicycle object that you can call a function to rotate the wheels, and get the wheel count (2) from a variable or an accessor function. There are entire books written on object inheritance, polymorphism, reflection, and other nuances of the object oriented methodology and I really don't want to rehash all of that in a five to ten minute article. I assume that if you are here reading this, you know what an object is and all of the fun things you can do with them. I’m going to make a bold statement about objects in general from the compilers perspective though: Classes are just fancy structs. In fact, starting with C++11 you can have a struct with functions. The only difference is that structs have everything by default public whereas a class is by default private. We are going to do some more C/C++ namespace hacking in this article too, so if you enjoyed that in the last one, more is coming.</span></div>
<br /><h1 dir="ltr" style="line-height: 1.38; margin-bottom: 6pt; margin-top: 20pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 20pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">Objects only hold data</span></h1>
<br /><div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">I know that most of us are used to the idea of a class (or object) as being a mysterious collection of data and functions that we can create, copy, destroy, extend, or use in a polymorphic fashion. The truth is that the functions inside a class are just regular functions mangled by the compiler, and the class is only a pointer to the data inside of it. Let’s take this simple object here:</span></div>
<br /><div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"><span style="border: none; display: inline-block; height: 412px; overflow: hidden; width: 624px;"><img height="412" src="https://lh5.googleusercontent.com/X3X6GzwjcD_ljQMatA65xsNGpLMH8lWWrIEQRA0myRC-HeFjqsdovap9kwlXJR80lBRFirPdcKuw7tSmMgVncTfhLcEaWAEAmoRzMQ30d6ZMVz3vCmvjhBzahdjPNRHl7GqVBos6" style="margin-left: 0px; margin-top: 0px;" width="624" /></span></span></div>
<br /><div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">Now, if we compile this and look at the symbols exported, we see the function and a reference to printf in C linkage.</span></div>
<div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<br /></div>
<div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"><span style="border: none; display: inline-block; height: 412px; overflow: hidden; width: 624px;"><img height="412" src="https://lh4.googleusercontent.com/y42lNKFdQJzrm2MuEWejMtdS2xDgIsv6z67bEYBTOkbnWOqP8FFcd7EzDBjfv2Tsbb-bSNeb0lCLMftPMqryJFwg0bdUXkQx-yNBADzaxQqn4VuxYqGhLOtJCqAcrFTdg8hmr8NQ" style="margin-left: 0px; margin-top: 0px;" width="624" /></span></span></div>
<div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">Notice that the function, even though private is still defined in the file. That is all that it is, a function, and as such can be called from regular old C. So, would this work?</span></div>
<div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"><span style="border: none; display: inline-block; height: 412px; overflow: hidden; width: 624px;"><img height="412" src="https://lh5.googleusercontent.com/nqOip_A-RTvn0qtgomyd19Z-1tm6eFuUMrIwCjDkoYU05uSuWe3Atagtw68ZF-Pss_rWK5H7adJn80kBjFcedMIDF02zEYBYe00f9hBcC95gTB2_MRwNDDmlADgeBZEZOlYOO4Rt" style="margin-left: 0px; margin-top: 0px;" width="624" /></span></span></div>
<br /><div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">Let's find out:</span></div>
<br /><div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"><span style="border: none; display: inline-block; height: 412px; overflow: hidden; width: 624px;"><img height="412" src="https://lh4.googleusercontent.com/viVkQH80Gl3lgiyDhRrFKLbylNoqSvvawhesvzJkrwDPQcGdnUMo_WMVJoFmBej1eWf69YChSiq8eZ20Vjzszl4brJ0DlthDTkp-_k4Vy90IKHvA3zrj9-6jp6IE8Brrxk4zoT6-" style="margin-left: 0px; margin-top: 0px;" width="624" /></span></span></div>
<div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">Kablammo! But there is a very good reason for that. Remember how I said an object is just a pointer to some data? One thing the compiler does under the hood is for every object function call, there is a hidden first parameter that is the object's address to represent “this”. Now our example object here has two ints as it data elements. So let's try an experiment.</span></div>
<br /><div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"><span style="border: none; display: inline-block; height: 412px; overflow: hidden; width: 624px;"><img height="412" src="https://lh6.googleusercontent.com/pO_oiLPL07M1na8vDpHqqWzoIctrbQTNo0Wdzg6JHQaAnE4jJDeKXQV4D9Ah2xCg2onXy5AAP8F7Qfn39u5UxwJ9tUTBzaTN-D5axZfr2Kn8RXT37z5l54dHKzJAGCh0crdZkOth" style="margin-left: 0px; margin-top: 0px;" width="624" /></span></span></div>
<br /><div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">Here since we know what the object’s data looks like, we create a C struct that is identical. We also pass the </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: italic; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">address</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> of the struct as the sole parameter to a function declared inside of an object to have none.</span></div>
<br /><div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"><span style="border: none; display: inline-block; height: 412px; overflow: hidden; width: 624px;"></span></span></div>
<div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"><span style="border: none; display: inline-block; height: 412px; overflow: hidden; width: 624px;"><img height="412" src="https://lh3.googleusercontent.com/CSuG8Ww5P1nrdu1mwonwwF7mcGrWlBuS26qJSU5LRGXy3by8rp5GqruZxJC1daoQXyOice0G2iYZzE2w6IEb_MsECRhG3GwfJH4gi-mkBAaEq5A8NtqToCNWrWBrZzYv0VgYPKCU" style="margin-left: 0px; margin-top: 0px;" width="624" /></span></span></div>
<div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">This really works! Try it yourself. Also notice that I never linked against the libstdc++ library, it’s all just the standard C library using gcc. From C, we were able to create a faux object, and call a real objects functions against it. Also, remember the definition of our function here? Its private. All of that stuff is just for us, as people to help organize data. We could also create an inherited class from this one and it would still work, that is a compiler feature too.</span></div>
<br /><div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">Next up, language features we take for granted that are part of the c++ library itself. These cool hacks which show how data is organized work best on simple objects. When you get into more complex situations, things get bigger and more complicated. However, it all boils down to the simple things. You are calling a function with a hidden first parameter as a pointer to the objects “this” parameter for regular functions, and for static functions, you are literally just calling a function. The rest of object oriented methodology is simply determining </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: italic; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">what</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> function to call and the appropriate </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: italic; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">address</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> to pass to it. The bottom line, your class can be as complex as you want, but it will still only use up as much data as you have defined. A class with two ints in it will use up 8 bytes of memory whether it has 2 functions or 200.</span></div>
Chris Beneschhttp://www.blogger.com/profile/10552098270462277588noreply@blogger.com0tag:blogger.com,1999:blog-981120337687591219.post-2333855681262237802020-05-04T09:32:00.001-07:002020-05-04T09:32:40.784-07:00How to properly dispose of a hard drive<iframe allowfullscreen="" frameborder="0" height="270" src="https://www.youtube.com/embed/orm13uyKtu0" width="480"></iframe>Chris Beneschhttp://www.blogger.com/profile/10552098270462277588noreply@blogger.com0tag:blogger.com,1999:blog-981120337687591219.post-20145030123627140562020-03-21T10:21:00.001-07:002020-03-21T10:22:20.455-07:00Mac N Cheese Chicken CasseroleAs we all hunker down to let the Corona Virus passover us (spelling intentional), a lot of us are used to eating out, ordering pickup food, or otherwise and find our culinary skills are lacking. I figured I would share some very simple, but nutritious recipes when we want to eat in.<br />
<br />
Supplies needed:<br />
<br />
1 Box Kraft (or other brand) Mac N Cheese mix<br />
2 Large cans of canned chicken (10 oz per can)<br />
1/4 cup of butter (1/2 full stick)<br />
1/4 cup of shredded sharp cheddar cheese (optional, or about 1 good sized handful out of a bag) <br />
1 Can opener<br />
1 metal fork (for stirring) <br />
2 Microwave safe bowls, glass or hard plastic (tupperware wont do)<br />
2 sheets of full size paper towels connected<br />
<br />
Now I know the serving size on these makes it seem like you could feed a party out of it, but I'm a big boy and most people are too, so its basically 2-3 people.<br />
<br />
Est Time to prepare - 10 minutes<br />
<br />
Open chicken cans one at a time, by puncturing then on opposite sides of the can with the can opener and then drain water into the sink. If you have cats they will come running probably. Once drained open the rest of the way up.<br />
<br />
Dump both cans of chicken into microwave safe bowl #1, and wrap in paper towels (it will pop and try to blow out all over your microwave, this stops that and keeps the moisture in)<br />
<br />
Microwave for approx 2 minutes. Retain paper towels and set cooked chicken aside.<br />
<br />
Pour noodles from Mac N Cheese box into bowl #2, and cover noodles with water plus about 3/8 inch. Stir pretty well until water is cloudy.<br />
<br />
Microwave for 2:30 with paper towels saved from before underneath bowl. (it very likely will boil over and the towels will help avoid making your microwave wet)<br />
<br />
Stir noodles again<br />
<br />
Microwave an additional 2 - 3 minutes until boiling water subsides under rim of bowl.<br />
<br />
While noodles are being heated, take your 1/4 cup of butter and chop into pieces about 1/2 inch wide.<br />
<br />
Add cheese powder packet, chicken from earlier, chopped up butter and cheese into noodles and stir. Stir until butter pieces are completely melted and everything is an even yellow color. Dont worry about the little amount of water in the bowl, it will harden with the noodle coating and cheese poweder into a snot like consistency. Dont stir too hard or else the chicken pieces will fragment into little tiny pieces, you want to be semi gentle to get the large chicken pieces still preserved.<br />
<br />
You can also add spices such as pepper, or jalapeno slices. You can also give the cheese a nice mix with parmesan, or if you like it extra "stringy" a slice or two of meunster. There are a lot of add ons to make this better to suit your taste, but here are some things to avoid (tried and ewwwwwed):<br />
<br />
Garlic<br />
Seafood (shrimp, lobster, crab, etc..) instead of chicken<br />
Excessive salt, the butter has some, and so does the cheese powder<br />
Carrots<br />
CauliflowerChris Beneschhttp://www.blogger.com/profile/10552098270462277588noreply@blogger.com1tag:blogger.com,1999:blog-981120337687591219.post-53096659930761156102019-11-28T22:47:00.000-08:002019-11-29T13:21:05.084-08:00Disassembling C++ - Part 1 (Intro, overloads)<h2 style="text-align: center;">
Disassembling C++</h2>
<h3 style="text-align: left;">
Introduction</h3>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
In this next series of articles, I will delve into the inner workings of
the C++ and how it accomplishes its object oriented behavior. It was
after all, one of the first object oriented languages, and is one of the
most evolved. It was first released in 1985, but was actually designed
in 1979 by Bjarne Stroustrup as a next generation of the popular C
programming language developed by Bell Labs. But enough history, you
can look it up yourself on wikipedia or go to Bjarne's home page at
http://www.stroustrup.com . The purpose of this isnt to show you how to
program in C++, but more how it works. This is targeted for old timers
like me who have used C most of their career and have gone kicking and
screaming into object oriented land, or just the intellectually curious.
The first thing is that C++ is C under the hood. Its dressed up nicely
and does some fancy object oriented tricks, but when it starts
generating machine code, its more or less C.</div>
<div style="text-align: left;">
</div>
<h3 style="text-align: left;">
</h3>
<h3 style="text-align: left;">
Compile time or run time behavior</h3>
<div style="text-align: left;">
<br />
One of the biggest advantages to knowing how all of this works under the
hood is that you can implement more compile time features and less run
time ones. If accessing an object takes a standard library to sort
through a list of strings, it will be much slower than just taking a
pointer the compiler passed in. It is not all compile time magic, some
of it is handled in the low level libstdc++ library. I like everyone
else always assumed it was all just compiler tricks and never thought
about it. Then one day came as a hobbyist OS developer that I wanted to
explore the APCI tables, and C++ looked like an obvious choice. Thats
where I not only learned how a lot of this works under the hood, but
developed an appreciation for the tight integration of the library and
compiler.</div>
<div style="text-align: left;">
</div>
<h3 style="text-align: left;">
</h3>
<h3 style="text-align: left;">
Overloads</h3>
<div style="text-align: left;">
<br />
One very useful feature is overloading. You can use the same function name with different parameters. For example:</div>
<div style="text-align: left;">
</div>
<div style="text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;">int add(int n1,int n2)</span></div>
<div style="text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;">double add(double n1,double n2)</span></div>
<div style="text-align: left;">
</div>
<div style="text-align: left;">
In fact, you may have been bitten by it during compile time, especially
with math functions. The way that it accomplishes it is through a
process called mangling. The function is renamed according to its
parameters and is the one used instead. In the example two functions
above those are two distinct functions. This is a compile time feature
and will have no effect on run speed, so overload away. Lets show an
example, take the following simple program:</div>
<div style="text-align: left;">
</div>
<div style="text-align: left;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="text-align: left;">
</div>
<div style="text-align: left;">
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZlrWVpR2_oQx_qWUmo4kIq_vNt-UZUcgIvWs6AAk_IXksw28SVPVLvr_h0wgx1XfYmjlZEXaQcPoKi2TCu2W2kjf4ESV1MTysXCaGEBRQ6CWb03Nz6WoWQXEAUE8pBe7bDqPzEoXVx4o/s1600/pic1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="531" data-original-width="836" height="406" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZlrWVpR2_oQx_qWUmo4kIq_vNt-UZUcgIvWs6AAk_IXksw28SVPVLvr_h0wgx1XfYmjlZEXaQcPoKi2TCu2W2kjf4ESV1MTysXCaGEBRQ6CWb03Nz6WoWQXEAUE8pBe7bDqPzEoXVx4o/s640/pic1.png" width="640" /></a></div>
<br />
<br />
To ensure that the correct function gets called, it translates
return_value to two different function names based on the parameters.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEjz1yi9sY9ZzMJAbTdKpD4XfLufnYMvLuucWTbiRYcJC5G5XCP918hHRpM-qPYprVBQwShrIkJMFFUdtnXYf9EPJbc5qO1ah8ZA4trjI4ZxXkXUO7u6e1I9F00SUBT0JD3iUKsnr6ocA/s1600/pic2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="531" data-original-width="836" height="406" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEjz1yi9sY9ZzMJAbTdKpD4XfLufnYMvLuucWTbiRYcJC5G5XCP918hHRpM-qPYprVBQwShrIkJMFFUdtnXYf9EPJbc5qO1ah8ZA4trjI4ZxXkXUO7u6e1I9F00SUBT0JD3iUKsnr6ocA/s640/pic2.png" width="640" /></a></div>
<br /></div>
<div style="text-align: left;">
</div>
<div style="text-align: left;">
</div>
<div style="text-align: left;">
</div>
<div style="text-align: left;">
<br />
Now wait a minute, why is main() named exactly like it is named? That is
because it was declared as having "C" linkage. To define a function as
having C linkage, you just surround it with extern "C". For example:<br />
<br /></div>
<div style="text-align: left;">
</div>
<div style="text-align: left;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-D7J1oqW8n8RaHEWMR3GYLHczeWgMpt1i0E7zdAVg2DrcVzV755R_sfEaJ92df8ZFDDrz7dvu-6xeNXRZkrGeOhwlZSvGJ5cRJ0YlVxbM-DU5vgtSuQujAaOXI7uH_ENntb4qPcl5fHQ/s1600/pic3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="531" data-original-width="836" height="406" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-D7J1oqW8n8RaHEWMR3GYLHczeWgMpt1i0E7zdAVg2DrcVzV755R_sfEaJ92df8ZFDDrz7dvu-6xeNXRZkrGeOhwlZSvGJ5cRJ0YlVxbM-DU5vgtSuQujAaOXI7uH_ENntb4qPcl5fHQ/s640/pic3.png" width="640" /></a></div>
<br />
<br /></div>
<div style="text-align: left;">
Notice that I declared the function extern "C" in its <i>definition</i>
and not in the code of the function itself. That way the compiler knows
this function needs to be in the regular C namespace. And
consequently:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjI5w2bXsSjoQpJqXzgEMzi19YcazLrr9b3vIBIlAasW_AnC9KVRQTq6ndx31p-5KMKWUw0iFG7RUg08sB_hbX5e45VVThKscOFmioPg7wF-ZaSLMPHsGDiGNzPqp_8JmfQKulQ-BIF5Tg/s1600/pic4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="531" data-original-width="836" height="406" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjI5w2bXsSjoQpJqXzgEMzi19YcazLrr9b3vIBIlAasW_AnC9KVRQTq6ndx31p-5KMKWUw0iFG7RUg08sB_hbX5e45VVThKscOFmioPg7wF-ZaSLMPHsGDiGNzPqp_8JmfQKulQ-BIF5Tg/s640/pic4.png" width="640" /></a></div>
</div>
<div style="text-align: left;">
</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Mixed C and C++ namespaces can cause a lot of headaches. nm is a
wonderful tool to show you exactly what is going on in there. To
de-mangle the names, use the tool c++filt, or sometimes called cxxfilt
on older systems.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi89eUBKAhSb5VQJ7YNiOjxZAcccL_kEpYe4l4n96x2936dpFaxNRUFga2Z84LU1M8VY3MQHk6fzlaCqiHzVUsi7PH-nYtao_MUzm8i6TVspK-mju9_xUjjFfTSQQ4YfLusXRzr4PFKWRw/s1600/pic5.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="531" data-original-width="836" height="406" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi89eUBKAhSb5VQJ7YNiOjxZAcccL_kEpYe4l4n96x2936dpFaxNRUFga2Z84LU1M8VY3MQHk6fzlaCqiHzVUsi7PH-nYtao_MUzm8i6TVspK-mju9_xUjjFfTSQQ4YfLusXRzr4PFKWRw/s640/pic5.png" width="640" /></a></div>
</div>
<div style="text-align: left;">
</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
c++filt can take text either through standard input as shown above, or
as a command line argument. The man page can explain it all. Notice
how the return values are not shown? That is because you can only
overload a function based on the <i>parameters </i>and not the return value, evn though the mangled name includes the return value.<br />
</div>
<div style="text-align: left;">
</div>
<div style="text-align: left;">
Remember how earlier I said that its all C under the hood? Consider the following two files:<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEincuMh48YlKIFSzWj8lWMYbTNP7bGxO8Aw5KhM2pwdELJ3LKtoVlFm7NZlIcJYqJRdSOgm3IiSGxsqUNCwK_MEOpxc0wPOpGH9y7OUGzmqAGsaKiiVjAzdi4TcTtRJMRhoNCfEgTznzLg/s1600/pic6.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="531" data-original-width="836" height="406" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEincuMh48YlKIFSzWj8lWMYbTNP7bGxO8Aw5KhM2pwdELJ3LKtoVlFm7NZlIcJYqJRdSOgm3IiSGxsqUNCwK_MEOpxc0wPOpGH9y7OUGzmqAGsaKiiVjAzdi4TcTtRJMRhoNCfEgTznzLg/s640/pic6.png" width="640" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
Notice we defined the external C++ function under its mangled name to be called from main inside the C namespace. And sure enough:</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_JOZgWE_H0kBmszNNm3EIpO9wAPSqlSp_xIbLUuqYBkhbQEwAow9OaHETzQmltpT-itBVYD0O0m3ntBr-8CmqV4CdPZdmNmZ3XXdVjP1n1nfPJ7rYNavztpral1W-FSSGVvfw_h4jTyA/s1600/pic7.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="531" data-original-width="836" height="406" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_JOZgWE_H0kBmszNNm3EIpO9wAPSqlSp_xIbLUuqYBkhbQEwAow9OaHETzQmltpT-itBVYD0O0m3ntBr-8CmqV4CdPZdmNmZ3XXdVjP1n1nfPJ7rYNavztpral1W-FSSGVvfw_h4jTyA/s640/pic7.png" width="640" /></a></div>
<br />
You can call C++ code from within a purely C context if you know the overloaded function name. Obviously there are much better ways to do this, but for our purposes it illustrates an important point. Although you can think semantically that a function just operates two different ways, you are in fact calling two completely different and distinct functions. This concept applies to classes too. More to come in succeeding articles, we have barely scratched the surface.</div>
Chris Beneschhttp://www.blogger.com/profile/10552098270462277588noreply@blogger.com0tag:blogger.com,1999:blog-981120337687591219.post-72777913199532155242018-05-28T12:49:00.001-07:002018-05-28T12:49:16.240-07:00Tips For Creating C++ ClassesWhen creating new C++ classes, especially as a beginning programmer, you can end up with a mess that is barely more than a bunch of functions that you have to qualify with a class name. Before designing and writing a new class, keep the following ideals with OOP in mind.<br />
<br />
<ol>
<li> This new class should be able to be used by any other program. It should serve a useful purpose that someone besides yourself should find useful.</li>
<li> If your new class is going to be nothing but functions and hold very little or no data, consider a namespace instead.</li>
<li>If your new class has more than two static functions, consider moving them into a namespace as well.</li>
<li>If your application has more than two singleton objects, consolidate if possible.</li>
</ol>
<br />
Now when starting to code, start from the ground up.<br />
<ol>
<li>Start with the data. The variables that this class will hold. All of them *should* be private, unless there is a good reason not to for one or two. Unless there is a reason, do not use unsigned variables. -1 is awfully handy to indicate an error condition.</li>
<li>Constructors... How will people most commonly use your class? How can you make it as convenient as possible either inside your own application or someone else using it.</li>
<li>Declare the functions you want public to do things with the data other than get/set it.</li>
<li>Inside the header file write a set of generic get/set functions to access every member of data earlier declared private. The reasons are varied and complex, but trust me in the long run its worth it. Make them all quick little one line functions unless you need locking or something more complex. This frees up your source file for the meat of the class's logic.</li>
<li>And finally, the complex or working parts of the class, write in the source file.</li>
</ol>
Chris Beneschhttp://www.blogger.com/profile/10552098270462277588noreply@blogger.com3tag:blogger.com,1999:blog-981120337687591219.post-23217779602916447342018-01-21T15:56:00.000-08:002018-01-21T15:56:27.254-08:00Multithreaded SOAP server using QT and C++In todays world, one of the common things is to be able to serve SOAP requests. As an enterprise grows, the need for fast response times and optimizations at every possible place becomes more and more important. I wrote a generalized SOAP server in C++ using QT Creator and gsoap that connects to a database and runs in multiple threads with prepared statements. Its a little involved of a process, but I hope the ideas here can help others with similar problems.<br />
<br />
Environment: QT Creator, Postgres 9.4, gsoap 2.8 on Ubuntu. Very little if any is OS specific, other than that its made for *nix. So, FreeBSD and the like should work just fine too.<br />
<br />
By the end of this, you will have a SOAP server that connects to a database, that uses multiple threads which are already started and connected to the database, prepared cursors, and an easy path forward to develop new services on it. We will also add the ability to retrieve the WSDL from the service by accessing it via http://service/?wsdl . Otherwise known as a GET handler.<br />
<br />
<b><span style="font-size: large;">Step 1: Create the project in QT</span></b><br />
<b><span style="font-size: large;"><br /></span></b>
In QT Creator, create a new project, QT Console application. For this example, we will use gsoap to create C++ classes which are generated from a .h file. It should place the generated files in a subdirectory named soap. First off lets create the soap definition file. In this example, soapDef.h, which will be running in the "beer" namespace and named "beersoap". Yup, its for a brewing website backend. Make sure you have network, core, and sql included and add LIBS += -lgsoap++<br />
<br />
<div style="background-color: black; color: cyan;">
<pre><code>
//gsoap beer service name: beersoap
//gsoap beer service port: http://localhost:7575/
//gsoap beer service namespace: urn:beersoap
/**
* Simple ping operation to verify operation
*/
int beer__ping(void *,char *&pong);
/**
* Count user records, used mainly for testing DB connection
*/
int beer__usercount(void *_,int &numUsers);
</code>
</pre>
</div>
<span style="font-family: inherit; font-size: x-small;"><span style="background-color: white;">Secondly, to support WSDL retrieval, we can convert the wsdl file generated by gsoap into a C++ file with the text as a variable which we can reference by an extern. A little shell script magic will do that for us with the following file: wsdl2cpp.sh</span></span><br />
<div style="background-color: black; color: cyan;">
<pre><code>
#!/bin/sh
wsdlfile=${1}
cppfile=${2}
echo "const char *wsdlTxt = " > ${cppfile}
cat ${wsdlfile} | sed -e 's:":\\":g' \
-e 's:^:":' \
-e 's:$:\\n":' >> ${cppfile}
echo ";" >> ${cppfile}
</code>
</pre>
</div>
<div>
And finally tie it all together inside your .pro file<br />
<br />
<div style="background-color: black; color: cyan;">
<pre><code>
GSOAPFLAGS=-S -L -c++11 -x -i -d $${_PRO_FILE_PWD_}/soap
gsoap.depends = $${_PRO_FILE_PWD_}/soapDef.h
gsoap.target = $${_PRO_FILE_PWD_}/soap/soapC.cpp
gsoap.commands = \
soapcpp2 $${GSOAPFLAGS} $${_PRO_FILE_PWD_}/soapDef.h && \
$${_PRO_FILE_PWD_}/wsdl2cpp.sh \
$${_PRO_FILE_PWD_}/soap/beersoap.wsdl \
$${_PRO_FILE_PWD_}/wsdl.cpp
QMAKE_EXTRA_TARGETS += gsoap
PRE_TARGETDEPS += $${_PRO_FILE_PWD_}/soap/soapC.cpp
</code>
</pre>
</div>
Now, when we build, including the file wsdl.cpp will create a const char * named wsdlTxt that has the entire WSDL file in it. In our case with the GSOAPFLAGS we say server side code only, no library generation, use c++11, no xml files, create a C++ class for our methods, and the output directory is source/soap.<br />
<br />
<span style="font-size: large;"><b>Step 2: Create Database</b></span><br />
<span style="font-size: large;"><b><br /></b></span>
The next step is to create a database to test this out with. Create a users table, we are just going to have a method to count the records in there. And another table called prepstmts. Two fields, a char(32) or similar and a text fields. Once it connects, we will use it to create a collection of prepared statements. By far the longest running database operation is connecting and preparing a statement. We want to offload that to start up routines instead of during processing. Next, create a configuration file for the program to use. We will specify the location using QT's command line option parser framework.<br />
<br />
<br /></div>
<div style="background-color: black; color: cyan;">
<pre><code>
# Beersoap config file
dbhost = localhost
dbuser = beergineer
dbpass =
dbname = beergineer
tcplisten = 7575
threadpool = 5
</code></pre>
</div>
<div>
<br />
Ok.. now we have a config file, our makefile will handle the gsoap stuff for us automatically (remember to include all the .cpp and .h files it creates into your project, including wsdl.cpp), now lets do some coding and show how easy QT makes a lot of the mundane C++ tasks, and how relatively simple something as advanced as a thread pool can be written and managed.<br />
<br />
<span style="font-size: large;"><b>Step 3: main.cpp</b></span><br />
<span style="font-size: large;"><b><br /></b></span>
This is the application entry point. As any good programmer will tell you, your main function should do initialization, basic sanity checks, then launch the real application. QT makes things like parsing command line options in a nice, standard Unix like way really easy, especially with C++11. Here is my main.cpp file:<br />
<br /></div>
<div style="background-color: black; color: cyan;">
<pre><code>#include <QCoreApplication>
#include <iostream>
#include <QCommandLineParser>
#include "Config.h"
#include "Server.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QCoreApplication::setApplicationName("beersoapd");
QCoreApplication::setApplicationVersion("DEV");
QCommandLineParser parser;
parser.setApplicationDescription("Beergineer Soap Server");
parser.addHelpOption();
parser.addVersionOption();
parser.addOptions({
{{"c","configFile"},"Config File Location","configFile"}
});
parser.process(a);
Config *myConfig = new Config(parser.value(configFile));
if (!myConfig->isValid()) {
std::cerr << "Config invalid" << std::endl;
return 1;
}
new Server(&a,myConfig);
return a.exec();
}
</code></pre>
</div>
<br />
Dont worry about the Config and Server objects yet, we will create those in a minute. What this does, is calling it with a -h option shows the help, -v shows the version, and the config file we created earlier can be specified with either -c or --configFile. The output is formatted well and all the nastiness of getopt and the old school way is handled for you. The reason for a config file instead of a plethora of switches is extensibility, keeping commands reasonable in length, and for security. If you have everything as an option, any user on the machine can see things like passwords, etc.. if they are specified on the command line. The Config object is really simple and just reads a file, splits any lines that arent blank or start with # by the equals sign and stores it in a QMap, then provides an accessor method to get the values of it. Heres the header<br />
<br />
Config.h:
<br />
<div style="background-color: black; color: cyan;">
<pre><code>
#ifndef CONFIG_H
#define CONFIG_H
#include <QString>
#include <QMap>
class Config
{
public:
Config(QString confFile);
QString getSetting(QString name);
bool isValid() { return myValid; }
private:
QMap<qstring tring=""> config;
bool myValid;
};
#endif // CONFIG_H
</qstring></code></pre>
</div>
<br />
And Config.cpp<br />
<div style="background-color: black; color: cyan;">
<pre><code>#include "Config.h"
#include <QFile>
#include <iostream>
Config::Config(QString confFile)
{
config.clear();
myValid = false;
QFile f(confFile);
if (!f.exists()) {
std::cerr << "File does not exist: " << confFile.toStdString() << std::endl;
return;
}
f.open(QIODevice::ReadOnly);
while (!f.atEnd())
{
QString line = f.readLine();
if (line.trimmed().length() == 0 || line.startsWith("#")) continue;
int idx = line.indexOf("=");
QString key = line.left(idx-1).trimmed();
QString val = line.right(line.length()-idx-1).trimmed();
config[key] = val;
}
f.close();
QStringList reqattribs;
reqattribs << "dbhost" << "dbuser" << "dbpass" << "dbname" << "tcplisten" << "threadpool";
if (!config.size()) return;
for (int i =0; i < reqattribs.size(); i++)
{
if (config.find(reqattribs.at(i)) == config.end()) {
std::cerr << "Missing required value: " << reqattribs.at(i).toStdString() << std::endl;
return;
}
}
myValid = true;
}
QString Config::getSetting(QString name)
{
if (config.find(name) == config.end()) return "";
return config[name];
}
</code></pre>
</div>
<br />
As you can see, fairly simple, boiler plate code. Next we get into the actual networking part of it. Qt makes this exceptionally easy using the QTcpServer class. So without further adue, here is the Server.h class definition<br />
<div style="background-color: black; color: cyan;">
<pre><code>
#ifndef SERVER_H
#define SERVER_H
#include <QTcpServer>
#include <QVector>
#include "Config.h"
#include "SoapThread.h"
class Server : public QTcpServer
{
Q_OBJECT
public:
Server(QObject *parent,Config *confPtr);
private:
Config *myConfig;
void incomingConnection(qintptr handle);
QVector<SoapThread *> myThreads;
signals:
void newConnection();
};
#endif // SERVER_H
</code></pre>
</div>
<br />
A few items of note here. Using this class is very easy, you pretty much just tell it which port / address to use and the override of incomingConnection will pass in the new socket descriptor. The vector of SoapThread objects are all initialized on start up and in the next file I will show how we connect a connection to a thread. Here is the Server.cpp file:<br />
<div style="background-color: black; color: cyan;">
<pre><code>#include "Server.h"
#include <iostream>
#include "SoapThread.h"
Server::Server(QObject *parent,Config *confPtr)
{
setParent(parent);
listen(QHostAddress::Any,confPtr->getSetting("tcplisten").toUShort());
myConfig = confPtr;
int maxThreads = confPtr->getSetting("threadpool").toInt();
for (int i =0; i < maxThreads; i++)
{
SoapThread *p = new SoapThread(confPtr);
myThreads.append(p);
p->start();
p->moveToThread(p);
connect(this,SIGNAL(newConnection()),p,SLOT(serveRequest()));
}
}
void Server::incomingConnection(qintptr handle)
{
int maxThreads = myConfig->getSetting("threadpool").toInt();
int tries = 50000;
while (tries)
{
for (int i =0; i < maxThreads; i++)
{
SoapThread *p = myThreads[i];
if (!p->isBusy())
{
p->setPendingDescriptor(handle);
emit(newConnection());
return;
}
}
tries--;
sched_yield();
}
::close(handle);
}
</code></pre>
</div>
<br />
Theres a lot packed in here, this is the heart of the application that does the magic. First in the constructor, we set up the listening socket, which returns immediately, the actual listening happens when the QCoreApplication enters its event loop in the exec() call. Then we create the threads, each one is created, then started, then, very important because its not intuitive, move the object to itself. What happens is the event loop and signal / slot dispatchers will remain in the current thread until you actually move it after the thread is started. The single signal is connected to each thread. The thread that has the socket descriptor set will process the request. In real practice a minimal number of threads will be started and if all of them are busy, another one will be started then used to serve the request, then as they are used less and less and there is a sufficient timeout, the thread will clean itself up. For this purposes though, this is just an example.<br />
<br />
Now for the thread class itself.<br />
<div style="background-color: black; color: cyan;">
<pre><code>#ifndef SOAPTHREAD_H
#define SOAPTHREAD_H
#include "Config.h"
#include <QThread>
#include <QReadWriteLock>
#include <QSqlDatabase>
#include <QSqlQuery>
#include "soap/soapbeersoapService.h"
class SoapThread : public QThread
{
Q_OBJECT
public:
SoapThread(Config *cfgPtr);
void run();
bool isBusy();
void setPendingDescriptor(qintptr);
QSqlDatabase dbHandle;
QMap<QString,QSqlQuery> prepStmts;
public slots:
void serveRequest();
private:
Config *myConfig;
beersoapService *mySoap;
bool myBusy;
QReadWriteLock myLock;
qintptr myPendingDescriptor;
};
#endif // SOAPTHREAD_H
</code></pre>
</div>
A little bit to explain here. First, the code is in a threaded enivronment, so a lock is essential. Qt makes locking easy, but it must still be done with care. We also have not only a database handle, which must be unique per thread, but all the values in the prepstmts table (from step 2) will become prepared statements, stored by name in the prepStmts table.<br />
<br />
And the implementation:<br />
<div style="background-color: black; color: cyan;">
<pre><code>#include "SoapThread.h"
#include <QObject>
#include <QWriteLocker>
#include <QReadLocker>
#include "Server.h"
int get_handler(struct soap *s);
SoapThread::SoapThread(Config *cfgPtr)
{
myConfig = cfgPtr;
myBusy = true;
myPendingDescriptor = 0;
}
void SoapThread::run()
{
QWriteLocker lock(&myLock);
mySoap = new beersoapService();
mySoap->user = (void *)this;
mySoap->fget = get_handler;
dbHandle = QSqlDatabase::addDatabase("QPSQL",
QString("%1").arg((quintptr)this,QT_POINTER_SIZE *2,16,QChar('0')));
dbHandle.setDatabaseName(myConfig->getSetting("dbname"));
dbHandle.setUserName(myConfig->getSetting("dbuser"));
QString hostname = myConfig->getSetting("dbhost");
if (hostname.length() > 0 && hostname != "localhost")
dbHandle.setHostName(hostname);
dbHandle.open();
QSqlQuery pstmtq(dbHandle);
pstmtq.exec("SELECT * FROM prepstmts");
prepStmts.clear();
while (pstmtq.next())
{
QSqlQuery p(dbHandle);
p.prepare(pstmtq.value(1).toString().trimmed());
prepStmts.insert(pstmtq.value(0).toString().trimmed(),p);
}
pstmtq.finish();
myBusy = false;
lock.unlock();
exec();
}
void SoapThread::serveRequest()
{
if (!myPendingDescriptor || myBusy) return;
QWriteLocker lock(&myLock);
myBusy = true;
mySoap->socket = myPendingDescriptor;
myPendingDescriptor = 0;
lock.unlock();
mySoap->serve();
myBusy = false;
}
bool SoapThread::isBusy()
{
QReadLocker lock(&myLock);
return myBusy;
}
void SoapThread::setPendingDescriptor(qintptr d)
{
if (myBusy || myPendingDescriptor) return;
QWriteLocker lock(&myLock);
myPendingDescriptor = d;
}
</code></pre>
</div>
<br />
Ok, to begin with, the real magic doesnt happen in the constructor. At that point we are still running in the original thread, however, once start is called in the top object, run() is called here, but in a new thread that this object represents. In there is where we make the connection to the database, create the prepared statements. and set up everything. The setPendingDescriptor method sets the descriptor of the incoming request for this thread to execute momentarily. isBusy returns if it is still executing a request. All variables read or set from outside the thread need the locking around them, as shown above. There is also way more error checking / handing that must be done for the database access / prep statements but for the sake of brevity I just connected. When the Server object has an incoming connection, the serveRequest function actually gets called in every thread. But since only the first non-busy thread got it set, the other ones just return immediately. Also in the run method, the gsoap object is created here and the fget field is set to a static function shown next. This supports non SOAP calls to the server (remember http://address?wsdl). Yup thats coming up. We also use the user field to point to the thread object so we have access to the public properties of the prepared statements and connected database handle.<br />
<br />
The next and last file is the implementation of the soap methods themselves.
<br />
<div style="background-color: black; color: cyan;">
<pre><code>/*
* Core and standard functions for the beersoap service
*/
#include "soap/beersoap.nsmap"
#include <QString>
#include <QVariant>
#include "soap/soapbeersoapService.h"
#include "SoapThread.h"
extern const char *wsdlTxt;
/**
* \brief Used to determine soap service operation, simply returns the string "PONG!"
*
* \param [in] pong Pointer to return string
* \return status of soap operation in this case always OK
*/
static const char *pongStr = "PONG!";
int beersoapService::ping(void *_param_1, char *&pong)
{
(void)_param_1;
pong = (char *)pongStr;
return SOAP_OK;
}
/**
* \brief Handle GET requests
*/
int get_handler(struct soap *s)
{
if (!s) return SOAP_GET_METHOD;
QString url = s->path;
int idx = url.indexOf("?");
if (idx == -1) {
// handle generic get request here
return 404;
}
QString queryStr = url.right(url.length()-idx-1);
// WSDL Request
if (queryStr == "wsdl")
{
s->http_content = "text/xml";
soap_response(s,SOAP_FILE);
soap_send_raw(s,wsdlTxt,strlen(wsdlTxt));
soap_end_send(s);
return SOAP_OK;
}
return 404;
}
/**
* \brief Count the rows in the users table, demonstrates database access
*/
int beersoapService::usercount(void *_, int &numUsers)
{
(void)_; // unused
numUsers = -1;
SoapThread *sThread = (SoapThread *)user;
QSqlQuery q = sThread->prepStmts.find("countUsers").value();
q.exec();
q.isValid();
q.isSelect();
q.first();
numUsers = q.value(0).toInt();
q.finish();
return SOAP_OK;
}
</code></pre>
</div>
<br />
So, after all of that, once you get it to compile and run, you now have gsoap objects running in a multithreaded server. The database objects are already connected and ready to go before running a service function, so as you can see with only a few modifications can be made to run very quickly. Hopefully some of the explanations and code here can help someoneChris Beneschhttp://www.blogger.com/profile/10552098270462277588noreply@blogger.com4tag:blogger.com,1999:blog-981120337687591219.post-31957178691835908732018-01-21T15:53:00.002-08:002018-01-21T15:53:46.823-08:00FreeBSD 9.0 and up - How to set up an IPSec VPN in the real worldAfter a frustrating week trying to figure out how to do this, I finally got it, and rather belabor why I switched to OpenBSD years ago the first time and why I want to come back to FreeBSD, and am still not completely sure I made the right decision I will forego the political talk and move on to the technical details.<br />
<br />
Here is my setup. I work from a remote location. I have a whole /24 subnet here at home that I want all of the computers on it to be able to access multiple subnets at work as if I were in the building. I used to use VPN client software, aka Nortel, but that was very picky and dropped frequently. So a couple years ago, we realized the box we had been using also spoke IPSec, so we endeavored to set up a connection between the two. I got it working great on OpenBSD, but since there was multiple subnets, I would frequently lose one of them and have to "bounce" the whole IPSec thing. In fact it became so frequent (2-3 times a day) that I wrote a shell script and would just run it when I say that things had hanged. I got to where I was lucky enough to catch it before a terminal session dropped and I lost a bunch of typing I had done in a vi session. I heard FreeBSD is better, so I'm willing to try it. I spent all weekend making the switch and I will let you know about the performance later when I've had a chance to try it myself during a typical workday.<br />
<br />
Ok, so just to be clear. The end result is we are going to connect a subnet at a home network / small office with multiple subnets at a corporate office using IPSec and FreeBSD 9.0. Inserted that to help Google find it.<br />
<br />
<b><span style="font-size: large;">Step 1 - Rebuild the kernel with IPSec support.</span></b><br />
<br />
During install, make sure and select that you want the system source installed as well as the ports tree.<br />
<br />
We are going to rebuild a kernel on an x64 (amd64) system and call it ROUTER, you can adjust it to what you need, as if youre reading this that should make perfect sense, ie i386<br />
cd /usr/src/sys/amd64/conf<br />
cp GENERIC ROUTER<br />
vi ROUTER<br />
<br />
Inside the file change ident to ROUTER, and add the following lines somewhere<br />
<br />
option IPSEC<br />
option IPSEC_NAT_T<br />
device crypto<br />
<br />
Maybe take out some stuff you dont need, ie SCSI, RAID cards, whatever.<br />
<br />
then<br />
cd ../../..<br />
make buildkernel KERNCONF=ROUTER && make installkernel KERNCONF=ROUTER<br />
<br />
Go have a cup of coffee or watch a Star Trek episode, it will be busy for 30 mins or so. Once its done, reboot and you will be in your new kernel. In case you have came here after reading the handbook, the option IPSEC_NAT_T is to make an error go away that racoon throws if its not compiled in. It may not technically be needed, but it takes one lead off the table when trying to troubleshoot. The error is something like unable to set udp encap.<br />
<br />
<b><span style="font-size: large;">Step 2 - Install ipsec-tools from ports.</span></b><br />
<br />
<span style="font-size: large;"><span style="font-size: small;">The easiest way, assuming you already have basic networking up: pkg_add -r ipsec-tools</span></span><br />
<br />
<span style="font-size: large;"><span style="font-size: small;">Or cd /usr/ports/security/ipsec-tools then make install if you need special options. If you are reading this, you are probably fine with the defaults.</span></span><br />
<br />
<span style="font-size: large;"><span style="font-size: small;"><b><span style="font-size: large;">Step 3 - Initial networking setup</span></b></span></span><br />
<br />
<span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;">From here on out I will use something very similar to the network I actually set up. The first step is to set up one subnet, then we can add the other one(s) after its working.</span></span><b><span style="font-size: large;"> </span></b> </span></span><b><span style="font-size: large;"> </span></b>In case you came here from the handbook, there is good news in case you were thinking of having to acquire all of this information like how the box is set up internally, dont fear. <u><b>a gif0 tunnel is NOT required</b></u>.<br />
<br />
My setup is as follows, I am behind a regular DSL wireless router so that wireless devices and my TV dont have access to the corporate network, and my IP address is 192.168.0.2 . My internal subnet that I want to connect with work is 192.168.231.0/24. My internet facing address is 66.22.33.44. My network I am trying to connect to is 189.168.157.0/24 and the VPN server is 189.168.156.233. Later I will want to add the 189.168.158.0/24 network as well.<br />
<br />
Another point of interest, unlike OpenBSD, <i>a connection attempt that will use the tunnel must be made before it will start to negotiate a connection</i>. So, here is what I did at first and later migrated to the startup scripts.<br />
<br />
route add 189.168.157.0/24 192.168.231.1<br />
<br />
Save a file name pingit.sh with the following command in it (assuming you know that a pingable machine on the other side exists at octet 11). It will send a single ping to the box to start the tunnel, but not wait real long.<br />
<br />
ping -c 1 -W 1 189.168.157.11<br />
<br />
Save this file for later when we are ready to test...<br />
<br />
<span style="font-size: large;"><b>Step 4 - IPSec tools configuration</b></span><br />
<br />
<span style="font-size: large;"><span style="font-size: small;">I am using a pre-shared key in my setup, so the first file is <b>psk.txt</b>. It lives in <b>/usr/local/etc/racoon</b> as does the other files.</span></span><br />
<br />
<span style="font-size: large;"><span style="font-size: small;">It has the format: host key one per line, so in my case it would be</span></span><br />
<br />
<span style="font-size: large;"><span style="font-size: small;">189.168.156.233 </span><b> </b></span>Mysecretpa33word<br />
<br />
Wow, difficult, huh? I didnt pay attention and just put the key in there without the host and it took me 2 hours to figure it out. It also took me another hour to figure out that it needs <b>chmod 600 psk.txt</b>.<br />
<br />
The next file, although deceptively easy, is picky and doesnt give much in the way of informative error messages when things go wrong.<br />
<br />
<b>ipsec.conf</b> or <b>setkey.conf</b>, your choice I guess.<br />
<br />
flush;<br />
spdflush;<br />
spdadd 192.168.231.0/24 189.168.157.0/24 any -P out ipsec esp/tunnel/192.168.0.2-189.168.156.233/unique;<br />
spdadd 189.168.157.0/24 192.168.231.0/24 any -P in ipsec esp/tunnel/189.168.156.233-192.168.0.2/unique;<br />
<br />
Pretty self explanatory, but one note. If you have only one subnet to join, use "require" instead of "unique" on the end of the line.<br />
<br />
spdadd yoursubnet theirsubnet any -P out ipsec esp/tunnel/youraddr-theiraddr/unique;<br />
<br />
and reverse the addresses and use in instead of out.<br />
<br />
Now, if when testing you get errors about a send error, check this file. Its easy to write and deceptively easy and you can have an address transposed.<br />
<br />
Now on to the <b>racoon.conf</b> file:<br />
<br />
path pre_shared_key "/usr/local/etc/racoon/psk.txt"; #location of pre-shared key file<br />
log debug2; #log verbosity setting: set to 'notify' when testing and debugging is complete<br />
<br />
padding # options are not to be changed<br />
{<br />
maximum_length 20;<br />
randomize off;<br />
strict_check off;<br />
exclusive_tail off;<br />
}<br />
<br />
timer # timing options. change as needed<br />
{<br />
counter 5;<br />
interval 20 sec;<br />
persend 1;<br />
# natt_keepalive 15 sec;<br />
phase1 30 sec;<br />
phase2 15 sec;<br />
}<br />
<br />
listen # address [port] that racoon will listening on<br />
{<br />
isakmp <b>192.168.0.2 [500]</b>;<br />
}<br />
<br />
remote <b>189.168.156.233</b><br />
{<br />
exchange_mode main;<br />
my_identifier address<b> 66.22.33.44</b>;<br />
nat_traversal off;<br />
initial_contact on;<br />
# Phase 1<br />
<b> proposal {</b><br />
<b> encryption_algorithm 3des;<br /> hash_algorithm md5;<br /> authentication_method pre_shared_key;<br /> lifetime time 3600 sec;<br /> dh_group modp1024;<br /> }</b><br />
}<br />
<br />
<br />
#phase 2<br />
sainfo anonymous <br />
{<br />
<b> lifetime time 1200 sec;<br /> encryption_algorithm 3des;<br /> authentication_algorithm hmac_md5;<br /> compression_algorithm deflate;</b><br />
}<br />
<br />
Pay most attention to the bolded areas above.<br />
<br />
<b><span style="font-size: large;">Step 4 - Firing it up</span></b><br />
<br />
<span style="font-size: large;"><span style="font-size: small;">Set up 3 terminal sessions. 1 have a constant tcpdump -lnvvi <youroutsideinterface> udp port 500 running. Another to use the script from step 3. And the last one to run the VPN.</span></span><br />
<br />
<span style="font-size: large;"><span style="font-size: small;">First, only needs to be done once, or if it changes.</span></span><br />
<span style="font-size: large;"><span style="font-size: small;">setkey -f /usr/local/etc/ipsec.conf</span></span><br />
<br />
<span style="font-size: large;"><span style="font-size: small;">Then to fire it up:</span></span><br />
<span style="font-size: large;"><span style="font-size: small;">racoon -F</span></span><br />
<br />
<span style="font-size: large;"><span style="font-size: small;">That tells it to run in foreground mode outputting to stdout so you can see it.</span></span><br />
<span style="font-size: large;"><span style="font-size: small;">Assuming there are no errors and its stays running, </span></span><b><span style="font-size: large;"> </span></b>hit the script from step 3. ./pingit.sh<br />
<br />
It may take 3-4 times to successfully connect, unless you see a reason why not. But eventually, you will not only see pings come back, but a message like this:<br />
<br />
UPDATE succeeded: ESP/Tunnel 192.168.0.2[500]->189.168.156.233[500] spi=67865083(0x40b89fb)<br />
<br />
Here are some common other errors you may see and their solutions:<br />
<br />
If you get<br />
<br />
Jul 7 08:15:28 maricopacomputer racoon: DEBUG: 1 times of 100 bytes message will be sent to 189.168.156.233[500]<br />
Jul 7 08:15:28 maricopacomputer racoon: DEBUG: 2fed9acf 4dbbc616 00000000 00000000 01100200 00000000 00000064 0d000034 00000001 00000001 00000028 01010001 00000020 01010000 800b0001 800c0e10 80010005 80030001 80020001 80040002 00000014 afcad713 68a1f1c9 6b8696fc 77570100<br />
Jul 7 08:15:28 maricopacomputer racoon: DEBUG: resend phase1 packet 2fed9acf4dbbc616:0000000000000000<br />
<br />
And no reply, check that you arent routing traffic to the end host through your internal network.<br />
<br />
<br />
If you get:<br />
<br />
2012-07-07 00:16:02: ERROR: <b>phase1 negotiation failed due to send error. dad1f78e51bb5b7e:<wbr></wbr>0000000000000000</b><br />
2012-07-07 00:16:02: ERROR: failed to begin ipsec sa negotication.<br />
<br />
Check setkey.conf closely, especially that the addresses are not transposed or anything.<br />
<br />
<b><span style="font-size: large;">Step 5 - Adding other subnets</span></b><br />
<br />
<span style="font-size: large;"><span style="font-size: small;">Now that you have subnet 1 working, lets say you want to add a second subnet?</span></span><br />
<br />
<span style="font-size: large;"><span style="font-size: small;">That is very easy. First of all, add the new records in ipsec.conf, 2 for each. Then, notice how I had sainfo <b>anonymous</b> </span></span><b><span style="font-size: large;"> </span></b>in racoon.conf? Use that, it makes it so much easier than specifying individual subnets over again. In <b>ipsec.conf</b> change <b>require </b>to <b>unique</b>. Otherwise it will only be able to use one subnet at a time and will drop one and connect the other each time there is traffic for the other one.<br />
<br />
<b><span style="font-size: large;">Liability:</span></b><br />
<br />
<span style="font-size: large;"><span style="font-size: small;">This set up worked for me. It may not work for you. Hopefully it helps other sysadmins out there set up ipsec tunnels in FreeBSD 9.0. Happy VPN'ing!</span></span><br />
<br />
<span style="font-size: large;"><span style="font-size: small;"><b><span style="font-size: large;">Update:</span></b></span></span><br />
<br />
<span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;">After getting this set up over the weekend, today was a day with it in use for work. And let me say... nice. It seems more responsive and even a little quicker than OpenBSD. It may be a beyatch to set up, but it runs like a champ once it is.</span></span><b><span style="font-size: large;"> </span></b> </span></span><b><span style="font-size: large;"> </span></b>Chris Beneschhttp://www.blogger.com/profile/10552098270462277588noreply@blogger.com1tag:blogger.com,1999:blog-981120337687591219.post-63479382197682048402018-01-21T13:46:00.000-08:002018-01-21T15:07:56.149-08:00Qt5 Tic-Tac-ToeThis was a fairly simple tic-tac-toe game. The logic behind it is that it goes through every available move and sees which square will give the computer the highest chance of winning. If two or more squares are equal, it just selects one at random. To build this program, yu will need a C++ compiler and qt version 5 including qmake installed. The idea between using characters X and O or to do native drawing commands was a tough one, but I finally settled on native drawing to a pixmap that was pretty big and then applied them to the squares. The squares themselves are buttons, so that made the event system a lot easier to use.<br />
<br />
Get source from here: <a href="https://github.com/beneschtech/qt5-tictactoe">https://github.com/beneschtech/qt5-tictactoe</a>Chris Beneschhttp://www.blogger.com/profile/10552098270462277588noreply@blogger.com0tag:blogger.com,1999:blog-981120337687591219.post-23196585173900982262017-11-26T11:24:00.000-08:002017-11-26T11:31:11.897-08:00ELF LoaderWe all use slightly different environments to do OS development, mine is BOCHS on a *nix platform of some kind and creating a final ELF file to load into memory. ELF has a very adaptable format, and can pack multiple things into one file. For example, your main kernel up in high memory and a stub for BIOS calls down in low memory, all in one file. A lot of bootloader examples out there use the PHT (Program Header Table) to determine where to load code at. I however, use the SHT (Section header table). That allows you to insert a random binary into the final ELF using objcopy at an alternate address.<br />
<br />
For now, I am using the floppy model. Pretending a final executable is a 1.44M floppy and the bootloader, kernel loader and kernel itself are just appended on to each other on the disk image. The loader I am about to provide also has the added benefit of loading the file one sector at a time, no matter how large, so that it can take a kernel of arbitrary size and load it.<br />
<br />
It sets a generic page table allowing access to the first 16M of memory, loads 16 bit, 32 and 64 bit descriptors in the GDT, giving the final segment as 0x28 when it launches into 64 bit mode. It also enables the SSE registers, since a lot of the code made by my C++ compiler of choice (clang) has SSE registers in heavy use. Why not? Clear 128 bits of memory or more in one instruction vs 64. An able programmer should be able to adapt this to use on a hard drive and to make it a little more dynamic. This file is intended to be loaded by the MBR and executed at 0x6000<br />
<br />
<blockquote class="tr_bq">
<style type="text/css">
p, li { white-space: pre-wrap; }
</style>
<pre><!--StartFragment--><span style="color: #ff55ff;">%include</span><span style="color: silver;"> </span><span style="color: #ff55ff;">"loaderconstants.inc"</span></pre>
<pre>[<span style="background-color: black; color: #aaaaaa; font-family: 'Cantarell'; font-size: 12pt; font-weight: 600;">ORG</span><span style="color: silver;"> </span>0x<span style="color: #ff55ff;">6000</span>]</pre>
<pre>[BITS<span style="color: silver;"> </span><span style="color: #ff55ff;">16</span>]</pre>
<pre>
</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Store</span><span style="color: silver;"> </span><span style="color: #55ffff;">boot</span><span style="color: silver;"> </span><span style="color: #55ffff;">drive</span></pre>
<pre>MOV<span style="color: silver;"> </span>[bootDrive],DL</pre>
<pre>
</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Read</span><span style="color: silver;"> </span><span style="color: #55ffff;">first</span><span style="color: silver;"> </span><span style="color: #55ffff;">sector</span><span style="color: silver;"> </span><span style="color: #55ffff;">of</span><span style="color: silver;"> </span><span style="color: #55ffff;">ELF</span><span style="color: silver;"> </span><span style="color: #55ffff;">image</span><span style="color: silver;"> </span><span style="color: #55ffff;">and</span><span style="color: silver;"> </span><span style="color: #55ffff;">get</span><span style="color: silver;"> </span><span style="color: #55ffff;">needed</span><span style="color: silver;"> </span><span style="color: #55ffff;">data</span><span style="color: silver;"> </span><span style="color: #55ffff;">from</span><span style="color: silver;"> </span><span style="color: #55ffff;">it</span></pre>
<pre>XOR<span style="color: silver;"> </span>EAX,EAX</pre>
<pre>MOV<span style="color: silver;"> </span>AX,kernelLBAAddr</pre>
<pre>CALL<span style="color: silver;"> </span>ReadSector</pre>
<pre>
</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">First</span><span style="color: silver;"> </span><span style="color: #55ffff;">lets</span><span style="color: silver;"> </span><span style="color: #55ffff;">make</span><span style="color: silver;"> </span><span style="color: #55ffff;">sure</span><span style="color: silver;"> </span><span style="color: #55ffff;">its</span><span style="color: silver;"> </span><span style="color: #55ffff;">actually</span><span style="color: silver;"> </span><span style="color: #55ffff;">an</span><span style="color: silver;"> </span><span style="color: #55ffff;">ELF</span></pre>
<pre>MOV<span style="color: silver;"> </span>EAX,0x<span style="color: #ff55ff;">464</span>c<span style="color: #ff55ff;">457</span>f</pre>
<pre><span style="color: #ffff55; font-weight: 600;">CMP</span><span style="color: silver;"> </span>DWORD<span style="color: silver;"> </span>EAX,[0x<span style="color: #ff55ff;">7000</span>]</pre>
<pre>JNZ<span style="color: silver;"> </span>badELF</pre>
<pre><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Make</span><span style="color: silver;"> </span><span style="color: #55ffff;">sure</span><span style="color: silver;"> </span><span style="color: #55ffff;">its</span><span style="color: silver;"> </span><span style="color: #55ffff;">64</span><span style="color: silver;"> </span><span style="color: #55ffff;">bit</span><span style="color: silver;"> </span><span style="color: #55ffff;">little</span><span style="color: silver;"> </span><span style="color: #55ffff;">endian</span></pre>
<pre>MOV<span style="color: silver;"> </span>AX,0x0102</pre>
<pre><span style="color: #ffff55; font-weight: 600;">CMP</span><span style="color: silver;"> </span>WORD<span style="color: silver;"> </span>AX,[0x<span style="color: #ff55ff;">7004</span>]</pre>
<pre>JNZ<span style="color: silver;"> </span>badELF</pre>
<pre>
</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Enter</span><span style="color: silver;"> </span><span style="color: #55ffff;">unreal</span><span style="color: silver;"> </span><span style="color: #55ffff;">mode,</span><span style="color: silver;"> </span><span style="color: #55ffff;">must</span><span style="color: silver;"> </span><span style="color: #55ffff;">be</span><span style="color: silver;"> </span><span style="color: #55ffff;">done</span><span style="color: silver;"> </span><span style="color: #55ffff;">before</span><span style="color: silver;"> </span><span style="color: #55ffff;">using</span><span style="color: silver;"> </span><span style="color: #55ffff;">copyData</span><span style="color: silver;"> </span><span style="color: #55ffff;">function</span></pre>
<pre><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Lets</span><span style="color: silver;"> </span><span style="color: #55ffff;">assume</span><span style="color: silver;"> </span><span style="color: #55ffff;">we</span><span style="color: silver;"> </span><span style="color: #55ffff;">have</span><span style="color: silver;"> </span><span style="color: #55ffff;">a</span><span style="color: silver;"> </span><span style="color: #55ffff;">computer</span><span style="color: silver;"> </span><span style="color: #55ffff;">built</span><span style="color: silver;"> </span><span style="color: #55ffff;">after</span><span style="color: silver;"> </span><span style="color: #55ffff;">1997</span></pre>
<pre>MOV<span style="color: silver;"> </span>AX,0x<span style="color: #ff55ff;">2401</span></pre>
<pre>INT<span style="color: silver;"> </span>0x<span style="color: #ff55ff;">15</span></pre>
<pre>CLI</pre>
<pre>LGDT<span style="color: silver;"> </span>[GDTR]</pre>
<pre>MOV<span style="color: silver;"> </span>EAX,CR0</pre>
<pre>INC<span style="color: silver;"> </span>EAX</pre>
<pre>MOV<span style="color: silver;"> </span>CR0,EAX</pre>
<pre>MOV<span style="color: silver;"> </span>BX,0x<span style="color: #ff55ff;">20</span></pre>
<pre>MOV<span style="color: silver;"> </span>FS,BX</pre>
<pre>DEC<span style="color: silver;"> </span>EAX</pre>
<pre>MOV<span style="color: silver;"> </span>CR0,EAX</pre>
<pre>STI</pre>
<pre>
</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Now</span><span style="color: silver;"> </span><span style="color: #55ffff;">we</span><span style="color: silver;"> </span><span style="color: #55ffff;">should</span><span style="color: silver;"> </span><span style="color: #55ffff;">be</span><span style="color: silver;"> </span><span style="color: #55ffff;">in</span><span style="color: silver;"> </span><span style="color: #55ffff;">16</span><span style="color: silver;"> </span><span style="color: #55ffff;">bit</span><span style="color: silver;"> </span><span style="color: #55ffff;">REAL</span><span style="color: silver;"> </span><span style="color: #55ffff;">mode</span><span style="color: silver;"> </span><span style="color: #55ffff;">with</span><span style="color: silver;"> </span><span style="color: #55ffff;">access</span><span style="color: silver;"> </span><span style="color: #55ffff;">to</span><span style="color: silver;"> </span><span style="color: #55ffff;">the</span><span style="color: silver;"> </span><span style="color: #55ffff;">first</span><span style="color: silver;"> </span><span style="color: #55ffff;">4G</span><span style="color: silver;"> </span><span style="color: #55ffff;">of</span><span style="color: silver;"> </span><span style="color: #55ffff;">RAM</span><span style="color: silver;"> </span><span style="color: #55ffff;">through</span><span style="color: silver;"> </span><span style="color: #55ffff;">FS</span></pre>
<pre>MOV<span style="color: silver;"> </span>BX,[0x<span style="color: #ff55ff;">703</span>C]</pre>
<pre>MOV<span style="color: silver;"> </span>EAX,[0x<span style="color: #ff55ff;">7018</span>]</pre>
<pre>MOV<span style="color: silver;"> </span>[krnlEntry],EAX</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">GET</span><span style="color: silver;"> </span><span style="color: #55ffff;">SHT</span><span style="color: silver;"> </span><span style="color: #55ffff;">Address</span></pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">We</span><span style="color: silver;"> </span><span style="color: #55ffff;">use</span><span style="color: silver;"> </span><span style="color: #55ffff;">the</span><span style="color: silver;"> </span><span style="color: #55ffff;">Section</span><span style="color: silver;"> </span><span style="color: #55ffff;">Header</span><span style="color: silver;"> </span><span style="color: #55ffff;">instead</span><span style="color: silver;"> </span><span style="color: #55ffff;">of</span><span style="color: silver;"> </span><span style="color: #55ffff;">the</span><span style="color: silver;"> </span><span style="color: #55ffff;">PHT,</span><span style="color: silver;"> </span><span style="color: #55ffff;">so</span><span style="color: silver;"> </span><span style="color: #55ffff;">that</span><span style="color: silver;"> </span><span style="color: #55ffff;">we</span><span style="color: silver;"> </span><span style="color: #55ffff;">can</span><span style="color: silver;"> </span><span style="color: #55ffff;">have</span><span style="color: silver;"> </span><span style="color: #55ffff;">an</span><span style="color: silver;"> </span><span style="color: #55ffff;">extra</span><span style="color: silver;"> </span><span style="color: #55ffff;">section</span><span style="color: silver;"> </span><span style="color: #55ffff;">in</span><span style="color: silver;"> </span><span style="color: #55ffff;">a</span><span style="color: silver;"> </span><span style="color: #55ffff;">seperate</span><span style="color: silver;"> </span><span style="color: #55ffff;">location,</span><span style="color: silver;"> </span><span style="color: #55ffff;">ie</span><span style="color: silver;"> </span><span style="color: #55ffff;">an</span></pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">x86</span><span style="color: silver;"> </span><span style="color: #55ffff;">Real</span><span style="color: silver;"> </span><span style="color: #55ffff;">mode</span><span style="color: silver;"> </span><span style="color: #55ffff;">interrupt</span><span style="color: silver;"> </span><span style="color: #55ffff;">handler</span><span style="color: silver;"> </span><span style="color: #55ffff;">at</span><span style="color: silver;"> </span><span style="color: #55ffff;">0x5000</span><span style="color: silver;"> </span><span style="color: #55ffff;">while</span><span style="color: silver;"> </span><span style="color: #55ffff;">the</span><span style="color: silver;"> </span><span style="color: #55ffff;">kernel</span><span style="color: silver;"> </span><span style="color: #55ffff;">itself</span><span style="color: silver;"> </span><span style="color: #55ffff;">resides</span><span style="color: silver;"> </span><span style="color: #55ffff;">at</span><span style="color: silver;"> </span><span style="color: #55ffff;">1MB</span></pre>
<pre>MOV<span style="color: silver;"> </span>DWORD<span style="color: silver;"> </span>ESI,[0x<span style="color: #ff55ff;">7028</span>]</pre>
<pre><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Get</span><span style="color: silver;"> </span><span style="color: #55ffff;">sizeof(SHT)</span></pre>
<pre>MOV<span style="color: silver;"> </span>AX,[0x<span style="color: #ff55ff;">703</span>A]<span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Size</span><span style="color: silver;"> </span><span style="color: #55ffff;">of</span><span style="color: silver;"> </span><span style="color: #55ffff;">entry</span></pre>
<pre>MUL<span style="color: silver;"> </span>BX<span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">num</span><span style="color: silver;"> </span><span style="color: #55ffff;">entries</span></pre>
<pre>
</pre>
<pre>
</pre>
<pre>XOR<span style="color: silver;"> </span>ECX,ECX</pre>
<pre>MOV<span style="color: silver;"> </span>CX,AX</pre>
<pre>MOV<span style="color: silver;"> </span>EDI,0x<span style="color: #ff55ff;">8000</span></pre>
<pre>
</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Copy</span><span style="color: silver;"> </span><span style="color: #55ffff;">SHT</span><span style="color: silver;"> </span><span style="color: #55ffff;">to</span><span style="color: silver;"> </span><span style="color: #55ffff;">0x8000</span><span style="color: silver;"> </span><span style="color: #55ffff;">using</span><span style="color: silver;"> </span><span style="color: #55ffff;">our</span><span style="color: silver;"> </span><span style="color: #55ffff;">data</span><span style="color: silver;"> </span><span style="color: #55ffff;">read</span><span style="color: silver;"> </span><span style="color: #55ffff;">functions</span></pre>
<pre>CALL<span style="color: silver;"> </span>copyData</pre>
<pre>
</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Now</span><span style="color: silver;"> </span><span style="color: #55ffff;">that</span><span style="color: silver;"> </span><span style="color: #55ffff;">we</span><span style="color: silver;"> </span><span style="color: #55ffff;">have</span><span style="color: silver;"> </span><span style="color: #55ffff;">our</span><span style="color: silver;"> </span><span style="color: #55ffff;">sections</span><span style="color: silver;"> </span><span style="color: #55ffff;">in</span><span style="color: silver;"> </span><span style="color: #55ffff;">memory,</span><span style="color: silver;"> </span><span style="color: #55ffff;">lets</span><span style="color: silver;"> </span><span style="color: #55ffff;">go</span><span style="color: silver;"> </span><span style="color: #55ffff;">through</span><span style="color: silver;"> </span><span style="color: #55ffff;">them</span><span style="color: silver;"> </span><span style="color: #55ffff;">one</span><span style="color: silver;"> </span><span style="color: #55ffff;">by</span><span style="color: silver;"> </span><span style="color: #55ffff;">one</span><span style="color: silver;"> </span><span style="color: #55ffff;">and</span><span style="color: silver;"> </span><span style="color: #55ffff;">load</span><span style="color: silver;"> </span><span style="color: #55ffff;">them</span></pre>
<pre>XCHG<span style="color: silver;"> </span>BX,CX</pre>
<pre>MOV<span style="color: silver;"> </span>BX,0x<span style="color: #ff55ff;">8010</span></pre>
<pre>elfLoop:</pre>
<pre>PUSH<span style="color: silver;"> </span>CX</pre>
<pre>MOV<span style="color: silver;"> </span>EDI,[BX]<span style="color: silver;"> </span><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Destination</span><span style="color: silver;"> </span><span style="color: #55ffff;">address</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">ADD</span><span style="color: silver;"> </span>BX,<span style="color: #ff55ff;">8</span></pre>
<pre>MOV<span style="color: silver;"> </span>ESI,[BX]<span style="color: silver;"> </span><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Offset</span><span style="color: silver;"> </span><span style="color: #55ffff;">into</span><span style="color: silver;"> </span><span style="color: #55ffff;">file</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">ADD</span><span style="color: silver;"> </span>BX,<span style="color: #ff55ff;">8</span></pre>
<pre>MOV<span style="color: silver;"> </span>ECX,[BX]<span style="color: silver;"> </span><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Size</span></pre>
<pre>
</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">None</span><span style="color: silver;"> </span><span style="color: #55ffff;">of</span><span style="color: silver;"> </span><span style="color: #55ffff;">these</span><span style="color: silver;"> </span><span style="color: #55ffff;">can</span><span style="color: silver;"> </span><span style="color: #55ffff;">be</span><span style="color: silver;"> </span><span style="color: #55ffff;">zero</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">CMP</span><span style="color: silver;"> </span>EDI,DWORD<span style="color: silver;"> </span>0</pre>
<pre>JZ<span style="color: silver;"> </span>.elSkipSection</pre>
<pre><span style="color: #ffff55; font-weight: 600;">CMP</span><span style="color: silver;"> </span>ESI,DWORD<span style="color: silver;"> </span>0</pre>
<pre>JZ<span style="color: silver;"> </span>.elSkipSection</pre>
<pre><span style="color: #ffff55; font-weight: 600;">CMP</span><span style="color: silver;"> </span>ECX,DWORD<span style="color: silver;"> </span>0</pre>
<pre>JZ<span style="color: silver;"> </span>.elSkipSection</pre>
<pre><span style="color: #ffff55; font-weight: 600;">CMP</span><span style="color: silver;"> </span>DWORD<span style="color: silver;"> </span>[stackStart],0</pre>
<pre>JNZ<span style="color: silver;"> </span>.itsLoaded</pre>
<pre><span style="color: silver;"> </span>MOV<span style="color: silver;"> </span>DWORD<span style="color: silver;"> </span>[stackStart],EDI</pre>
<pre>.itsLoaded:</pre>
<pre>CALL<span style="color: silver;"> </span>copyData</pre>
<pre>MOV<span style="color: silver;"> </span>DWORD<span style="color: silver;"> </span>[mallocStart],EDI</pre>
<pre>.elSkipSection:</pre>
<pre><span style="color: #ffff55; font-weight: 600;">ADD</span><span style="color: silver;"> </span>BX,0x<span style="color: #ff55ff;">30</span></pre>
<pre>POP<span style="color: silver;"> </span>CX</pre>
<pre>LOOP<span style="color: silver;"> </span>elfLoop</pre>
<pre>
</pre>
<pre>
</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Create</span><span style="color: silver;"> </span><span style="color: #55ffff;">page</span><span style="color: silver;"> </span><span style="color: #55ffff;">tables,</span><span style="color: silver;"> </span><span style="color: #55ffff;">assume</span><span style="color: silver;"> </span><span style="color: #55ffff;">2MB</span><span style="color: silver;"> </span><span style="color: #55ffff;">pages</span><span style="color: silver;"> </span><span style="color: #55ffff;">are</span><span style="color: silver;"> </span><span style="color: #55ffff;">okay</span></pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Identity</span><span style="color: silver;"> </span><span style="color: #55ffff;">map</span><span style="color: silver;"> </span><span style="color: #55ffff;">the</span><span style="color: silver;"> </span><span style="color: #55ffff;">first</span><span style="color: silver;"> </span><span style="color: #55ffff;">16MB</span><span style="color: silver;"> </span><span style="color: #55ffff;">We</span><span style="color: silver;"> </span><span style="color: #55ffff;">can</span><span style="color: silver;"> </span><span style="color: #55ffff;">set</span><span style="color: silver;"> </span><span style="color: #55ffff;">the</span><span style="color: silver;"> </span><span style="color: #55ffff;">rest</span><span style="color: silver;"> </span><span style="color: #55ffff;">up</span><span style="color: silver;"> </span><span style="color: #55ffff;">inside</span><span style="color: silver;"> </span><span style="color: #55ffff;">the</span><span style="color: silver;"> </span><span style="color: #55ffff;">kernel,</span><span style="color: silver;"> </span><span style="color: #55ffff;">for</span><span style="color: silver;"> </span><span style="color: #55ffff;">now</span></pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">we</span><span style="color: silver;"> </span><span style="color: #55ffff;">know</span><span style="color: silver;"> </span><span style="color: #55ffff;">that</span><span style="color: silver;"> </span><span style="color: #55ffff;">we</span><span style="color: silver;"> </span><span style="color: #55ffff;">have</span><span style="color: silver;"> </span><span style="color: #55ffff;">at</span><span style="color: silver;"> </span><span style="color: #55ffff;">LEAST</span><span style="color: silver;"> </span><span style="color: #55ffff;">16M</span></pre>
<pre>MOV<span style="color: silver;"> </span>EDI,0x<span style="color: #ff55ff;">10000</span></pre>
<pre>MOV<span style="color: silver;"> </span>DWORD<span style="color: silver;"> </span>[FS:EDI],0x<span style="color: #ff55ff;">11003</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">ADD</span><span style="color: silver;"> </span>EDI,0x<span style="color: #ff55ff;">1000</span></pre>
<pre>MOV<span style="color: silver;"> </span>DWORD<span style="color: silver;"> </span>[FS:EDI],0x<span style="color: #ff55ff;">12003</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">ADD</span><span style="color: silver;"> </span>EDI,0x<span style="color: #ff55ff;">1000</span></pre>
<pre>MOV<span style="color: silver;"> </span>DWORD<span style="color: silver;"> </span>[FS:EDI],0x000083</pre>
<pre><span style="color: #ffff55; font-weight: 600;">ADD</span><span style="color: silver;"> </span>EDI,0x<span style="color: #ff55ff;">8</span></pre>
<pre>MOV<span style="color: silver;"> </span>DWORD<span style="color: silver;"> </span>[FS:EDI],0x<span style="color: #ff55ff;">200083</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">ADD</span><span style="color: silver;"> </span>EDI,0x<span style="color: #ff55ff;">8</span></pre>
<pre>MOV<span style="color: silver;"> </span>DWORD<span style="color: silver;"> </span>[FS:EDI],0x<span style="color: #ff55ff;">400083</span><span style="color: #55ffff;">;</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">ADD</span><span style="color: silver;"> </span>EDI,0x<span style="color: #ff55ff;">8</span></pre>
<pre>MOV<span style="color: silver;"> </span>DWORD<span style="color: silver;"> </span>[FS:EDI],0x<span style="color: #ff55ff;">600083</span><span style="color: #55ffff;">;</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">ADD</span><span style="color: silver;"> </span>EDI,0x<span style="color: #ff55ff;">8</span></pre>
<pre>MOV<span style="color: silver;"> </span>DWORD<span style="color: silver;"> </span>[FS:EDI],0x<span style="color: #ff55ff;">800083</span><span style="color: #55ffff;">;</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">ADD</span><span style="color: silver;"> </span>EDI,0x<span style="color: #ff55ff;">8</span></pre>
<pre>MOV<span style="color: silver;"> </span>DWORD<span style="color: silver;"> </span>[FS:EDI],0xa00083<span style="color: #55ffff;">;</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">ADD</span><span style="color: silver;"> </span>EDI,0x<span style="color: #ff55ff;">8</span></pre>
<pre>MOV<span style="color: silver;"> </span>DWORD<span style="color: silver;"> </span>[FS:EDI],0xc00083<span style="color: #55ffff;">;</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">ADD</span><span style="color: silver;"> </span>EDI,0x<span style="color: #ff55ff;">8</span></pre>
<pre>MOV<span style="color: silver;"> </span>DWORD<span style="color: silver;"> </span>[FS:EDI],0xe00083<span style="color: #55ffff;">;</span></pre>
<pre>
</pre>
<pre>
</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Get</span><span style="color: silver;"> </span><span style="color: #55ffff;">int</span><span style="color: silver;"> </span><span style="color: #55ffff;">15</span><span style="color: silver;"> </span><span style="color: #55ffff;">memory</span><span style="color: silver;"> </span><span style="color: #55ffff;">map</span><span style="color: silver;"> </span><span style="color: #55ffff;">and</span><span style="color: silver;"> </span><span style="color: #55ffff;">store</span><span style="color: silver;"> </span><span style="color: #55ffff;">the</span><span style="color: silver;"> </span><span style="color: #55ffff;">pmode</span><span style="color: silver;"> </span><span style="color: #55ffff;">idt</span><span style="color: silver;"> </span><span style="color: #55ffff;">in</span><span style="color: silver;"> </span><span style="color: #55ffff;">preperation</span><span style="color: silver;"> </span><span style="color: #55ffff;">for</span><span style="color: silver;"> </span><span style="color: #55ffff;">bios</span><span style="color: silver;"> </span><span style="color: #55ffff;">calls</span><span style="color: silver;"> </span><span style="color: #55ffff;">from</span><span style="color: silver;"> </span><span style="color: #55ffff;">the</span><span style="color: silver;"> </span><span style="color: #55ffff;">kernel</span></pre>
<pre>SIDT<span style="color: silver;"> </span>[0x<span style="color: #ff55ff;">7000</span>]</pre>
<pre>
</pre>
<pre><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">use</span><span style="color: silver;"> </span><span style="color: #55ffff;">the</span><span style="color: silver;"> </span><span style="color: #55ffff;">INT</span><span style="color: silver;"> </span><span style="color: #55ffff;">0x15,</span><span style="color: silver;"> </span><span style="color: #55ffff;">eax=</span><span style="color: silver;"> </span><span style="color: #55ffff;">0xE820</span><span style="color: silver;"> </span><span style="color: #55ffff;">BIOS</span><span style="color: silver;"> </span><span style="color: #55ffff;">function</span><span style="color: silver;"> </span><span style="color: #55ffff;">to</span><span style="color: silver;"> </span><span style="color: #55ffff;">get</span><span style="color: silver;"> </span><span style="color: #55ffff;">a</span><span style="color: silver;"> </span><span style="color: #55ffff;">memory</span><span style="color: silver;"> </span><span style="color: #55ffff;">map</span></pre>
<pre><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">inputs:</span><span style="color: silver;"> </span><span style="color: #55ffff;">es:di</span><span style="color: silver;"> </span><span style="color: #55ffff;">-></span><span style="color: silver;"> </span><span style="color: #55ffff;">destination</span><span style="color: silver;"> </span><span style="color: #55ffff;">buffer</span><span style="color: silver;"> </span><span style="color: #55ffff;">for</span><span style="color: silver;"> </span><span style="color: #55ffff;">24</span><span style="color: silver;"> </span><span style="color: #55ffff;">byte</span><span style="color: silver;"> </span><span style="color: #55ffff;">entries</span></pre>
<pre><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">outputs:</span><span style="color: silver;"> </span><span style="color: #55ffff;">bp</span><span style="color: silver;"> </span><span style="color: #55ffff;">=</span><span style="color: silver;"> </span><span style="color: #55ffff;">entry</span><span style="color: silver;"> </span><span style="color: #55ffff;">count,</span><span style="color: silver;"> </span><span style="color: #55ffff;">trashes</span><span style="color: silver;"> </span><span style="color: #55ffff;">all</span><span style="color: silver;"> </span><span style="color: #55ffff;">registers</span><span style="color: silver;"> </span><span style="color: #55ffff;">except</span><span style="color: silver;"> </span><span style="color: #55ffff;">esi</span></pre>
<pre>MOV<span style="color: silver;"> </span>DI,0x<span style="color: #ff55ff;">7012</span></pre>
<pre>xor<span style="color: silver;"> </span>ebx,<span style="color: silver;"> </span>ebx<span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">ebx</span><span style="color: silver;"> </span><span style="color: #55ffff;">must</span><span style="color: silver;"> </span><span style="color: #55ffff;">be</span><span style="color: silver;"> </span><span style="color: #55ffff;">0</span><span style="color: silver;"> </span><span style="color: #55ffff;">to</span><span style="color: silver;"> </span><span style="color: #55ffff;">start</span></pre>
<pre>xor<span style="color: silver;"> </span>bp,<span style="color: silver;"> </span>bp<span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">keep</span><span style="color: silver;"> </span><span style="color: #55ffff;">an</span><span style="color: silver;"> </span><span style="color: #55ffff;">entry</span><span style="color: silver;"> </span><span style="color: #55ffff;">count</span><span style="color: silver;"> </span><span style="color: #55ffff;">in</span><span style="color: silver;"> </span><span style="color: #55ffff;">bp</span></pre>
<pre>mov<span style="color: silver;"> </span>edx,<span style="color: silver;"> </span>0x0534D<span style="color: #ff55ff;">4150</span><span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Place</span><span style="color: silver;"> </span><span style="color: #55ffff;">"SMAP"</span><span style="color: silver;"> </span><span style="color: #55ffff;">into</span><span style="color: silver;"> </span><span style="color: #55ffff;">edx</span></pre>
<pre>mov<span style="color: silver;"> </span>eax,<span style="color: silver;"> </span>0xe<span style="color: #ff55ff;">820</span></pre>
<pre>mov<span style="color: silver;"> </span>[es:di<span style="color: silver;"> </span>+<span style="color: silver;"> </span><span style="color: #ff55ff;">20</span>],<span style="color: silver;"> </span>dword<span style="color: silver;"> </span><span style="color: #ff55ff;">1</span><span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">force</span><span style="color: silver;"> </span><span style="color: #55ffff;">a</span><span style="color: silver;"> </span><span style="color: #55ffff;">valid</span><span style="color: silver;"> </span><span style="color: #55ffff;">ACPI</span><span style="color: silver;"> </span><span style="color: #55ffff;">3.X</span><span style="color: silver;"> </span><span style="color: #55ffff;">entry</span></pre>
<pre>mov<span style="color: silver;"> </span>ecx,<span style="color: silver;"> </span><span style="color: #ff55ff;">24</span><span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">ask</span><span style="color: silver;"> </span><span style="color: #55ffff;">for</span><span style="color: silver;"> </span><span style="color: #55ffff;">24</span><span style="color: silver;"> </span><span style="color: #55ffff;">bytes</span></pre>
<pre>int<span style="color: silver;"> </span>0x<span style="color: #ff55ff;">15</span></pre>
<pre>jc<span style="color: silver;"> </span>short<span style="color: silver;"> </span>.failed<span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">carry</span><span style="color: silver;"> </span><span style="color: #55ffff;">set</span><span style="color: silver;"> </span><span style="color: #55ffff;">on</span><span style="color: silver;"> </span><span style="color: #55ffff;">first</span><span style="color: silver;"> </span><span style="color: #55ffff;">call</span><span style="color: silver;"> </span><span style="color: #55ffff;">means</span><span style="color: silver;"> </span><span style="color: #55ffff;">"unsupported</span><span style="color: silver;"> </span><span style="color: #55ffff;">function"</span></pre>
<pre>mov<span style="color: silver;"> </span>edx,<span style="color: silver;"> </span>0x0534D<span style="color: #ff55ff;">4150</span><span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Some</span><span style="color: silver;"> </span><span style="color: #55ffff;">BIOSes</span><span style="color: silver;"> </span><span style="color: #55ffff;">apparently</span><span style="color: silver;"> </span><span style="color: #55ffff;">trash</span><span style="color: silver;"> </span><span style="color: #55ffff;">this</span><span style="color: silver;"> </span><span style="color: #55ffff;">register?</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">cmp</span><span style="color: silver;"> </span>eax,<span style="color: silver;"> </span>edx<span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">on</span><span style="color: silver;"> </span><span style="color: #55ffff;">success,</span><span style="color: silver;"> </span><span style="color: #55ffff;">eax</span><span style="color: silver;"> </span><span style="color: #55ffff;">must</span><span style="color: silver;"> </span><span style="color: #55ffff;">have</span><span style="color: silver;"> </span><span style="color: #55ffff;">been</span><span style="color: silver;"> </span><span style="color: #55ffff;">reset</span><span style="color: silver;"> </span><span style="color: #55ffff;">to</span><span style="color: silver;"> </span><span style="color: #55ffff;">"SMAP"</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">jne</span><span style="color: silver;"> </span>short<span style="color: silver;"> </span>.failed</pre>
<pre>test<span style="color: silver;"> </span>ebx,<span style="color: silver;"> </span>ebx<span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">ebx</span><span style="color: silver;"> </span><span style="color: #55ffff;">=</span><span style="color: silver;"> </span><span style="color: #55ffff;">0</span><span style="color: silver;"> </span><span style="color: #55ffff;">implies</span><span style="color: silver;"> </span><span style="color: #55ffff;">list</span><span style="color: silver;"> </span><span style="color: #55ffff;">is</span><span style="color: silver;"> </span><span style="color: #55ffff;">only</span><span style="color: silver;"> </span><span style="color: #55ffff;">1</span><span style="color: silver;"> </span><span style="color: #55ffff;">entry</span><span style="color: silver;"> </span><span style="color: #55ffff;">long</span><span style="color: silver;"> </span><span style="color: #55ffff;">(worthless)</span></pre>
<pre>je<span style="color: silver;"> </span>short<span style="color: silver;"> </span>.failed</pre>
<pre><span style="color: #ffff55; font-weight: 600;">jmp</span><span style="color: silver;"> </span>short<span style="color: silver;"> </span>.jmpin</pre>
<pre>.e<span style="color: #ff55ff;">820</span>l<span style="background-color: black; color: #aaaaaa; font-family: 'Cantarell'; font-size: 12pt; font-weight: 600;">p:</span></pre>
<pre>mov<span style="color: silver;"> </span>eax,<span style="color: silver;"> </span>0xe<span style="color: #ff55ff;">820</span><span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">eax,</span><span style="color: silver;"> </span><span style="color: #55ffff;">ecx</span><span style="color: silver;"> </span><span style="color: #55ffff;">get</span><span style="color: silver;"> </span><span style="color: #55ffff;">trashed</span><span style="color: silver;"> </span><span style="color: #55ffff;">on</span><span style="color: silver;"> </span><span style="color: #55ffff;">every</span><span style="color: silver;"> </span><span style="color: #55ffff;">int</span><span style="color: silver;"> </span><span style="color: #55ffff;">0x15</span><span style="color: silver;"> </span><span style="color: #55ffff;">call</span></pre>
<pre>mov<span style="color: silver;"> </span>[es:di<span style="color: silver;"> </span>+<span style="color: silver;"> </span><span style="color: #ff55ff;">20</span>],<span style="color: silver;"> </span>dword<span style="color: silver;"> </span><span style="color: #ff55ff;">1</span><span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">force</span><span style="color: silver;"> </span><span style="color: #55ffff;">a</span><span style="color: silver;"> </span><span style="color: #55ffff;">valid</span><span style="color: silver;"> </span><span style="color: #55ffff;">ACPI</span><span style="color: silver;"> </span><span style="color: #55ffff;">3.X</span><span style="color: silver;"> </span><span style="color: #55ffff;">entry</span></pre>
<pre>mov<span style="color: silver;"> </span>ecx,<span style="color: silver;"> </span><span style="color: #ff55ff;">24</span><span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">ask</span><span style="color: silver;"> </span><span style="color: #55ffff;">for</span><span style="color: silver;"> </span><span style="color: #55ffff;">24</span><span style="color: silver;"> </span><span style="color: #55ffff;">bytes</span><span style="color: silver;"> </span><span style="color: #55ffff;">again</span></pre>
<pre>int<span style="color: silver;"> </span>0x<span style="color: #ff55ff;">15</span></pre>
<pre>jc<span style="color: silver;"> </span>short<span style="color: silver;"> </span>.e<span style="color: #ff55ff;">820</span>f<span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">carry</span><span style="color: silver;"> </span><span style="color: #55ffff;">set</span><span style="color: silver;"> </span><span style="color: #55ffff;">means</span><span style="color: silver;"> </span><span style="color: #55ffff;">"end</span><span style="color: silver;"> </span><span style="color: #55ffff;">of</span><span style="color: silver;"> </span><span style="color: #55ffff;">list</span><span style="color: silver;"> </span><span style="color: #55ffff;">already</span><span style="color: silver;"> </span><span style="color: #55ffff;">reached"</span></pre>
<pre>mov<span style="color: silver;"> </span>edx,<span style="color: silver;"> </span>0x0534D<span style="color: #ff55ff;">4150</span><span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">repair</span><span style="color: silver;"> </span><span style="color: #55ffff;">potentially</span><span style="color: silver;"> </span><span style="color: #55ffff;">trashed</span><span style="color: silver;"> </span><span style="color: #55ffff;">register</span></pre>
<pre>.jmpin:</pre>
<pre>jcxz<span style="color: silver;"> </span>.skipent<span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">skip</span><span style="color: silver;"> </span><span style="color: #55ffff;">any</span><span style="color: silver;"> </span><span style="color: #55ffff;">0</span><span style="color: silver;"> </span><span style="color: #55ffff;">length</span><span style="color: silver;"> </span><span style="color: #55ffff;">entries</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">cmp</span><span style="color: silver;"> </span>cl,<span style="color: silver;"> </span><span style="color: #ff55ff;">20</span><span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">got</span><span style="color: silver;"> </span><span style="color: #55ffff;">a</span><span style="color: silver;"> </span><span style="color: #55ffff;">24</span><span style="color: silver;"> </span><span style="color: #55ffff;">byte</span><span style="color: silver;"> </span><span style="color: #55ffff;">ACPI</span><span style="color: silver;"> </span><span style="color: #55ffff;">3.X</span><span style="color: silver;"> </span><span style="color: #55ffff;">response?</span></pre>
<pre>jbe<span style="color: silver;"> </span>short<span style="color: silver;"> </span>.notext</pre>
<pre>test<span style="color: silver;"> </span>byte<span style="color: silver;"> </span>[es:di<span style="color: silver;"> </span>+<span style="color: silver;"> </span><span style="color: #ff55ff;">20</span>],<span style="color: silver;"> </span><span style="color: #ff55ff;">1</span><span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">if</span><span style="color: silver;"> </span><span style="color: #55ffff;">so:</span><span style="color: silver;"> </span><span style="color: #55ffff;">is</span><span style="color: silver;"> </span><span style="color: #55ffff;">the</span><span style="color: silver;"> </span><span style="color: #55ffff;">"ignore</span><span style="color: silver;"> </span><span style="color: #55ffff;">this</span><span style="color: silver;"> </span><span style="color: #55ffff;">data"</span><span style="color: silver;"> </span><span style="color: #55ffff;">bit</span><span style="color: silver;"> </span><span style="color: #55ffff;">clear?</span></pre>
<pre>je<span style="color: silver;"> </span>short<span style="color: silver;"> </span>.skipent</pre>
<pre>.notext:</pre>
<pre>mov<span style="color: silver;"> </span>ecx,<span style="color: silver;"> </span>[es:di<span style="color: silver;"> </span>+<span style="color: silver;"> </span><span style="color: #ff55ff;">8</span>]<span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">get</span><span style="color: silver;"> </span><span style="color: #55ffff;">lower</span><span style="color: silver;"> </span><span style="color: #55ffff;">dword</span><span style="color: silver;"> </span><span style="color: #55ffff;">of</span><span style="color: silver;"> </span><span style="color: #55ffff;">memory</span><span style="color: silver;"> </span><span style="color: #55ffff;">region</span><span style="color: silver;"> </span><span style="color: #55ffff;">length</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">or</span><span style="color: silver;"> </span>ecx,<span style="color: silver;"> </span>[es:di<span style="color: silver;"> </span>+<span style="color: silver;"> </span><span style="color: #ff55ff;">12</span>]<span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">"or"</span><span style="color: silver;"> </span><span style="color: #55ffff;">it</span><span style="color: silver;"> </span><span style="color: #55ffff;">with</span><span style="color: silver;"> </span><span style="color: #55ffff;">upper</span><span style="color: silver;"> </span><span style="color: #55ffff;">dword</span><span style="color: silver;"> </span><span style="color: #55ffff;">to</span><span style="color: silver;"> </span><span style="color: #55ffff;">test</span><span style="color: silver;"> </span><span style="color: #55ffff;">for</span><span style="color: silver;"> </span><span style="color: #55ffff;">zero</span></pre>
<pre>jz<span style="color: silver;"> </span>.skipent<span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">if</span><span style="color: silver;"> </span><span style="color: #55ffff;">length</span><span style="color: silver;"> </span><span style="color: #55ffff;">qword</span><span style="color: silver;"> </span><span style="color: #55ffff;">is</span><span style="color: silver;"> </span><span style="color: #55ffff;">0,</span><span style="color: silver;"> </span><span style="color: #55ffff;">skip</span><span style="color: silver;"> </span><span style="color: #55ffff;">entry</span></pre>
<pre>inc<span style="color: silver;"> </span>bp<span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">got</span><span style="color: silver;"> </span><span style="color: #55ffff;">a</span><span style="color: silver;"> </span><span style="color: #55ffff;">good</span><span style="color: silver;"> </span><span style="color: #55ffff;">entry:</span><span style="color: silver;"> </span><span style="color: #55ffff;">++count,</span><span style="color: silver;"> </span><span style="color: #55ffff;">move</span><span style="color: silver;"> </span><span style="color: #55ffff;">to</span><span style="color: silver;"> </span><span style="color: #55ffff;">next</span><span style="color: silver;"> </span><span style="color: #55ffff;">storage</span><span style="color: silver;"> </span><span style="color: #55ffff;">spot</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">add</span><span style="color: silver;"> </span>di,<span style="color: silver;"> </span><span style="color: #ff55ff;">24</span></pre>
<pre>.skipent:</pre>
<pre>test<span style="color: silver;"> </span>ebx,<span style="color: silver;"> </span>ebx<span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">if</span><span style="color: silver;"> </span><span style="color: #55ffff;">ebx</span><span style="color: silver;"> </span><span style="color: #55ffff;">resets</span><span style="color: silver;"> </span><span style="color: #55ffff;">to</span><span style="color: silver;"> </span><span style="color: #55ffff;">0,</span><span style="color: silver;"> </span><span style="color: #55ffff;">list</span><span style="color: silver;"> </span><span style="color: #55ffff;">is</span><span style="color: silver;"> </span><span style="color: #55ffff;">complete</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">jne</span><span style="color: silver;"> </span>short<span style="color: silver;"> </span>.e<span style="color: #ff55ff;">820</span>lp</pre>
<pre>.e<span style="color: #ff55ff;">820</span>f:</pre>
<pre>mov<span style="color: silver;"> </span>[0x<span style="color: #ff55ff;">7010</span>],<span style="color: silver;"> </span>bp<span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">store</span><span style="color: silver;"> </span><span style="color: #55ffff;">the</span><span style="color: silver;"> </span><span style="color: #55ffff;">entry</span><span style="color: silver;"> </span><span style="color: #55ffff;">count</span></pre>
<pre>clc<span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">there</span><span style="color: silver;"> </span><span style="color: #55ffff;">is</span><span style="color: silver;"> </span><span style="color: #55ffff;">"jc"</span><span style="color: silver;"> </span><span style="color: #55ffff;">on</span><span style="color: silver;"> </span><span style="color: #55ffff;">end</span><span style="color: silver;"> </span><span style="color: #55ffff;">of</span><span style="color: silver;"> </span><span style="color: #55ffff;">list</span><span style="color: silver;"> </span><span style="color: #55ffff;">to</span><span style="color: silver;"> </span><span style="color: #55ffff;">this</span><span style="color: silver;"> </span><span style="color: #55ffff;">point,</span><span style="color: silver;"> </span><span style="color: #55ffff;">so</span><span style="color: silver;"> </span><span style="color: #55ffff;">the</span><span style="color: silver;"> </span><span style="color: #55ffff;">carry</span><span style="color: silver;"> </span><span style="color: #55ffff;">must</span><span style="color: silver;"> </span><span style="color: #55ffff;">be</span><span style="color: silver;"> </span><span style="color: #55ffff;">cleared</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">JMP</span><span style="color: silver;"> </span>LetsGo</pre>
<pre>.failed:</pre>
<pre>stc<span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">"function</span><span style="color: silver;"> </span><span style="color: #55ffff;">unsupported"</span><span style="color: silver;"> </span><span style="color: #55ffff;">error</span><span style="color: silver;"> </span><span style="color: #55ffff;">exit</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">JMP</span><span style="color: silver;"> </span>LetsGo</pre>
<pre>
</pre>
<pre>LetsGo:</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Lets</span><span style="color: silver;"> </span><span style="color: #55ffff;">jump</span><span style="color: silver;"> </span><span style="color: #55ffff;">from</span><span style="color: silver;"> </span><span style="color: #55ffff;">16</span><span style="color: silver;"> </span><span style="color: #55ffff;">bit</span><span style="color: silver;"> </span><span style="color: #55ffff;">to</span><span style="color: silver;"> </span><span style="color: #55ffff;">32</span><span style="color: silver;"> </span><span style="color: #55ffff;">to</span><span style="color: silver;"> </span><span style="color: #55ffff;">64</span><span style="color: silver;"> </span><span style="color: #55ffff;">then</span><span style="color: silver;"> </span><span style="color: #55ffff;">to</span><span style="color: silver;"> </span><span style="color: #55ffff;">the</span><span style="color: silver;"> </span><span style="color: #55ffff;">kernel</span></pre>
<pre>CLI<span style="color: silver;"> </span><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Goodbye</span><span style="color: silver;"> </span><span style="color: #55ffff;">interrupts</span><span style="color: silver;"> </span><span style="color: #55ffff;">until</span><span style="color: silver;"> </span><span style="color: #55ffff;">we</span><span style="color: silver;"> </span><span style="color: #55ffff;">are</span><span style="color: silver;"> </span><span style="color: #55ffff;">in</span><span style="color: silver;"> </span><span style="color: #55ffff;">C++</span><span style="color: silver;"> </span><span style="color: #55ffff;">code</span></pre>
<pre>MOV<span style="color: silver;"> </span>EAX,CR0</pre>
<pre>INC<span style="color: silver;"> </span>EAX</pre>
<pre>MOV<span style="color: silver;"> </span>CR0,EAX</pre>
<pre><span style="color: #ffff55; font-weight: 600;">JMP</span><span style="color: silver;"> </span>0x<span style="color: #ff55ff;">18</span>:mode<span style="color: #ff55ff;">32</span></pre>
<pre>mode32:</pre>
<pre>[BITS<span style="color: silver;"> </span><span style="color: #ff55ff;">32</span>]</pre>
<pre>MOV<span style="color: silver;"> </span>AX,0x<span style="color: #ff55ff;">20</span></pre>
<pre>MOV<span style="color: silver;"> </span><span style="background-color: black; color: #aaaaaa; font-family: 'Cantarell'; font-size: 12pt; font-weight: 600;">DS</span>,AX</pre>
<pre>MOV<span style="color: silver;"> </span>DX,0x<span style="color: #ff55ff;">3</span>F<span style="color: #ff55ff;">2</span><span style="color: silver;"> </span><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Turn</span><span style="color: silver;"> </span><span style="color: #55ffff;">the</span><span style="color: silver;"> </span><span style="color: #55ffff;">floppy</span><span style="color: silver;"> </span><span style="color: #55ffff;">motor</span><span style="color: silver;"> </span><span style="color: #55ffff;">off,</span><span style="color: silver;"> </span><span style="color: #55ffff;">its</span><span style="color: silver;"> </span><span style="color: #55ffff;">annoying!</span></pre>
<pre>MOV<span style="color: silver;"> </span>AL,0xC</pre>
<pre>OUT<span style="color: silver;"> </span>DX,AL</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Set</span><span style="color: silver;"> </span><span style="color: #55ffff;">PAE</span><span style="color: silver;"> </span><span style="color: #55ffff;">and</span><span style="color: silver;"> </span><span style="color: #55ffff;">PGE</span><span style="color: silver;"> </span><span style="color: #55ffff;">bit</span></pre>
<pre>MOV<span style="color: silver;"> </span>EAX,<span style="color: silver;"> </span><span style="color: #ff55ff;">10100000</span>b</pre>
<pre>MOV<span style="color: silver;"> </span>CR<span style="color: #ff55ff;">4</span>,EAX</pre>
<pre>MOV<span style="color: silver;"> </span>EDI,0x<span style="color: #ff55ff;">10000</span></pre>
<pre>MOV<span style="color: silver;"> </span>CR<span style="color: #ff55ff;">3</span>,EDI</pre>
<pre>MOV<span style="color: silver;"> </span>ECX,<span style="color: silver;"> </span>0xC0000080<span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Read</span><span style="color: silver;"> </span><span style="color: #55ffff;">from</span><span style="color: silver;"> </span><span style="color: #55ffff;">the</span><span style="color: silver;"> </span><span style="color: #55ffff;">EFER</span><span style="color: silver;"> </span><span style="color: #55ffff;">MSR.</span></pre>
<pre>RDMSR</pre>
<pre><span style="color: #ffff55; font-weight: 600;">OR</span><span style="color: silver;"> </span>EAX,<span style="color: silver;"> </span>0x00000500<span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Set</span><span style="color: silver;"> </span><span style="color: #55ffff;">the</span><span style="color: silver;"> </span><span style="color: #55ffff;">LME</span><span style="color: silver;"> </span><span style="color: #55ffff;">bit.</span></pre>
<pre>WRMSR</pre>
<pre>
</pre>
<pre>MOV<span style="color: silver;"> </span>EBX,CR0<span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Activate</span><span style="color: silver;"> </span><span style="color: #55ffff;">long</span><span style="color: silver;"> </span><span style="color: #55ffff;">mode</span><span style="color: silver;"> </span><span style="color: #55ffff;">-</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">OR</span><span style="color: silver;"> </span>EBX,0x<span style="color: #ff55ff;">80000001</span><span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">-</span><span style="color: silver;"> </span><span style="color: #55ffff;">by</span><span style="color: silver;"> </span><span style="color: #55ffff;">enabling</span><span style="color: silver;"> </span><span style="color: #55ffff;">paging</span><span style="color: silver;"> </span><span style="color: #55ffff;">and</span><span style="color: silver;"> </span><span style="color: #55ffff;">protection</span><span style="color: silver;"> </span><span style="color: #55ffff;">simultaneously.</span></pre>
<pre>MOV<span style="color: silver;"> </span>CR0,EBX</pre>
<pre>
</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Now</span><span style="color: silver;"> </span><span style="color: #55ffff;">lets</span><span style="color: silver;"> </span><span style="color: #55ffff;">set</span><span style="color: silver;"> </span><span style="color: #55ffff;">up</span><span style="color: silver;"> </span><span style="color: #55ffff;">and</span><span style="color: silver;"> </span><span style="color: #55ffff;">activate</span><span style="color: silver;"> </span><span style="color: #55ffff;">all</span><span style="color: silver;"> </span><span style="color: #55ffff;">of</span><span style="color: silver;"> </span><span style="color: #55ffff;">that</span><span style="color: silver;"> </span><span style="color: #55ffff;">fancy</span><span style="color: silver;"> </span><span style="color: #55ffff;">math</span><span style="color: silver;"> </span><span style="color: #55ffff;">coprocessor</span><span style="color: silver;"> </span><span style="color: #55ffff;">support</span></pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">SSE</span><span style="color: silver;"> </span><span style="color: #55ffff;">Instructions</span></pre>
<pre>
</pre>
<pre>MOV<span style="color: silver;"> </span>EAX,CR0</pre>
<pre><span style="color: #ffff55; font-weight: 600;">AND</span><span style="color: silver;"> </span>AX,0xfffb</pre>
<pre><span style="color: #ffff55; font-weight: 600;">OR</span><span style="color: silver;"> </span>AX,<span style="color: #ff55ff;">2</span></pre>
<pre>MOV<span style="color: silver;"> </span>CR0,EAX</pre>
<pre>MOV<span style="color: silver;"> </span>EAX,CR<span style="color: #ff55ff;">4</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">OR</span><span style="color: silver;"> </span>AX,<span style="color: #ff55ff;">3</span><span style="color: silver;"> </span><<<span style="color: silver;"> </span><span style="color: #ff55ff;">9</span></pre>
<pre>MOV<span style="color: silver;"> </span>CR<span style="color: #ff55ff;">4</span>,EAX</pre>
<pre>
</pre>
<pre><span style="color: #ffff55; font-weight: 600;">JMP</span><span style="color: silver;"> </span>0x<span style="color: #ff55ff;">28</span>:<span style="color: silver;"> </span>longMode</pre>
<pre>longMode:</pre>
<pre>[BITS<span style="color: silver;"> </span><span style="color: #ff55ff;">64</span>]</pre>
<pre>MOV<span style="color: silver;"> </span>AX,0x<span style="color: #ff55ff;">30</span></pre>
<pre>MOV<span style="color: silver;"> </span><span style="background-color: black; color: #aaaaaa; font-family: 'Cantarell'; font-size: 12pt; font-weight: 600;">DS</span>,AX</pre>
<pre>MOV<span style="color: silver;"> </span>ES,AX</pre>
<pre>MOV<span style="color: silver;"> </span>FS,AX</pre>
<pre>MOV<span style="color: silver;"> </span>GS,AX</pre>
<pre>MOV<span style="color: silver;"> </span>SS,AX</pre>
<pre>XOR<span style="color: silver;"> </span>RSP,RSP</pre>
<pre>MOV<span style="color: silver;"> </span>ESP,[stackStart]</pre>
<pre>MOV<span style="color: silver;"> </span>QWORD<span style="color: silver;"> </span>RAX,[krnlEntry]</pre>
<pre>XOR<span style="color: silver;"> </span>RDI,RDI</pre>
<pre>MOV<span style="color: silver;"> </span>EDI,[mallocStart]</pre>
<pre>MOV<span style="color: silver;"> </span>RBP,RSP</pre>
<pre>CALL<span style="color: silver;"> </span>RAX</pre>
<pre>
</pre>
<pre>CLI</pre>
<pre>HLT</pre>
<pre>
</pre>
<pre>[BITS<span style="color: silver;"> </span><span style="color: #ff55ff;">16</span>]</pre>
<pre>RET</pre>
<pre>
</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Functions</span></pre>
<pre>
</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Copies</span><span style="color: silver;"> </span><span style="color: #55ffff;">data</span><span style="color: silver;"> </span><span style="color: #55ffff;">from</span><span style="color: silver;"> </span><span style="color: #55ffff;">ESI</span><span style="color: silver;"> </span><span style="color: #55ffff;">bytes</span><span style="color: silver;"> </span><span style="color: #55ffff;">into</span><span style="color: silver;"> </span><span style="color: #55ffff;">the</span><span style="color: silver;"> </span><span style="color: #55ffff;">file</span><span style="color: silver;"> </span><span style="color: #55ffff;">to</span><span style="color: silver;"> </span><span style="color: #55ffff;">address</span><span style="color: silver;"> </span><span style="color: #55ffff;">EDI</span><span style="color: silver;"> </span><span style="color: #55ffff;">of</span><span style="color: silver;"> </span><span style="color: #55ffff;">size</span><span style="color: silver;"> </span><span style="color: #55ffff;">ECX</span><span style="color: silver;"> </span><span style="color: #55ffff;">bytes</span></pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Dynamically</span><span style="color: silver;"> </span><span style="color: #55ffff;">loads</span><span style="color: silver;"> </span><span style="color: #55ffff;">sectors</span><span style="color: silver;"> </span><span style="color: #55ffff;">as</span><span style="color: silver;"> </span><span style="color: #55ffff;">needed</span></pre>
<pre>copyData:</pre>
<pre>PUSH<span style="color: silver;"> </span>EBX</pre>
<pre>PUSH<span style="color: silver;"> </span>ESI</pre>
<pre>PUSH<span style="color: silver;"> </span>EAX</pre>
<pre>PUSH<span style="color: silver;"> </span>EDX</pre>
<pre>PUSH<span style="color: silver;"> </span>ECX</pre>
<pre>
</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">First</span><span style="color: silver;"> </span><span style="color: #55ffff;">get</span><span style="color: silver;"> </span><span style="color: #55ffff;">starting</span><span style="color: silver;"> </span><span style="color: #55ffff;">sector</span></pre>
<pre>XOR<span style="color: silver;"> </span>EAX,EAX</pre>
<pre>XOR<span style="color: silver;"> </span>EDX,EDX</pre>
<pre>MOV<span style="color: silver;"> </span>EAX,ESI</pre>
<pre>MOV<span style="color: silver;"> </span>EBX,<span style="color: #ff55ff;">512</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">DIV</span><span style="color: silver;"> </span>EBX</pre>
<pre><span style="color: #ffff55; font-weight: 600;">ADD</span><span style="color: silver;"> </span>EAX,kernelLBAAddr</pre>
<pre>CALL<span style="color: silver;"> </span>ReadSector</pre>
<pre>
</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Copy</span><span style="color: silver;"> </span><span style="color: #55ffff;">from</span><span style="color: silver;"> </span><span style="color: #55ffff;">first</span><span style="color: silver;"> </span><span style="color: #55ffff;">sector</span></pre>
<pre>MOV<span style="color: silver;"> </span>ECX,0x<span style="color: #ff55ff;">200</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">SUB</span><span style="color: silver;"> </span>ECX,EDX<span style="color: silver;"> </span><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">ecx</span><span style="color: silver;"> </span><span style="color: #55ffff;">has</span><span style="color: silver;"> </span><span style="color: #55ffff;">rest</span><span style="color: silver;"> </span><span style="color: #55ffff;">of</span><span style="color: silver;"> </span><span style="color: #55ffff;">sector</span><span style="color: silver;"> </span><span style="color: #55ffff;">count</span></pre>
<pre>POP<span style="color: silver;"> </span>EBX<span style="color: silver;"> </span><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">actual</span><span style="color: silver;"> </span><span style="color: #55ffff;">requested</span><span style="color: silver;"> </span><span style="color: #55ffff;">bytes</span><span style="color: silver;"> </span><span style="color: #55ffff;">in</span><span style="color: silver;"> </span><span style="color: #55ffff;">ebx</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">CMP</span><span style="color: silver;"> </span>EBX,ECX<span style="color: silver;"> </span><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Is</span><span style="color: silver;"> </span><span style="color: #55ffff;">it</span><span style="color: silver;"> </span><span style="color: #55ffff;">less?</span><span style="color: silver;"> </span><span style="color: #55ffff;">Can</span><span style="color: silver;"> </span><span style="color: #55ffff;">it</span><span style="color: silver;"> </span><span style="color: #55ffff;">all</span><span style="color: silver;"> </span><span style="color: #55ffff;">really</span><span style="color: silver;"> </span><span style="color: #55ffff;">fit</span><span style="color: silver;"> </span><span style="color: #55ffff;">in</span><span style="color: silver;"> </span><span style="color: #55ffff;">one</span><span style="color: silver;"> </span><span style="color: #55ffff;">sector?</span></pre>
<pre>JC<span style="color: silver;"> </span>.onlyOneNeeded<span style="color: silver;"> </span><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Yup</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">SUB</span><span style="color: silver;"> </span>EBX,ECX</pre>
<pre>PUSH<span style="color: silver;"> </span>EBX</pre>
<pre><span style="color: #ffff55; font-weight: 600;">JMP</span><span style="color: silver;"> </span>.doCopy</pre>
<pre>.onlyOneNeeded:</pre>
<pre>XCHG<span style="color: silver;"> </span>EBX,ECX</pre>
<pre>PUSH<span style="color: silver;"> </span>DWORD<span style="color: silver;"> </span>0</pre>
<pre>.doCop<span style="background-color: black; color: #aaaaaa; font-family: 'Cantarell'; font-size: 12pt; font-weight: 600;">y:</span></pre>
<pre>MOV<span style="color: silver;"> </span>ESI,EDX</pre>
<pre><span style="color: #ffff55; font-weight: 600;">ADD</span><span style="color: silver;"> </span>ESI,0x<span style="color: #ff55ff;">7000</span></pre>
<pre>CALL<span style="color: silver;"> </span>copyBytes</pre>
<pre>
</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Ok,</span><span style="color: silver;"> </span><span style="color: #55ffff;">how</span><span style="color: silver;"> </span><span style="color: #55ffff;">much</span><span style="color: silver;"> </span><span style="color: #55ffff;">is</span><span style="color: silver;"> </span><span style="color: #55ffff;">left?</span></pre>
<pre>.cdSectorLoo<span style="background-color: black; color: #aaaaaa; font-family: 'Cantarell'; font-size: 12pt; font-weight: 600;">p:</span></pre>
<pre>POP<span style="color: silver;"> </span>ECX</pre>
<pre><span style="color: #ffff55; font-weight: 600;">CMP</span><span style="color: silver;"> </span>ECX,0</pre>
<pre>JZ<span style="color: silver;"> </span>.cdDone<span style="color: silver;"> </span><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">No</span><span style="color: silver;"> </span><span style="color: #55ffff;">more</span><span style="color: silver;"> </span><span style="color: #55ffff;">data?</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">CMP</span><span style="color: silver;"> </span>ECX,0x<span style="color: #ff55ff;">200</span></pre>
<pre>JC<span style="color: silver;"> </span>.cdLastSector<span style="color: silver;"> </span><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Less</span><span style="color: silver;"> </span><span style="color: #55ffff;">than</span><span style="color: silver;"> </span><span style="color: #55ffff;">one</span><span style="color: silver;"> </span><span style="color: #55ffff;">sector</span><span style="color: silver;"> </span><span style="color: #55ffff;">of</span><span style="color: silver;"> </span><span style="color: #55ffff;">data</span><span style="color: silver;"> </span><span style="color: #55ffff;">left</span></pre>
<pre>
</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Read</span><span style="color: silver;"> </span><span style="color: #55ffff;">a</span><span style="color: silver;"> </span><span style="color: #55ffff;">whole</span><span style="color: silver;"> </span><span style="color: #55ffff;">sector</span><span style="color: silver;"> </span><span style="color: #55ffff;">and</span><span style="color: silver;"> </span><span style="color: #55ffff;">transfer</span><span style="color: silver;"> </span><span style="color: #55ffff;">up</span><span style="color: silver;"> </span><span style="color: #55ffff;">to</span><span style="color: silver;"> </span><span style="color: #55ffff;">destination</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">SUB</span><span style="color: silver;"> </span>ECX,0x<span style="color: #ff55ff;">200</span></pre>
<pre>PUSH<span style="color: silver;"> </span>ECX</pre>
<pre>INC<span style="color: silver;"> </span>EAX</pre>
<pre>CALL<span style="color: silver;"> </span>ReadSector</pre>
<pre>MOV<span style="color: silver;"> </span>ECX,0x<span style="color: #ff55ff;">200</span></pre>
<pre>MOV<span style="color: silver;"> </span>ESI,0x<span style="color: #ff55ff;">7000</span></pre>
<pre>CALL<span style="color: silver;"> </span>copyBytes</pre>
<pre><span style="color: #ffff55; font-weight: 600;">JMP</span><span style="color: silver;"> </span>.cdSectorLoop</pre>
<pre>
</pre>
<pre>.cdLastSector:</pre>
<pre>INC<span style="color: silver;"> </span>EAX</pre>
<pre>CALL<span style="color: silver;"> </span>ReadSector</pre>
<pre>MOV<span style="color: silver;"> </span>ESI,0x<span style="color: #ff55ff;">7000</span></pre>
<pre>CALL<span style="color: silver;"> </span>copyBytes</pre>
<pre>
</pre>
<pre>.cdDone:</pre>
<pre>POP<span style="color: silver;"> </span>EDX</pre>
<pre>POP<span style="color: silver;"> </span>EAX</pre>
<pre>POP<span style="color: silver;"> </span>ESI</pre>
<pre>POP<span style="color: silver;"> </span>EBX</pre>
<pre>RET</pre>
<pre>
</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Copies</span><span style="color: silver;"> </span><span style="color: #55ffff;">bytes</span><span style="color: silver;"> </span><span style="color: #55ffff;">from</span><span style="color: silver;"> </span><span style="color: #55ffff;">esi</span><span style="color: silver;"> </span><span style="color: #55ffff;">to</span><span style="color: silver;"> </span><span style="color: #55ffff;">edi</span></pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">We</span><span style="color: silver;"> </span><span style="color: #55ffff;">have</span><span style="color: silver;"> </span><span style="color: #55ffff;">to</span><span style="color: silver;"> </span><span style="color: #55ffff;">do</span><span style="color: silver;"> </span><span style="color: #55ffff;">this</span><span style="color: silver;"> </span><span style="color: #55ffff;">this</span><span style="color: silver;"> </span><span style="color: #55ffff;">way</span><span style="color: silver;"> </span><span style="color: #55ffff;">since</span><span style="color: silver;"> </span><span style="color: #55ffff;">16</span><span style="color: silver;"> </span><span style="color: #55ffff;">bit</span><span style="color: silver;"> </span><span style="color: #55ffff;">rep</span><span style="color: silver;"> </span><span style="color: #55ffff;">movsb</span><span style="color: silver;"> </span><span style="color: #55ffff;">will</span><span style="color: silver;"> </span><span style="color: #55ffff;">only</span><span style="color: silver;"> </span><span style="color: #55ffff;">do</span><span style="color: silver;"> </span><span style="color: #55ffff;">64k</span><span style="color: silver;"> </span><span style="color: #55ffff;">of</span><span style="color: silver;"> </span><span style="color: #55ffff;">ram,</span><span style="color: silver;"> </span><span style="color: #55ffff;">this</span><span style="color: silver;"> </span><span style="color: #55ffff;">can</span><span style="color: silver;"> </span><span style="color: #55ffff;">access</span><span style="color: silver;"> </span><span style="color: #55ffff;">the</span><span style="color: silver;"> </span><span style="color: #55ffff;">first</span><span style="color: silver;"> </span><span style="color: #55ffff;">4G</span></pre>
<pre>copyBytes:</pre>
<pre>PUSH<span style="color: silver;"> </span>AX</pre>
<pre>.cbLoo<span style="background-color: black; color: #aaaaaa; font-family: 'Cantarell'; font-size: 12pt; font-weight: 600;">p:</span></pre>
<pre>MOV<span style="color: silver;"> </span>AL,[FS:ESI]</pre>
<pre>MOV<span style="color: silver;"> </span>[FS:EDI],AL</pre>
<pre>INC<span style="color: silver;"> </span>ESI</pre>
<pre>INC<span style="color: silver;"> </span>EDI</pre>
<pre>LOOP<span style="color: silver;"> </span>.cbLoop</pre>
<pre>POP<span style="color: silver;"> </span>AX</pre>
<pre>RET</pre>
<pre>
</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Read</span><span style="color: silver;"> </span><span style="color: #55ffff;">a</span><span style="color: silver;"> </span><span style="color: #55ffff;">sector</span><span style="color: silver;"> </span><span style="color: #55ffff;">with</span><span style="color: silver;"> </span><span style="color: #55ffff;">the</span><span style="color: silver;"> </span><span style="color: #55ffff;">LBA</span><span style="color: silver;"> </span><span style="color: #55ffff;">address</span><span style="color: silver;"> </span><span style="color: #55ffff;">in</span><span style="color: silver;"> </span><span style="color: #55ffff;">EAX</span><span style="color: silver;"> </span><span style="color: #55ffff;">into</span><span style="color: silver;"> </span><span style="color: #55ffff;">0x7000</span></pre>
<pre>ReadSector:</pre>
<pre>PUSHAD</pre>
<pre>MOV<span style="color: silver;"> </span>[currSector],EAX</pre>
<pre>CALL<span style="color: silver;"> </span>incrementSpinner</pre>
<pre>MOV<span style="color: silver;"> </span>DL,[bootDrive]</pre>
<pre><span style="color: #ffff55; font-weight: 600;">CMP</span><span style="color: silver;"> </span>DL,0x<span style="color: #ff55ff;">80</span></pre>
<pre>JNC<span style="color: silver;"> </span>.readHDD</pre>
<pre><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">We</span><span style="color: silver;"> </span><span style="color: #55ffff;">dont</span><span style="color: silver;"> </span><span style="color: #55ffff;">need</span><span style="color: silver;"> </span><span style="color: #55ffff;">dword</span><span style="color: silver;"> </span><span style="color: #55ffff;">support</span><span style="color: silver;"> </span><span style="color: #55ffff;">for</span><span style="color: silver;"> </span><span style="color: #55ffff;">a</span><span style="color: silver;"> </span><span style="color: #55ffff;">floppy</span></pre>
<pre>CALL<span style="color: silver;"> </span>LBAtoCHS</pre>
<pre>MOV<span style="color: silver;"> </span>DL,[bootDrive]</pre>
<pre>MOV<span style="color: silver;"> </span>AX,0x<span style="color: #ff55ff;">201</span></pre>
<pre>MOV<span style="color: silver;"> </span>BX,0x<span style="color: #ff55ff;">7000</span></pre>
<pre>INT<span style="color: silver;"> </span>0x<span style="color: #ff55ff;">13</span></pre>
<pre>JC<span style="color: silver;"> </span>readError</pre>
<pre>POPAD</pre>
<pre>RET</pre>
<pre>.readHDD:</pre>
<pre>MOV<span style="color: silver;"> </span>DWORD<span style="color: silver;"> </span>[HDDReadPacket.sector],EAX</pre>
<pre>MOV<span style="color: silver;"> </span>AX,0x<span style="color: #ff55ff;">4200</span></pre>
<pre>MOV<span style="color: silver;"> </span>SI,HDDReadPacket</pre>
<pre>INT<span style="color: silver;"> </span>0x<span style="color: #ff55ff;">13</span></pre>
<pre>JC<span style="color: silver;"> </span>readError</pre>
<pre>POPAD</pre>
<pre>RET</pre>
<pre>
</pre>
<pre>
</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Converts</span><span style="color: silver;"> </span><span style="color: #55ffff;">LBA</span><span style="color: silver;"> </span><span style="color: #55ffff;">to</span><span style="color: silver;"> </span><span style="color: #55ffff;">CHS</span><span style="color: silver;"> </span><span style="color: #55ffff;">address</span><span style="color: silver;"> </span><span style="color: #55ffff;">for</span><span style="color: silver;"> </span><span style="color: #55ffff;">a</span><span style="color: silver;"> </span><span style="color: #55ffff;">1.44</span><span style="color: silver;"> </span><span style="color: #55ffff;">floppy</span></pre>
<pre>LBAtoCHS:</pre>
<pre><span style="color: #55ffff;">;[in</span><span style="color: silver;"> </span><span style="color: #55ffff;">AX=LBA</span><span style="color: silver;"> </span><span style="color: #55ffff;">Sector]</span></pre>
<pre><span style="color: #55ffff;">;[out</span><span style="color: silver;"> </span><span style="color: #55ffff;">DX,CX]</span></pre>
<pre>XOR<span style="color: silver;"> </span>CX,CX</pre>
<pre>XOR<span style="color: silver;"> </span>DX,DX</pre>
<pre><span style="color: #ffff55; font-weight: 600;">DIV</span><span style="color: silver;"> </span>WORD<span style="color: silver;"> </span>[flpSecTrk]</pre>
<pre>INC<span style="color: silver;"> </span>DX</pre>
<pre>MOV<span style="color: silver;"> </span>CL,DL</pre>
<pre>XOR<span style="color: silver;"> </span>DX,DX</pre>
<pre><span style="color: #ffff55; font-weight: 600;">DIV</span><span style="color: silver;"> </span>WORD<span style="color: silver;"> </span>[flpHds]</pre>
<pre>MOV<span style="color: silver;"> </span>DH,DL</pre>
<pre>MOV<span style="color: silver;"> </span>CH,AL</pre>
<pre>RET</pre>
<pre>
</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Incrememnts</span><span style="color: silver;"> </span><span style="color: #55ffff;">the</span><span style="color: silver;"> </span><span style="color: #55ffff;">spinner</span><span style="color: silver;"> </span><span style="color: #55ffff;">so</span><span style="color: silver;"> </span><span style="color: #55ffff;">that</span><span style="color: silver;"> </span><span style="color: #55ffff;">the</span><span style="color: silver;"> </span><span style="color: #55ffff;">user</span><span style="color: silver;"> </span><span style="color: #55ffff;">can</span><span style="color: silver;"> </span><span style="color: #55ffff;">see</span><span style="color: silver;"> </span><span style="color: #55ffff;">something</span><span style="color: silver;"> </span><span style="color: #55ffff;">is</span><span style="color: silver;"> </span><span style="color: #55ffff;">happening</span></pre>
<pre>incrementSpinner:</pre>
<pre>PUSH<span style="color: silver;"> </span>SI</pre>
<pre>PUSH<span style="color: silver;"> </span>CX</pre>
<pre>MOV<span style="color: silver;"> </span>SI,txtSpinner</pre>
<pre>XOR<span style="color: silver;"> </span>CX,CX</pre>
<pre>MOV<span style="color: silver;"> </span>CL,[txtSpPos]</pre>
<pre>INC<span style="color: silver;"> </span>CL</pre>
<pre>.incrementSpinner<span style="color: #ff55ff;">1</span>:</pre>
<pre><span style="color: #ffff55; font-weight: 600;">ADD</span><span style="color: silver;"> </span>SI,<span style="color: #ff55ff;">3</span></pre>
<pre>LOOP<span style="color: silver;"> </span>.incrementSpinner<span style="color: #ff55ff;">1</span></pre>
<pre>MOV<span style="color: silver;"> </span>CL,[txtSpPos]</pre>
<pre>CALL<span style="color: silver;"> </span>printString</pre>
<pre>INC<span style="color: silver;"> </span>CL</pre>
<pre><span style="color: #ffff55; font-weight: 600;">CMP</span><span style="color: silver;"> </span>CL,<span style="color: #ff55ff;">4</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">JLE</span><span style="color: silver;"> </span>.incrementSpinnerOut</pre>
<pre>MOV<span style="color: silver;"> </span>CL,0</pre>
<pre>.incrementSpinnerOut:</pre>
<pre>MOV<span style="color: silver;"> </span>[txtSpPos],CL</pre>
<pre>POP<span style="color: silver;"> </span>CX</pre>
<pre>POP<span style="color: silver;"> </span>SI</pre>
<pre>RET</pre>
<pre>
</pre>
<pre>printString:</pre>
<pre>PUSH<span style="color: silver;"> </span>AX</pre>
<pre>PUSH<span style="color: silver;"> </span>BX</pre>
<pre>PUSH<span style="color: silver;"> </span>CX</pre>
<pre>MOV<span style="color: silver;"> </span>AH,0xe</pre>
<pre>XOR<span style="color: silver;"> </span>BX,BX</pre>
<pre>XOR<span style="color: silver;"> </span>CX,CX</pre>
<pre>.printStringLoo<span style="background-color: black; color: #aaaaaa; font-family: 'Cantarell'; font-size: 12pt; font-weight: 600;">p:</span></pre>
<pre>LODSB</pre>
<pre>TEST<span style="color: silver;"> </span>AL,AL</pre>
<pre>JZ<span style="color: silver;"> </span>.printStringExit</pre>
<pre>INT<span style="color: silver;"> </span>0x<span style="color: #ff55ff;">10</span></pre>
<pre><span style="color: #ffff55; font-weight: 600;">JMP</span><span style="color: silver;"> </span>.printStringLoop</pre>
<pre>.printStringExit:</pre>
<pre>POP<span style="color: silver;"> </span>CX</pre>
<pre>POP<span style="color: silver;"> </span>BX</pre>
<pre>POP<span style="color: silver;"> </span>AX</pre>
<pre>RET</pre>
<pre>
</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Error</span><span style="color: silver;"> </span><span style="color: #55ffff;">functions</span></pre>
<pre>readError:</pre>
<pre>MOV<span style="color: silver;"> </span>SI,readErrorStr</pre>
<pre>CALL<span style="color: silver;"> </span>printString</pre>
<pre>CLI</pre>
<pre>HLT</pre>
<pre>readErrorStr<span style="color: silver;"> </span>db<span style="color: silver;"> </span><span style="color: #ff55ff;">13</span>,<span style="color: #ff55ff;">10</span>,<span style="color: #ff55ff;">13</span>,<span style="color: #ff55ff;">10</span>,<span style="color: #ff55ff;">"Disk</span><span style="color: silver;"> </span><span style="color: #ff55ff;">Read</span><span style="color: silver;"> </span><span style="color: #ff55ff;">error"</span>,0</pre>
<pre>
</pre>
<pre>badELF:</pre>
<pre>MOV<span style="color: silver;"> </span>SI,badELFStr</pre>
<pre>CALL<span style="color: silver;"> </span>printString</pre>
<pre>CLI</pre>
<pre>HLT</pre>
<pre>badELFStr<span style="color: silver;"> </span>db<span style="color: silver;"> </span><span style="color: #ff55ff;">13</span>,<span style="color: #ff55ff;">10</span>,<span style="color: #ff55ff;">13</span>,<span style="color: #ff55ff;">10</span>,<span style="color: #ff55ff;">"Corrupted</span><span style="color: silver;"> </span><span style="color: #ff55ff;">ELF</span><span style="color: silver;"> </span><span style="color: #ff55ff;">Image!"</span>,0</pre>
<pre>
</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Data</span></pre>
<pre>txtSpinner<span style="color: silver;"> </span>db<span style="color: silver;"> </span>0,0,0,<span style="color: #ff55ff;">"/"</span>,<span style="color: #ff55ff;">8</span>,0,<span style="color: #ff55ff;">"-"</span>,<span style="color: #ff55ff;">8</span>,0,<span style="color: #ff55ff;">"\"</span>,<span style="color: #ff55ff;">8</span>,0,<span style="color: #ff55ff;">"|"</span>,<span style="color: #ff55ff;">8</span>,0,<span style="color: #ff55ff;">"."</span>,0</pre>
<pre>txtSpPos<span style="color: silver;"> </span>db<span style="color: silver;"> </span>0</pre>
<pre>bootDrive<span style="color: silver;"> </span>db<span style="color: silver;"> </span>0</pre>
<pre>currSector<span style="color: silver;"> </span>dd<span style="color: silver;"> </span>0</pre>
<pre>flpSecTrk<span style="color: silver;"> </span>dw<span style="color: silver;"> </span><span style="color: #ff55ff;">18</span></pre>
<pre>flpHds<span style="color: silver;"> </span>dw<span style="color: silver;"> </span><span style="color: #ff55ff;">2</span></pre>
<pre>krnlEntry<span style="color: silver;"> </span>dq<span style="color: silver;"> </span>0</pre>
<pre>mallocStart<span style="color: silver;"> </span>dd<span style="color: silver;"> </span>0</pre>
<pre>stackStart<span style="color: silver;"> </span>dd<span style="color: silver;"> </span>0</pre>
<pre>HDDReadPacket:</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Some</span><span style="color: silver;"> </span><span style="color: #55ffff;">of</span><span style="color: silver;"> </span><span style="color: #55ffff;">these</span><span style="color: silver;"> </span><span style="color: #55ffff;">values</span><span style="color: silver;"> </span><span style="color: #55ffff;">are</span><span style="color: silver;"> </span><span style="color: #55ffff;">static</span></pre>
<pre>db<span style="color: silver;"> </span>0x<span style="color: #ff55ff;">10</span></pre>
<pre>db<span style="color: silver;"> </span>0</pre>
<pre>dw<span style="color: silver;"> </span><span style="color: #ff55ff;">1</span></pre>
<pre>dw<span style="color: silver;"> </span>0x<span style="color: #ff55ff;">7000</span></pre>
<pre>dw<span style="color: silver;"> </span>0</pre>
<pre>.sector<span style="color: silver;"> </span>dq<span style="color: silver;"> </span>0</pre>
<pre>
</pre>
<pre>ALIGN<span style="color: silver;"> </span><span style="color: #ff55ff;">8</span></pre>
<pre>GDT:</pre>
<pre>dq<span style="color: silver;"> </span>0</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">16</span><span style="color: silver;"> </span><span style="color: #55ffff;">Bit</span></pre>
<pre><span style="color: silver;"> </span>dd<span style="color: silver;"> </span>0x0000ffff<span style="color: silver;"> </span><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Code</span><span style="color: silver;"> </span><span style="color: #55ffff;">0x8</span></pre>
<pre><span style="color: silver;"> </span>dd<span style="color: silver;"> </span>0x00009c00</pre>
<pre><span style="color: silver;"> </span>dd<span style="color: silver;"> </span>0x0000ffff<span style="color: silver;"> </span><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Data</span><span style="color: silver;"> </span><span style="color: #55ffff;">0x10</span></pre>
<pre><span style="color: silver;"> </span>dd<span style="color: silver;"> </span>0x00009200</pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">32</span><span style="color: silver;"> </span><span style="color: #55ffff;">Bit</span><span style="color: silver;"> </span><span style="color: #55ffff;">Segments</span></pre>
<pre><span style="color: silver;"> </span>dd<span style="color: silver;"> </span>0x0000ffff<span style="color: silver;"> </span><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Code</span><span style="color: silver;"> </span><span style="color: #55ffff;">0x18</span></pre>
<pre><span style="color: silver;"> </span>dd<span style="color: silver;"> </span>0x00cf<span style="color: #ff55ff;">9</span>c00</pre>
<pre><span style="color: silver;"> </span>dd<span style="color: silver;"> </span>0x0000ffff<span style="color: silver;"> </span><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Data</span><span style="color: silver;"> </span><span style="color: #55ffff;">0x20</span></pre>
<pre><span style="color: silver;"> </span>dd<span style="color: silver;"> </span>0x00cf<span style="color: #ff55ff;">9200</span></pre>
<pre><span style="color: #55ffff;">;;</span><span style="color: silver;"> </span><span style="color: #55ffff;">64</span><span style="color: silver;"> </span><span style="color: #55ffff;">bit</span></pre>
<pre><span style="color: silver;"> </span>dq<span style="color: silver;"> </span>0x002f<span style="color: #ff55ff;">98000000</span>ffff<span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Code</span><span style="color: silver;"> </span><span style="color: #55ffff;">0x28</span></pre>
<pre><span style="color: silver;"> </span>dq<span style="color: silver;"> </span>0x002f<span style="color: #ff55ff;">92000000</span>ffff<span style="color: silver;"> </span><span style="color: #55ffff;">;</span><span style="color: silver;"> </span><span style="color: #55ffff;">Data</span><span style="color: silver;"> </span><span style="color: #55ffff;">0x30</span></pre>
<pre>
</pre>
<pre>GDTR:</pre>
<pre>dw<span style="color: silver;"> </span>(GDTR-GDT)-<span style="color: #ff55ff;">1</span></pre>
<pre>dd<span style="color: silver;"> </span>GDT</pre>
<pre>
</pre>
<pre>TIMES<span style="color: silver;"> </span>(<span style="color: #ff55ff;">512</span><span style="color: silver;"> </span>*<span style="color: silver;"> </span>(loaderNumSects))-(<span style="color: #ff55ff;">$-$$)</span><span style="color: silver;"> </span><span style="color: #ff55ff;">DB</span><span style="color: silver;"> </span><span style="color: #ff55ff;">90</span></pre>
<pre>
<!--EndFragment--></pre>
</blockquote>
Chris Beneschhttp://www.blogger.com/profile/10552098270462277588noreply@blogger.com1