<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>assembly tutorial Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/assembly-tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/assembly-tutorial/</link>
	<description></description>
	<lastBuildDate>Sun, 22 Feb 2026 03:21:33 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>assembly tutorial Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/assembly-tutorial/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>x86-64 Assembly Pointers &#038; Dereferencing Explained &#8211; Hybrid C++/YASM Example</title>
		<link>https://www.NeuralLantern.com/x86-64-assembly-pointers-dereferencing-explained-hybrid-c-yasm-example/</link>
					<comments>https://www.NeuralLantern.com/x86-64-assembly-pointers-dereferencing-explained-hybrid-c-yasm-example/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 22 Feb 2026 03:21:32 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly C++ interop]]></category>
		<category><![CDATA[assembly data section]]></category>
		<category><![CDATA[assembly memory addresses]]></category>
		<category><![CDATA[assembly pointers]]></category>
		<category><![CDATA[assembly tutorial]]></category>
		<category><![CDATA[dereferencing assembly]]></category>
		<category><![CDATA[extern C assembly]]></category>
		<category><![CDATA[hybrid C++ assembly]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[passing pointers assembly]]></category>
		<category><![CDATA[stack alignment assembly]]></category>
		<category><![CDATA[systems programming]]></category>
		<category><![CDATA[x86 assembly tutorial]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[x86-64 calling convention]]></category>
		<category><![CDATA[x86-64 pointers]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=305</guid>

					<description><![CDATA[<p>This video explains pointers and dereferencing in x86-64 YASM assembly and demonstrates passing pointers between assembly and C++ in a hybrid program. We show how to read a C string from assembly, modify a long via pointer dereference so the change is visible in C++, and send assembly-owned data (string, long, double) back to C++ using pointers.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-pointers-dereferencing-explained-hybrid-c-yasm-example/">x86-64 Assembly Pointers &amp; Dereferencing Explained &#8211; Hybrid C++/YASM Example</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="x86-64 Assembly Pointers &amp; Dereferencing Explained - Hybrid C++/YASM Example" width="1380" height="776" src="https://www.youtube.com/embed/jzj0iqC-XJI?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">Learn pointers &amp; dereferencing in x86-64 YASM assembly and how to pass them correctly between assembly and C++ in a hybrid program. We build a small working example that sends strings, longs, and doubles both directions using pointers, modifies values across module boundaries, and explains why pointer-to-double still uses general-purpose registers. Includes a quick demo of stack misalignment crash + fix.</p>



<p class="wp-block-paragraph">Great for assembly beginners moving to real programs, systems programming students, or anyone curious how low-level code talks to C/C++.</p>



<p class="wp-block-paragraph">00:00 Introduction to Pointers and Dereferencing in x86-64 Assembly<br>00:28 Pointers explained in C++<br>01:02 Changing values via pointers in C++<br>01:43 Pointers in assembly basics<br>02:09 Defining variables and pointers in YASM data section<br>03:23 Pointers are always integers even to doubles<br>04:20 Function arguments are pointers treated as 64-bit integers<br>05:00 Driver C++ code overview<br>05:58 Marking extern &#8220;C&#8221; functions<br>06:40 Local stack variables and passing pointers<br>07:51 Stack lifetime warning<br>08:34 Assembly data section strings and numbers<br>09:39 Print null-terminated string helper functions<br>10:38 External symbols and hey_driver_print_this<br>11:29 Point function prologue and stack alignment<br>13:04 Extra push for 16-byte alignment<br>14:20 Printing welcome message from assembly<br>16:00 Driver sees initial long value<br>16:58 Printing received string from C++<br>18:20 Using received char pointer without dereference<br>20:21 Modifying incoming long via dereference<br>21:46 Driver sees modified long value 101<br>22:43 Calling back to C++ to print assembly-owned data<br>23:48 Passing pointers to assembly string long and double<br>25:08 Driver prints assembly-owned values and addresses<br>26:14 Summary of pointer passing between modules<br>26:36 Stack alignment crash demonstration<br>27:39 Adding extra push/pop fixes segfault<br>28:00 Closing remarks and call to subscribe</p>



<p class="wp-block-paragraph">=-=-=-=-=-=-=-=-=</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>



<li>Twitter / X: https://x.com/NeuralLantern</li>



<li>Rumble: https://rumble.com/c/c-3696939</li>



<li>BitChute: https://www.bitchute.com/channel/pg1Pvv5dN4Gt</li>



<li>Daily Motion: https://www.dailymotion.com/neurallantern</li>



<li>Minds: https://www.minds.com/neurallantern/</li>



<li>Odysee: https://odysee.com/@NeuralLantern:5</li>
</ul>



<p class="wp-block-paragraph">Please show your support!</p>



<ul class="wp-block-list">
<li>Buy me a coffee: https://ko-fi.com/neurallantern</li>



<li>Subscribe + Sharing on Social Media</li>



<li>Leave a comment or suggestion</li>



<li>Subscribe to Blog: https://www.NeuralLantern.com</li>



<li>Watching the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p class="wp-block-paragraph">Hey there! In this video we&#8217;re going to talk about pointers and dereferencing in a YASM x8664</p>



<p class="wp-block-paragraph">assembly program, also as a hybrid program so that assembly and C++ can talk to each other</p>



<p class="wp-block-paragraph">and send each other pointers and send each other data and things like that.</p>



<p class="wp-block-paragraph">for what pointers are.</p>



<p class="wp-block-paragraph">I&#8217;m going to write in C++ for a second.</p>



<p class="wp-block-paragraph">Suppose you have a pointer for an integer.</p>



<p class="wp-block-paragraph">We&#8217;ll call it P.</p>



<p class="wp-block-paragraph">Suppose you have an integer by itself.</p>



<p class="wp-block-paragraph">We&#8217;ll call it A.</p>



<p class="wp-block-paragraph">Let&#8217;s say that the value of A is 5.</p>



<p class="wp-block-paragraph">And if you wanted to say that P points to A,</p>



<p class="wp-block-paragraph">you could say P equals the address of A.</p>



<p class="wp-block-paragraph">I&#8217;ll put C++ at the top here.</p>



<p class="wp-block-paragraph">And so now if I set A to 6</p>



<p class="wp-block-paragraph">then I print P a dereference of P this is not like a full pointers tutorial</p>



<p class="wp-block-paragraph">but basically by changing a I&#8217;m changing what P thinks it sees as a value</p>



<p class="wp-block-paragraph">assuming ID reference it I could also let me do a print 6 here I could also</p>



<p class="wp-block-paragraph">just change the value through P I could say dereference P and I could say equals</p>



<p class="wp-block-paragraph">would actually print a seven right so you know you can have regular variables global variables</p>



<p class="wp-block-paragraph">whatever kind of you know memory stuff on the stack and to get a pointer to it you really just</p>



<p class="wp-block-paragraph">need to get its memory location in c++ it&#8217;s kind of easy syntactically you can see what&#8217;s happening</p>



<p class="wp-block-paragraph">in assembly you really just need the memory location stored somewhere you could store that</p>



<p class="wp-block-paragraph">variable that just simply stored the memory location of some other variable.</p>



<p class="wp-block-paragraph">You could have a 64-bit register store the value of a variable.</p>



<p class="wp-block-paragraph">Let&#8217;s say we have like a, I don&#8217;t know, my whatever, my number let&#8217;s say inside of assembly.</p>



<p class="wp-block-paragraph">I&#8217;ll do ASM here and we say it&#8217;s a quad word and it starts off as this number or whatever.</p>



<p class="wp-block-paragraph">So if you haven&#8217;t seen my previous videos, go see them for the basics of assembly and</p>



<p class="wp-block-paragraph">of assembly and linking and make files and all that stuff but you know if you</p>



<p class="wp-block-paragraph">have an assembly program and you have a data section and you define a global</p>



<p class="wp-block-paragraph">variable like this what you&#8217;re basically saying is I want to take this giant</p>



<p class="wp-block-paragraph">number and I want to write it into eight bytes that&#8217;s the DQ it says data quad</p>



<p class="wp-block-paragraph">word I want to write that giant number across eight bytes and then I want to</p>



<p class="wp-block-paragraph">get a pointer to it stored in the my number symbol so my number is not</p>



<p class="wp-block-paragraph">actually the value it&#8217;s a pointer to the value so you know later if you want to</p>



<p class="wp-block-paragraph">you know later if you want to move you know something into a register if you did this</p>



<p class="wp-block-paragraph">that would move the pointer into rax but if you did this</p>



<p class="wp-block-paragraph">with deref symbols after it or around it then you would move</p>



<p class="wp-block-paragraph">maybe i&#8217;ll put that into rex you&#8217;d move that actual number that we specified into rex</p>



<p class="wp-block-paragraph">into Rx. It&#8217;s important to understand also that pointers are integers even when we&#8217;re pointing to</p>



<p class="wp-block-paragraph">doubles. So for example sometimes people make this mistake they&#8217;ll say you know my double</p>



<p class="wp-block-paragraph">and they&#8217;ll say it&#8217;s a quad word meaning this is going to be a 64-bit double precision floating</p>



<p class="wp-block-paragraph">point number and they&#8217;ll do like 44.55 or whatever. So that is a double and it is in memory</p>



<p class="wp-block-paragraph">you know what is the symbol of my double remember it&#8217;s supposed to be just a</p>



<p class="wp-block-paragraph">pointer right it can&#8217;t be an actual double because a memory location is not</p>



<p class="wp-block-paragraph">a double a memory location is an integer so that means if you wanted to move a</p>



<p class="wp-block-paragraph">pointer into a register you would only be able to move the pointer into a</p>



<p class="wp-block-paragraph">regular general purpose register not a floating point register and you should</p>



<p class="wp-block-paragraph">use the regular movement instructions for just regular general purpose</p>



<p class="wp-block-paragraph">So keep that in mind if you see a signature like this like let&#8217;s say function F and we have</p>



<p class="wp-block-paragraph">You know, let&#8217;s say long a and long B and actually let&#8217;s do pointers</p>



<p class="wp-block-paragraph">Let&#8217;s say long pointer a and long pointer</p>



<p class="wp-block-paragraph">B and double pointer C all three of those arguments are actually 64 bit integers</p>



<p class="wp-block-paragraph">Because they&#8217;re all pointers even if one of the pointers points to adult a double</p>



<p class="wp-block-paragraph">double why did I say dull pointers aren&#8217;t dull they&#8217;re exciting okay so I&#8217;m gonna open up some</p>



<p class="wp-block-paragraph">code here real fast so usually I don&#8217;t explain my uh my driver I&#8217;m gonna explain it to you this time</p>



<p class="wp-block-paragraph">because it&#8217;s kind of doing a little bit more than my other videos um again if you don&#8217;t have uh the</p>



<p class="wp-block-paragraph">knowledge of how to make a make file see my other videos because that&#8217;s explained there for now I&#8217;m</p>



<p class="wp-block-paragraph">what we really need to do is write a driver and an assembly module for a</p>



<p class="wp-block-paragraph">hybrid program again hybrid programs covered in other videos so the driver is</p>



<p class="wp-block-paragraph">pretty easy I&#8217;m just going to copy paste it honestly here and then just kind of</p>



<p class="wp-block-paragraph">explain it to you the driver is pretty easy we&#8217;re going to do I O stream so we</p>



<p class="wp-block-paragraph">can print stuff we&#8217;re going to mark an external function called point as extern</p>



<p class="wp-block-paragraph">C so that just disables name mangling which means the C++ module will be able</p>



<p class="wp-block-paragraph">will be able to call on this function called point and it won&#8217;t expect that</p>



<p class="wp-block-paragraph">the point function has its name mangled like C++ does the reason being is that</p>



<p class="wp-block-paragraph">point is actually going to be in a side it&#8217;s going to be inside assembly where</p>



<p class="wp-block-paragraph">its name will not be mangled this disables the ability to overload but</p>



<p class="wp-block-paragraph">that&#8217;s okay we don&#8217;t care it&#8217;s going to take two pointers a pointer to a character</p>



<p class="wp-block-paragraph">and a pointer to a long since both of those are pointers they&#8217;re both</p>



<p class="wp-block-paragraph">64-bit integers even the character pointer and then we have a function that is internal to this</p>



<p class="wp-block-paragraph">module called hey driver print this remember we&#8217;re inside of the driver program right now</p>



<p class="wp-block-paragraph">so if you look at the bottom it&#8217;s just a function that takes in some pointers</p>



<p class="wp-block-paragraph">and then prints some stuff so it&#8217;s going to print like it&#8217;s going to print what the string is</p>



<p class="wp-block-paragraph">it&#8217;s going to print what the long is my dog&#8217;s growling at me i&#8217;m going to ignore him because</p>



<p class="wp-block-paragraph">i literally just let him pee and poop at this point now he&#8217;s harassing me for treats</p>



<p class="wp-block-paragraph">now he&#8217;s harassing me for treats he always does this okay so uh the string the long the double</p>



<p class="wp-block-paragraph">this function expects to receive three pointers to different data types it&#8217;s just going to print</p>



<p class="wp-block-paragraph">all of them and the point get it the point of this function is we&#8217;re going to go inside of</p>



<p class="wp-block-paragraph">the assembly module and then have the assembly module call on this function so that we can we</p>



<p class="wp-block-paragraph">can prove that we can have stuff sent from assembly to c plus plus or c using pointers</p>



<p class="wp-block-paragraph">using pointers we can have data sent over so anyway that&#8217;s why both of these</p>



<p class="wp-block-paragraph">are in here the point needs to be marked as no name mangling because point is</p>



<p class="wp-block-paragraph">inside of assembly which will not name mangle and then hey driver print this</p>



<p class="wp-block-paragraph">that needs to have name mangling disabled also so that the assembly</p>



<p class="wp-block-paragraph">module can call on this other than that we&#8217;re just basically inside of a main</p>



<p class="wp-block-paragraph">saying hey this is the c string we&#8217;re making a c string inside of the main function notice how</p>



<p class="wp-block-paragraph">this is a local variable so that c string is going to show up on the stack it&#8217;s going to show up in</p>



<p class="wp-block-paragraph">the area that is owned by main for main stack area same thing for my long that&#8217;s a local variable on</p>



<p class="wp-block-paragraph">the stack um and but then we can actually send pointers to those pieces of data to another</p>



<p class="wp-block-paragraph">function in another module you don&#8217;t have to only transport globals or stuff on the heap</p>



<p class="wp-block-paragraph">or stuff on the heap, you can transport pointers to local variables. Just make sure that by the</p>



<p class="wp-block-paragraph">time this function finishes, then nowhere else is actually using that data because,</p>



<p class="wp-block-paragraph">well, being on the stack, once main function or once any function finishes, then its portion of</p>



<p class="wp-block-paragraph">the stack will be cleaned up and removed and it&#8217;ll be junk data. You&#8217;ll probably get a seg fault.</p>



<p class="wp-block-paragraph">But for now, we&#8217;re not going to use anything on the stack. We&#8217;re not going to use these local</p>



<p class="wp-block-paragraph">just going to use them quickly on this call to point and then we&#8217;re going to return to the</p>



<p class="wp-block-paragraph">operating system and finish the program. So that&#8217;s the driver. Now the hard part. Let&#8217;s do this in</p>



<p class="wp-block-paragraph">assembly. So for starters, I&#8217;m going to make a data section and just explain it to you very,</p>



<p class="wp-block-paragraph">very quickly. Again, if you don&#8217;t understand the basics of YASM x86-64 assembly, did I mention</p>



<p class="wp-block-paragraph">that that&#8217;s what this language is at the beginning of the video? I guess I should put that in the</p>



<p class="wp-block-paragraph">put that in the description or record an announcement that I can tack on at the beginning</p>



<p class="wp-block-paragraph">or something. Anyway, so if you don&#8217;t understand how to do this, see my other videos, but basically</p>



<p class="wp-block-paragraph">we&#8217;re going to make a data section. We&#8217;re going to define some strings. Here&#8217;s like an announcement.</p>



<p class="wp-block-paragraph">Oh, we&#8217;re inside of, you know, the module now, the assembly module. And now we&#8217;re going to print</p>



<p class="wp-block-paragraph">the received string. And then we&#8217;re going to make a string that is owned by assembly, which we can</p>



<p class="wp-block-paragraph">into C++ when we call the function inside of the driver.</p>



<p class="wp-block-paragraph">So this string is owned by the assembly module.</p>



<p class="wp-block-paragraph">Notice how these are null terminated strings.</p>



<p class="wp-block-paragraph">I just have like a comma zero there,</p>



<p class="wp-block-paragraph">which means I have some extra functions</p>



<p class="wp-block-paragraph">I&#8217;m gonna paste in that we&#8217;re not really gonna talk about</p>



<p class="wp-block-paragraph">because they&#8217;ve been discussed in other videos</p>



<p class="wp-block-paragraph">just so that we can print null terminated strings.</p>



<p class="wp-block-paragraph">Then I&#8217;ve got a new line here,</p>



<p class="wp-block-paragraph">you know, carriage return line feed.</p>



<p class="wp-block-paragraph">And then I&#8217;ve just got some numbers</p>



<p class="wp-block-paragraph">that are owned by the assembly module.</p>



<p class="wp-block-paragraph">Then I&#8217;ve got a system write call,</p>



<p class="wp-block-paragraph">call code one for the system call writes and file descriptor standard output so I</p>



<p class="wp-block-paragraph">can print just to the terminal again if you don&#8217;t understand this see my other</p>



<p class="wp-block-paragraph">videos so now let&#8217;s start the actual text section so this is where our</p>



<p class="wp-block-paragraph">instructions start so we got the text section here and we&#8217;re going to use some</p>



<p class="wp-block-paragraph">external symbols don&#8217;t worry about these I&#8217;m just using my own little library to</p>



<p class="wp-block-paragraph">and input integers if you have access to this library use it if you don&#8217;t if you&#8217;re watching</p>



<p class="wp-block-paragraph">at home and you don&#8217;t have this library then that&#8217;s fine you can use you know printf or</p>



<p class="wp-block-paragraph">scanf or something like that to get and print floats from and to the user</p>



<p class="wp-block-paragraph">but yeah I&#8217;m just using that and then I&#8217;m marking an external function here called hey driver print</p>



<p class="wp-block-paragraph">this if you recall the driver module has a function called hey driver print this so</p>



<p class="wp-block-paragraph">just allows my assembly code to call on that external function. Okay now next</p>



<p class="wp-block-paragraph">piece of code. This is going to be… actually I&#8217;m going to paste the print</p>



<p class="wp-block-paragraph">null terminated string function and related code because it&#8217;s just like a</p>



<p class="wp-block-paragraph">big giant mess and we&#8217;re mostly going to ignore it. So just to show you what I&#8217;m</p>



<p class="wp-block-paragraph">doing here I have a function called print null terminated string so that I</p>



<p class="wp-block-paragraph">can print these strings up here and then I have it rely on a function called</p>



<p class="wp-block-paragraph">string length that I have implemented up here and all it does is just</p>



<p class="wp-block-paragraph">implemented up here and all it does just calculates the length of the string and</p>



<p class="wp-block-paragraph">then a crlf function so I can just call that so that&#8217;s all explained in other</p>



<p class="wp-block-paragraph">videos don&#8217;t worry about it for now we&#8217;re going to start the actual entry</p>



<p class="wp-block-paragraph">point remember the driver was just gonna call point right so now we just have to</p>



<p class="wp-block-paragraph">implement point in the assembly module so that&#8217;s gonna be like down here our</p>



<p class="wp-block-paragraph">our entry point so the signature for this function is going to be character</p>



<p class="wp-block-paragraph">pointer and then a long pointer and it doesn&#8217;t return anything and remember</p>



<p class="wp-block-paragraph">that if we look back at the driver that should match the signature right it&#8217;s a</p>



<p class="wp-block-paragraph">character pointer and a long pointer and of course this is just a comment that</p>



<p class="wp-block-paragraph">reminds me of what to do in assembly you don&#8217;t really have a signature you just</p>



<p class="wp-block-paragraph">sort of use registers but I&#8217;m reminding myself that RDI is going to be a</p>



<p class="wp-block-paragraph">character pointer and RSI is going to be a long pointer.</p>



<p class="wp-block-paragraph">Here&#8217;s a note to myself that I&#8217;m going to use R12 and R13, which means</p>



<p class="wp-block-paragraph">the first thing that I should do, well actually before I even do that, I should</p>



<p class="wp-block-paragraph">return from this function because it is a function. I marked it as global</p>



<p class="wp-block-paragraph">so that the other module could call it, the driver module could call it. Again,</p>



<p class="wp-block-paragraph">see my other videos for hybrid programs.</p>



<p class="wp-block-paragraph">But so now the, you know, if the driver calls this function, then now we&#8217;re inside of</p>



<p class="wp-block-paragraph">and there&#8217;s a return statement so it&#8217;s a valid function I should preserve the</p>



<p class="wp-block-paragraph">registers that I&#8217;m going to use that are marked as Kali saved for the ABI so I&#8217;m</p>



<p class="wp-block-paragraph">going to go prologue and then an epilogue and I&#8217;m going to say push r12 and push</p>



<p class="wp-block-paragraph">r13 and then I&#8217;m going to pop r13 pop r12 they should be in reverse order if</p>



<p class="wp-block-paragraph">you&#8217;ve seen my other videos you&#8217;ll know this and the the thing about this</p>



<p class="wp-block-paragraph">the thing about this particular program is we&#8217;re going to run into stack alignment issues</p>



<p class="wp-block-paragraph">so uh if you don&#8217;t know about stack alignment and how it can crash your program without you</p>



<p class="wp-block-paragraph">realizing what&#8217;s wrong see my other videos but for now we&#8217;ll assume you know that and uh i i</p>



<p class="wp-block-paragraph">already know from running this program in advance that it&#8217;s going to be out of alignment by eight</p>



<p class="wp-block-paragraph">bytes so i&#8217;m just going to push an extra register onto the stack and that&#8217;s going to put it back</p>



<p class="wp-block-paragraph">I know it looks weird, but this is going to work.</p>



<p class="wp-block-paragraph">Let me get rid of this here.</p>



<p class="wp-block-paragraph">Okay, so.</p>



<p class="wp-block-paragraph">And then maybe if I can remember at the end of the video,</p>



<p class="wp-block-paragraph">I can just remove that extra push-pop pair,</p>



<p class="wp-block-paragraph">and you&#8217;ll see the program starts crashing.</p>



<p class="wp-block-paragraph">But at home, you can do it just to double check.</p>



<p class="wp-block-paragraph">So the first thing I really want to do is,</p>



<p class="wp-block-paragraph">after I push and pop,</p>



<p class="wp-block-paragraph">is save our incoming arguments.</p>



<p class="wp-block-paragraph">Remember, the first integer argument</p>



<p class="wp-block-paragraph">and the second integer argument,</p>



<p class="wp-block-paragraph">argument they come in as RDI and RSI in assembly per the ABI if both of these</p>



<p class="wp-block-paragraph">things are pointers it doesn&#8217;t matter what the data type is it could be</p>



<p class="wp-block-paragraph">pointing to anything including a double and these would still be considered</p>



<p class="wp-block-paragraph">integer arguments because well RDI and RSI are just going to be loaded up with</p>



<p class="wp-block-paragraph">memory locations which which are integers so I&#8217;m going to save our</p>



<p class="wp-block-paragraph">arguments to R12 and R13 now justifying our push and pop pair then I&#8217;m going to</p>



<p class="wp-block-paragraph">little welcome message so print a little welcome message again you don&#8217;t need to know about this</p>



<p class="wp-block-paragraph">function but it&#8217;s explained in other videos that I&#8217;ve already published we&#8217;re going to print our</p>



<p class="wp-block-paragraph">hello beginning message I&#8217;m getting nervous he needs to take a second poop sometimes it&#8217;s poopoo</p>



<p class="wp-block-paragraph">number two time for him and he&#8217;s not really just lying about a treat but he did go pee and poop</p>



<p class="wp-block-paragraph">But he did go pee and poop already.</p>



<p class="wp-block-paragraph">Okay, he just left and walked away.</p>



<p class="wp-block-paragraph">Okay, if he comes back, I&#8217;m letting him out this time.</p>



<p class="wp-block-paragraph">I&#8217;ll pause the video if he does it again.</p>



<p class="wp-block-paragraph">Okay, I&#8217;m pausing the video.</p>



<p class="wp-block-paragraph">No pee lied.</p>



<p class="wp-block-paragraph">He went outside, lifted up his little leg, and a couple of drops of pee came out.</p>



<p class="wp-block-paragraph">Now he&#8217;s staring at me like he deserves a treat.</p>



<p class="wp-block-paragraph">Sorry, buddy.</p>



<p class="wp-block-paragraph">I wish I could eat constantly all day long, too.</p>



<p class="wp-block-paragraph">But life isn&#8217;t always fair.</p>



<p class="wp-block-paragraph">isn&#8217;t always fair anyway let&#8217;s see I might even lined up on the camera</p>



<p class="wp-block-paragraph">anymore I don&#8217;t even know so we&#8217;re looking at this code here is going to</p>



<p class="wp-block-paragraph">print a welcome message let&#8217;s see if that actually works so I&#8217;m gonna do make</p>



<p class="wp-block-paragraph">run again make files are whoops what did I do loader dot asm what did I do what</p>



<p class="wp-block-paragraph">did I do I somehow copy pasted the wrong make file</p>



<p class="wp-block-paragraph">What&#8217;s the name of my source code file?</p>



<p class="wp-block-paragraph">It&#8217;s point.</p>



<p class="wp-block-paragraph">I guess I&#8217;ll just change it, and then it&#8217;ll probably work.</p>



<p class="wp-block-paragraph">It&#8217;s still in assembly module.</p>



<p class="wp-block-paragraph">Hopefully that didn&#8217;t mess it up too bad by copy-pasting the wrong source code.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">What is going on here?</p>



<p class="wp-block-paragraph">Floater.</p>



<p class="wp-block-paragraph">Oh, I need to change that.</p>



<p class="wp-block-paragraph">Hang on.</p>



<p class="wp-block-paragraph">Let me fix this.</p>



<p class="wp-block-paragraph">I don&#8217;t know if I&#8217;m going to edit this out.</p>



<p class="wp-block-paragraph">out. It&#8217;s fun to watch me struggle sometimes. There we go.</p>



<p class="wp-block-paragraph">Point.</p>



<p class="wp-block-paragraph">Alright, let&#8217;s give it another try.</p>



<p class="wp-block-paragraph">Oh no, star dot so no such file a directory. Dang it.</p>



<p class="wp-block-paragraph">Okay, now this seems to work. I may or may not have edited</p>



<p class="wp-block-paragraph">that out. I copy pasted the wrong source code into my make</p>



<p class="wp-block-paragraph">file. So I had to manually adjust it. Then I forgot to</p>



<p class="wp-block-paragraph">copy paste my library file into the build directory. So I had</p>



<p class="wp-block-paragraph">The driver sees my long as whatever.</p>



<p class="wp-block-paragraph">What&#8217;s going on?</p>



<p class="wp-block-paragraph">Print an alternate string begin.</p>



<p class="wp-block-paragraph">Oh, the driver is printing a bunch of stuff.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">I started to think, why does it look like the program has a lot of stuff going on?</p>



<p class="wp-block-paragraph">Oh, that&#8217;s the driver.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So the driver says it sees its long as 100.</p>



<p class="wp-block-paragraph">And then now we&#8217;re inside of the point module.</p>



<p class="wp-block-paragraph">So that&#8217;s the only thing we&#8217;ve done in assembly so far.</p>



<p class="wp-block-paragraph">so far then the driver has regained control maybe I should add a couple of</p>



<p class="wp-block-paragraph">new lines in there so I don&#8217;t get confused again we will do a C out and L</p>



<p class="wp-block-paragraph">and we&#8217;ll do two of those run the program again and then I won&#8217;t get</p>



<p class="wp-block-paragraph">confused about the messages okay so now we&#8217;re inside of the point module and</p>



<p class="wp-block-paragraph">nothing is happening so points let me get rid of the make file here and</p>



<p class="wp-block-paragraph">and we&#8217;re just printing a welcome message nothing else so now let&#8217;s print</p>



<p class="wp-block-paragraph">the received string so what am I talking about so we&#8217;re gonna print a prefix</p>



<p class="wp-block-paragraph">basically saying hey we received the following string right so if you look at</p>



<p class="wp-block-paragraph">the symbol message received string it&#8217;s just gonna say we&#8217;re now printing the</p>



<p class="wp-block-paragraph">received string and then it&#8217;ll print it so what are we actually printing we&#8217;re</p>



<p class="wp-block-paragraph">What is R12? R12 is a character pointer to the print me string. And so basically this</p>



<p class="wp-block-paragraph">function print null terminated string, it takes a character pointer. So we&#8217;re giving it a character</p>



<p class="wp-block-paragraph">pointer that we received. When point was called by the driver, notice how it gave a pointer to</p>



<p class="wp-block-paragraph">the C string. You know, all arrays are basically pointers. They&#8217;re just different syntactically</p>



<p class="wp-block-paragraph">just different syntactically sometimes so if i declare an array of some length and i give the</p>



<p class="wp-block-paragraph">symbol somewhere that symbol is really a character pointer so um by calling point with my c string</p>



<p class="wp-block-paragraph">i&#8217;m calling point inside of the assembly module with this character pointer so that means even</p>



<p class="wp-block-paragraph">though this c string is owned by the driver by the c plus plus module the assembly module has access</p>



<p class="wp-block-paragraph">So that means we should be able to print it right now already.</p>



<p class="wp-block-paragraph">So just the rest of it is just like giving a pointer.</p>



<p class="wp-block-paragraph">And notice how I&#8217;m not dereferencing R12.</p>



<p class="wp-block-paragraph">If I did dereferencing around R12, then we would be looking to that address and seeing what&#8217;s there,</p>



<p class="wp-block-paragraph">which wouldn&#8217;t work for printing a null terminated string.</p>



<p class="wp-block-paragraph">So let&#8217;s just run it again.</p>



<p class="wp-block-paragraph">I don&#8217;t know if you can hear him.</p>



<p class="wp-block-paragraph">This dude is growling at me still because he wants another treat.</p>



<p class="wp-block-paragraph">He just got denied.</p>



<p class="wp-block-paragraph">He&#8217;s trying to do it again.</p>



<p class="wp-block-paragraph">do it again. I let him outside people. He&#8217;s been outside like three times already and he just went</p>



<p class="wp-block-paragraph">out like two minutes ago. Okay. I love him so much. It hurts my heart and he knows eventually he&#8217;s</p>



<p class="wp-block-paragraph">going to break me because it hurts my heart or I&#8217;m like too distracted. It&#8217;s like, you know,</p>



<p class="wp-block-paragraph">pulling the crank on a slot machine in Vegas. You know, eventually something comes out.</p>



<p class="wp-block-paragraph">That&#8217;s what he does to me. I&#8217;ve accidentally trained him. So now printing the received</p>



<p class="wp-block-paragraph">Now printing the received string and notice how it prints.</p>



<p class="wp-block-paragraph">Hello, this is a C string owned by me.</p>



<p class="wp-block-paragraph">So our assembly module is able to print a C string that was created locally by a C++ module.</p>



<p class="wp-block-paragraph">So we&#8217;re handing around pointers.</p>



<p class="wp-block-paragraph">Nice.</p>



<p class="wp-block-paragraph">Can you hear me?</p>



<p class="wp-block-paragraph">He&#8217;s getting louder.</p>



<p class="wp-block-paragraph">So now let&#8217;s modify the incoming long.</p>



<p class="wp-block-paragraph">Can you shush your freaking pants, please?</p>



<p class="wp-block-paragraph">Shush your pants.</p>



<p class="wp-block-paragraph">shush your pants you know the sad thing also is he&#8217;s so old that he&#8217;s deaf now</p>



<p class="wp-block-paragraph">so he used to know what shush your pants meant it meant I&#8217;m not listening to you</p>



<p class="wp-block-paragraph">and you might as well stop because I&#8217;m not gonna do anything based on your</p>



<p class="wp-block-paragraph">harassment but now he can&#8217;t hear me say shush your pants so he just harasses me</p>



<p class="wp-block-paragraph">all day and all night okay um so I&#8217;m gonna copy paste a little bit more code</p>



<p class="wp-block-paragraph">Modify the incoming long.</p>



<p class="wp-block-paragraph">So remember again that the point function, it received a pointer to a long.</p>



<p class="wp-block-paragraph">We&#8217;re calling the long change me on the inside of this, but it&#8217;s coming in as R13.</p>



<p class="wp-block-paragraph">And if you notice what I&#8217;m doing here is I&#8217;m just saying let&#8217;s increase the long.</p>



<p class="wp-block-paragraph">So I&#8217;m going to dereference R13 because R13 is a pointer.</p>



<p class="wp-block-paragraph">So I&#8217;m saying let&#8217;s go to the memory and change the long that is inside of memory.</p>



<p class="wp-block-paragraph">And we have to specify that it is a keyword.</p>



<p class="wp-block-paragraph">it as a keyword so that we you know we don&#8217;t confuse the system the system might think are</p>



<p class="wp-block-paragraph">you modifying a keyword or like a double word or like a word like how big is your data all we know</p>



<p class="wp-block-paragraph">is it&#8217;s an integer because it&#8217;s the increase instruction so I&#8217;m saying we got a keyword you</p>



<p class="wp-block-paragraph">know a 64-bit integer sitting at that memory location I want you to dereference it and increase</p>



<p class="wp-block-paragraph">it and going back to the driver we&#8217;re providing a pointer to our long so the long starts off is 100</p>



<p class="wp-block-paragraph">and we&#8217;re just giving a pointer to it the next thing that we can do is we can</p>



<p class="wp-block-paragraph">ask the driver to print our own stuff actually you know what let&#8217;s run the program right now</p>



<p class="wp-block-paragraph">just to show that the driver can see the change in the long so i&#8217;m going to run it again notice how</p>



<p class="wp-block-paragraph">first when the driver says hello it sees its own long as 100 then we&#8217;re inside the assembly module</p>



<p class="wp-block-paragraph">long and then we return to the caller which is the driver notice how at the</p>



<p class="wp-block-paragraph">very end of the program the driver sees its long as being 101 so we were able to</p>



<p class="wp-block-paragraph">modify data that was owned by a different module just by passing pointers</p>



<p class="wp-block-paragraph">and de-referencing them okay cool so now the next thing that we should do is let&#8217;s</p>



<p class="wp-block-paragraph">ask the driver to print our own stuff that we own because remember if you go</p>



<p class="wp-block-paragraph">to the very top you know we own some stuff we own some we own a long we own</p>



<p class="wp-block-paragraph">float, right? So we want to be able to do something with that. So I&#8217;m going to copy paste this,</p>



<p class="wp-block-paragraph">ask the driver to print our own stuff. So I&#8217;m going to move three items inside of arguments</p>



<p class="wp-block-paragraph">for a function call. And then I&#8217;m going to make a function call calling the function,</p>



<p class="wp-block-paragraph">Hey driver, print this again, Hey driver, print this is actually owned by the C++ module.</p>



<p class="wp-block-paragraph">a pointer to a long and a pointer to a double remember even pointers to doubles are actually</p>



<p class="wp-block-paragraph">integers so they use the general purpose register so that&#8217;s the three arguments right there rdi rsi</p>



<p class="wp-block-paragraph">and rdx m and then we&#8217;re giving the first pointer is going to be the c string so message string</p>



<p class="wp-block-paragraph">inside asm so you can see that&#8217;s this right here and then the next pointer is the long</p>



<p class="wp-block-paragraph">inside ASM and the third is the float where did I just go I&#8217;m getting confused my dog is harassing</p>



<p class="wp-block-paragraph">me right now so bad notice how I&#8217;m not dereferencing so like if when we were increasing the incoming</p>



<p class="wp-block-paragraph">long before R13 was a pointer so we dereferenced while we increased so that we would increase the</p>



<p class="wp-block-paragraph">actual value and not the pointer and not the pointer&#8217;s memory location but here we&#8217;re not</p>



<p class="wp-block-paragraph">C++ module the actual pointers to our data. We don&#8217;t want to give it the data itself. We want</p>



<p class="wp-block-paragraph">to give pointers to the data so we&#8217;re not derefing with the brackets. So then we call it and when we</p>



<p class="wp-block-paragraph">get back in here it should just be able to print everything. So I&#8217;m going to run it one more time.</p>



<p class="wp-block-paragraph">We&#8217;re going to make it and run it and so now let&#8217;s see. So here we&#8217;re inside of our assembly module</p>



<p class="wp-block-paragraph">And then here the assembly module has just called on hey driver print this.</p>



<p class="wp-block-paragraph">Remember the C++ module doesn&#8217;t actually call this function.</p>



<p class="wp-block-paragraph">The assembly module calls it.</p>



<p class="wp-block-paragraph">So we&#8217;re like going back and forth.</p>



<p class="wp-block-paragraph">We&#8217;re kind of crisscrossing.</p>



<p class="wp-block-paragraph">So now the drivers print this function says we got the following string.</p>



<p class="wp-block-paragraph">Notice how that&#8217;s the string that is owned by assembly.</p>



<p class="wp-block-paragraph">So we define that inside of our data section in the assembly module.</p>



<p class="wp-block-paragraph">And then it prints the long.</p>



<p class="wp-block-paragraph">It prints it as hex.</p>



<p class="wp-block-paragraph">And it just sort of prints the value.</p>



<p class="wp-block-paragraph">it just sort of prints the value then it prints it as hex again and then prints at the value</p>



<p class="wp-block-paragraph">i think actually not hex i think this prints the memory location let&#8217;s double check real fast</p>



<p class="wp-block-paragraph">yeah so remember um in c plus plus i know this is not like a c plus plus video but um</p>



<p class="wp-block-paragraph">if the long is a pointer then if we just print it without dereferencing it we should see a memory</p>



<p class="wp-block-paragraph">location so it&#8217;s telling us uh that the long&#8217;s memory location is this and the doubles memory</p>



<p class="wp-block-paragraph">location is that and if you stare at those two numbers long enough and you understand hex which</p>



<p class="wp-block-paragraph">And do you understand hex, which you can see my other videos for?</p>



<p class="wp-block-paragraph">You&#8217;ll see that those memory locations are right next to each other because that&#8217;s the way we define them inside of assembly.</p>



<p class="wp-block-paragraph">So we now have the ability to have data that is owned by assembly and give it to C++ or C using pointers.</p>



<p class="wp-block-paragraph">No problem at all.</p>



<p class="wp-block-paragraph">And then the printing driver thing exits and then the actual driver regains control.</p>



<p class="wp-block-paragraph">And it just says that it sees it&#8217;s long as 101.</p>



<p class="wp-block-paragraph">it sees it&#8217;s long as 101 so uh yeah that&#8217;s that&#8217;s pretty much all i wanted to show you for this</p>



<p class="wp-block-paragraph">now you hopefully are an expert at passing data back and forth between various modules using</p>



<p class="wp-block-paragraph">pointers we&#8217;re not using references because references are like a little bit a little bit</p>



<p class="wp-block-paragraph">less compatible pointers are just really easy they totally work in assembly no problem</p>



<p class="wp-block-paragraph">one more thing i just wanted to show you real fast before we go even though there&#8217;s another</p>



<p class="wp-block-paragraph">video you should check out for stack alignment I just want you to see what</p>



<p class="wp-block-paragraph">happens if I remove this extra push-pop pair so now my stack is about eight</p>



<p class="wp-block-paragraph">bytes off of its previous alignment because you know we&#8217;re not pushing an</p>



<p class="wp-block-paragraph">extra eight byte value and somewhere inside of the let&#8217;s see print null</p>



<p class="wp-block-paragraph">terminated string and then the hey driver print this oh and then we go into</p>



<p class="wp-block-paragraph">like a bunch of C stuff the program should probably crash because anytime</p>



<p class="wp-block-paragraph">you use a GCC function or a GCC library or something like that the stack has to</p>



<p class="wp-block-paragraph">be aligned to 16 bytes so if it&#8217;s off by 8 then it&#8217;ll crash and how did I know</p>



<p class="wp-block-paragraph">that I needed this well I just ran it first and it crashed and then I added</p>



<p class="wp-block-paragraph">the extra push pop pair and it didn&#8217;t crash and I realized it was definitely</p>



<p class="wp-block-paragraph">one more time we should get a seg fault yeah we get a seg fault stack alignment oh no with no</p>



<p class="wp-block-paragraph">description of what&#8217;s going on if you were in gcc you could i mean sorry if you were in gdb you</p>



<p class="wp-block-paragraph">could probably figure that out eventually but why not just give it a try add another push pop pair</p>



<p class="wp-block-paragraph">run the program again with no other modifications now it totally works</p>



<p class="wp-block-paragraph">okay well uh i think that&#8217;s uh that&#8217;s all i have for this video thank you so much for watching i</p>



<p class="wp-block-paragraph">I hope you learned a little bit of stuff and you had a little bit of fun.</p>



<p class="wp-block-paragraph">I will see you in the next video.</p>



<p class="wp-block-paragraph">Hey everybody.</p>



<p class="wp-block-paragraph">Thanks for watching this video again from the bottom of my heart.</p>



<p class="wp-block-paragraph">I really appreciate it.</p>



<p class="wp-block-paragraph">I do hope you did learn something and have some fun.</p>



<p class="wp-block-paragraph">If you could do me a please, a small little favor,</p>



<p class="wp-block-paragraph">could you please subscribe and follow this channel or these videos</p>



<p class="wp-block-paragraph">or whatever it is you do on the current social media website</p>



<p class="wp-block-paragraph">that you&#8217;re looking at right now.</p>



<p class="wp-block-paragraph">It would really mean the world to me</p>



<p class="wp-block-paragraph">would really mean the world to me and it&#8217;ll help make more videos and grow</p>



<p class="wp-block-paragraph">this community so we&#8217;ll be able to do more videos longer videos better videos</p>



<p class="wp-block-paragraph">or just I&#8217;ll be able to keep making videos in general so please do do me a</p>



<p class="wp-block-paragraph">kindness and and subscribe you know sometimes I&#8217;m sleeping in the middle of</p>



<p class="wp-block-paragraph">the night and I just wake up because I know somebody subscribed or followed it</p>



<p class="wp-block-paragraph">just wakes me up and I get filled with joy that&#8217;s exactly what happens every</p>



<p class="wp-block-paragraph">single time so you could do it as a nice favor to me or you could you control me</p>



<p class="wp-block-paragraph">up in the middle of the night just subscribe and then I&#8217;ll just wake up I promise that&#8217;s what will</p>



<p class="wp-block-paragraph">happen also if you look at the middle of the screen right now you should see a QR code which</p>



<p class="wp-block-paragraph">you can scan in order to go to the website which I think is also named somewhere at the bottom of</p>



<p class="wp-block-paragraph">this video and it&#8217;ll take you to my main website where you can just kind of like see all the videos</p>



<p class="wp-block-paragraph">I published and the services and tutorials and things that I offer and all that good stuff and</p>



<p class="wp-block-paragraph">for</p>



<p class="wp-block-paragraph">Clarifications or errata or just future videos that you want to see please leave a comment or if you just want to say hey</p>



<p class="wp-block-paragraph">What&#8217;s up? What&#8217;s going on? You know, just send me a comment, whatever</p>



<p class="wp-block-paragraph">I also wake up for those in the middle of the night. I get I wake up in a cold sweat. I&#8217;m like this</p>



<p class="wp-block-paragraph">It would really it really mean the world to me. I would really appreciate it. So again, thank you so much for watching this video and</p>



<p class="wp-block-paragraph">darkness, which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-pointers-dereferencing-explained-hybrid-c-yasm-example/">x86-64 Assembly Pointers &amp; Dereferencing Explained &#8211; Hybrid C++/YASM Example</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/x86-64-assembly-pointers-dereferencing-explained-hybrid-c-yasm-example/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Signed Integer Division in x86-64 Assembly with IDIV and CQO</title>
		<link>https://www.NeuralLantern.com/signed-integer-division-in-x86-64-assembly-with-idiv-and-cqo/</link>
					<comments>https://www.NeuralLantern.com/signed-integer-division-in-x86-64-assembly-with-idiv-and-cqo/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 14 Dec 2025 09:12:38 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[64-bit assembly]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[assembly language programming]]></category>
		<category><![CDATA[assembly tutorial]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[cqo instruction]]></category>
		<category><![CDATA[idiv instruction]]></category>
		<category><![CDATA[integer division assembly]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[rax rdx division]]></category>
		<category><![CDATA[signed division]]></category>
		<category><![CDATA[ubuntu assembly programming]]></category>
		<category><![CDATA[x86 assembly]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=266</guid>

					<description><![CDATA[<p>Step-by-step tutorial on performing signed integer division in x86-64 assembly language using the IDIV instruction and CQO for sign extension. Includes complete working Yasm code on Ubuntu/Linux.</p>
<p>The post <a href="https://www.NeuralLantern.com/signed-integer-division-in-x86-64-assembly-with-idiv-and-cqo/">Signed Integer Division in x86-64 Assembly with IDIV and CQO</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Signed Integer Division in x86-64 Assembly with IDIV and CQO" width="1380" height="776" src="https://www.youtube.com/embed/axasPyjJFAA?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">In this video, I walk through how to perform signed integer division in x86-64 assembly language using Yasm on Ubuntu/Linux.</p>



<p class="wp-block-paragraph">We focus on 64-bit signed integers and the IDIV instruction. I explain the setup: loading the dividend into RAX, using CQO for proper sign extension into RDX, loading the divisor, and executing IDIV. After division, the quotient ends up in RAX and the remainder in RDX.</p>



<p class="wp-block-paragraph">I reference the excellent free book &#8220;x86-64 Assembly Language Programming with Ubuntu&#8221; and show practical code examples, including a complete working program that prints both the quotient and remainder.</p>



<p class="wp-block-paragraph">We test with small numbers (256 / 233) and larger ones to see it in action. I also touch on the difference between signed (IDIV) and unsigned (DIV) division, and why CQO is needed for correct sign handling.</p>



<p class="wp-block-paragraph">Perfect for anyone learning low-level programming, assembly language, or wanting to understand how integer division really works under the hood.</p>



<p class="wp-block-paragraph">Introduction to Signed Division 00:00:00<br>Referencing the Assembly Book 00:00:28<br>IDIV Instruction Overview 00:01:16<br>Dividend and Divisor Setup 00:01:24<br>Sign Extension and CQO 00:04:18<br>Quotient and Remainder Results 00:04:40<br>Setting Up the Program 00:05:13<br>Data Section and Strings 00:06:22<br>Function Prologue and Preservation 00:09:20<br>Loading the Dividend into RAX 00:10:00<br>Sign Extension with CQO 00:11:24<br>Loading the Divisor 00:12:36<br>Performing IDIV 00:14:19<br>Saving Quotient and Remainder 00:16:48<br>Printing the Results 00:17:20<br>Running and Testing the Program 00:18:51<br>Testing Larger Numbers 00:19:48<br>Returning the Remainder 00:21:42<br>Signed vs Unsigned Division 00:23:48<br>Conclusion and Thanks 00:25:02</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>
</ul>



<p class="wp-block-paragraph">Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



<li>Leaving a comment or suggestion</li>



<li>Subscribing to our Blog</li>



<li>Watching the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p class="wp-block-paragraph">Hello there.</p>



<p class="wp-block-paragraph">In this video, I&#8217;d like to talk to you about dividing signed integers in an x86-64 Yasm</p>



<p class="wp-block-paragraph">assembly program.</p>



<p class="wp-block-paragraph">I&#8217;m going to be focusing on 64-bit integers and I&#8217;m going to use an instruction called</p>



<p class="wp-block-paragraph">iDiv.</p>



<p class="wp-block-paragraph">Okay, so for starters, I just want to give you an overview of the process.</p>



<p class="wp-block-paragraph">I&#8217;m going to use an awesome book that I love to reference.</p>



<p class="wp-block-paragraph">awesome book that i love to reference i did not write this book the book is titled x86 64 assembly</p>



<p class="wp-block-paragraph">language programming with ubuntu i mention it in a lot of other videos the author here</p>



<p class="wp-block-paragraph">is a genius and this book is open source and freely available so go look it up get yourself a</p>



<p class="wp-block-paragraph">copy and become an assembly expert too anyway so what i&#8217;m going to do is go into this book</p>



<p class="wp-block-paragraph">instruction set overview and then inside of that i&#8217;m going to click on 7.5 integer arithmetic</p>



<p class="wp-block-paragraph">instructions or arithmetic i don&#8217;t know whatever you prefer to say it as but and then i&#8217;m going to</p>



<p class="wp-block-paragraph">go inside of there and i&#8217;m going to click on 754 integer division so you know the basic idea for</p>



<p class="wp-block-paragraph">dividing integers in a yasm assembly program on x86 64 is just to load up your dividend you know</p>



<p class="wp-block-paragraph">top portion I guess that is the numerator and then the divisor which</p>



<p class="wp-block-paragraph">would be the denominator with separate registers so load up the dividend with a</p>



<p class="wp-block-paragraph">register or put the dividend into a register and then put the divisor into</p>



<p class="wp-block-paragraph">another register they&#8217;re going to be special registers we&#8217;re going to be</p>



<p class="wp-block-paragraph">using the A and D registers you can kind of see that at the bottom already and</p>



<p class="wp-block-paragraph">then once that&#8217;s set up properly you just sort of use the IDIV function or</p>



<p class="wp-block-paragraph">instruction and there&#8217;s another instruction we can use to make things a</p>



<p class="wp-block-paragraph">little bit more simple because if you kind of notice what what this is saying</p>



<p class="wp-block-paragraph">here it&#8217;s saying that the the dividend usually needs to be stacked between two</p>



<p class="wp-block-paragraph">other registers at the same time and that&#8217;s a little bit confusing</p>



<p class="wp-block-paragraph">especially if these registers right here notice how a DX colon ax that means it</p>



<p class="wp-block-paragraph">16-bit versions of two different registers just sort of like pushed up against each other so</p>



<p class="wp-block-paragraph">Obviously if you know how to manipulate bits which I&#8217;m gonna do other videos for in the future</p>



<p class="wp-block-paragraph">Then you could do that with any size, but just I&#8217;m gonna make the easiest example that I can here. I</p>



<p class="wp-block-paragraph">Love this line. I just found it today</p>



<p class="wp-block-paragraph">As always division by zero will crash the program and damage the space-time continuum. So try not to divide by zero. I love it</p>



<p class="wp-block-paragraph">nifty little table inside of the book that sort of shows you how you&#8217;re supposed to set up the</p>



<p class="wp-block-paragraph">numerator and denominator or the divisor what was the other word that they used dividend and divisor</p>



<p class="wp-block-paragraph">i usually say numerator and denominator anyway so remember in in if you watched my previous videos</p>



<p class="wp-block-paragraph">then you&#8217;ll know that we have smaller versions of our registers like for example uh this eax right</p>



<p class="wp-block-paragraph">here that&#8217;s actually the rax register but it&#8217;s only about half of it uh half of those bytes are</p>



<p class="wp-block-paragraph">half of those bytes are being ignored so all of these different versions that you&#8217;re seeing are</p>



<p class="wp-block-paragraph">just different sizes of the a and d registers so i&#8217;m going to go to the simplest example here and</p>



<p class="wp-block-paragraph">just kind of show you that basically what they&#8217;re trying to do is force you to have a numerator that</p>



<p class="wp-block-paragraph">is bigger than the denominator that way it&#8217;s guaranteed you&#8217;ll have some kind of a quotient</p>



<p class="wp-block-paragraph">or you know more likely that you&#8217;ll have some kind of a quotient and and a remainder that kind of</p>



<p class="wp-block-paragraph">that kind of makes sense because if you divide a number that&#8217;s much smaller than</p>



<p class="wp-block-paragraph">the denominator then you&#8217;ll just end up with zero remainder the original number</p>



<p class="wp-block-paragraph">pretty much in integer division this video is not about float division float</p>



<p class="wp-block-paragraph">division is more precise so in other words if I want to divide something by a</p>



<p class="wp-block-paragraph">64-bit integer then I need to first set up a 128-bit integer and that means I</p>



<p class="wp-block-paragraph">and that means I have to take two 64-bit integers, the D register and the A register.</p>



<p class="wp-block-paragraph">I have to load both of those registers up with 64 bits each so that when you imagine them being combined,</p>



<p class="wp-block-paragraph">it&#8217;s a 128-bit register or 128-bit value.</p>



<p class="wp-block-paragraph">Once I have something that is sufficiently large, then I can divide it by something else.</p>



<p class="wp-block-paragraph">Then the result will show up in the A and D registers.</p>



<p class="wp-block-paragraph">the actual quotient, you know, the actual result of dividing without a fractional part at the end,</p>



<p class="wp-block-paragraph">just, you know, truncated data. Then the remainder will be in the D register, the RDX register. So at</p>



<p class="wp-block-paragraph">least for this form, I just want you to know you can do it with all these other forms if you want</p>



<p class="wp-block-paragraph">to. But in this example, I&#8217;m just going to take 128 bits, divide it by 64 bits, and then I&#8217;ll get</p>



<p class="wp-block-paragraph">So that&#8217;s the basic idea. Here&#8217;s some sample code in the book. I love it. It&#8217;s wonderful.</p>



<p class="wp-block-paragraph">But I&#8217;m going to go ahead and start writing my own program now.</p>



<p class="wp-block-paragraph">Okay, so I got a solution up here. I&#8217;m going to just copy paste my data section and explain it briefly.</p>



<p class="wp-block-paragraph">Okay, so here is, oh, maybe I should first say, here&#8217;s a sample program that I&#8217;m using.</p>



<p class="wp-block-paragraph">You don&#8217;t have to worry too much about what&#8217;s actually inside of it.</p>



<p class="wp-block-paragraph">The C module and the make file, they&#8217;re standard stuff that I usually use in all of my videos.</p>



<p class="wp-block-paragraph">all of my videos this is not a make file video see my other videos if you want to learn how to</p>



<p class="wp-block-paragraph">make your own make files but long story short i&#8217;m just going to type you know make run in order to</p>



<p class="wp-block-paragraph">just get this program to run and then for the c program i&#8217;m compiling a hybrid program so i mean</p>



<p class="wp-block-paragraph">it&#8217;s just easier for me to just kind of get started this way so i have my main entry point sitting</p>



<p class="wp-block-paragraph">inside of a c source code file that gets compiled and linked into my main program and it just calls</p>



<p class="wp-block-paragraph">a function called math right here which is just going to be you know this source code right here</p>



<p class="wp-block-paragraph">so if you want to make hybrid programs you want to learn the basics of assembly you want to learn</p>



<p class="wp-block-paragraph">anything that i&#8217;m not really talking about in this video see my other videos</p>



<p class="wp-block-paragraph">for today or for this video it&#8217;s just going to be integer division anyway i&#8217;m going to paste in</p>



<p class="wp-block-paragraph">some starter code here for my math module i&#8217;ve got a data section and i&#8217;ve got some strings</p>



<p class="wp-block-paragraph">strings or anything but just keep in mind I&#8217;m going to eventually print the division result</p>



<p class="wp-block-paragraph">quotient is and then I&#8217;m going to actually print it and then I&#8217;m going to print the division</p>



<p class="wp-block-paragraph">remainder is and then actually print it so I&#8217;m just I&#8217;m just setting up strings to make my</p>



<p class="wp-block-paragraph">results look a little bit prettier that&#8217;s all I&#8217;m really doing crlf that&#8217;s just new line stuff</p>



<p class="wp-block-paragraph">system calls here&#8217;s the system service code for writing to something writing to a file or standard</p>



<p class="wp-block-paragraph">standard output again that&#8217;s in another video file descriptor one another video and then return</p>



<p class="wp-block-paragraph">value I&#8217;m just choosing to return the number seven from this module I don&#8217;t know why I just</p>



<p class="wp-block-paragraph">I like to reuse code and I&#8217;m not going to change it and then we&#8217;re going to take two different</p>



<p class="wp-block-paragraph">integers here and uh 56 and oh I think in my starter code I actually hard-coded those numbers</p>



<p class="wp-block-paragraph">after everything is working so um i&#8217;m going to start my text section here by just copy pasting</p>



<p class="wp-block-paragraph">a little thing that says okay here&#8217;s where my code is going to go now all my instructions</p>



<p class="wp-block-paragraph">and then uh here&#8217;s my entry point which is just my function called math you want to write functions</p>



<p class="wp-block-paragraph">see my other videos but this is basically it you just kind of have something enter and then return</p>



<p class="wp-block-paragraph">from it when you&#8217;re done and if you mess up any registers you have to preserve them with push and</p>



<p class="wp-block-paragraph">you have to preserve them with push and pop pairs.</p>



<p class="wp-block-paragraph">Another thing that I&#8217;m going to add that I&#8217;m not going to explain in this video</p>



<p class="wp-block-paragraph">is I&#8217;m going to do calls to one of my own personal libraries.</p>



<p class="wp-block-paragraph">And basically I&#8217;m just going to mark two functions as external.</p>



<p class="wp-block-paragraph">They&#8217;re sitting in a shared object that I made.</p>



<p class="wp-block-paragraph">Don&#8217;t worry about that. This video is not about printing.</p>



<p class="wp-block-paragraph">It&#8217;s just about dividing. But I want to be able to easily print.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to use that library.</p>



<p class="wp-block-paragraph">So don&#8217;t worry about that.</p>



<p class="wp-block-paragraph">for this video is my function called divide test because notice how in the entry point of math</p>



<p class="wp-block-paragraph">remember the c module the driver it calls on this math function the math function eventually or</p>



<p class="wp-block-paragraph">immediately calls on divide test and so here&#8217;s the prototype basically for my divide test function</p>



<p class="wp-block-paragraph">I&#8217;m just going to say it&#8217;s void takes no arguments that way it can just do everything by itself</p>



<p class="wp-block-paragraph">as a little demo and it&#8217;s a little bit easier so that means I don&#8217;t really need to preserve any</p>



<p class="wp-block-paragraph">I am using them and if you see here well I guess if it was going to take arguments you don&#8217;t need</p>



<p class="wp-block-paragraph">to preserve the argument register so I should have just said yeah we are going to use some</p>



<p class="wp-block-paragraph">registers and therefore we&#8217;re going to preserve them so our 12 14 and 15 oh why am I not using</p>



<p class="wp-block-paragraph">13 I must have erased that at an earlier date whatever we&#8217;re going to use these three registers</p>



<p class="wp-block-paragraph">make this an official function and because I know I&#8217;m going to use r12 14 and 15 I&#8217;m just going to</p>



<p class="wp-block-paragraph">preserve them with a push pop pair so I&#8217;m going to say this is the prologue whoops and I&#8217;m going to</p>



<p class="wp-block-paragraph">push r12 push r14 push r15 respect the abi if you don&#8217;t other pro other parts of the program</p>



<p class="wp-block-paragraph">will probably crash epilogue is just popping all those in reverse order so that I can basically</p>



<p class="wp-block-paragraph">basically preserve them for the caller.</p>



<p class="wp-block-paragraph">Okay, so now we&#8217;re good with all our registers.</p>



<p class="wp-block-paragraph">Next thing I want to do is I want to load up, let&#8217;s see,</p>



<p class="wp-block-paragraph">I want to load up RAX with a number.</p>



<p class="wp-block-paragraph">Now maybe I&#8217;ll fix this right now.</p>



<p class="wp-block-paragraph">What was 256?</p>



<p class="wp-block-paragraph">It was the my integer B.</p>



<p class="wp-block-paragraph">So I&#8217;m going to hit main memory,</p>



<p class="wp-block-paragraph">which is a little slower than using an immediate,</p>



<p class="wp-block-paragraph">but hey, it&#8217;s more flexible.</p>



<p class="wp-block-paragraph">but hey it&#8217;s more flexible and so basically i&#8217;m going to set up rax which is the numerator if we</p>



<p class="wp-block-paragraph">go back to the book real fast if our intention is to divide a 128 bit uh uh like combined register</p>



<p class="wp-block-paragraph">by a 64 bit something then uh well rax is the lower 64 bit so that&#8217;s why i&#8217;m loading that up</p>



<p class="wp-block-paragraph">first i&#8217;m just saying well i&#8217;ve got 64 bits worth of stuff a quad word worth of stuff sitting in</p>



<p class="wp-block-paragraph">so i&#8217;m just going to load that into rax then i just have to somehow take care of rdx so obviously</p>



<p class="wp-block-paragraph">if you wanted to you could just you know take rdx and just set a bunch of zeros to it but that</p>



<p class="wp-block-paragraph">would probably only work if you were guaranteed that rax was a positive number so that its sign</p>



<p class="wp-block-paragraph">bit was zero at the very left if it was a negative number its sign bit would be a one at the very</p>



<p class="wp-block-paragraph">And that wouldn&#8217;t be very good if RDX plus RAX, you know, lined up are supposed to be a negative number because you would want ones all the way through to RDX.</p>



<p class="wp-block-paragraph">So there&#8217;s a special instruction we can use called CQO.</p>



<p class="wp-block-paragraph">I&#8217;m just going to paste that in here just to make things a little bit easier because I&#8217;m really trying to divide a 64-bit integer by something.</p>



<p class="wp-block-paragraph">The system wants a 128-bit integer.</p>



<p class="wp-block-paragraph">basically this instruction right here CQO whoops what happened how come it&#8217;s not lined up</p>



<p class="wp-block-paragraph">CQO basically just takes RAX whatever is in there and stretches it onto the 128 bit combination of</p>



<p class="wp-block-paragraph">RDX colon RAX so remember when you see this kind of thing remember when you see RDX colon RAX or</p>



<p class="wp-block-paragraph">just like a register colon another register that&#8217;s usually telling you that the system will think of</p>



<p class="wp-block-paragraph">in a combined way as one longer register so that&#8217;s really what it is cqo is going to say</p>



<p class="wp-block-paragraph">let&#8217;s look at rex and let&#8217;s uh you know fix it up and then also fix up uh rdx so that when they&#8217;re</p>



<p class="wp-block-paragraph">combined they make sense as just one number um and you know that that would work pretty easily</p>



<p class="wp-block-paragraph">like i said before if it was a positive number but not necessarily in all cases so we&#8217;ll just use</p>



<p class="wp-block-paragraph">is, let&#8217;s see, set up the denominator. So let&#8217;s see, here, I&#8217;m going to say set up denominator.</p>



<p class="wp-block-paragraph">And you know, we can hard code the 233 there, but I think I&#8217;ve already decided,</p>



<p class="wp-block-paragraph">let&#8217;s just use variables. So the A, I think has 233. Where the heck&#8217;s my A? There we go.</p>



<p class="wp-block-paragraph">So I&#8217;m going to say my integer A. And here I&#8217;m going to put the pointer to the A variable,</p>



<p class="wp-block-paragraph">to the a variable and then i&#8217;m going to dereference it and um oh one thing that we have to remember is</p>



<p class="wp-block-paragraph">when the system is using your registers like rax here it knows rax is the 64-bit form of the a</p>



<p class="wp-block-paragraph">register so if you use different forms of your register then it knows how many bits you want</p>



<p class="wp-block-paragraph">like for example here we just kind of go back up uh this eax that&#8217;s still the a register but the e</p>



<p class="wp-block-paragraph">that it&#8217;s a you know 32 bits instead of 64 bits but there&#8217;s no there&#8217;s no way for</p>



<p class="wp-block-paragraph">the system to really know that that memory location is 64 bits how does it</p>



<p class="wp-block-paragraph">how does it know if I&#8217;m if I&#8217;m trying to load from memory a one byte integer or</p>



<p class="wp-block-paragraph">two byte integer or four bytes or eight bytes it doesn&#8217;t really know so I kind</p>



<p class="wp-block-paragraph">of have to specify what the data size is of the integer at that place in memory</p>



<p class="wp-block-paragraph">this probably wouldn&#8217;t even compile. But if it did compile, you probably should be nervous that</p>



<p class="wp-block-paragraph">it would compile and do something wrong. So I&#8217;m just going to specify exactly what the data size</p>



<p class="wp-block-paragraph">is for that. So now we&#8217;ve set up our numerator and also set up our denominator. And the next step is</p>



<p class="wp-block-paragraph">to just actually divide. Let me copy paste that real fast. So now we actually divide. Why is my</p>



<p class="wp-block-paragraph">Oh, I know what&#8217;s going on.</p>



<p class="wp-block-paragraph">I have the tab size set up differently between my two editors.</p>



<p class="wp-block-paragraph">I got to fix that.</p>



<p class="wp-block-paragraph">Or how about never fix it so I can always complain and you can laugh at me.</p>



<p class="wp-block-paragraph">That&#8217;s okay.</p>



<p class="wp-block-paragraph">So on line 85, we&#8217;re going to divide the numerator by the denominator.</p>



<p class="wp-block-paragraph">And we&#8217;ll say the numerator is, you know, RAX with RDX in front of it.</p>



<p class="wp-block-paragraph">the denominator is just one register which is uh what have I done wrong here oh no no I&#8217;m okay I&#8217;m</p>



<p class="wp-block-paragraph">okay r12 so if you look back at the I got a little confused here because uh the book you know it</p>



<p class="wp-block-paragraph">wants uh the d and the a registers as the numerator but then it says op64 as the denominator you can</p>



<p class="wp-block-paragraph">just kind of use a bunch of different stuff for that but I&#8217;m choosing to divide by another register</p>



<p class="wp-block-paragraph">So basically I&#8217;m taking the denominator and loading it up into R12.</p>



<p class="wp-block-paragraph">And then when I call my idiv instruction, I&#8217;m just saying integer division.</p>



<p class="wp-block-paragraph">And I want you to divide whatever&#8217;s in the RDX, RAX set as I want you to divide it by R12.</p>



<p class="wp-block-paragraph">At that point, RAX and RDX will be overwritten.</p>



<p class="wp-block-paragraph">So that data is now destroyed, but they contain the answer.</p>



<p class="wp-block-paragraph">So RAX now contains the answer.</p>



<p class="wp-block-paragraph">I&#8217;m just gonna say quotient and the RAX register contains the remainder so for</p>



<p class="wp-block-paragraph">example if you are familiar with the let me pull this up real fast is that the</p>



<p class="wp-block-paragraph">calculator no there is if you&#8217;re familiar with the modulo instruction like if we</p>



<p class="wp-block-paragraph">said let&#8217;s see what was it again 233 okay so let&#8217;s do 256 divided by 233 this</p>



<p class="wp-block-paragraph">this gives us the number one and then some sort of like a fractional remainder</p>



<p class="wp-block-paragraph">with integer division we&#8217;re not going to get the fractional remainder it&#8217;s just literally going to</p>



<p class="wp-block-paragraph">be the number one sitting in rax and then in rdx it&#8217;s going to give us the result of using the</p>



<p class="wp-block-paragraph">modulo operator which is going to be 23 so it&#8217;s going to be one remainder 23</p>



<p class="wp-block-paragraph">is what we should see unless i&#8217;m totally wrong so then right away</p>



<p class="wp-block-paragraph">of our division which is a good practice you know like the rex and rdx those are registers that</p>



<p class="wp-block-paragraph">could be quickly destroyed especially if you called another function or a system call or</p>



<p class="wp-block-paragraph">something so i&#8217;m just going to save them right away so that&#8217;s why we saved 14 and 15</p>



<p class="wp-block-paragraph">in addition to r12 so r12 we were using for the denominator and then we&#8217;re using 14 and 15 to</p>



<p class="wp-block-paragraph">save them real fast and now I&#8217;m just going to do easy stuff that I&#8217;ve talked about in other videos</p>



<p class="wp-block-paragraph">just to sort of print out an answer so I&#8217;m just going to I&#8217;m just going to write this up real</p>



<p class="wp-block-paragraph">fast copy paste this and then just kind of briefly talk about oh look I&#8217;m using a system call</p>



<p class="wp-block-paragraph">explained in another video to print out that little prefix message you know the result of your</p>



<p class="wp-block-paragraph">division here&#8217;s the quotient and then a colon and then I can actually print the number</p>



<p class="wp-block-paragraph">personal library to print a 64-bit integer and so that way it&#8217;s easy for me and notice how I&#8217;m</p>



<p class="wp-block-paragraph">printing r14 I&#8217;m giving it to the call to the function as the first argument which is rdi</p>



<p class="wp-block-paragraph">what is r14 again that&#8217;s just the quotient so here I&#8217;m printing the quotient and then I do the</p>



<p class="wp-block-paragraph">same thing for the remainder and then we&#8217;re going to do a paste here and I&#8217;m just going to print my</p>



<p class="wp-block-paragraph">prefix of the remainder you know here&#8217;s your remainder and then I&#8217;m going to actually print</p>



<p class="wp-block-paragraph">And then I&#8217;m going to actually print it.</p>



<p class="wp-block-paragraph">And the remainder was in R15.</p>



<p class="wp-block-paragraph">So 14 and 15 had the answer.</p>



<p class="wp-block-paragraph">And then the other register just kind of had, or the R12 just kind of had the denominator.</p>



<p class="wp-block-paragraph">So I think that&#8217;s pretty much all I need.</p>



<p class="wp-block-paragraph">Oh, there&#8217;s one more function that I forgot to put in there.</p>



<p class="wp-block-paragraph">I&#8217;m fond of writing a little CRLF function.</p>



<p class="wp-block-paragraph">This is not part of the video, really.</p>



<p class="wp-block-paragraph">But it&#8217;s just a special function that I can call on to do a carriage return line feed.</p>



<p class="wp-block-paragraph">And I&#8217;m just, I just can never stop using this.</p>



<p class="wp-block-paragraph">stop using this. I know I could just prefix or suffix my strings, you know, with like a CRLF,</p>



<p class="wp-block-paragraph">but I like to be able to call every time I want to see our lab. Okay. So we have the program now.</p>



<p class="wp-block-paragraph">Let&#8217;s see if it actually works. Is that my terminal? Yeah. Okay. I&#8217;m going to go clear</p>



<p class="wp-block-paragraph">and make, hopefully it actually works. So, oh yeah, there we are. Okay. So everything seemed</p>



<p class="wp-block-paragraph">It printed hello from the driver.</p>



<p class="wp-block-paragraph">And then when we went inside of the assembly module,</p>



<p class="wp-block-paragraph">this is really what we&#8217;re interested in.</p>



<p class="wp-block-paragraph">It said the division result, the quotient is the number one</p>



<p class="wp-block-paragraph">and the division remainder is the number 23.</p>



<p class="wp-block-paragraph">That&#8217;s what we said before, right?</p>



<p class="wp-block-paragraph">So it&#8217;s going to be 23 is the remainder.</p>



<p class="wp-block-paragraph">If you just do the modular operator or if you&#8217;re dividing,</p>



<p class="wp-block-paragraph">whoops, if you&#8217;re dividing, then you&#8217;ll just get a one.</p>



<p class="wp-block-paragraph">Remember like the quotient is just going to, you know,</p>



<p class="wp-block-paragraph">truncate or remove or throw away all the extra data.</p>



<p class="wp-block-paragraph">all the extra data it&#8217;s not going to round up or down or anything it&#8217;s just going to say</p>



<p class="wp-block-paragraph">anything past the decimal point is gone and that&#8217;s why the uh the remainder is pretty important</p>



<p class="wp-block-paragraph">then of course you know just for fun we could i don&#8217;t know put like some other numbers in here</p>



<p class="wp-block-paragraph">if we wanted to maybe i&#8217;ll copy paste this just put some other numbers let&#8217;s put like a giant</p>



<p class="wp-block-paragraph">number and see what happens because these are 64-bit numbers we can do it um i probably should</p>



<p class="wp-block-paragraph">have switched a and b right because doesn&#8217;t it kind of feel like a&#8217;s on top so it should be the</p>



<p class="wp-block-paragraph">top so it should be the numerator or whatever so I&#8217;m just gonna hit a bunch of stuff here and then</p>



<p class="wp-block-paragraph">divide that by some other stuff see if this comes up with a more fun answer so one more time clear</p>



<p class="wp-block-paragraph">and make and it&#8217;s telling me that uh whoa that&#8217;s the result I guess probably should have printed</p>



<p class="wp-block-paragraph">out the original numbers but let me just put that into the calculator just for fun we&#8217;ll say this</p>



<p class="wp-block-paragraph">just sort of go back to the calculator it enters so uh oh the squiggly lines</p>



<p class="wp-block-paragraph">that means that&#8217;s not exactly the result i got to figure out how to change the mode on</p>



<p class="wp-block-paragraph">this calculator so i can get precise let&#8217;s see can i do that right here</p>



<p class="wp-block-paragraph">exact no can i try it again with exact all right this is not a tutorial on the</p>



<p class="wp-block-paragraph">it as is maybe i&#8217;ll do can i do a slightly smaller number yeah there we go okay i&#8217;ll take</p>



<p class="wp-block-paragraph">off three digits so that the calculator will at least show me something okay run it one more time</p>



<p class="wp-block-paragraph">and then so now we have smaller stuff the answer should have been 38677662 38677662 yeah that looks</p>



<p class="wp-block-paragraph">the decimal point so the point six is not on there and then we run the modulo</p>



<p class="wp-block-paragraph">to see what the remainder is and it should say one two eight six three yeah</p>



<p class="wp-block-paragraph">okay and then the seven is getting returned I don&#8217;t know do you want to</p>



<p class="wp-block-paragraph">return the remainder for some reason we could do it let&#8217;s just return the</p>



<p class="wp-block-paragraph">remainder might as well make this video a little bit more fun so recall that in</p>



<p class="wp-block-paragraph">order to return something if the return value is an integer and not a float or</p>



<p class="wp-block-paragraph">float or if it&#8217;s a pointer then you&#8217;ll just use RAX as the return register so I&#8217;m just going to do</p>



<p class="wp-block-paragraph">move return the remainder to the color okay I&#8217;m going to move something into RAX it&#8217;s going to be</p>



<p class="wp-block-paragraph">R15 nice now this is a function with a return type I&#8217;m going to say along here and then</p>



<p class="wp-block-paragraph">up here, I guess, RAX is already going to be pre-filled now after that call.</p>



<p class="wp-block-paragraph">But you could imagine if you were doing more stuff between the call and the return,</p>



<p class="wp-block-paragraph">you would probably want to save the return value from RAX in some other register like R12.</p>



<p class="wp-block-paragraph">And then that means you got to add a push pop pair to preserve it.</p>



<p class="wp-block-paragraph">And then we&#8217;ll definitely just, well, maybe I&#8217;ll say move RAX into RAX</p>



<p class="wp-block-paragraph">totally useless. That&#8217;s why I&#8217;m commenting it out. So we&#8217;ll do one less instruction.</p>



<p class="wp-block-paragraph">So basically after we get back from divide test, RAX is going to have the</p>



<p class="wp-block-paragraph">return value and math, this function would also use RAX for its return value. So if we just sort</p>



<p class="wp-block-paragraph">of don&#8217;t do anything, we should now see that the driver receives the remainder as its return value.</p>



<p class="wp-block-paragraph">Let me open that up again real fast just to make sure we understand what&#8217;s going on with</p>



<p class="wp-block-paragraph">the driver.</p>



<p class="wp-block-paragraph">Even though this is not a driver video, it says external long math.</p>



<p class="wp-block-paragraph">It names that as a long function.</p>



<p class="wp-block-paragraph">And then when we call the function, we just kind of grab a long result, 64 bit int, and</p>



<p class="wp-block-paragraph">then we print it with printf because this is a C program, not C++.</p>



<p class="wp-block-paragraph">integers, assigned integers. Let&#8217;s see in assembly. Let&#8217;s see. Where&#8217;s that? Yeah, there we go.</p>



<p class="wp-block-paragraph">So just keep in mind what I&#8217;ve been showing you is idiv, which is a signed integer, which is what</p>



<p class="wp-block-paragraph">you probably want, which means the integer can be positive or negative. And, you know, if you know</p>



<p class="wp-block-paragraph">for sure that you&#8217;re going to be dividing two positive numbers, and there&#8217;s no chance that</p>



<p class="wp-block-paragraph">that the numerator or denominator or the quotient or you know any of those things are going to be</p>



<p class="wp-block-paragraph">negative then you could you could divide unsigned integers and then you can use larger values</p>



<p class="wp-block-paragraph">because remember when we have signed values we basically lose half of the I guess maximum</p>



<p class="wp-block-paragraph">possible value that we can represent because we lose one bit to the fact that the integer is signed</p>



<p class="wp-block-paragraph">is for signed sorry i div is for signed and regular div is for unsigned and then</p>



<p class="wp-block-paragraph">yeah i guess that&#8217;s basically all i really needed to show you there&#8217;s a bunch of nice code in this</p>



<p class="wp-block-paragraph">book i&#8217;m not gonna i&#8217;m not gonna look at it right now so okay thank you so much for watching this</p>



<p class="wp-block-paragraph">video i hope you learned a little bit of stuff and had a little bit of fun i&#8217;ll see you in the next</p>



<p class="wp-block-paragraph">Hey everybody! Thanks for watching this video again from the bottom of my heart. I really</p>



<p class="wp-block-paragraph">appreciate it. I do hope you did learn something and have some fun. If you could do me a please,</p>



<p class="wp-block-paragraph">a small little favor, could you please subscribe and follow this channel or these videos or</p>



<p class="wp-block-paragraph">whatever it is you do on the current social media website that you&#8217;re looking at right now. It would</p>



<p class="wp-block-paragraph">really mean the world to me and it&#8217;ll help make more videos and grow this community. So we&#8217;ll be</p>



<p class="wp-block-paragraph">able to do more videos, longer videos, better videos, or just I&#8217;ll be able to keep making</p>



<p class="wp-block-paragraph">or just I&#8217;ll be able to keep making videos in general.</p>



<p class="wp-block-paragraph">So please do me a kindness and subscribe.</p>



<p class="wp-block-paragraph">You know, sometimes I&#8217;m sleeping in the middle of the night</p>



<p class="wp-block-paragraph">and I just wake up because I know somebody subscribed or followed.</p>



<p class="wp-block-paragraph">It just wakes me up and I get filled with joy.</p>



<p class="wp-block-paragraph">That&#8217;s exactly what happens every single time.</p>



<p class="wp-block-paragraph">So you could do it as a nice favor to me</p>



<p class="wp-block-paragraph">or you could troll me if you want to just wake me up in the middle of the night.</p>



<p class="wp-block-paragraph">Just subscribe and then I&#8217;ll just wake up.</p>



<p class="wp-block-paragraph">I promise that&#8217;s what will happen.</p>



<p class="wp-block-paragraph">Also, if you look at the middle of the screen right now,</p>



<p class="wp-block-paragraph">the screen right now you should see a qr code which you can scan in order to go to the website</p>



<p class="wp-block-paragraph">which i think is also named somewhere at the bottom of this video and it&#8217;ll take you to my</p>



<p class="wp-block-paragraph">main website where you can just kind of like see all the videos i published and the services and</p>



<p class="wp-block-paragraph">tutorials and things that i offer and all that good stuff and uh if you have a suggestion for uh</p>



<p class="wp-block-paragraph">clarifications or errata or just future videos that you want to see please leave a comment or</p>



<p class="wp-block-paragraph">what&#8217;s going on you know just send me a comment whatever I also wake up for those in the middle</p>



<p class="wp-block-paragraph">of the night I get I wake up in a cold sweat and I&#8217;m like it would really it really mean the world</p>



<p class="wp-block-paragraph">to me I would really appreciate it so again thank you so much for watching this video and</p>



<p class="wp-block-paragraph">enjoy the cool music as as I fade into the darkness which is coming for us all</p>



<p class="wp-block-paragraph">Thank you.</p>



<p class="wp-block-paragraph">you</p>
<p>The post <a href="https://www.NeuralLantern.com/signed-integer-division-in-x86-64-assembly-with-idiv-and-cqo/">Signed Integer Division in x86-64 Assembly with IDIV and CQO</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/signed-integer-division-in-x86-64-assembly-with-idiv-and-cqo/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Mix C++, C, and Assembly into One Program &#8211; Hybrid Program Full Build Tutorial</title>
		<link>https://www.NeuralLantern.com/mix-c-c-and-assembly-into-one-program-hybrid-program-full-build-tutorial/</link>
					<comments>https://www.NeuralLantern.com/mix-c-c-and-assembly-into-one-program-hybrid-program-full-build-tutorial/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 09 Nov 2025 01:47:40 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly tutorial]]></category>
		<category><![CDATA[build system]]></category>
		<category><![CDATA[C and assembly]]></category>
		<category><![CDATA[C++ assembly interop]]></category>
		<category><![CDATA[c++ tutorial]]></category>
		<category><![CDATA[extern C]]></category>
		<category><![CDATA[hybrid programming]]></category>
		<category><![CDATA[linking object files]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[Makefile tutorial]]></category>
		<category><![CDATA[mixed language programming]]></category>
		<category><![CDATA[name mangling]]></category>
		<category><![CDATA[object files]]></category>
		<category><![CDATA[systems programming]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=248</guid>

					<description><![CDATA[<p>Learn to build a hybrid program mixing C++, C, and x86-64 assembly. Covers object files, linking, extern "C", name mangling, and a full Makefile. Run one executable with modules calling each other.</p>
<p>The post <a href="https://www.NeuralLantern.com/mix-c-c-and-assembly-into-one-program-hybrid-program-full-build-tutorial/">Mix C++, C, and Assembly into One Program &#8211; Hybrid Program Full Build Tutorial</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Mix C++, C, and Assembly into One Program - Hybrid Program Full Build Tutorial" width="1380" height="776" src="https://www.youtube.com/embed/Izu807v7NbU?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">Want to write a single program that uses C++ for high-level logic, C for portability, and assembly for speed? This video shows exactly how to do it.</p>



<p class="wp-block-paragraph">We build a complete hybrid program from scratch:</p>



<ul class="wp-block-list">
<li>driver.cpp &#8211; contains main(), prints intro, calls assembly</li>



<li>c.c &#8211; simple printf() function</li>



<li>cpp.cpp &#8211; C++ function (with extern &#8220;C&#8221; to disable name mangling)</li>



<li>hello.asm &#8211; assembly module that prints messages, calls C and C++ functions</li>
</ul>



<p class="wp-block-paragraph">You’ll see:</p>



<ul class="wp-block-list">
<li>Why hybrid programs matter</li>



<li>How source files → object files → final executable</li>



<li>The role of the driver and why main() lives once</li>



<li>Name mangling in C++ and how extern &#8220;C&#8221; fixes interop</li>



<li>Global / extern in assembly to expose and call functions</li>



<li>A clean Makefile that compiles each module separately</li>



<li>Live build and execution with output explained line-by-line</li>
</ul>



<p class="wp-block-paragraph">By the end you’ll know how to call across languages safely, pass basic args via registers, and structure projects for fast incremental builds.</p>



<p class="wp-block-paragraph">No prior assembly required &#8211; just basic C/C++ familiarity. Code is copy-paste ready.</p>



<p class="wp-block-paragraph">Timestamps in comments. Source files linked below.</p>



<p class="wp-block-paragraph">Like &amp; subscribe for more systems programming!</p>



<p class="wp-block-paragraph">What is a Hybrid Program 00:00:00<br>Why Use Hybrid Programs 00:00:21<br>Program Modules Overview 00:01:10<br>Compilation to Object Files 00:02:23<br>Makefile Introduction 00:04:26<br>Driver Module Setup 00:06:43<br>Name Mangling Explained 00:07:18<br>C++ Module with extern C 00:07:43<br>Driver Main Function 00:10:12<br>C Module Implementation 00:11:52<br>Assembly Module Setup 00:12:41<br>Assembly Data Section 00:13:03<br>Assembly Text Section 00:14:18<br>Global and External Functions 00:14:35<br>Assembly System Calls 00:16:20<br>Calling C from Assembly 00:18:58<br>Calling C++ from Assembly 00:20:32<br>Program Execution Flow 00:21:05<br>Running the Program 00:23:59<br>Passing Arguments Between Modules 00:26:23<br>Hybrid Program Summary 00:29:11</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>
</ul>



<p class="wp-block-paragraph">Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



<li>Leaving a comment or suggestion</li>



<li>Subscribing to our Blog</li>



<li>Watching the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p class="wp-block-paragraph">Hello there. Let&#8217;s talk about hybrid programs.</p>



<p class="wp-block-paragraph">What&#8217;s a hybrid program?</p>



<p class="wp-block-paragraph">A hybrid program is basically a program where you have multiple modules,</p>



<p class="wp-block-paragraph">many written in different languages, all compiled into one executable program.</p>



<p class="wp-block-paragraph">Why would you want to make a hybrid program? Well, you know,</p>



<p class="wp-block-paragraph">maybe you have C++ as one of the modules that you&#8217;re creating and C++ is a high</p>



<p class="wp-block-paragraph">And C++ is a high level language, so it makes it easier to write big, robust programs with GUIs and like big design, big fancy design patterns and things.</p>



<p class="wp-block-paragraph">But maybe at some point you want to interface with the C program.</p>



<p class="wp-block-paragraph">So it might make sense to, you know, kind of jump into some C code.</p>



<p class="wp-block-paragraph">Maybe also you want to write a little module in assembly because maybe one part of your code needs to be really, really fast and efficient in some way.</p>



<p class="wp-block-paragraph">the code compiles down to machine code.</p>



<p class="wp-block-paragraph">Assembly is just, you know, a level up from machine code.</p>



<p class="wp-block-paragraph">So let&#8217;s see, I&#8217;m just gonna write a quick program</p>



<p class="wp-block-paragraph">to demonstrate that you can do this</p>



<p class="wp-block-paragraph">and you can have these modules call each other.</p>



<p class="wp-block-paragraph">First off, I should probably draw a little bit</p>



<p class="wp-block-paragraph">about what I&#8217;m even talking about with multiple modules.</p>



<p class="wp-block-paragraph">So I&#8217;m gonna do my little annotator here</p>



<p class="wp-block-paragraph">and yeah, there we go.</p>



<p class="wp-block-paragraph">Basically imagine that you have a C++ module</p>



<p class="wp-block-paragraph">and we&#8217;ll call it the driver.</p>



<p class="wp-block-paragraph">and we&#8217;ll call it the driver and I&#8217;ll say like driver.cpp. Okay I think I&#8217;m</p>



<p class="wp-block-paragraph">drawing this backwards. I&#8217;m gonna draw this down here. Driver.cpp.</p>



<p class="wp-block-paragraph">Excuse my penmanship I don&#8217;t have a smoother setup on this thing. So we have</p>



<p class="wp-block-paragraph">driver.cpp that&#8217;s one source code. Maybe you have like another module, another</p>



<p class="wp-block-paragraph">just some C code. The driver, usually the way I like to refer to it, and one of my favorite,</p>



<p class="wp-block-paragraph">you know, assembly books refers to it is the driver is where the main function is so that</p>



<p class="wp-block-paragraph">it&#8217;s sort of like the entry point of your program. But then maybe we&#8217;ll have another module that has</p>



<p class="wp-block-paragraph">some other kind of CPP code in there. I&#8217;m just going to call this file cpp.cpp. And maybe we</p>



<p class="wp-block-paragraph">have another module that&#8217;s written in assembly. I&#8217;m going to call this hello.asm. Okay, so just</p>



<p class="wp-block-paragraph">Okay, so just several source code files.</p>



<p class="wp-block-paragraph">We will compile them each to an object file.</p>



<p class="wp-block-paragraph">Sometimes, I think, with new programmers,</p>



<p class="wp-block-paragraph">they think that you just compile a source code directly to the executable.</p>



<p class="wp-block-paragraph">We&#8217;re not going to do that in this case.</p>



<p class="wp-block-paragraph">We&#8217;re going to get a little bit fancier.</p>



<p class="wp-block-paragraph">Doing it this way, by the way, will increase your compiling efficiency</p>



<p class="wp-block-paragraph">when you start getting used to make files.</p>



<p class="wp-block-paragraph">I don&#8217;t know if you&#8217;re already using those, but it&#8217;s a good idea.</p>



<p class="wp-block-paragraph">So we&#8217;ll write a script to compile or make file to compile</p>



<p class="wp-block-paragraph">or some kind of a build system to compile.</p>



<p class="wp-block-paragraph">We&#8217;ll compile the CPP driver to just a file called</p>



<p class="wp-block-paragraph">driver.o, O for object file.</p>



<p class="wp-block-paragraph">So O for object file depends on driver.cpp.</p>



<p class="wp-block-paragraph">We&#8217;ll have another file called c.o,</p>



<p class="wp-block-paragraph">so the object file c.o depends on c.c and so forth.</p>



<p class="wp-block-paragraph">So I&#8217;m just gonna go cpp.o, circle it,</p>



<p class="wp-block-paragraph">and then down here we&#8217;ll do hello.o.</p>



<p class="wp-block-paragraph">Notice how all of these modules, regardless of the language they are written in, compile</p>



<p class="wp-block-paragraph">down to an object file.</p>



<p class="wp-block-paragraph">That&#8217;s really what it is.</p>



<p class="wp-block-paragraph">The higher level languages are just an illusion for us humans.</p>



<p class="wp-block-paragraph">So then at the very top, we want our actual executable file.</p>



<p class="wp-block-paragraph">We&#8217;ll call it main because that&#8217;s kind of a standard thing sometimes.</p>



<p class="wp-block-paragraph">So it&#8217;s like our main, we&#8217;ll have a file named main that we can execute.</p>



<p class="wp-block-paragraph">is going to be the main function.</p>



<p class="wp-block-paragraph">Spoiler, it&#8217;s going to be inside the driver.</p>



<p class="wp-block-paragraph">Remember, the main function, if you&#8217;re using GCC libraries,</p>



<p class="wp-block-paragraph">has to appear in exactly one place, one module in your program.</p>



<p class="wp-block-paragraph">It should appear one time and one time alone, not zero or more than one.</p>



<p class="wp-block-paragraph">So the main executable depends on the driver object file,</p>



<p class="wp-block-paragraph">the C object file, and the C++ object file,</p>



<p class="wp-block-paragraph">and the assembly hello object file.</p>



<p class="wp-block-paragraph">Looks complicated at first until you stare at it a while,</p>



<p class="wp-block-paragraph">complicated at first until you stare at it a while and then you just realize,</p>



<p class="wp-block-paragraph">oh, every source code is going to compile to its own object file.</p>



<p class="wp-block-paragraph">And then after we have all the object files,</p>



<p class="wp-block-paragraph">we&#8217;re going to have our executable just sort of linked together at the final</p>



<p class="wp-block-paragraph">linking stage. So with that in mind,</p>



<p class="wp-block-paragraph">I&#8217;m just going to copy paste a make file that I already wrote.</p>



<p class="wp-block-paragraph">It&#8217;s like my pre solution because this is not a make file video.</p>



<p class="wp-block-paragraph">And I just want you to know just real fast what it looks like.</p>



<p class="wp-block-paragraph">All I&#8217;m doing is I&#8217;m going to make some variables, uh,</p>



<p class="wp-block-paragraph">for my C++ compiler flags and my C compiler flags and my Yasm assembler flags.</p>



<p class="wp-block-paragraph">I&#8217;m really not doing anything fancy.</p>



<p class="wp-block-paragraph">I&#8217;m just sort of setting up some variables so I don&#8217;t have to type.</p>



<p class="wp-block-paragraph">The newer Ubuntu stuffs need a no executable stack flag and a no pi flag.</p>



<p class="wp-block-paragraph">So here we&#8217;re just like making variables for linking.</p>



<p class="wp-block-paragraph">My executable&#8217;s name is going to be main over here.</p>



<p class="wp-block-paragraph">I personally like to make make files that default to a cute help menu.</p>



<p class="wp-block-paragraph">help menu. I know most people just default to building, but, uh, you know, for educational</p>



<p class="wp-block-paragraph">purposes, I think this is a little more fun, a little easier. So this way later I can just type</p>



<p class="wp-block-paragraph">make menu or make build or make run or make clean or whatever I want to do. And, um,</p>



<p class="wp-block-paragraph">I&#8217;m going to make a target called run, which is what we&#8217;re going to be using. And then a target</p>



<p class="wp-block-paragraph">called build, which just only builds everything. The run target obviously depends on build so that</p>



<p class="wp-block-paragraph">to run it and and if you don&#8217;t know make files at this point it&#8217;s okay this is not a make file video</p>



<p class="wp-block-paragraph">i&#8217;m going to make some make file videos in the future</p>



<p class="wp-block-paragraph">then my binary the main program is just going to depend on all these object files notice how it</p>



<p class="wp-block-paragraph">depends on the driver the hello the c plus plus and the c object files so really uh at the linking</p>



<p class="wp-block-paragraph">stage we&#8217;re just going to use g plus plus to just sort of link all the object files together</p>



<p class="wp-block-paragraph">Then we have a little section to compile each source code file.</p>



<p class="wp-block-paragraph">So in order to make driver.o, I&#8217;m going to compile,</p>



<p class="wp-block-paragraph">I&#8217;m going to depend on the driver.cpp.</p>



<p class="wp-block-paragraph">And then when it comes to actually compiling, compiling it,</p>



<p class="wp-block-paragraph">I&#8217;m just going to type the commands to compile it here.</p>



<p class="wp-block-paragraph">Don&#8217;t worry about these crazy variables.</p>



<p class="wp-block-paragraph">I&#8217;m going to make more videos in the future.</p>



<p class="wp-block-paragraph">And then each module gets compiled in its own way.</p>



<p class="wp-block-paragraph">Then I just have a little target at the bottom called clean because I like to be</p>



<p class="wp-block-paragraph">able to just clean my build area of object files.</p>



<p class="wp-block-paragraph">I don&#8217;t know about you, but I like a clean build area.</p>



<p class="wp-block-paragraph">I&#8217;m a clean build area guy.</p>



<p class="wp-block-paragraph">I don&#8217;t like a dirty build area.</p>



<p class="wp-block-paragraph">So that was the make file.</p>



<p class="wp-block-paragraph">Again, this is not a make file video,</p>



<p class="wp-block-paragraph">so don&#8217;t worry too much about it right now.</p>



<p class="wp-block-paragraph">The next thing we should do is make a driver.</p>



<p class="wp-block-paragraph">Again, I&#8217;m gonna copy paste from my solution here</p>



<p class="wp-block-paragraph">because the driver should be easy.</p>



<p class="wp-block-paragraph">This is really, this video is mostly gonna be</p>



<p class="wp-block-paragraph">for people who have never done assembly before.</p>



<p class="wp-block-paragraph">So I&#8217;m just gonna assume you know a little bit</p>



<p class="wp-block-paragraph">about C++ and C already.</p>



<p class="wp-block-paragraph">So I&#8217;m going to say touch, let&#8217;s see, driver.cpp so that I can get the driver file in there.</p>



<p class="wp-block-paragraph">And I&#8217;m going to go nano driver, probably put it down in the first place.</p>



<p class="wp-block-paragraph">Then I&#8217;m just going to paste my solution.</p>



<p class="wp-block-paragraph">Oops, you can tell I copy pasted this from my IDE because I have the extra tabs in there, which I love.</p>



<p class="wp-block-paragraph">One thing to keep in mind is that, I guess like one of the first rules that I want to tell you for hybrid programs</p>



<p class="wp-block-paragraph">is that assembly doesn&#8217;t understand something called name mangling.</p>



<p class="wp-block-paragraph">mangling. So what is name mangling? Name mangling is basically when you have a,</p>



<p class="wp-block-paragraph">oh, this is not going to get called. So this is maybe not a great idea to show you name</p>



<p class="wp-block-paragraph">mangling. Let me, let me start on one of the other ones real fast too. Let&#8217;s do</p>



<p class="wp-block-paragraph">the C++ module. Okay. So I&#8217;m going to go nano CPP dot CPP. I know it&#8217;s a silly name. I know it.</p>



<p class="wp-block-paragraph">So CPP dot CPP, this is just a simple C++ program, which is meant to be called by</p>



<p class="wp-block-paragraph">by the assembly module just to show you that we can.</p>



<p class="wp-block-paragraph">And so we just have a little function here,</p>



<p class="wp-block-paragraph">void, you know, the CPP function.</p>



<p class="wp-block-paragraph">So it&#8217;s just a function that doesn&#8217;t really return anything.</p>



<p class="wp-block-paragraph">Inside of it, I&#8217;m just going to print,</p>



<p class="wp-block-paragraph">hello, this is the C++ function, no problem.</p>



<p class="wp-block-paragraph">Notice at the top,</p>



<p class="wp-block-paragraph">this is the first important thing that you should understand.</p>



<p class="wp-block-paragraph">In C++, we have overloads, right?</p>



<p class="wp-block-paragraph">You can have the same function name,</p>



<p class="wp-block-paragraph">but with different argument lists.</p>



<p class="wp-block-paragraph">And so that gets resolved under the hood</p>



<p class="wp-block-paragraph">by the name actually kind of changing</p>



<p class="wp-block-paragraph">based on what the argument signature is,</p>



<p class="wp-block-paragraph">what the prototype signature is.</p>



<p class="wp-block-paragraph">So that&#8217;s great and all,</p>



<p class="wp-block-paragraph">but in assembly, at least at this level,</p>



<p class="wp-block-paragraph">there&#8217;s no way to really differentiate between overloads.</p>



<p class="wp-block-paragraph">In assembly, we&#8217;ll just call a name of some function.</p>



<p class="wp-block-paragraph">But if we try to call a name,</p>



<p class="wp-block-paragraph">let&#8217;s say of just the CPP function,</p>



<p class="wp-block-paragraph">if we say call the CPP function in assembly,</p>



<p class="wp-block-paragraph">it&#8217;s not going to work</p>



<p class="wp-block-paragraph">because the actual function has not been compiled to have that name.</p>



<p class="wp-block-paragraph">It&#8217;s been compiled to have the CPP function</p>



<p class="wp-block-paragraph">and a bunch of extra symbols indicating what the argument list is.</p>



<p class="wp-block-paragraph">This is great for C++ programs, but it&#8217;s poor.</p>



<p class="wp-block-paragraph">It&#8217;s bad for other modules to be able to call the C++ function</p>



<p class="wp-block-paragraph">because they won&#8217;t really know what the actual name is</p>



<p class="wp-block-paragraph">after the mangling has been done.</p>



<p class="wp-block-paragraph">So what you want to do is make a little block called Xtern C,</p>



<p class="wp-block-paragraph">just like this.</p>



<p class="wp-block-paragraph">You say extern and then C in quotes and then do a scope with braces.</p>



<p class="wp-block-paragraph">And inside of that, just put the prototypes of any function that you want to be available to other modules.</p>



<p class="wp-block-paragraph">So, for example, we want the CPP function to be available to assembly modules and C modules, not just other C++ modules.</p>



<p class="wp-block-paragraph">So, I&#8217;m just going to put the prototype of it right there.</p>



<p class="wp-block-paragraph">You can put as many prototypes as you want.</p>



<p class="wp-block-paragraph">I think they should come from the same source file.</p>



<p class="wp-block-paragraph">different source file where you want something to be available elsewhere,</p>



<p class="wp-block-paragraph">you would put an extern C block for its prototype.</p>



<p class="wp-block-paragraph">This disables name mangling,</p>



<p class="wp-block-paragraph">which means the compiled name of the CPP function will actually be the CPP</p>



<p class="wp-block-paragraph">function. So it&#8217;s pretty good. This will make it available to assembly.</p>



<p class="wp-block-paragraph">So now that I&#8217;ve kind of explained that I can maybe close this and go back to</p>



<p class="wp-block-paragraph">what I was doing before. So this is the main driver program.</p>



<p class="wp-block-paragraph">We don&#8217;t need to engage in a dis,</p>



<p class="wp-block-paragraph">we don&#8217;t need to disable name mangling for the main function because no one is going to call main</p>



<p class="wp-block-paragraph">except the GCC libraries or like the OS. But when the C++ module calls other modules, it needs to</p>



<p class="wp-block-paragraph">be able to call the right name. And remember in C++, there&#8217;s name mangling enabled by default.</p>



<p class="wp-block-paragraph">So if I tell it, let&#8217;s say on this line right here, or you can do alt N to get line numbers on nano.</p>



<p class="wp-block-paragraph">we want to call on the hello module will C++ by default will assume that hello has name</p>



<p class="wp-block-paragraph">mangling applied to it, which means it will actually try to call some name that is totally</p>



<p class="wp-block-paragraph">different based on the prototype signature, the incoming arguments and things like that.</p>



<p class="wp-block-paragraph">But so we want to disable that because we wanted to just call the actual name because</p>



<p class="wp-block-paragraph">hello is going to be a function that we make inside of assembly.</p>



<p class="wp-block-paragraph">So it&#8217;s not going to be name mangled.</p>



<p class="wp-block-paragraph">So that&#8217;s why up here on line seven, we enter its signature.</p>



<p class="wp-block-paragraph">Enter its signature. Oh, actually this is from a different version of something that I was doing. I&#8217;m not going to return anything from hello</p>



<p class="wp-block-paragraph">We&#8217;ll do name a demangling with our extern C block just void hello</p>



<p class="wp-block-paragraph">And now when we call hello, it&#8217;s actually going to call a function called hello without any other</p>



<p class="wp-block-paragraph">mangling same thing for the C S E A</p>



<p class="wp-block-paragraph">Oh</p>



<p class="wp-block-paragraph">That was from a different version. I wrote we&#8217;re not going to be calling C in this module. Don&#8217;t worry</p>



<p class="wp-block-paragraph">We&#8217;re going to keep it simple</p>



<p class="wp-block-paragraph">So pretty much we&#8217;re going to print hello and then we&#8217;re going to print,</p>



<p class="wp-block-paragraph">then we&#8217;re going to call the hello and then we&#8217;re going to say goodbye.</p>



<p class="wp-block-paragraph">And then that&#8217;s going to be the end of the program.</p>



<p class="wp-block-paragraph">So let me close out of that and just double check what I have written right now.</p>



<p class="wp-block-paragraph">So we&#8217;ve got CPP and okay, we need to do C, nano C dot C.</p>



<p class="wp-block-paragraph">Okay, so let me get my solution real fast here.</p>



<p class="wp-block-paragraph">It&#8217;s a simple program.</p>



<p class="wp-block-paragraph">Literally, it&#8217;s just a C program that prints something.</p>



<p class="wp-block-paragraph">Remember in C++, we&#8217;ve got the IO stream, but we don&#8217;t have that in C.</p>



<p class="wp-block-paragraph">that in C. So I&#8217;m just going to use printf. Oh, by the way, this is a C function. Hi.</p>



<p class="wp-block-paragraph">So done with that. Pretty easy. Shoot. You know what? I need to start learning how to do</p>



<p class="wp-block-paragraph">clear and LSA. So I can just go up, up, up. So C++, C and the driver</p>



<p class="wp-block-paragraph">and the make file, which is huge. I just realized that. And now I guess we can start</p>



<p class="wp-block-paragraph">or hello function.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So maybe I want to write this in the Genie program.</p>



<p class="wp-block-paragraph">So I&#8217;m going to, well, maybe I&#8217;ll first, I&#8217;ll say touch,</p>



<p class="wp-block-paragraph">and I&#8217;ll say hello.assembly.</p>



<p class="wp-block-paragraph">And then now that it&#8217;s there, I can try to open it up.</p>



<p class="wp-block-paragraph">Let&#8217;s see.</p>



<p class="wp-block-paragraph">Hello.assembly.</p>



<p class="wp-block-paragraph">Okay, so now let&#8217;s write an assembly program.</p>



<p class="wp-block-paragraph">I&#8217;m not going to spend too much time talking about this,</p>



<p class="wp-block-paragraph">but I&#8217;ll say like data section here,</p>



<p class="wp-block-paragraph">and we&#8217;ll say section.data.</p>



<p class="wp-block-paragraph">We&#8217;ll say section.data and then we&#8217;ll say system calls.</p>



<p class="wp-block-paragraph">This is not a system calls video.</p>



<p class="wp-block-paragraph">I&#8217;m just gonna do this very quickly without explaining.</p>



<p class="wp-block-paragraph">System write is gonna be code one, I guess.</p>



<p class="wp-block-paragraph">And then we&#8217;re gonna do file descriptors, one for stdout.</p>



<p class="wp-block-paragraph">Whoops, stdout, it&#8217;s gonna be equal to one.</p>



<p class="wp-block-paragraph">This is not a file descriptors video.</p>



<p class="wp-block-paragraph">I&#8217;ll put FDs, I guess.</p>



<p class="wp-block-paragraph">And then we&#8217;ll make some C strings.</p>



<p class="wp-block-paragraph">C strings, you know, just character arrays, basically.</p>



<p class="wp-block-paragraph">I&#8217;ll say, well, you know what?</p>



<p class="wp-block-paragraph">I don&#8217;t want to type this all out</p>



<p class="wp-block-paragraph">because this is not a lesson on variables.</p>



<p class="wp-block-paragraph">I&#8217;m just going to copy paste my existing solution here.</p>



<p class="wp-block-paragraph">And, you know, long story short,</p>



<p class="wp-block-paragraph">I&#8217;m going to try to make this a pretty program</p>



<p class="wp-block-paragraph">that prints hello and goodbye and things.</p>



<p class="wp-block-paragraph">And so we just have like some variables</p>



<p class="wp-block-paragraph">that sort of just you know old strings and hold the strings length and we&#8217;re</p>



<p class="wp-block-paragraph">gonna do one that calls a new line and so forth actually that&#8217;ll be a different</p>



<p class="wp-block-paragraph">video but I&#8217;ll leave it in there for now so now let&#8217;s make the text section</p>



<p class="wp-block-paragraph">remember the text section in Yasm is just for the actual code of your program</p>



<p class="wp-block-paragraph">and then we need to set up some external symbols. So right now, at least for the purposes of this</p>



<p class="wp-block-paragraph">source code, this source code is inside of the hello module. So you can imagine we&#8217;re inside of</p>



<p class="wp-block-paragraph">hello.o. All the o&#8217;s get linked together in the executable, but between themselves, there&#8217;s kind</p>



<p class="wp-block-paragraph">of a little bit of like a border situation happening. There&#8217;s like some separation. So</p>



<p class="wp-block-paragraph">are going to be available to the other modules,</p>



<p class="wp-block-paragraph">even if they&#8217;re part of the same executable.</p>



<p class="wp-block-paragraph">It&#8217;s kind of convenient because you would like sometimes</p>



<p class="wp-block-paragraph">to be able to write tons and tons of functions</p>



<p class="wp-block-paragraph">that are only available inside of your assembly module,</p>



<p class="wp-block-paragraph">and they might not be available to other modules,</p>



<p class="wp-block-paragraph">and therefore you don&#8217;t want to pollute the namespace,</p>



<p class="wp-block-paragraph">I guess, of the global namespace of the executable.</p>



<p class="wp-block-paragraph">So by default, if I make a function called hello,</p>



<p class="wp-block-paragraph">how you make a function, just a quick recap here.</p>



<p class="wp-block-paragraph">This is not a functions video.</p>



<p class="wp-block-paragraph">make a symbol with the name of the function you want and then you just put a ret after it and then</p>



<p class="wp-block-paragraph">you hope that whoever got you there is going to use the call instruction but anyway for now</p>



<p class="wp-block-paragraph">we&#8217;re just going to say this is our hello function okay i cannot call the hello function now from</p>



<p class="wp-block-paragraph">another module i have to make it visible to the other modules with a special keyword called global</p>



<p class="wp-block-paragraph">our program the hello function will be available to the other modules but not anything else that</p>



<p class="wp-block-paragraph">I type without marking it as global so let&#8217;s go ahead and do let&#8217;s do a welcome message and this</p>



<p class="wp-block-paragraph">is not a system call a video maybe I should just copy paste this so just looking at it this is a</p>



<p class="wp-block-paragraph">going to quickly you know breeze through what we&#8217;re doing in case you&#8217;re interested but you</p>



<p class="wp-block-paragraph">don&#8217;t have to care too much about this right now we&#8217;re going to tell rax what system call we want</p>



<p class="wp-block-paragraph">to make the code for that is what i put up in system right which is just code one means let&#8217;s</p>



<p class="wp-block-paragraph">write to a file we&#8217;ll give it the file handle uh that&#8217;s going to be the variable we made called fd</p>



<p class="wp-block-paragraph">std out which means we&#8217;re going to be giving it the file handle of one which means we&#8217;re telling</p>



<p class="wp-block-paragraph">to print to standard output because file handle one is always standard output.</p>



<p class="wp-block-paragraph">And then we just give it the a pointer to the first character of the message</p>



<p class="wp-block-paragraph">string that we made and then tell it how long that message is. Then we do system call this should</p>



<p class="wp-block-paragraph">actually um this this should actually print a string. Okay so then uh let&#8217;s see down here I&#8217;m</p>



<p class="wp-block-paragraph">going to make a function called crlf because I&#8217;m lazy and I don&#8217;t like constantly adding extra stuff</p>



<p class="wp-block-paragraph">constantly adding extra stuff to the string so that there will be like a new line line feed</p>



<p class="wp-block-paragraph">situation so i usually like to make a little function somewhere called crlf carriage return</p>



<p class="wp-block-paragraph">line feed is what it stands for and it&#8217;s the same thing it&#8217;s just like load up the system call</p>



<p class="wp-block-paragraph">registers so that it knows i want to print the string which is just a line feed</p>



<p class="wp-block-paragraph">carriage return line feed and then actually call system call and then return to the caller so this</p>



<p class="wp-block-paragraph">back up here let&#8217;s see crlf i guess i am going to be doing it it&#8217;s just a string with the the</p>



<p class="wp-block-paragraph">slash r slash n or the carriage return line feed characters in ascii those are 13 comma 10 so i can</p>



<p class="wp-block-paragraph">put that there instead of a literal quoted string okay so we got that and then that means after the</p>



<p class="wp-block-paragraph">welcome message i can just like call crlf in order to have uh the cursor jump down to the next uh</p>



<p class="wp-block-paragraph">after that string is notice how I don&#8217;t have any line feeds after my strings that&#8217;s kind of why I</p>



<p class="wp-block-paragraph">like to do crlf and it lets me just call crlf a bunch of times if I want to just like space things</p>



<p class="wp-block-paragraph">out temporarily okay so we&#8217;ve got the hello function and now we&#8217;re going to print another</p>



<p class="wp-block-paragraph">message here I&#8217;ll just explain this real fast so I&#8217;m going to print a message about calling a c</p>



<p class="wp-block-paragraph">So same thing, we&#8217;re just going to tell the system call service that we want to print a string and it&#8217;s going to be our string here that says, you know, we&#8217;re about to print this.</p>



<p class="wp-block-paragraph">We&#8217;re about to, what do we say?</p>



<p class="wp-block-paragraph">We&#8217;re about to call the C function and then later we&#8217;re going to call the C++ function.</p>



<p class="wp-block-paragraph">So we&#8217;re just going to print a message announcing and do CRLF.</p>



<p class="wp-block-paragraph">So that&#8217;s really nothing new at all.</p>



<p class="wp-block-paragraph">And then down here we actually call the C function, which is not going to be in this module.</p>



<p class="wp-block-paragraph">It&#8217;s going to be in the C module, the other source code.</p>



<p class="wp-block-paragraph">Somewhere in this program, we need to let our assembly module know</p>



<p class="wp-block-paragraph">that the C function is available because it kind of goes both ways.</p>



<p class="wp-block-paragraph">In assembly, I have to mark a function as global if I want other modules to be able</p>



<p class="wp-block-paragraph">to call that function, but I also have to mark a function as external</p>



<p class="wp-block-paragraph">if I want to be able to call some other modules function.</p>



<p class="wp-block-paragraph">So we have to go back up to the top.</p>



<p class="wp-block-paragraph">I mean, somewhere in the text section, I think would be fine.</p>



<p class="wp-block-paragraph">But for me, I just like to put it at the top.</p>



<p class="wp-block-paragraph">put it at the top at the top so I&#8217;ll say externals and then I&#8217;ll go extern</p>



<p class="wp-block-paragraph">keyword and then just name the function that I intend to call this way when the</p>



<p class="wp-block-paragraph">linker is trying to link all the objects together it doesn&#8217;t get upset because</p>



<p class="wp-block-paragraph">you tried to call a C function which is not in the current module it&#8217;ll</p>



<p class="wp-block-paragraph">understand that it&#8217;s gonna happen you know like it&#8217;s gonna call other modules</p>



<p class="wp-block-paragraph">for that it usually gets upset right away and then while we&#8217;re at it let&#8217;s do</p>



<p class="wp-block-paragraph">Let&#8217;s turn the CPP function because that&#8217;s going to come from the C++ module.</p>



<p class="wp-block-paragraph">So now we have both of our external functions set up.</p>



<p class="wp-block-paragraph">We&#8217;ve marked our global hello function so other modules can call us.</p>



<p class="wp-block-paragraph">And then let&#8217;s see, oh, what did I do wrong?</p>



<p class="wp-block-paragraph">I put the C message in the wrong spot.</p>



<p class="wp-block-paragraph">I was inside of the CRLF function.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to copy paste that back up here.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">Then we&#8217;ll do the same thing for the C++ message,</p>



<p class="wp-block-paragraph">or for the C++ stuff.</p>



<p class="wp-block-paragraph">I will first print a C++ message</p>



<p class="wp-block-paragraph">with these lines up here.</p>



<p class="wp-block-paragraph">That&#8217;s just like a regular system call,</p>



<p class="wp-block-paragraph">and then I&#8217;ll do a CRLF, so nothing new,</p>



<p class="wp-block-paragraph">just printing something.</p>



<p class="wp-block-paragraph">And then I will actually call the C++ function</p>



<p class="wp-block-paragraph">called the CPP function.</p>



<p class="wp-block-paragraph">Then I&#8217;ll say goodbye with another block of text.</p>



<p class="wp-block-paragraph">Again, not super important or complicated,</p>



<p class="wp-block-paragraph">so I&#8217;m just gonna paste it.</p>



<p class="wp-block-paragraph">Just another string.</p>



<p class="wp-block-paragraph">string. And then when we&#8217;re done, I&#8217;ll just say, you know, return, we&#8217;re done. So that means if</p>



<p class="wp-block-paragraph">you notice how we don&#8217;t have like any symbols in here called underscore start, that means this is</p>



<p class="wp-block-paragraph">not a pure assembly program. And we don&#8217;t even have a symbol inside of here that is called main.</p>



<p class="wp-block-paragraph">So that means this hello ASM module is not going to be the entry point of our program. It&#8217;s just</p>



<p class="wp-block-paragraph">going to expose a function called hello, which does some stuff. The entry point of our program</p>



<p class="wp-block-paragraph">really, let&#8217;s see, where is it? Oh, gosh, I think I already closed it for some reason. It&#8217;s the</p>



<p class="wp-block-paragraph">driver program, which we talked about. So remember the driver program, it&#8217;s got a main here. So that</p>



<p class="wp-block-paragraph">means this is going to be what the GCC libraries or the operating system calls, however you want</p>



<p class="wp-block-paragraph">to think of it. So this is the entry point of our program, it&#8217;ll just print something quickly,</p>



<p class="wp-block-paragraph">then it&#8217;ll call the hello function, which means execution will jump in to the hello module. So</p>



<p class="wp-block-paragraph">module and and also it would be really easy to apply this logic to have a c module call on an</p>



<p class="wp-block-paragraph">assembly module and in fact you can use this logic to have every type of module call on any other i</p>



<p class="wp-block-paragraph">mean just if you have a c++ function either being defined in the current module or being referenced</p>



<p class="wp-block-paragraph">in another module just make sure that you have an extern c block and then if you&#8217;re inside of a c</p>



<p class="wp-block-paragraph">and you&#8217;re going to call a non-name mingled module like C or assembly,</p>



<p class="wp-block-paragraph">then just do this block here.</p>



<p class="wp-block-paragraph">No problem.</p>



<p class="wp-block-paragraph">What else can I really tell you?</p>



<p class="wp-block-paragraph">The C module, we looked at that.</p>



<p class="wp-block-paragraph">The C++, we looked at that.</p>



<p class="wp-block-paragraph">Pretty easy.</p>



<p class="wp-block-paragraph">Hello module.</p>



<p class="wp-block-paragraph">I mean, I guess we&#8217;re kind of done already.</p>



<p class="wp-block-paragraph">So what we should see now is a little intro here.</p>



<p class="wp-block-paragraph">Hello, my name is whatever.</p>



<p class="wp-block-paragraph">And then when we jump into the hello module,</p>



<p class="wp-block-paragraph">we should see a little welcome message about being in the assembly module.</p>



<p class="wp-block-paragraph">And then we should see a message about calling the C function.</p>



<p class="wp-block-paragraph">And then the C function should print.</p>



<p class="wp-block-paragraph">Let me open that up for you.</p>



<p class="wp-block-paragraph">Let&#8217;s see.</p>



<p class="wp-block-paragraph">The C function should print.</p>



<p class="wp-block-paragraph">Just, oh, by the way, this is the C function.</p>



<p class="wp-block-paragraph">And then when it returns at the very end, remember, even in a void function, there is</p>



<p class="wp-block-paragraph">a return statement implicit at the end of scope.</p>



<p class="wp-block-paragraph">So when it returns, it&#8217;ll come back down here.</p>



<p class="wp-block-paragraph">And then the C++ message will get printed.</p>



<p class="wp-block-paragraph">printed and then the C++ function will get called which is going to end up being</p>



<p class="wp-block-paragraph">just it just prints you know another message this is the C++ function</p>



<p class="wp-block-paragraph">then when it returns implicitly at the end of the function</p>



<p class="wp-block-paragraph">then we get down to here where we just basically say goodbye and then return to the caller</p>



<p class="wp-block-paragraph">the caller in this case is going to be the driver program because this whole time we were just inside</p>



<p class="wp-block-paragraph">called. So then the driver will say goodbye and then it will return control to the GCC libraries</p>



<p class="wp-block-paragraph">or the operating system ending the program. Let&#8217;s see if that works. Okay, clear. And so,</p>



<p class="wp-block-paragraph">you know, the way I wreck my make files, I&#8217;d like to do a little menu. I type make and it&#8217;s like,</p>



<p class="wp-block-paragraph">oh, the menu, make menu, make build, just in case, just to see what&#8217;s up. Okay, clear.</p>



<p class="wp-block-paragraph">say, let&#8217;s do clear and make run because that&#8217;s really what we&#8217;re after.</p>



<p class="wp-block-paragraph">Again, run will implicitly call build.</p>



<p class="wp-block-paragraph">So do that.</p>



<p class="wp-block-paragraph">And it seems to have succeeded.</p>



<p class="wp-block-paragraph">It wasn&#8217;t actually very exciting.</p>



<p class="wp-block-paragraph">Let me do a make clean so you can see it compile real fast.</p>



<p class="wp-block-paragraph">We run it.</p>



<p class="wp-block-paragraph">It&#8217;s compiling.</p>



<p class="wp-block-paragraph">That was still kind of fast.</p>



<p class="wp-block-paragraph">So up here is basically the make file, all these lines.</p>



<p class="wp-block-paragraph">They&#8217;re just calling your normal compiler and assembler tools and linker tools.</p>



<p class="wp-block-paragraph">linker tools and then here&#8217;s the driver like we said it prints the hello message</p>



<p class="wp-block-paragraph">you know my name is whatever I love that name I have a strange name generator</p>



<p class="wp-block-paragraph">that I found online that&#8217;s not my name you can call me that if you want to and</p>



<p class="wp-block-paragraph">then we have the message that we&#8217;re going to call the hello module so now</p>



<p class="wp-block-paragraph">we&#8217;ve called into the hello function inside of the assembly module assembly</p>



<p class="wp-block-paragraph">module prints hello you know I&#8217;m inside of here now and then the assembly module</p>



<p class="wp-block-paragraph">Now we&#8217;re going to call the C function and then the C function actually</p>



<p class="wp-block-paragraph">executes and it is now printing with its print F function call that it did,</p>



<p class="wp-block-paragraph">you know, by the way, this is a C function. Hi.</p>



<p class="wp-block-paragraph">And then it returns to the caller.</p>



<p class="wp-block-paragraph">Maybe I could have added another print statement if I felt like I wanted to</p>



<p class="wp-block-paragraph">spend more time, but it returns to the caller, which is the hello function.</p>



<p class="wp-block-paragraph">Then we get to the next part of the hello function where it&#8217;s like now calling</p>



<p class="wp-block-paragraph">the C function as a print statement.</p>



<p class="wp-block-paragraph">Then it actually does call the C function. The C function prints itself out.</p>



<p class="wp-block-paragraph">prints itself out hello this is the C function and then the assembly module</p>



<p class="wp-block-paragraph">says goodbye it says it&#8217;s finished and then it returns to the caller so finally</p>



<p class="wp-block-paragraph">at the end of this print statement the there&#8217;s like that little return statement</p>



<p class="wp-block-paragraph">at the bottom of the hello module that returns to the caller which was the</p>



<p class="wp-block-paragraph">driver dot CPP program so then the driver prints the driver is now back in</p>



<p class="wp-block-paragraph">the driver is now back in control. You can see that message behind the terminal here.</p>



<p class="wp-block-paragraph">The driver is now back in control. Then it says goodbye. Then it returns zero to the operating</p>



<p class="wp-block-paragraph">system and the whole program is done. So it&#8217;s important to understand that if you want to</p>



<p class="wp-block-paragraph">pass arguments, right now I&#8217;m just going to say, you know, I&#8217;m going to make another video that</p>



<p class="wp-block-paragraph">is more robust in the future about passing arguments back and forth between these. It&#8217;s</p>



<p class="wp-block-paragraph">important to understand for now that you can pass arguments between all these different</p>



<p class="wp-block-paragraph">between all these different module functions if you want.</p>



<p class="wp-block-paragraph">Like if I wanted to give the.</p>



<p class="wp-block-paragraph">If I wanted to give the hello function.</p>



<p class="wp-block-paragraph">An incoming argument, let&#8217;s say like an integer or a character value or a pointer</p>



<p class="wp-block-paragraph">or something like that, I could just load up appropriate registers.</p>



<p class="wp-block-paragraph">Remember, we have argument registers in assembly.</p>



<p class="wp-block-paragraph">I think I can never remember if RSI or RDI are the first one.</p>



<p class="wp-block-paragraph">I&#8217;m going to say RDI is the first argument just off the top of my head.</p>



<p class="wp-block-paragraph">I always look that up.</p>



<p class="wp-block-paragraph">So if I wanted to call, let&#8217;s say give an integer argument to hello when I originally</p>



<p class="wp-block-paragraph">called it, then from the driver, I literally would just have to type an integer argument</p>



<p class="wp-block-paragraph">in there and then under the hood, the RDI register, assuming that I&#8217;m right about RDI</p>



<p class="wp-block-paragraph">being the first argument, the RDI register would get loaded up with that value.</p>



<p class="wp-block-paragraph">your assembly program all you&#8217;d have to do is do something with the RDI register</p>



<p class="wp-block-paragraph">you know load it up save it do whatever it is that you think you&#8217;re gonna do so</p>



<p class="wp-block-paragraph">that&#8217;s how you pass an integer or like a character or a pointer from C++ to</p>



<p class="wp-block-paragraph">assembly it works the same exact way if you want to pass arguments when calling</p>



<p class="wp-block-paragraph">from C to assembly and when if you want to pass integers from assembly to C or</p>



<p class="wp-block-paragraph">or C++, it&#8217;s the same thing just backwards.</p>



<p class="wp-block-paragraph">First, you load up RDI as the first argument register with whatever value you want to pass</p>



<p class="wp-block-paragraph">to C or C++.</p>



<p class="wp-block-paragraph">And then in the signature of the C++ function, you just expect, you know, a long or, you</p>



<p class="wp-block-paragraph">know, whatever you were going to get and the register will already be loaded up.</p>



<p class="wp-block-paragraph">Remember, the C++ is not really happening on the machine.</p>



<p class="wp-block-paragraph">It gets compiled down to assembly.</p>



<p class="wp-block-paragraph">So this source code that we&#8217;re looking at, it&#8217;s another illusion for our benefit.</p>



<p class="wp-block-paragraph">benefit. It&#8217;s not happening. It&#8217;s getting assembled and then compiled down to machine code. And then</p>



<p class="wp-block-paragraph">that is what&#8217;s actually happening on the machine. So when you write that you have an argument in a</p>



<p class="wp-block-paragraph">C++ function signature prototype, you&#8217;re not really writing that at all. You&#8217;re just saying</p>



<p class="wp-block-paragraph">that the A variable should get loaded from whatever the RDI register had.</p>



<p class="wp-block-paragraph">Or if you previously loaded the RDI register and you want to call a C++ function, that&#8217;s</p>



<p class="wp-block-paragraph">a C++ function that&#8217;s what it would expect the RDI register so it can work pretty seamlessly</p>



<p class="wp-block-paragraph">just calling things back and forth um so yeah I guess that&#8217;s all I really had to say</p>



<p class="wp-block-paragraph">this is the basics of writing a hybrid program thank you for watching I hope you learned a</p>



<p class="wp-block-paragraph">little bit of stuff and had a little bit of fun see you in the next video</p>



<p class="wp-block-paragraph">thanks for watching this video again from the bottom of my heart I really</p>



<p class="wp-block-paragraph">appreciate it I do hope you did learn something and have some fun if you could</p>



<p class="wp-block-paragraph">do me a please a small little favor could you please subscribe and follow</p>



<p class="wp-block-paragraph">this channel or these videos or whatever it is you do on the current social media</p>



<p class="wp-block-paragraph">website that you&#8217;re looking at right now it would really mean the world to me and</p>



<p class="wp-block-paragraph">it&#8217;ll help make more videos and grow this community so we&#8217;ll be able to do</p>



<p class="wp-block-paragraph">more videos longer videos better videos or just I&#8217;ll be able to keep making</p>



<p class="wp-block-paragraph">videos in general. So please do me a kindness and subscribe. You know, sometimes I&#8217;m sleeping</p>



<p class="wp-block-paragraph">in the middle of the night and I just wake up because I know somebody subscribed or followed.</p>



<p class="wp-block-paragraph">It just wakes me up and I get filled with joy. That&#8217;s exactly what happens every single time.</p>



<p class="wp-block-paragraph">So you could do it as a nice favor to me or you could troll me if you want to just wake me up in</p>



<p class="wp-block-paragraph">the middle of the night, just subscribe and then I&#8217;ll just wake up. I promise that&#8217;s what&#8217;ll happen.</p>



<p class="wp-block-paragraph">Also, if you look at the middle of the screen right now, you should see a QR code, which you</p>



<p class="wp-block-paragraph">QR code which you can scan in order to go to the website which I think is also named somewhere at</p>



<p class="wp-block-paragraph">the bottom of this video and it&#8217;ll take you to my main website where you can just kind of like see</p>



<p class="wp-block-paragraph">all the videos I published and the services and tutorials and things that I offer and all that</p>



<p class="wp-block-paragraph">good stuff and if you have a suggestion for clarifications or errata or just future videos</p>



<p class="wp-block-paragraph">that you want to see please leave a comment or if you just want to say hey what&#8217;s up what&#8217;s going on</p>



<p class="wp-block-paragraph">you know just send me a comment whatever i also wake up for those in the middle of the night i get</p>



<p class="wp-block-paragraph">i wake up in a cold sweat and i&#8217;m like it would really it really mean the world to me i would</p>



<p class="wp-block-paragraph">really appreciate it so again thank you so much for watching this video and um enjoy the cool music</p>



<p class="wp-block-paragraph">as as i fade into the darkness which is coming for us all</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/mix-c-c-and-assembly-into-one-program-hybrid-program-full-build-tutorial/">Mix C++, C, and Assembly into One Program &#8211; Hybrid Program Full Build Tutorial</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/mix-c-c-and-assembly-into-one-program-hybrid-program-full-build-tutorial/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Master Command Line Arguments in x86-64 Assembly with YASM</title>
		<link>https://www.NeuralLantern.com/master-command-line-arguments-in-x86-64-assembly-with-yasm/</link>
					<comments>https://www.NeuralLantern.com/master-command-line-arguments-in-x86-64-assembly-with-yasm/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 08 Sep 2025 07:44:41 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[argc argv]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[assembly tutorial]]></category>
		<category><![CDATA[command line arguments]]></category>
		<category><![CDATA[Linux programming]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[stack pointer]]></category>
		<category><![CDATA[system calls]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=221</guid>

					<description><![CDATA[<p>Learn to access command line arguments in x86-64 YASM assembly on Linux. Master stack handling and argv looping in this clear, practical tutorial!</p>
<p>The post <a href="https://www.NeuralLantern.com/master-command-line-arguments-in-x86-64-assembly-with-yasm/">Master Command Line Arguments in x86-64 Assembly with YASM</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Master Command Line Arguments in x86-64 Assembly with YASM" width="1380" height="776" src="https://www.youtube.com/embed/hRl3hKvHiyM?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">Want to master command line arguments in pure x86-64 YASM assembly on Linux? This video breaks down how to access argc and argv directly from the stack, without GCC libraries. Follow along as we write a program to loop through and print user-provided arguments, complete with a visual stack explanation. Whether you&#8217;re new to assembly or leveling up, this tutorial offers clear, practical insights for coding low-level programs. Check out my other videos for more assembly tips, and subscribe for more coding goodness!</p>



<p class="wp-block-paragraph">Introduction to Command Line Arguments 00:00:00<br>Target Architecture and Assumptions 00:00:04<br>Recap of Command Line Arguments 00:00:56<br>Accessing Arguments in Pure Assembly 00:01:47<br>Program Structure and Data Section 00:02:19<br>Stack Pointer and Argument Count 00:04:16<br>Accessing Argument Vector (argv) 00:07:14<br>Looping Through Arguments 00:12:16<br>Incrementing to Next Argument 00:16:45<br>Visualizing the Stack 00:19:24<br>Running the Program with Arguments 00:21:56<br>Conclusion and Call to Subscribe 00:23:48</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>
</ul>



<p class="wp-block-paragraph">Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



<li>Leaving a comment or suggestion</li>



<li>Subscribing to our Blog</li>



<li>Watching the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p class="wp-block-paragraph">hey there let&#8217;s talk about accepting command line arguments in a pure assembly program</p>



<p class="wp-block-paragraph">written for x86-64 aka amd yasm assembly</p>



<p class="wp-block-paragraph">on linux anyway so uh there are going to be a lot of topics that i that i sort of mention in this</p>



<p class="wp-block-paragraph">video that are not actually covered in this video so if you feel yourself getting lost like for</p>



<p class="wp-block-paragraph">program in assembly in the first place.</p>



<p class="wp-block-paragraph">If you don&#8217;t know what command line arguments are,</p>



<p class="wp-block-paragraph">if you don&#8217;t know the terminal, things like that,</p>



<p class="wp-block-paragraph">you probably want to check out my previous videos</p>



<p class="wp-block-paragraph">because they will be explained in detail.</p>



<p class="wp-block-paragraph">For now, I&#8217;m just going to assume you know how to do all this basic stuff.</p>



<p class="wp-block-paragraph">And the only thing that I need to show you</p>



<p class="wp-block-paragraph">is how to write a program in Yasm assembly that can</p>



<p class="wp-block-paragraph">grab your command line arguments.</p>



<p class="wp-block-paragraph">So, you know, just to do a quick recap of what I&#8217;m even talking about.</p>



<p class="wp-block-paragraph">echo we can launch it with no arguments it doesn&#8217;t do anything but we can give</p>



<p class="wp-block-paragraph">it some arguments we can say hello and then we can say goodbye and that&#8217;s two</p>



<p class="wp-block-paragraph">arguments that I&#8217;m giving to echo echo will grab on to the arguments it was</p>



<p class="wp-block-paragraph">given it&#8217;s actually going to receive three arguments the index zero argument</p>



<p class="wp-block-paragraph">being the name of itself its own program and then the next two arguments being</p>



<p class="wp-block-paragraph">the stuff that I typed after the program but you can see that it somehow figured</p>



<p class="wp-block-paragraph">two arguments as strings and print them right so imagine you have an assembly</p>



<p class="wp-block-paragraph">program and you want the user to be able to launch your program and add</p>



<p class="wp-block-paragraph">arguments at the end of the command line and have your program behave in some</p>



<p class="wp-block-paragraph">certain way according to what the user wants the question then becomes how do</p>



<p class="wp-block-paragraph">you access those arguments it&#8217;s a little bit easier in GCC look at my other video</p>



<p class="wp-block-paragraph">that I posted about the topic for GCC linked assembly programs it&#8217;s a little</p>



<p class="wp-block-paragraph">You pretty much just look at RDI and RSI to get your argc and your character pointer array.</p>



<p class="wp-block-paragraph">When you&#8217;re dealing with pure assembly though, the GCC libraries don&#8217;t bundle up the command</p>



<p class="wp-block-paragraph">line arguments in a really convenient way for you.</p>



<p class="wp-block-paragraph">You&#8217;ve got to look for them elsewhere.</p>



<p class="wp-block-paragraph">It&#8217;s honestly not that much harder.</p>



<p class="wp-block-paragraph">It&#8217;s just a little, feels a little weird.</p>



<p class="wp-block-paragraph">I think at least for me, I was hoping to see the arguments in a register and I did not.</p>



<p class="wp-block-paragraph">But anyway, so here&#8217;s my program.</p>



<p class="wp-block-paragraph">Again, this is not an assembly programming video.</p>



<p class="wp-block-paragraph">an assembly programming video if you need to know how to program assembly see my other videos for</p>



<p class="wp-block-paragraph">now i&#8217;m just going to quickly go over it i&#8217;ve got my data section in yasm i am printing out a couple</p>



<p class="wp-block-paragraph">of strings or i i have a couple of strings defined i&#8217;m saying like the module has started i&#8217;m going</p>



<p class="wp-block-paragraph">to begin printing arguments i have some system call codes so that i can write just to standard</p>



<p class="wp-block-paragraph">output and so that i can exit the program this is pure assembly so we don&#8217;t actually return from</p>



<p class="wp-block-paragraph">we just get jumped into and then we call exit normally and then here&#8217;s the standard output pipe</p>



<p class="wp-block-paragraph">again another video covers that and then i&#8217;ve got the exit success code again another video covers</p>



<p class="wp-block-paragraph">that but basically you know exiting zero is usually what you do for success then i&#8217;ve got the</p>



<p class="wp-block-paragraph">text section which holds all my assembly instructions and this is my entry point we don&#8217;t</p>



<p class="wp-block-paragraph">really need to uh push and pop these registers because we&#8217;re just going to exit the program when</p>



<p class="wp-block-paragraph">it&#8217;s totally fine we would need to preserve Kali saved registers if we were</p>



<p class="wp-block-paragraph">jumping into a main function or if this was a different function that was being</p>



<p class="wp-block-paragraph">jumped into but we&#8217;re not so we don&#8217;t have to anyway here&#8217;s my entry point the</p>



<p class="wp-block-paragraph">underscore start for a pure assembly program and then I&#8217;m going to grab the</p>



<p class="wp-block-paragraph">incoming arguments when you&#8217;re linking with GCC which we&#8217;re not doing here in</p>



<p class="wp-block-paragraph">this video then the arguments come in very easily they just come in argc</p>



<p class="wp-block-paragraph">up in RDI and arg sorry argc comes into RDI and the character pointer array comes in as RSI so you</p>



<p class="wp-block-paragraph">can imagine in GCC this would be integer main and then integer argc character pointer array argv</p>



<p class="wp-block-paragraph">right hopefully you&#8217;ve done this in a higher level language so you have a better idea of what I&#8217;m</p>



<p class="wp-block-paragraph">talking about if not I guess that&#8217;s okay it will still allow you to grab even if you don&#8217;t</p>



<p class="wp-block-paragraph">it will still allow you to grab,</p>



<p class="wp-block-paragraph">even if you don&#8217;t understand this in C++.</p>



<p class="wp-block-paragraph">So argc is actually the stack pointer.</p>



<p class="wp-block-paragraph">The stack pointer register, RSP,</p>



<p class="wp-block-paragraph">always tells you where the top of the stack is,</p>



<p class="wp-block-paragraph">like what&#8217;s the last piece of data</p>



<p class="wp-block-paragraph">that we actually have sitting on the stack.</p>



<p class="wp-block-paragraph">So that&#8217;s gonna be the count of arguments.</p>



<p class="wp-block-paragraph">argc is always gonna be the stack pointer.</p>



<p class="wp-block-paragraph">So if you dereference the stack pointer register,</p>



<p class="wp-block-paragraph">pointer register then what you&#8217;re doing is you know the stack pointer register</p>



<p class="wp-block-paragraph">actually points to memory locations within the stack so if the memory</p>



<p class="wp-block-paragraph">location that it&#8217;s looking at is where argc is stored then you dereference it</p>



<p class="wp-block-paragraph">with the brackets and you&#8217;ll actually get argc so right away I&#8217;m just gonna</p>



<p class="wp-block-paragraph">steal argc off the top of the stack and this is a good idea to do right away</p>



<p class="wp-block-paragraph">because if you start doing other stuff you might end up modifying the stack</p>



<p class="wp-block-paragraph">especially if you start calling functions and things like that and and</p>



<p class="wp-block-paragraph">and and these these pieces of information will be lost or they&#8217;ll be a lot harder to find so right</p>



<p class="wp-block-paragraph">away i&#8217;m just going to say argc goes directly into r12 and i have a little comment reminder</p>



<p class="wp-block-paragraph">for myself at the top saying r12 is now argc same thing for uh r13 i&#8217;m going to say that&#8217;s a pointer</p>



<p class="wp-block-paragraph">to my character pointer array i talked about this in depth on my other video where we talked about</p>



<p class="wp-block-paragraph">an assembly program but basically if you look at uh if you look at argv right here it&#8217;s not just a</p>



<p class="wp-block-paragraph">pointer and it&#8217;s not just an array it&#8217;s a pointer to an array and if you recall in higher level</p>



<p class="wp-block-paragraph">languages or just i guess anything an array itself is a pointer because argv the symbol let&#8217;s say we</p>



<p class="wp-block-paragraph">were in a higher level language argv can only point to one thing so it&#8217;s going to point to the very</p>



<p class="wp-block-paragraph">but the beginning of your array is not going to be one string like one argument that the user gave</p>



<p class="wp-block-paragraph">you because the user could give you many arguments instead it&#8217;s an array of pointers</p>



<p class="wp-block-paragraph">so it&#8217;s a character pointer array meaning argv is a pointer to a pointer</p>



<p class="wp-block-paragraph">the first item in any array is what is pointed to by the symbol so like imagine if we had just</p>



<p class="wp-block-paragraph">had just for the sake of argument imagine if we had an integer array let&#8217;s say we had like 500</p>



<p class="wp-block-paragraph">integers in like a c++ program so the symbol a is what i&#8217;m trying to say the symbol a</p>



<p class="wp-block-paragraph">is really a pointer that points to the first integer in that array and then later of course</p>



<p class="wp-block-paragraph">you can dereference that pointer with some sort of an index to get the index 5 integer or the</p>



<p class="wp-block-paragraph">the array and a pointer to the first item.</p>



<p class="wp-block-paragraph">Or I guess you could say it&#8217;s both a pointer to the array</p>



<p class="wp-block-paragraph">and a pointer to the first item,</p>



<p class="wp-block-paragraph">because it&#8217;s kind of the same thing.</p>



<p class="wp-block-paragraph">So that means if we grab argv,</p>



<p class="wp-block-paragraph">then we&#8217;ll be grabbing a pointer.</p>



<p class="wp-block-paragraph">If we then dereference that pointer,</p>



<p class="wp-block-paragraph">it would tell us where another pointer is.</p>



<p class="wp-block-paragraph">And then that other pointer would point to a string</p>



<p class="wp-block-paragraph">representing the first argument.</p>



<p class="wp-block-paragraph">first argument so we&#8217;ll explain that more as i do this loop i&#8217;m going to i&#8217;m going to show you</p>



<p class="wp-block-paragraph">some code that actually loops through all the arguments anyway the thing about where to find</p>



<p class="wp-block-paragraph">that argv is it&#8217;s just the next pointer or i guess it&#8217;s the it&#8217;s the next item that the stack holds</p>



<p class="wp-block-paragraph">so remember i told you that rsp when this when this function comes in rsp is a pointer to wherever</p>



<p class="wp-block-paragraph">we&#8217;re we&#8217;re looking what memory location the top of the stack is so if we dereference that we get</p>



<p class="wp-block-paragraph">So if we dereference that, we get argc.</p>



<p class="wp-block-paragraph">Well, we just need to go find the next previous item in the stack</p>



<p class="wp-block-paragraph">and then dereference that so that we can get the first pointer or argv,</p>



<p class="wp-block-paragraph">you know, the first pointer to a pointer,</p>



<p class="wp-block-paragraph">or the first double pointer in your array of pointer to pointers.</p>



<p class="wp-block-paragraph">Why am I doing plus eight?</p>



<p class="wp-block-paragraph">This might actually make sense as is,</p>



<p class="wp-block-paragraph">sense as is but it&#8217;s important to understand that when you add stuff to the stack you&#8217;re actually</p>



<p class="wp-block-paragraph">decreasing the memory locations uh that you are pointing to so like if i if i increase a stack</p>



<p class="wp-block-paragraph">this is not a stack video there&#8217;s other videos that i have for that um if we add something to</p>



<p class="wp-block-paragraph">the top of a stack you imagine the stack growing visually in a in a vertical direction like it&#8217;s</p>



<p class="wp-block-paragraph">growing up right but inside of the computer the memory locations are actually going down</p>



<p class="wp-block-paragraph">If we&#8217;re looking at the stack pointer and we&#8217;re adding eight, what it&#8217;s saying is that</p>



<p class="wp-block-paragraph">we&#8217;re looking back into memory that was already added for the stack.</p>



<p class="wp-block-paragraph">So that&#8217;s like if we grew the stack, that would be subtracting from the memory location.</p>



<p class="wp-block-paragraph">So like looking further up in the stack would be subtracting from the memory location because</p>



<p class="wp-block-paragraph">the stack grows upward in the abstract, but downward in memory locations.</p>



<p class="wp-block-paragraph">So that means if we add eight, that means we&#8217;re going in the other direction.</p>



<p class="wp-block-paragraph">that means we&#8217;re going in the other direction.</p>



<p class="wp-block-paragraph">We&#8217;re looking downward into the stack.</p>



<p class="wp-block-paragraph">So what that means is that the top two items on the stack</p>



<p class="wp-block-paragraph">are first argc and then second, that first pointer,</p>



<p class="wp-block-paragraph">you know, the argv argument.</p>



<p class="wp-block-paragraph">And that&#8217;s how we access it with plus eight.</p>



<p class="wp-block-paragraph">Why is it a plus eight instead of plus something else?</p>



<p class="wp-block-paragraph">Because all pointers on 64-bit systems are 64-bit integers</p>



<p class="wp-block-paragraph">and 64-bit integers are eight bytes.</p>



<p class="wp-block-paragraph">If we&#8217;re talking about eight bytes, sorry,</p>



<p class="wp-block-paragraph">if we&#8217;re talking about eight bits per byte.</p>



<p class="wp-block-paragraph">So this is just how you access it.</p>



<p class="wp-block-paragraph">What about this other thing going on here?</p>



<p class="wp-block-paragraph">What&#8217;s this LEA instruction?</p>



<p class="wp-block-paragraph">Usually you see a move instruction, right?</p>



<p class="wp-block-paragraph">So we basically want to look at the stack pointer,</p>



<p class="wp-block-paragraph">but look one level lower.</p>



<p class="wp-block-paragraph">And each item on the stack is also going to be eight bytes.</p>



<p class="wp-block-paragraph">We want to look one item lower on the stack,</p>



<p class="wp-block-paragraph">and then we want to dereference it.</p>



<p class="wp-block-paragraph">but we don&#8217;t want to dereference and store the dereferenced value we still want to grab the</p>



<p class="wp-block-paragraph">actual pointer the thing is I shouldn&#8217;t have said dereference in the first place these are dereferencing</p>



<p class="wp-block-paragraph">brackets right so like up here on line 46 when you put brackets around rsp it dereferences whatever</p>



<p class="wp-block-paragraph">value rsp holds so therefore when you put brackets around this you kind of expect that you&#8217;re going</p>



<p class="wp-block-paragraph">to be dereferencing right but we don&#8217;t want to dereference we just want the original address that</p>



<p class="wp-block-paragraph">showing us. So the problem with this is that when we say RSP plus eight,</p>



<p class="wp-block-paragraph">we can&#8217;t actually do a mathematical formula unless we&#8217;re inside of brackets, which means we</p>



<p class="wp-block-paragraph">can&#8217;t do them in a move instruction without accidentally dereferencing and losing a pointer</p>



<p class="wp-block-paragraph">to the actual array of pointers. If we dereferenced like this, we would just end up with,</p>



<p class="wp-block-paragraph">you know, a pointer to one string. So the way around this is instead of using the move instruction,</p>



<p class="wp-block-paragraph">instead of using the move instruction we use the lea instruction the lea instruction allows us to</p>



<p class="wp-block-paragraph">put a formula inside of brackets so that the assembler won&#8217;t get confused but then it won&#8217;t</p>



<p class="wp-block-paragraph">put the dereferenced value into r13 it&#8217;ll just put the actual value of whatever we see</p>



<p class="wp-block-paragraph">with that formula meaning it&#8217;ll give us the memory location of the item sitting one under the top of</p>



<p class="wp-block-paragraph">this at this point r13 is now the memory location of the of the item sitting one underneath the top</p>



<p class="wp-block-paragraph">of the stack and then we can de-reference that later in order to look at all of our arguments</p>



<p class="wp-block-paragraph">so the next part of our code is just going to print an intro message so there&#8217;s like this intro</p>



<p class="wp-block-paragraph">string up here hello and um i&#8217;m basically going to call a custom function that i made to just sort of</p>



<p class="wp-block-paragraph">Printing. Don&#8217;t worry about this code right here.</p>



<p class="wp-block-paragraph">This is not the point of the video.</p>



<p class="wp-block-paragraph">I just made a custom function that just kind of helped me print.</p>



<p class="wp-block-paragraph">Don&#8217;t worry about that.</p>



<p class="wp-block-paragraph">The real meat of this video is that we&#8217;re going to loop through all arguments</p>



<p class="wp-block-paragraph">and just print every single argument that the user provided.</p>



<p class="wp-block-paragraph">So you can imagine this is maybe the top of a while loop here.</p>



<p class="wp-block-paragraph">Notice how, oh, I forgot to replace main with start with start.</p>



<p class="wp-block-paragraph">I had another version of this program that used main.</p>



<p class="wp-block-paragraph">pretend we have this is called the main loop instead of the the main functions</p>



<p class="wp-block-paragraph">loop anyway so main loop initialize first thing I&#8217;m going to do is I&#8217;m going</p>



<p class="wp-block-paragraph">to initialize the loop by saying we&#8217;re looking at index 0 okay no problem so</p>



<p class="wp-block-paragraph">we&#8217;re looking at index 0 we want to do that we want to look at all the</p>



<p class="wp-block-paragraph">arguments you know we want to look at index 0 and index 1 and index 2 and we</p>



<p class="wp-block-paragraph">just want to keep going until we&#8217;re out of arguments remember that we also have</p>



<p class="wp-block-paragraph">the number of arguments coming from argc now sitting in r12. So that means if we have a</p>



<p class="wp-block-paragraph">counter that starts at zero with r14 and then we know how many arguments there are in r12,</p>



<p class="wp-block-paragraph">we should be able to know when the loop stops. So just a reminder that, you know, the loops here,</p>



<p class="wp-block-paragraph">the arrays here are zero-based indexed or zero-index based, which means, for example,</p>



<p class="wp-block-paragraph">that&#8217;s three arguments but the array of arguments is going to have indexes zero and one and two</p>



<p class="wp-block-paragraph">which means the last valid index is going to be two or the size minus one think about that if you</p>



<p class="wp-block-paragraph">have five arguments the last index that is valid is going to be four size minus one because it&#8217;s</p>



<p class="wp-block-paragraph">zero based so we can use that logic to figure out if we&#8217;re done so we start off with an index of zero</p>



<p class="wp-block-paragraph">here and then at the top of the loop i&#8217;m just going to quickly ask are we done how do we know</p>



<p class="wp-block-paragraph">are we done how do we know if we&#8217;re done we compare the current index we&#8217;re</p>



<p class="wp-block-paragraph">looking at with the count and we say if the current index we&#8217;re looking at is</p>



<p class="wp-block-paragraph">greater than or equal to the count then we know we&#8217;re already done we don&#8217;t need</p>



<p class="wp-block-paragraph">to look at any more indexes why am I saying is is the index greater than or</p>



<p class="wp-block-paragraph">equal to the count because remember if we have five arguments then the last</p>



<p class="wp-block-paragraph">valid index is four therefore if we find ourselves looking at index five we know</p>



<p class="wp-block-paragraph">already done five is equal to or greater than five, but four is not equal to or greater</p>



<p class="wp-block-paragraph">than five.</p>



<p class="wp-block-paragraph">So that&#8217;s why I&#8217;m using this logic here.</p>



<p class="wp-block-paragraph">I&#8217;m saying if we&#8217;re done, then jump to the done label.</p>



<p class="wp-block-paragraph">Basically what&#8217;s the done label?</p>



<p class="wp-block-paragraph">It&#8217;s main loop done.</p>



<p class="wp-block-paragraph">All it really does is it says goodbye and then it just exits the program with a system</p>



<p class="wp-block-paragraph">call.</p>



<p class="wp-block-paragraph">Again, I have other videos that explain system calls and all that stuff.</p>



<p class="wp-block-paragraph">So if we&#8217;re done, we, uh, we jumped down to the done area, but if not, then execute</p>



<p class="wp-block-paragraph">to the done area but if not then execution will fall through to this next loop or sorry this next</p>



<p class="wp-block-paragraph">label which is not necessarily we don&#8217;t really need a label there but i i like to put i like to</p>



<p class="wp-block-paragraph">put it there just to help myself remember this is what it looks like in terms of a while loop you</p>



<p class="wp-block-paragraph">know i have like here&#8217;s the top of the while loop here&#8217;s the little comparison part of the while loop</p>



<p class="wp-block-paragraph">here&#8217;s the body like the opening braces of the while loop and then here&#8217;s sort of like the</p>



<p class="wp-block-paragraph">closing braces i just like to do that but anyway after we decided that we are not done and we drop</p>



<p class="wp-block-paragraph">done and we drop through to the next actual instruction we&#8217;re just going to</p>



<p class="wp-block-paragraph">print the next argument so what&#8217;s the next argument we will dereference r13</p>



<p class="wp-block-paragraph">remember up here we took the second item sitting like you know one under the top</p>



<p class="wp-block-paragraph">of the stack and we just stuck it into r13 so this is r13 is now the address of</p>



<p class="wp-block-paragraph">the second item on the stack if we de-reference it then that&#8217;s going to give</p>



<p class="wp-block-paragraph">then that&#8217;s going to give us a pointer to our um sorry it&#8217;s going to give us a pointer to the first</p>



<p class="wp-block-paragraph">item to the first string i should say to the first string uh the f this the string of the first</p>



<p class="wp-block-paragraph">argument so if we dereference you know let me say this one more time just to make sure that i&#8217;m</p>



<p class="wp-block-paragraph">item, you know, the item right under the top of the stack, that&#8217;s going to be a pointer</p>



<p class="wp-block-paragraph">to the first argument string.</p>



<p class="wp-block-paragraph">So if we just simply dereference R13, then we&#8217;re basically telling RDI, here&#8217;s a pointer</p>



<p class="wp-block-paragraph">to the string that we want to print.</p>



<p class="wp-block-paragraph">Okay, so then I just call my helper function to print that string to standard output, no</p>



<p class="wp-block-paragraph">problem.</p>



<p class="wp-block-paragraph">And then we have to figure out like, how do we increment to go to the next string?</p>



<p class="wp-block-paragraph">might have been unclear earlier because gcc does it in a different way but on the stack</p>



<p class="wp-block-paragraph">every item underneath the the top of the stack is a is another pointer it&#8217;s part of the original argv</p>



<p class="wp-block-paragraph">array it&#8217;s another pointer to a different string so basically if i increase r14 here then i&#8217;m</p>



<p class="wp-block-paragraph">increasing the index counter that&#8217;ll help us eventually terminate but notice how here i&#8217;m</p>



<p class="wp-block-paragraph">Remember R13 is the second item, you know, the item right under the top of the stack.</p>



<p class="wp-block-paragraph">And if I dereference that, then I now have a pointer to the first argument string.</p>



<p class="wp-block-paragraph">If I want to go one lower into the stack, then I just add eight to that register&#8217;s value.</p>



<p class="wp-block-paragraph">Because again, remember, the stack grows downward in memory.</p>



<p class="wp-block-paragraph">So if I increase the value, then I&#8217;m sort of like going through previous items that were put into the stack.</p>



<p class="wp-block-paragraph">So imagine the stack here, it&#8217;s got like,</p>



<p class="wp-block-paragraph">you know, argc sitting on top</p>



<p class="wp-block-paragraph">and then underneath argc,</p>



<p class="wp-block-paragraph">it&#8217;s got a pointer to the first argument.</p>



<p class="wp-block-paragraph">And then under that in the stack,</p>



<p class="wp-block-paragraph">it&#8217;s got a pointer to the second argument.</p>



<p class="wp-block-paragraph">And then under that in the stack,</p>



<p class="wp-block-paragraph">it&#8217;s got a pointer to the third argument.</p>



<p class="wp-block-paragraph">Then under that, it&#8217;s got a pointer</p>



<p class="wp-block-paragraph">to the next argument and so forth.</p>



<p class="wp-block-paragraph">So every time we wanna go to the next arguments pointer,</p>



<p class="wp-block-paragraph">the next arguments string pointer,</p>



<p class="wp-block-paragraph">we just add eight to that R13 register,</p>



<p class="wp-block-paragraph">which was originally just pointing at the first string.</p>



<p class="wp-block-paragraph">so again why eight because pointers are 64-bit integers therefore they are eight bytes so I&#8217;m</p>



<p class="wp-block-paragraph">just literally increasing the index by one and then I&#8217;m moving that register r13 to point to</p>



<p class="wp-block-paragraph">the next pointer that way next time I dereference it up here on line 70 I&#8217;ll be dereferencing the</p>



<p class="wp-block-paragraph">next string and these strings don&#8217;t need to be contiguous they could be located anywhere it&#8217;s</p>



<p class="wp-block-paragraph">that is contiguous but it&#8217;s just full of pointers to other strings or sorry it&#8217;s full of if we de</p>



<p class="wp-block-paragraph">reference the stack pointer at any point then we will get the address of a string so then we will</p>



<p class="wp-block-paragraph">get a pointer to a string this double pointer stuff sometimes i get tongue tongue tired okay so uh</p>



<p class="wp-block-paragraph">when we&#8217;re done printing and incrementing then we just jump to the top of the loop</p>



<p class="wp-block-paragraph">And then finally, when we&#8217;re done, well, we&#8217;re done.</p>



<p class="wp-block-paragraph">Let me draw this out for you, because I think the way I&#8217;m explaining it might be</p>



<p class="wp-block-paragraph">a little bit unclear, so I just want to make sure that I&#8217;m being totally clear on</p>



<p class="wp-block-paragraph">this, okay, so I&#8217;ve got my little annotator here and you can imagine here&#8217;s a stack</p>



<p class="wp-block-paragraph">and it visually grows up.</p>



<p class="wp-block-paragraph">We can imagine that every time we add an item to the stack, it grows up.</p>



<p class="wp-block-paragraph">Like if you wanted to take a five, stick it on top of the stack.</p>



<p class="wp-block-paragraph">Well, then it would end up on the top of the stack, right?</p>



<p class="wp-block-paragraph">No problem.</p>



<p class="wp-block-paragraph">Right? No problem.</p>



<p class="wp-block-paragraph">Oh, maybe this needs to be bigger because of my pen size.</p>



<p class="wp-block-paragraph">So let&#8217;s do it like that.</p>



<p class="wp-block-paragraph">What the heck happened?</p>



<p class="wp-block-paragraph">I lost the whole desktop.</p>



<p class="wp-block-paragraph">There we go.</p>



<p class="wp-block-paragraph">So we have this and this and this and this.</p>



<p class="wp-block-paragraph">And so there&#8217;s probably some kind of a value sitting on top of the stack.</p>



<p class="wp-block-paragraph">When we started the program,</p>



<p class="wp-block-paragraph">RSP is a register that just has the memory location of the top of the stack,</p>



<p class="wp-block-paragraph">you know, the most recently added item.</p>



<p class="wp-block-paragraph">you know the most recently added item we know that this was actually arg c so if we dereferenced rsp</p>



<p class="wp-block-paragraph">that&#8217;ll go to wherever rx is and get it so we can basically say that rx is sitting on the top of</p>



<p class="wp-block-paragraph">the stack so you know we dereference our rsp which holds a memory location to this place in memory</p>



<p class="wp-block-paragraph">we just had an integer stored an eight byte integer okay cool so then the next lowest i guess</p>



<p class="wp-block-paragraph">the next highest item or the next lower item is arg v at index zero which is the same thing as</p>



<p class="wp-block-paragraph">just saying arg v remember a pointer to the array is really a pointer to the first item</p>



<p class="wp-block-paragraph">the next lower item is arg v at index one and so forth so we can just keep doing this we can keep</p>



<p class="wp-block-paragraph">keep doing this we can keep going lower and lower and lower on the stack by adding eight to r13</p>



<p class="wp-block-paragraph">because remember r13 originally pointed uh you know to this item right here i don&#8217;t know i&#8217;ll</p>



<p class="wp-block-paragraph">say like r13 maybe starts off pointing uh to the first argument because that&#8217;s the way we have it</p>



<p class="wp-block-paragraph">set up so every time the loop iterates if we dereference r13 then we&#8217;re getting a pointer to</p>



<p class="wp-block-paragraph">when we add eight bytes to r13 we&#8217;re really just moving it down to the next pointer that we can</p>



<p class="wp-block-paragraph">dereference hopefully that was a little bit more clear than what i said before or maybe you&#8217;re just</p>



<p class="wp-block-paragraph">a visual learner um you know it&#8217;s a good idea to try to explain things in many different ways</p>



<p class="wp-block-paragraph">but long story short we&#8217;re going down down down further in the stack to get more arguments and</p>



<p class="wp-block-paragraph">we&#8217;re just printing them and so now that we&#8217;ve explained it all we should be able to just run</p>



<p class="wp-block-paragraph">clear and make run and under the hood let me just show you real fast what I&#8217;ve got inside of my make</p>



<p class="wp-block-paragraph">file under the hood when we do make run you don&#8217;t need to know about make files I have a video that</p>



<p class="wp-block-paragraph">explains all of this don&#8217;t worry but well I mean you don&#8217;t need to know it to understand this video</p>



<p class="wp-block-paragraph">but you probably do need to know it notice how when I call the executable which in this case is</p>



<p class="wp-block-paragraph">I give it some arguments.</p>



<p class="wp-block-paragraph">I give it just some strings.</p>



<p class="wp-block-paragraph">First arg, second arg, third arg, fourth arg.</p>



<p class="wp-block-paragraph">So that&#8217;s the same thing as if I typed echo first arg,</p>



<p class="wp-block-paragraph">second arg, third arg, fourth arg, right?</p>



<p class="wp-block-paragraph">That&#8217;s what echo is doing.</p>



<p class="wp-block-paragraph">So in the make file, I&#8217;m just giving it those arguments.</p>



<p class="wp-block-paragraph">And then our program now is looping</p>



<p class="wp-block-paragraph">through all of the arguments.</p>



<p class="wp-block-paragraph">It knows when to stop because we grabbed argc</p>



<p class="wp-block-paragraph">and it knows where those strings are located</p>



<p class="wp-block-paragraph">are located because the pointers to those strings are just sitting on the stack so if we deref twice</p>



<p class="wp-block-paragraph">we&#8217;re derefing we&#8217;re doing a double dereference a pointer to a pointer</p>



<p class="wp-block-paragraph">at least we could say r13 is a pointer to a pointer but the actual values sitting inside</p>



<p class="wp-block-paragraph">the stack are just pointers to strings um so let&#8217;s just do this in a slightly different way</p>



<p class="wp-block-paragraph">clear and then I&#8217;ll say main because that&#8217;s the name of the program that I</p>



<p class="wp-block-paragraph">compiled you can imagine this is could be named something else and I&#8217;ll just put</p>



<p class="wp-block-paragraph">hello you are super cool now so I&#8217;m giving it one two three four five</p>



<p class="wp-block-paragraph">arguments in addition to main so it should print six things main hello you</p>



<p class="wp-block-paragraph">are super cool now so notice how it prints main hello you are super cool now</p>



<p class="wp-block-paragraph">Alright, that&#8217;s pretty much everything that I wanted to show you for the pure assembly</p>



<p class="wp-block-paragraph">version of Grappin Command Line Arguments.</p>



<p class="wp-block-paragraph">Thank you so much for listening.</p>



<p class="wp-block-paragraph">It&#8217;s a little late for me.</p>



<p class="wp-block-paragraph">The sun&#8217;s starting to come up.</p>



<p class="wp-block-paragraph">I&#8217;ve got to go and eat a bunch of cookies.</p>



<p class="wp-block-paragraph">So I hope you learned some stuff and I hope you had a little bit of fun.</p>



<p class="wp-block-paragraph">Thank you so much for watching this video.</p>



<p class="wp-block-paragraph">I will see you in the next one.</p>



<p class="wp-block-paragraph">Happy studying and coding and all that stuff.</p>



<p class="wp-block-paragraph">Hey everybody!</p>



<p class="wp-block-paragraph">Thanks for watching this video again from the bottom of my heart.</p>



<p class="wp-block-paragraph">I really appreciate it.</p>



<p class="wp-block-paragraph">I do hope you did learn something and have some fun.</p>



<p class="wp-block-paragraph">If you could do me a please, a small little favor,</p>



<p class="wp-block-paragraph">could you please subscribe and follow this channel or these videos</p>



<p class="wp-block-paragraph">or whatever it is you do on the current social media website</p>



<p class="wp-block-paragraph">that you&#8217;re looking at right now.</p>



<p class="wp-block-paragraph">It would really mean the world to me and it&#8217;ll help make more videos</p>



<p class="wp-block-paragraph">and grow this community.</p>



<p class="wp-block-paragraph">So we&#8217;ll be able to do more videos, longer videos, better videos,</p>



<p class="wp-block-paragraph">or just i&#8217;ll be able to keep making videos in general so please do do me a kindness and uh and</p>



<p class="wp-block-paragraph">subscribe you know sometimes i&#8217;m sleeping in the middle of the night and i just wake up because i</p>



<p class="wp-block-paragraph">know somebody subscribed or followed it just wakes me up and i get filled with joy that&#8217;s exactly what</p>



<p class="wp-block-paragraph">happens every single time so you could do it as a nice favor to me or you could you control me if</p>



<p class="wp-block-paragraph">you want to just wake me up in the middle of the night just subscribe and then i&#8217;ll i&#8217;ll just wake</p>



<p class="wp-block-paragraph">up i promise that&#8217;s what will happen also uh if you look at the middle of the screen right now you</p>



<p class="wp-block-paragraph">of the screen right now you should see a qr code which you can scan in order to go to the website</p>



<p class="wp-block-paragraph">which i think is also named somewhere at the bottom of this video and it&#8217;ll take you to my</p>



<p class="wp-block-paragraph">main website where you can just kind of like see all the videos i published and the services and</p>



<p class="wp-block-paragraph">tutorials and things that i offer and all that good stuff and uh if you have a suggestion for uh</p>



<p class="wp-block-paragraph">clarifications or errata or just future videos that you want to see please leave a comment or</p>



<p class="wp-block-paragraph">up what&#8217;s going on you know just send me a comment whatever i also wake up for those in the middle of</p>



<p class="wp-block-paragraph">the night i get i wake up in a cold sweat and i&#8217;m like it would really it really mean the world to</p>



<p class="wp-block-paragraph">me i would really appreciate it so again thank you so much for watching this video and um enjoy the</p>



<p class="wp-block-paragraph">cool music as as i fade into the darkness which is coming for us all</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/master-command-line-arguments-in-x86-64-assembly-with-yasm/">Master Command Line Arguments in x86-64 Assembly with YASM</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/master-command-line-arguments-in-x86-64-assembly-with-yasm/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Mastering x86-64 YASM Assembly: Functions Made Simple</title>
		<link>https://www.NeuralLantern.com/mastering-x86-64-yasm-assembly-functions-made-simple/</link>
					<comments>https://www.NeuralLantern.com/mastering-x86-64-yasm-assembly-functions-made-simple/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 24 Aug 2025 05:33:36 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[ABI compliance]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[assembly tutorial]]></category>
		<category><![CDATA[coding tutorial]]></category>
		<category><![CDATA[function calls]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[programming basics]]></category>
		<category><![CDATA[system calls]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=214</guid>

					<description><![CDATA[<p>Master x86-64 YASM assembly functions! Learn to write, call, and manage arguments &#038; returns in this beginner-friendly tutorial. Avoid crashes &#038; respect the ABI.</p>
<p>The post <a href="https://www.NeuralLantern.com/mastering-x86-64-yasm-assembly-functions-made-simple/">Mastering x86-64 YASM Assembly: Functions Made Simple</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Mastering x86-64 YASM Assembly: Functions Made Simple" width="1380" height="776" src="https://www.youtube.com/embed/KUshn1VczCA?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">Get hands-on with x86-64 YASM assembly in this in-depth tutorial on writing functions! Perfect for beginners and seasoned coders, we cover the basics of function creation, calling conventions, argument handling, and return values. Learn to avoid crashes, respect the ABI, and use prologue/epilogue for clean code. From printing messages to returning integers, see practical examples and tips to simplify your assembly programs. Subscribe for more low-level programming tutorials and take your skills to the next level!</p>



<p class="wp-block-paragraph">Introduction to Functions 00:00:00<br>Makefile Overview 00:00:19<br>Assembly Program Setup 00:02:01<br>Data Section Definitions 00:02:08<br>Text Section and Entry Point 00:03:02<br>Basic Assembly Program Demo 00:03:38<br>Return Codes Explained 00:04:30<br>Function Concepts Introduced 00:06:10<br>Creating a Simple Function 00:07:04<br>Function Call vs Jump 00:08:00<br>Adding Return Statement 00:09:06<br>Moving Print Logic to Function 00:09:48<br>Benefits of Functions 00:10:01<br>Calling Function Multiple Times 00:11:58<br>Creating Print Function with Arguments 00:14:00<br>Handling Function Arguments 00:15:45<br>Respecting the ABI 00:17:16<br>Prologue and Epilogue 00:23:15<br>Stack Operations 00:24:34<br>Calling Print Function 00:25:25<br>Avoiding Recursive Loop 00:34:17<br>Modifying Print Function to Print Line 00:35:21<br>Adding CRLF Function 00:30:13<br>Printing Multiple Messages 00:37:00<br>Returning Integer Values 00:37:48<br>Preserving Registers in Entry Point 00:41:50<br>Final Program Demo 00:43:24<br>Conclusion and Call to Subscribe 00:45:16</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>
</ul>



<p class="wp-block-paragraph">Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



<li>Leaving a comment or suggestion</li>



<li>Subscribing to our Blog</li>



<li>Watching the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p class="wp-block-paragraph">Hey there! Let&#8217;s talk about functions in x8664 Yasm Assembly.</p>



<p class="wp-block-paragraph">I&#8217;m just going to give you the basics of how to write a function.</p>



<p class="wp-block-paragraph">So let&#8217;s get started here. First thing I want to show you is just a makefile.</p>



<p class="wp-block-paragraph">This is not a makefile video, so you don&#8217;t have to worry about it too much.</p>



<p class="wp-block-paragraph">I just have it up because I need it for this demo.</p>



<p class="wp-block-paragraph">If you&#8217;re interested in makefiles, that would be a really, really good thing.</p>



<p class="wp-block-paragraph">thing i&#8217;ll probably make videos in the future about them but for now we&#8217;ll just say you hopefully have</p>



<p class="wp-block-paragraph">a script or method already set up that allows you to compile or assemble assembly programs</p>



<p class="wp-block-paragraph">so i&#8217;m just going to skim through it real fast if you&#8217;re interested you can kind of study it</p>



<p class="wp-block-paragraph">but basically i have like a little define here that allows me to print out a message that looks</p>



<p class="wp-block-paragraph">nice and then i define the repo path as the current path and i take the absolute path of that</p>



<p class="wp-block-paragraph">This is where the real work happens.</p>



<p class="wp-block-paragraph">I have Yasm flags because I&#8217;m using the Yasm assembler.</p>



<p class="wp-block-paragraph">I&#8217;m going to be assembling to x86 64.</p>



<p class="wp-block-paragraph">I like to out.</p>



<p class="wp-block-paragraph">I like to convert all of my warnings to errors to make sure that I don&#8217;t write sloppy code</p>



<p class="wp-block-paragraph">export debug symbols and export debug symbols for the linking stage to the name of my executable</p>



<p class="wp-block-paragraph">is going to be called a main.</p>



<p class="wp-block-paragraph">have set up and well I can just do make run or make debug as a little shortcut</p>



<p class="wp-block-paragraph">to run it in GDB if you don&#8217;t know GDB don&#8217;t worry that&#8217;s going to happen in a</p>



<p class="wp-block-paragraph">future video and make clean which just cleans the build area I like a clean</p>



<p class="wp-block-paragraph">build area and then this is the main command here we&#8217;re going to be using LD</p>



<p class="wp-block-paragraph">to do our linking because this is going to be a pure assembly program instead of</p>



<p class="wp-block-paragraph">a hybrid program which would require GCC or something else and then here&#8217;s my</p>



<p class="wp-block-paragraph">And then here&#8217;s my program main ASM is going to get compiled down to main.o and</p>



<p class="wp-block-paragraph">That&#8217;s it for the make pile</p>



<p class="wp-block-paragraph">Okay, so for the main assembly program I have some stuff written up already here, but I think I&#8217;m going to modify it for this video</p>



<p class="wp-block-paragraph">For starters, we have our data section which hopefully you understand already</p>



<p class="wp-block-paragraph">I&#8217;m just going to define a bunch of stuff in the data section like I&#8217;m going to define</p>



<p class="wp-block-paragraph">The system call code to write a string so I can print a message pretty easily</p>



<p class="wp-block-paragraph">so I can print a message pretty easily the system call code to actually exit the program</p>



<p class="wp-block-paragraph">remember when you&#8217;re writing a pure assembly program that has start as an entry point</p>



<p class="wp-block-paragraph">you need to be you know you&#8217;re responsible for exiting the program so the system call code is</p>



<p class="wp-block-paragraph">60 there and then I&#8217;m going to say exiting for success is a zero that&#8217;s kind of a standard thing</p>



<p class="wp-block-paragraph">and then I&#8217;m going to make two file descriptors for the standard input it&#8217;s going to be zero and</p>



<p class="wp-block-paragraph">the standard output it&#8217;s going to be one actually I don&#8217;t think we need that for this video we just</p>



<p class="wp-block-paragraph">We just need standard output, but not standard input.</p>



<p class="wp-block-paragraph">And then I&#8217;m just going to make two strings.</p>



<p class="wp-block-paragraph">That&#8217;s not my name, but I like those names.</p>



<p class="wp-block-paragraph">And then the BSS section.</p>



<p class="wp-block-paragraph">Actually, I&#8217;m going to get rid of that</p>



<p class="wp-block-paragraph">because we don&#8217;t really need that for this video.</p>



<p class="wp-block-paragraph">And then I have a text section, which is where all the code is.</p>



<p class="wp-block-paragraph">We&#8217;re going to begin our entry point.</p>



<p class="wp-block-paragraph">If you are writing a pure assembly program</p>



<p class="wp-block-paragraph">and this is your entry point module,</p>



<p class="wp-block-paragraph">make a function called underscore start.</p>



<p class="wp-block-paragraph">It&#8217;s not actually technically going to be a function because it won&#8217;t return anywhere.</p>



<p class="wp-block-paragraph">It&#8217;ll actually just call the system exit service.</p>



<p class="wp-block-paragraph">And then I mark start as global so that the operating system can go into it,</p>



<p class="wp-block-paragraph">can call it from the outside.</p>



<p class="wp-block-paragraph">And then all I&#8217;m doing right now is I&#8217;m printing a hello.</p>



<p class="wp-block-paragraph">So far, this is not really a demo of functions.</p>



<p class="wp-block-paragraph">This is just like a very basic pure assembly program.</p>



<p class="wp-block-paragraph">All it&#8217;s going to do is it&#8217;s going to enter.</p>



<p class="wp-block-paragraph">going to print the hello message and then down here it&#8217;s going to do a system call to exit the</p>



<p class="wp-block-paragraph">program with zero for success so let&#8217;s just see what that looks like real fast I&#8217;m going to do</p>



<p class="wp-block-paragraph">make run and then make run you can see the first line it just kind of prints and then the second</p>



<p class="wp-block-paragraph">line that&#8217;s make sort of echoing the command that I&#8217;m issuing it&#8217;s just going to assemble the main</p>



<p class="wp-block-paragraph">dot asm into a main dot o and then it&#8217;s letting me know that if there are any warnings it will</p>



<p class="wp-block-paragraph">It will refuse to compile.</p>



<p class="wp-block-paragraph">And then I use the LD linker to link the main executable.</p>



<p class="wp-block-paragraph">And then I actually run the executable.</p>



<p class="wp-block-paragraph">So only below this line that I have highlighted right now</p>



<p class="wp-block-paragraph">is gonna be the actual program.</p>



<p class="wp-block-paragraph">So you can see all it really did was just,</p>



<p class="wp-block-paragraph">you know, print the message and then exit.</p>



<p class="wp-block-paragraph">Just, you know, a little note here.</p>



<p class="wp-block-paragraph">I&#8217;ve probably said this in other videos,</p>



<p class="wp-block-paragraph">but the return code zero is kind of a standard</p>



<p class="wp-block-paragraph">to indicate success for programs,</p>



<p class="wp-block-paragraph">which is very useful if you want programs to automate other programs.</p>



<p class="wp-block-paragraph">If I change the system, let&#8217;s see what is it, exit success.</p>



<p class="wp-block-paragraph">If I just change that variable to a 3 so that I will exit with a return code of 3 instead of 0,</p>



<p class="wp-block-paragraph">then the operating system, or actually bash, since we&#8217;re in a terminal emulator,</p>



<p class="wp-block-paragraph">should consider the program to have exited in error.</p>



<p class="wp-block-paragraph">So you can see now it&#8217;s like, hey, hey, hey, the makefile system,</p>



<p class="wp-block-paragraph">oh not bash, the makefile system is like that program exited with code 3,</p>



<p class="wp-block-paragraph">three something went wrong and normally if you don&#8217;t add any extra stuff into your make file it&#8217;ll</p>



<p class="wp-block-paragraph">just refuse to continue at that point well so i&#8217;m just going to change it back to a zero and make</p>



<p class="wp-block-paragraph">sure that it still works and then we&#8217;ll get on to actually writing functions so i&#8217;m going to go clear</p>



<p class="wp-block-paragraph">and make run so it&#8217;s a little bit faster for me to run this repeatedly you can see actually the</p>



<p class="wp-block-paragraph">first time that i did that um the make file recompiled notice how it recompiled um and</p>



<p class="wp-block-paragraph">and relinked because I changed that source code.</p>



<p class="wp-block-paragraph">This is not a make file video, but I just want you to be aware.</p>



<p class="wp-block-paragraph">One of the great reasons to use make or a build system in general</p>



<p class="wp-block-paragraph">is because it makes it really easy to compile your code faster.</p>



<p class="wp-block-paragraph">Notice how if I didn&#8217;t actually change, let&#8217;s see.</p>



<p class="wp-block-paragraph">So I recompiled it because I just changed it again,</p>



<p class="wp-block-paragraph">but now I haven&#8217;t changed it since I&#8217;m going to recompile or rerun.</p>



<p class="wp-block-paragraph">Make just kind of like skips reassembling that file because it hasn&#8217;t changed.</p>



<p class="wp-block-paragraph">it can make your compilations and assemblies much, much faster.</p>



<p class="wp-block-paragraph">So anyway, functions. Functions in assembly are kind of simple and you&#8217;ll realize that</p>



<p class="wp-block-paragraph">some of the stuff that we take for granted in higher level languages is being done for us</p>



<p class="wp-block-paragraph">automatically by the compiler. But in assembly, we have labels. So for example, this start entry</p>



<p class="wp-block-paragraph">some valid symbols like characters underscore numbers I don&#8217;t think you can start a label with</p>



<p class="wp-block-paragraph">a number correct me if I&#8217;m wrong but I don&#8217;t think you can so we have like a label which is just like</p>



<p class="wp-block-paragraph">this collection of characters and then we have a colon after it and so you can jump into a label</p>



<p class="wp-block-paragraph">which I&#8217;ll talk about in a different video but you can jump into a label you can call a label as a</p>



<p class="wp-block-paragraph">function and so a label isn&#8217;t actually a function by itself you have to treat it in a certain way</p>



<p class="wp-block-paragraph">the abstract so notice how there&#8217;s no return statement at the bottom here let me write a</p>



<p class="wp-block-paragraph">function real fast and maybe i will have it so that i have a function that just says hello for</p>



<p class="wp-block-paragraph">me so i&#8217;ll make a label down here function that says hello and i&#8217;ll give it a label and i&#8217;ll say</p>



<p class="wp-block-paragraph">maybe like say hello and i&#8217;ll do a little label there first thing that you should do when you are</p>



<p class="wp-block-paragraph">trying to create a function is just put a return statement at the very end of it.</p>



<p class="wp-block-paragraph">That way it will return to the caller because if you don&#8217;t then it&#8217;s just going to continue</p>



<p class="wp-block-paragraph">to execute downward until it reaches no code and then the program will probably crash. In fact</p>



<p class="wp-block-paragraph">maybe I should do that. I&#8217;m going to do say hello as a label with no return so it&#8217;s not really a</p>



<p class="wp-block-paragraph">function now. It&#8217;s more of just like a label that you can jump to but there&#8217;s no code under it so</p>



<p class="wp-block-paragraph">I&#8217;m not even sure this will assemble, but it&#8217;ll definitely crash if we can call into this.</p>



<p class="wp-block-paragraph">So let me do, at the very start of our program, I&#8217;m going to type call say hello.</p>



<p class="wp-block-paragraph">Or actually, you know what, maybe first I&#8217;ll jump to hello.</p>



<p class="wp-block-paragraph">Jumping is going to be for a different video, but jumping just means go to that place.</p>



<p class="wp-block-paragraph">It&#8217;s a go-to statement, essentially.</p>



<p class="wp-block-paragraph">You&#8217;re not supposed to be able to return from a jump.</p>



<p class="wp-block-paragraph">but it doesn&#8217;t really follow the same design pattern logic as a function.</p>



<p class="wp-block-paragraph">So I&#8217;m going to immediately jump to the say hello logic and see what happens.</p>



<p class="wp-block-paragraph">I think that it&#8217;ll crash.</p>



<p class="wp-block-paragraph">Here we go.</p>



<p class="wp-block-paragraph">SegFault, core dumped, oh no.</p>



<p class="wp-block-paragraph">So, hey, it crashed.</p>



<p class="wp-block-paragraph">So instead of jumping there, let&#8217;s do a call.</p>



<p class="wp-block-paragraph">Now we&#8217;re treating it a little bit more like a function.</p>



<p class="wp-block-paragraph">We still don&#8217;t have the return statement down here.</p>



<p class="wp-block-paragraph">So it should still crash, I think.</p>



<p class="wp-block-paragraph">Let&#8217;s try that again.</p>



<p class="wp-block-paragraph">Yep, it crashed again.</p>



<p class="wp-block-paragraph">Because you&#8217;re supposed to exit from the operator, or you&#8217;re supposed to exit from the program</p>



<p class="wp-block-paragraph">properly to the operating system.</p>



<p class="wp-block-paragraph">So now I&#8217;m going to do a return statement here.</p>



<p class="wp-block-paragraph">There&#8217;s never an argument for the return statement.</p>



<p class="wp-block-paragraph">You just simply return to the caller.</p>



<p class="wp-block-paragraph">This uses the stack.</p>



<p class="wp-block-paragraph">If you don&#8217;t know what the stack is, I&#8217;ll probably make another video about that in</p>



<p class="wp-block-paragraph">the future.</p>



<p class="wp-block-paragraph">But the stack, you know, it helps your program understand where it just called to and how</p>



<p class="wp-block-paragraph">to get back from it.</p>



<p class="wp-block-paragraph">to get back from it it also stores local variables and things but so if i have a call it should jump</p>



<p class="wp-block-paragraph">down to the hello function and then return meaning it&#8217;ll just return to the caller immediately without</p>



<p class="wp-block-paragraph">actually doing anything but the program should be able to continue without crashing notice how</p>



<p class="wp-block-paragraph">there&#8217;s no crash here all it does is print out the welcome message and then no problem all right</p>



<p class="wp-block-paragraph">printing stuff and I&#8217;m just going to cut it and stick it inside of the say hello</p>



<p class="wp-block-paragraph">function that way when I call that function it should still print hello but</p>



<p class="wp-block-paragraph">now notice how we have less code to deal with in this primary function you know</p>



<p class="wp-block-paragraph">one of the first things that that programmers learn is that you know we&#8217;re</p>



<p class="wp-block-paragraph">human beings we&#8217;re not computers it&#8217;s really hard for us to write complex</p>



<p class="wp-block-paragraph">logic in a program without tools to help us and design patterns to help us to</p>



<p class="wp-block-paragraph">help us stay on track to help us you know make sure that we&#8217;re not going to</p>



<p class="wp-block-paragraph">we&#8217;re not going to be forgetting anything or screwing something up or you know to debug so</p>



<p class="wp-block-paragraph">you know one awesome design pattern is to write functions because you you you realize in your</p>



<p class="wp-block-paragraph">program you might be calling the same logic several times so you&#8217;re sort of like repeating a bunch of</p>



<p class="wp-block-paragraph">code that already is bad it&#8217;s really hard to debug repeating code and it&#8217;s really hard to make updates</p>



<p class="wp-block-paragraph">to it and it becomes unwieldy right so as soon as you realize you&#8217;re repeating the same logic</p>



<p class="wp-block-paragraph">places you should think about taking it and putting that logic into a function</p>



<p class="wp-block-paragraph">and then just simply calling that function many times it makes your coding</p>



<p class="wp-block-paragraph">a lot easier and then once you put all the work into getting a function to</p>



<p class="wp-block-paragraph">work you&#8217;re just you can just be done with it as soon as you&#8217;re sure that</p>



<p class="wp-block-paragraph">this function actually works you can just put it to the side and just forget</p>



<p class="wp-block-paragraph">about it you can stick it at the bottom of the source or just ignore it put it</p>



<p class="wp-block-paragraph">into a different module whatever you want to do and then the other parts of</p>



<p class="wp-block-paragraph">of the program that you&#8217;re still working on they become a lot more simple and so you know because</p>



<p class="wp-block-paragraph">we&#8217;re human beings we need all the advantages we can get to write powerful code so i am now just</p>



<p class="wp-block-paragraph">i&#8217;m not i don&#8217;t have to think anymore about all the system call things that&#8217;s happening</p>



<p class="wp-block-paragraph">to print the hello message all i have to do is think about calling the say hello function</p>



<p class="wp-block-paragraph">and of course this is simple but you can imagine using this for more complicated</p>



<p class="wp-block-paragraph">concepts in the future so let&#8217;s see if this still works if i didn&#8217;t screw this up</p>



<p class="wp-block-paragraph">It printed the same exact message.</p>



<p class="wp-block-paragraph">And if I keep running it, you know, the make file doesn&#8217;t compile as much, but it still</p>



<p class="wp-block-paragraph">runs the program.</p>



<p class="wp-block-paragraph">If you don&#8217;t believe me that this is inside of a function now, let&#8217;s actually just call</p>



<p class="wp-block-paragraph">that same function many times.</p>



<p class="wp-block-paragraph">We&#8217;ll call say hello.</p>



<p class="wp-block-paragraph">Let&#8217;s say we call it five times.</p>



<p class="wp-block-paragraph">This should work.</p>



<p class="wp-block-paragraph">I&#8217;m going to run the program again.</p>



<p class="wp-block-paragraph">Notice how it printed that message five times.</p>



<p class="wp-block-paragraph">Just to reemphasize my earlier point, wouldn&#8217;t it be a huge pain in the butt if you wrote</p>



<p class="wp-block-paragraph">all the code for the system call and the message printing five duplicate times?</p>



<p class="wp-block-paragraph">Wouldn&#8217;t that be hard to update? What if you had a hundred of those in there?</p>



<p class="wp-block-paragraph">What if you had something that was really complicated and you called on it like</p>



<p class="wp-block-paragraph">50 times,</p>



<p class="wp-block-paragraph">wouldn&#8217;t that be so much better than maintaining 50 different versions of the</p>



<p class="wp-block-paragraph">exact same idea of code and, and, you know,</p>



<p class="wp-block-paragraph">making sure that if you needed to upgrade it or change it in some way,</p>



<p class="wp-block-paragraph">you actually got it right for all 50 copies. So this is way better.</p>



<p class="wp-block-paragraph">All right. So that&#8217;s the basic idea for a function.</p>



<p class="wp-block-paragraph">for a function let&#8217;s see what else can i do um let&#8217;s let&#8217;s make a function that just prints</p>



<p class="wp-block-paragraph">anything because that would be kind of a good little practice in the uh in the arguments that</p>



<p class="wp-block-paragraph">we can use remember in c plus plus and other languages you have function signatures that</p>



<p class="wp-block-paragraph">you can use so for example say hello we know for sure that say hello is not actually doing anything</p>



<p class="wp-block-paragraph">it&#8217;s not receiving any arguments and it&#8217;s also not returning anything so if we know it&#8217;s not</p>



<p class="wp-block-paragraph">returning anything. So if we know it&#8217;s not returning anything, we can say that it&#8217;s a</p>



<p class="wp-block-paragraph">void function if we&#8217;re talking C++. We&#8217;ll do the name here. We&#8217;ll say say hello. And in the</p>



<p class="wp-block-paragraph">argument list, we know that it doesn&#8217;t take any arguments. So it&#8217;s just say hello with nothing,</p>



<p class="wp-block-paragraph">right? Okay. So let&#8217;s make another function that just prints something. And this function will</p>



<p class="wp-block-paragraph">kind of be a little redundant because if you just sort of look at the system call here,</p>



<p class="wp-block-paragraph">we have to load up the system call with the standard output and the system write code.</p>



<p class="wp-block-paragraph">So that&#8217;s going to be repeated.</p>



<p class="wp-block-paragraph">So we could move that into the print something function, but then, you know, giving an argument</p>



<p class="wp-block-paragraph">of like, here&#8217;s the string I want to print.</p>



<p class="wp-block-paragraph">And here&#8217;s the length of the string that I want to print that, that is something that</p>



<p class="wp-block-paragraph">we can pass as arguments every time.</p>



<p class="wp-block-paragraph">So we&#8217;ll be saving like a little bit of work, but it&#8217;s still, I think, I hope illustrates</p>



<p class="wp-block-paragraph">the idea.</p>



<p class="wp-block-paragraph">Maybe we will, oh, print something.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So print something.</p>



<p class="wp-block-paragraph">Maybe we&#8217;ll do the signature here.</p>



<p class="wp-block-paragraph">Print something will not return any value.</p>



<p class="wp-block-paragraph">So we&#8217;ll just give it a void return type.</p>



<p class="wp-block-paragraph">And I&#8217;ll do a description up here.</p>



<p class="wp-block-paragraph">Print a message given by a character pointer.</p>



<p class="wp-block-paragraph">A pointer to a string.</p>



<p class="wp-block-paragraph">character is also a pointer to a C string, not a regular string class, but a C string.</p>



<p class="wp-block-paragraph">So I don&#8217;t know how to describe it. I think I&#8217;ll just say a pointer to a C string</p>



<p class="wp-block-paragraph">and length and integer length maybe. So that means the print something function should take</p>



<p class="wp-block-paragraph">two arguments. We want to be able to call that function and tell it, here&#8217;s a pointer to the</p>



<p class="wp-block-paragraph">string I want you to print. And here&#8217;s how long the string is. So still talking about C++, I&#8217;m</p>



<p class="wp-block-paragraph">character pointer p and maybe i&#8217;ll put like string just to remind myself when i look at this later</p>



<p class="wp-block-paragraph">it&#8217;s a pointer to the string or like p char for the pointer to like the first character in the</p>



<p class="wp-block-paragraph">string something like that and then length so the length is going to be a long because we&#8217;re using</p>



<p class="wp-block-paragraph">64-bit integers in this video long and i&#8217;ll just say maybe size or length or something so now we</p>



<p class="wp-block-paragraph">I want to use the same symbol that I&#8217;m describing in this little prototype here.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to copy paste it and use a colon.</p>



<p class="wp-block-paragraph">And then because it&#8217;s a function, I immediately want to put a return statement at the very end.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So then what I need to do is grab the incoming arguments.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So, well, the first thing that every system call that prints a string should do,</p>



<p class="wp-block-paragraph">at least if it&#8217;s going to print it to standard output,</p>



<p class="wp-block-paragraph">to print it to standard output is it should start setting up the system call code registers or the</p>



<p class="wp-block-paragraph">system call registers to say all right the system call wants rax to describe what the system call</p>



<p class="wp-block-paragraph">is going to do we already have system write set up so that just means we&#8217;re telling syscall that</p>



<p class="wp-block-paragraph">we want to print something where do we want to print it we want to print to the standard output</p>



<p class="wp-block-paragraph">which if you look at earlier in the video that was just a file descriptor of one so that&#8217;s fine</p>



<p class="wp-block-paragraph">up the system call and then the next two things can be kind of variable uh you know we can we</p>



<p class="wp-block-paragraph">can grab that from incoming arguments so rsi wants a pointer to the string we&#8217;re not going to load</p>



<p class="wp-block-paragraph">hello string but we&#8217;re going to load whatever character pointer we were given in the incoming</p>



<p class="wp-block-paragraph">RDI is the first incoming integer argument.</p>



<p class="wp-block-paragraph">We&#8217;re not going to talk about mixed arguments with floats.</p>



<p class="wp-block-paragraph">If we just assume that we&#8217;re only going to use integer arguments or pointer arguments,</p>



<p class="wp-block-paragraph">then the first one is going to be RDI.</p>



<p class="wp-block-paragraph">So that means by the time print something gets called,</p>



<p class="wp-block-paragraph">by the time we come into this code right here,</p>



<p class="wp-block-paragraph">RDI should be loaded with the pointer to the character to the C string that we want to print.</p>



<p class="wp-block-paragraph">Again, this follows something called the ABI, which you should absolutely respect,</p>



<p class="wp-block-paragraph">which you should absolutely respect even if you&#8217;re the only one writing any of the code that you</p>



<p class="wp-block-paragraph">interact with your code will be bad and considered not good if you don&#8217;t respect the abi on this type</p>



<p class="wp-block-paragraph">of architecture so we definitely still need to load rsi with the string pointer but art whoops</p>



<p class="wp-block-paragraph">but rdi is what&#8217;s going to have the string pointer so we actually already just ruined rdi when we</p>



<p class="wp-block-paragraph">probably either load it up backwards or i think probably a better way to explain functions is</p>



<p class="wp-block-paragraph">let&#8217;s just use another register so i&#8217;m going to use another register to sort of store our incoming</p>



<p class="wp-block-paragraph">arguments i personally like to do this i admit that this will cost you cpu cycles to sort of</p>



<p class="wp-block-paragraph">like move things around in registers but it&#8217;s still way faster than sticking something into</p>



<p class="wp-block-paragraph">a global variable because then you&#8217;ll be hitting memory so first i&#8217;m going to say let&#8217;s move</p>



<p class="wp-block-paragraph">to r12 and I want to store the incoming argument so I&#8217;m just going to write here save the</p>



<p class="wp-block-paragraph">the p care you know c string character pointer or something like that so we&#8217;re going to save it</p>



<p class="wp-block-paragraph">and then the next thing we need to do is save the size because as soon as we load up rsi we&#8217;re also</p>



<p class="wp-block-paragraph">going to be destroying the second integer argument which should come to us in rsi</p>



<p class="wp-block-paragraph">Okay, so I&#8217;m going to save that too, I guess, with R13.</p>



<p class="wp-block-paragraph">So I&#8217;m going to move something into R13, RSI, and then save the size of the C string.</p>



<p class="wp-block-paragraph">Okay, so we saved both of those.</p>



<p class="wp-block-paragraph">Now, when we load RSI, we can just sort of say, RDI is saved.</p>



<p class="wp-block-paragraph">And then when we load RSI…</p>



<p class="wp-block-paragraph">Oh, that&#8217;ll be when we hit RDX.</p>



<p class="wp-block-paragraph">Oh, that&#8217;ll be when we hit RDX. Okay, I got confused. So we stored R12 was the character</p>



<p class="wp-block-paragraph">pointer. And then for the length of the string, we stored that as R13. Also for me personally,</p>



<p class="wp-block-paragraph">I feel like it&#8217;s a pretty good idea to kind of make a comment at the very top, because this is</p>



<p class="wp-block-paragraph">assembly. It&#8217;s really, really hard, right? Make a little comment at the top, just kind of reminding</p>



<p class="wp-block-paragraph">yourselves what you use all of the registers for. I know sometimes you&#8217;ll use the same register for</p>



<p class="wp-block-paragraph">sometimes you&#8217;ll use the same register for multiple purposes.</p>



<p class="wp-block-paragraph">You could either write that down</p>



<p class="wp-block-paragraph">or you could consider breaking up your function</p>



<p class="wp-block-paragraph">into multiple functions.</p>



<p class="wp-block-paragraph">That&#8217;s totally valid</p>



<p class="wp-block-paragraph">and it probably will make your life a lot easier.</p>



<p class="wp-block-paragraph">Anyway, so I&#8217;m gonna say register usage</p>



<p class="wp-block-paragraph">and then I&#8217;m gonna say R12 pointer to string, C string.</p>



<p class="wp-block-paragraph">And then I&#8217;m gonna say R13 is gonna be</p>



<p class="wp-block-paragraph">size of the string. And that seems simple but remember assembly especially when you&#8217;re new</p>



<p class="wp-block-paragraph">it&#8217;s really confusing and for me when I was first learning and even sometimes now I&#8217;ll be staring</p>



<p class="wp-block-paragraph">at a big blob of assembly code and I&#8217;ll just be like what register was I using you know or I&#8217;ll</p>



<p class="wp-block-paragraph">look at a register and I&#8217;ll be like what was that thing even for? So just make yourself a little</p>



<p class="wp-block-paragraph">comment up here to help you understand and then you&#8217;ll thank yourself later when you get a little</p>



<p class="wp-block-paragraph">a little bit too overloaded or maybe even if you come back to your code a month from</p>



<p class="wp-block-paragraph">now probably having totally forgotten what you even wrote then you&#8217;ll just have a nice</p>



<p class="wp-block-paragraph">little reminder here.</p>



<p class="wp-block-paragraph">It&#8217;s good to document and this is one of the ways that I recommend.</p>



<p class="wp-block-paragraph">Okay, so we&#8217;re saying that R12 and R13 are going to be used.</p>



<p class="wp-block-paragraph">We&#8217;re loading them up here from the incoming arguments.</p>



<p class="wp-block-paragraph">Now RDI and RSI are free to be destroyed if we want and of course we have to destroy them</p>



<p class="wp-block-paragraph">because RDI and RSI were incoming arguments when this function first came in but now that</p>



<p class="wp-block-paragraph">first came in but now that we&#8217;re going to be doing a system call we have to use</p>



<p class="wp-block-paragraph">them for incoming arguments to the sys call so they&#8217;re just they&#8217;re just meant</p>



<p class="wp-block-paragraph">to be constantly destroyed so we&#8217;re gonna do call code there and then the</p>



<p class="wp-block-paragraph">standard output is gonna be the first argument and then the string that we did</p>



<p class="wp-block-paragraph">which is an r12 is going to be the second argument and then rdx is going to</p>



<p class="wp-block-paragraph">be the next argument which is r13 which came into us through the second argument</p>



<p class="wp-block-paragraph">wait wait wait the string to write came into us through the first argument the</p>



<p class="wp-block-paragraph">came into us through the second argument but if you just kind of look at what we&#8217;re doing to the</p>



<p class="wp-block-paragraph">system call it&#8217;s actually the second and the third so I mean just forget about it okay so we set all</p>



<p class="wp-block-paragraph">that up let me make sure I&#8217;m not forgetting something by scrolling up real fast yeah so now</p>



<p class="wp-block-paragraph">we can do a system call one other thing that&#8217;s very very important about functions is you have</p>



<p class="wp-block-paragraph">to respect the ABI and that doesn&#8217;t just include using the right registers for incoming arguments</p>



<p class="wp-block-paragraph">out a call but you also have to keep in mind which registers are denoted as callee saved</p>



<p class="wp-block-paragraph">if something is callee saved then that means you know if i&#8217;m inside of print something</p>



<p class="wp-block-paragraph">i&#8217;m being called that means i&#8217;m the callee so that means if i&#8217;m going to use r12 and r13 which</p>



<p class="wp-block-paragraph">are designated as callee saved maybe i&#8217;ll pull up that my favorite assembly book after this</p>



<p class="wp-block-paragraph">but if i&#8217;m using registers which are designated as callee saved then i have to preserve those</p>



<p class="wp-block-paragraph">then I have to preserve those registers, meaning those registers,</p>



<p class="wp-block-paragraph">they should have their same values from when they first came in on the call</p>



<p class="wp-block-paragraph">when I exit the function. So when I exit the function, those registers,</p>



<p class="wp-block-paragraph">register values should appear to be untouched to the caller.</p>



<p class="wp-block-paragraph">Remember these registers are not local variables.</p>



<p class="wp-block-paragraph">They&#8217;re global to the entire system. There&#8217;s only one R12 on the whole CPU.</p>



<p class="wp-block-paragraph">So every program that runs every function that gets called,</p>



<p class="wp-block-paragraph">see the same exact R12 register. So if we&#8217;re not careful about preserving when we have to,</p>



<p class="wp-block-paragraph">we could end up crashing other programs or our own program. So anyway, let&#8217;s do something that</p>



<p class="wp-block-paragraph">I like to call the prologue and the epilogue. That&#8217;s another nod to my favorite assembly book,</p>



<p class="wp-block-paragraph">which I&#8217;ll probably show at the end of this video. So prologue is just, hey, let&#8217;s preserve</p>



<p class="wp-block-paragraph">the stuff that we need to preserve. We will preserve the R12 with a push and we will preserve</p>



<p class="wp-block-paragraph">and we will preserve the R13 with another push.</p>



<p class="wp-block-paragraph">We then have to restore them.</p>



<p class="wp-block-paragraph">So right now when we do this push,</p>



<p class="wp-block-paragraph">that just means that whatever value R12 and R13 had</p>



<p class="wp-block-paragraph">is going to go to the stack.</p>



<p class="wp-block-paragraph">So we&#8217;re going to hit memory.</p>



<p class="wp-block-paragraph">That&#8217;s not great, but like we have to in this case.</p>



<p class="wp-block-paragraph">We&#8217;re going to push it onto memory</p>



<p class="wp-block-paragraph">in order to preserve the value.</p>



<p class="wp-block-paragraph">And now we&#8217;re free to destroy the value if we want.</p>



<p class="wp-block-paragraph">And then right before we return,</p>



<p class="wp-block-paragraph">we&#8217;ll just pop those values.</p>



<p class="wp-block-paragraph">So that means we&#8217;re going to go to memory.</p>



<p class="wp-block-paragraph">We&#8217;re going to go to the stack.</p>



<p class="wp-block-paragraph">to go to the stack we&#8217;re going to grab the values that we just pushed and put them back onto the</p>



<p class="wp-block-paragraph">registers oops forgot to do 13 and 12 and I&#8217;ll call this the epilogue just meaning like we&#8217;re</p>



<p class="wp-block-paragraph">done we&#8217;re cleaning up and of course look very carefully at the fact that the push and the pop</p>



<p class="wp-block-paragraph">sequence are in reverse order notice how we push 12 first and we push our 13 second then at the end</p>



<p class="wp-block-paragraph">Then at the end, we pop R13 first, so it&#8217;s backwards.</p>



<p class="wp-block-paragraph">It&#8217;s like a little shell, you know, the R13s are on the inside</p>



<p class="wp-block-paragraph">and the R12s are on the outside.</p>



<p class="wp-block-paragraph">That logic would persist if we pushed,</p>



<p class="wp-block-paragraph">or if we had to preserve more Kali saved registers,</p>



<p class="wp-block-paragraph">if we were using more stuff.</p>



<p class="wp-block-paragraph">The reason we do that is because the stack as a data structure</p>



<p class="wp-block-paragraph">will return data to you backwards in the reverse order</p>



<p class="wp-block-paragraph">that you returned it or that you sent it into the stack.</p>



<p class="wp-block-paragraph">it into the stack so if i send a 12 and a 13 let&#8217;s just say the number 12 and the number 13</p>



<p class="wp-block-paragraph">into the stack then if i start popping stuff out of the stack it&#8217;s first going to give me the most</p>



<p class="wp-block-paragraph">recent item which is going to be the 13 so it&#8217;s going to give me backwards data and then it&#8217;ll</p>



<p class="wp-block-paragraph">give me the art the 12 seconds so um the type of data structure a stack is is first in last out</p>



<p class="wp-block-paragraph">last out no that&#8217;s a that&#8217;s a queue anyway so we&#8217;re preserving the registers we&#8217;re doing the</p>



<p class="wp-block-paragraph">system call we&#8217;re probably okay to call this function now so let me see if i can do this</p>



<p class="wp-block-paragraph">i&#8217;m going to comment out all of these things that the the say hello function does and i&#8217;m just going</p>



<p class="wp-block-paragraph">to call on print something in order to call that function but i have to load up my arguments i have</p>



<p class="wp-block-paragraph">pass arguments remember in the print something function it&#8217;s got a signature</p>



<p class="wp-block-paragraph">it wants a character pointer and a size we can&#8217;t just call it without giving it</p>



<p class="wp-block-paragraph">those things or it&#8217;s going to look at the registers anyway and just grab</p>



<p class="wp-block-paragraph">whatever junk data happened to be in there so that would be bad so first we</p>



<p class="wp-block-paragraph">have to load up the registers load up argument registers and call print</p>



<p class="wp-block-paragraph">load up the argument registers and call print something.</p>



<p class="wp-block-paragraph">So the first thing is going to be RDI, so we have to move something into RDI</p>



<p class="wp-block-paragraph">and then the second register for the second integer argument is going to be RSI.</p>



<p class="wp-block-paragraph">As far as I recall, if I&#8217;m wrong this is going to go horribly wrong.</p>



<p class="wp-block-paragraph">I&#8217;ll double check the book after this.</p>



<p class="wp-block-paragraph">And what do we want to do?</p>



<p class="wp-block-paragraph">We want to basically, inside of say hello,</p>



<p class="wp-block-paragraph">we could assume for now that say hello hardcodes the pointer and the length.</p>



<p class="wp-block-paragraph">So we&#8217;ll just say it&#8217;s going to grab the string from global and it&#8217;s going to grab the string</p>



<p class="wp-block-paragraph">length from the global variables.</p>



<p class="wp-block-paragraph">So it&#8217;s going to load up those two arguments and then call on print something.</p>



<p class="wp-block-paragraph">This still ends up being three lines.</p>



<p class="wp-block-paragraph">That&#8217;s why I was saying before, we&#8217;re not going to save too much, but we did save, you</p>



<p class="wp-block-paragraph">know, these two lines right here, you know, and the system call line.</p>



<p class="wp-block-paragraph">So it&#8217;s like slightly faster to do.</p>



<p class="wp-block-paragraph">You just load up two pointers and then you make a call.</p>



<p class="wp-block-paragraph">in this video so I can&#8217;t really make the function better but in the future when</p>



<p class="wp-block-paragraph">you learn how to loop and and learn about null terminated strings you could</p>



<p class="wp-block-paragraph">basically just pass a pointer to just the string only and then call on a</p>



<p class="wp-block-paragraph">function if the function was smart enough to scan through the string and</p>



<p class="wp-block-paragraph">figure out how long it was based on where the null terminator was but that&#8217;s</p>



<p class="wp-block-paragraph">gonna happen in another video anyway so if we call print something it should</p>



<p class="wp-block-paragraph">Notice also that we&#8217;re having a function call another function, which is pretty cool.</p>



<p class="wp-block-paragraph">So we have our entry point, it&#8217;s going to call on say hello, and then once we&#8217;re inside</p>



<p class="wp-block-paragraph">of say hello, we&#8217;re just going to load up some arguments and then call on the print</p>



<p class="wp-block-paragraph">something function.</p>



<p class="wp-block-paragraph">The print something function then does most of the work for the printing, and I think</p>



<p class="wp-block-paragraph">we&#8217;re ready to go.</p>



<p class="wp-block-paragraph">Let&#8217;s see if this works.</p>



<p class="wp-block-paragraph">All right, so we&#8217;re going to clear and then just run the program.</p>



<p class="wp-block-paragraph">Oh, that was so fast.</p>



<p class="wp-block-paragraph">That was so fast I don&#8217;t even feel like I&#8217;ve proved anything.</p>



<p class="wp-block-paragraph">Let&#8217;s comment out the system call so that all we really do is just load up some registers</p>



<p class="wp-block-paragraph">and then don&#8217;t do anything.</p>



<p class="wp-block-paragraph">And then you should see here that the print something function no longer actually does</p>



<p class="wp-block-paragraph">anything because it was too fast.</p>



<p class="wp-block-paragraph">Now if I uncomment the system call, you know that the print something function is actually</p>



<p class="wp-block-paragraph">doing something and it&#8217;s printing the message five times.</p>



<p class="wp-block-paragraph">We can also make some more messages if we wanted to,</p>



<p class="wp-block-paragraph">just as a way to make this video slightly more interesting.</p>



<p class="wp-block-paragraph">I&#8217;ll say msg1 is going to be a string of bytes,</p>



<p class="wp-block-paragraph">and I&#8217;m going to say this is message number one.</p>



<p class="wp-block-paragraph">Boring, sorry.</p>



<p class="wp-block-paragraph">And we&#8217;ll do a crlf there.</p>



<p class="wp-block-paragraph">Actually you know what, let&#8217;s make a function that does a crlf.</p>



<p class="wp-block-paragraph">Okay, let me just finish message one length,</p>



<p class="wp-block-paragraph">and we&#8217;ll do it equals that special string that you can do in yasm am i doing this right yeah okay</p>



<p class="wp-block-paragraph">so this is message number one so now we&#8217;ll call again with message we&#8217;ll call say hello just once</p>



<p class="wp-block-paragraph">and then we&#8217;ll call on print something loading it up first move something with rdi and then move</p>



<p class="wp-block-paragraph">message one is that what I had message underscore one and then RSI is going to</p>



<p class="wp-block-paragraph">get message underscore one length so basically we&#8217;re just you know print the</p>



<p class="wp-block-paragraph">first message it&#8217;s always a good idea to put comments on each line when you&#8217;re</p>



<p class="wp-block-paragraph">learning so that you don&#8217;t forget what&#8217;s happening in addition to like a comment</p>



<p class="wp-block-paragraph">for the whole block but I&#8217;m not going to do that here let&#8217;s just make sure that</p>



<p class="wp-block-paragraph">make sure that this actually works so it should print two different messages now</p>



<p class="wp-block-paragraph">one okay nice let&#8217;s enhance this a little bit more later i think i&#8217;m going to have us print like a i</p>



<p class="wp-block-paragraph">don&#8217;t know a character or maybe we&#8217;ll do a return value from something so we can just show you how</p>



<p class="wp-block-paragraph">to do the return values but i&#8217;m just going to enhance this a little bit more we&#8217;re going to say</p>



<p class="wp-block-paragraph">let&#8217;s make another function we&#8217;ll call it crlf the crlf function i don&#8217;t want it to do all the</p>



<p class="wp-block-paragraph">system call stuff because print something already does that for us i&#8217;m instead just going to make</p>



<p class="wp-block-paragraph">message so now that means i&#8217;m going to go crlf and that&#8217;s just going to be a 13 and a 10. remember</p>



<p class="wp-block-paragraph">you can define a c string with quotes but you can also separate quoted strings by commas and also</p>



<p class="wp-block-paragraph">integers just to sort of tack it on so you know this variable right here it&#8217;s going to be a</p>



<p class="wp-block-paragraph">sequence of characters representing the string that you see and then at the very end of the</p>



<p class="wp-block-paragraph">string it&#8217;s going to have a crlf carriage return line feed so that the cursor goes to the next line</p>



<p class="wp-block-paragraph">goes to the next line so I&#8217;m actually gonna take that off of message one</p>



<p class="wp-block-paragraph">because it&#8217;s gonna be here just on CRLF and then I&#8217;ll say CRLF length is gonna</p>



<p class="wp-block-paragraph">be two I could also do that special thing probably a little smarter CRLF</p>



<p class="wp-block-paragraph">yeah I mean it&#8217;s it&#8217;s better to avoid hard coding things if you can avoid</p>



<p class="wp-block-paragraph">hard coding the number two or any number just do it I guess I have to hard code</p>



<p class="wp-block-paragraph">So, so CRLF and then length.</p>



<p class="wp-block-paragraph">And so that means in my CRLF function, let&#8217;s see, let&#8217;s do CRLF, CRLF immediately return</p>



<p class="wp-block-paragraph">because it&#8217;s going to be a function.</p>



<p class="wp-block-paragraph">I&#8217;ll just say void CRLF with no arguments and prints a carriage return new line, also</p>



<p class="wp-block-paragraph">known as CRLF.</p>



<p class="wp-block-paragraph">and so that just kind of describes it and I don&#8217;t need to preserve RSI and RDI</p>



<p class="wp-block-paragraph">because those are not callie saved registers so respecting the ABI kind of</p>



<p class="wp-block-paragraph">helps you in this case I&#8217;m just gonna copy paste here and maybe I&#8217;ll copy</p>



<p class="wp-block-paragraph">paste that comment real fast too so I don&#8217;t have to like type that whole thing</p>



<p class="wp-block-paragraph">out and instead of doing hello string we&#8217;ll just say CRLF and then for here</p>



<p class="wp-block-paragraph">And now when I call CRLF, it should just make the cursor go down a little bit.</p>



<p class="wp-block-paragraph">So let&#8217;s test that out.</p>



<p class="wp-block-paragraph">I took the CRLF off of message one.</p>



<p class="wp-block-paragraph">So if we actually run this right now on modified, it should have the cursor.</p>



<p class="wp-block-paragraph">Now, what did I just do wrong?</p>



<p class="wp-block-paragraph">On number 28 and 29, instructions expected after label.</p>



<p class="wp-block-paragraph">I guess I forgot to complete something.</p>



<p class="wp-block-paragraph">Oh, I forgot to put, it&#8217;s a byte sequence right there.</p>



<p class="wp-block-paragraph">there and I forgot to put that this is basically a defined sequence you know</p>



<p class="wp-block-paragraph">that EQU okay my bad okay so now it prints notice how the message line it no</p>



<p class="wp-block-paragraph">longer does a new line and no longer jumps to the next line CRLF so that</p>



<p class="wp-block-paragraph">means when the program exits the terminal prompt is on the same line as</p>



<p class="wp-block-paragraph">the program just printed that&#8217;s ugly and awful so let&#8217;s call</p>



<p class="wp-block-paragraph">We could either say, let&#8217;s call CRLF after printing the first message,</p>



<p class="wp-block-paragraph">or we could just assume that print something is like print a line.</p>



<p class="wp-block-paragraph">I don&#8217;t know how you feel,</p>



<p class="wp-block-paragraph">but I think if we always called CRLF after every time we printed a message,</p>



<p class="wp-block-paragraph">I would probably be a little irritated.</p>



<p class="wp-block-paragraph">So maybe let&#8217;s change this to print line and then make it call CRLF.</p>



<p class="wp-block-paragraph">You can just keep adding this like 1013 at the end of every string, but I think I want</p>



<p class="wp-block-paragraph">to try to make this as compact as possible.</p>



<p class="wp-block-paragraph">So let&#8217;s add the CRLF for now and then we&#8217;ll change print something later.</p>



<p class="wp-block-paragraph">So we print the message.</p>



<p class="wp-block-paragraph">Now the prompt should be, you know, a line further down.</p>



<p class="wp-block-paragraph">So great works.</p>



<p class="wp-block-paragraph">I am now going to take the CRLF off and I&#8217;m going to put it inside of print something</p>



<p class="wp-block-paragraph">so that right after we do the system call to print the string in question, we&#8217;ll print</p>



<p class="wp-block-paragraph">we&#8217;ll print a new line this should now accomplish the same thing that we just saw</p>



<p class="wp-block-paragraph">oh no what did i do i think i did uh i think i have a infinite loop of recursive calls in there</p>



<p class="wp-block-paragraph">somewhere i had a stack overflow probably so let&#8217;s see what did i just do say hello is call</p>



<p class="wp-block-paragraph">print something and then crlf is call print something oh whoops print something calls crlf</p>



<p class="wp-block-paragraph">at the end so i should do one or the other and not have them call both so maybe just because crlf is</p>



<p class="wp-block-paragraph">special maybe i&#8217;ll just use a direct system call inside of crlf i thought i was so clever</p>



<p class="wp-block-paragraph">but i was wrong so we&#8217;re going to repeat a little code for the crlf and we&#8217;re going to load it up</p>



<p class="wp-block-paragraph">with um crlf dang how embarrassing i like to say this is the most public of my many humiliations</p>



<p class="wp-block-paragraph">of my many humiliations. But anyway, so now we don&#8217;t need to rely on print something. So this</p>



<p class="wp-block-paragraph">won&#8217;t be an infinite loop and it should work if print something calls on crlf now. Do it again.</p>



<p class="wp-block-paragraph">Okay, it works. So now let&#8217;s modify print something so that it is just print</p>



<p class="wp-block-paragraph">a line. Print a line. And then that, in my eyes, you don&#8217;t have to do it this way. It sort of</p>



<p class="wp-block-paragraph">to do it this way it sort of justifies printing a crlf at the end every single time so this is</p>



<p class="wp-block-paragraph">just my personal thinking and then maybe i&#8217;ll add a comment after prints a crlf</p>



<p class="wp-block-paragraph">so now when we call print line we no longer have to call crlf</p>



<p class="wp-block-paragraph">and then when we print that message should be pretty good oh we got to update that to print line</p>



<p class="wp-block-paragraph">Then for say hello, let&#8217;s just double check.</p>



<p class="wp-block-paragraph">We can get rid of this system call code here.</p>



<p class="wp-block-paragraph">Probably a good idea at this point.</p>



<p class="wp-block-paragraph">And then we just say print hello string.</p>



<p class="wp-block-paragraph">And then from the hello string, we can get rid of this CRLF at the end of it.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to get rid of that.</p>



<p class="wp-block-paragraph">Actually, let&#8217;s do it first without getting rid of it.</p>



<p class="wp-block-paragraph">Just so you see, oh, what did I do wrong?</p>



<p class="wp-block-paragraph">Lion 61, forgot to update the symbol name.</p>



<p class="wp-block-paragraph">Oh, there we go.</p>



<p class="wp-block-paragraph">call print line because we don&#8217;t have print something anymore and try it again notice how</p>



<p class="wp-block-paragraph">the first message hello my name is it has two line feeds after it because it is calling print line</p>



<p class="wp-block-paragraph">which will do a crlf but then also in the string itself there&#8217;s a crlf so again the point is i&#8217;m</p>



<p class="wp-block-paragraph">to my advantage.</p>



<p class="wp-block-paragraph">Okay, so we got that working now.</p>



<p class="wp-block-paragraph">I&#8217;m just going to print like five different kinds of messages.</p>



<p class="wp-block-paragraph">How about three?</p>



<p class="wp-block-paragraph">So we don&#8217;t have to sit here and watch me typing for too long.</p>



<p class="wp-block-paragraph">So I&#8217;m going to say message two and then I&#8217;m going to do message three.</p>



<p class="wp-block-paragraph">Message three.</p>



<p class="wp-block-paragraph">This is message three unless I got that wrong.</p>



<p class="wp-block-paragraph">One, two, three, one, two, three, one, two, three.</p>



<p class="wp-block-paragraph">Now we can just call a print string on three different messages.</p>



<p class="wp-block-paragraph">And I&#8217;ll just change this to say, you know, print the second message, print the third</p>



<p class="wp-block-paragraph">message.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to change that to message two and then message three.</p>



<p class="wp-block-paragraph">All right.</p>



<p class="wp-block-paragraph">So then I&#8217;m going to do that again.</p>



<p class="wp-block-paragraph">And then all three messages get printed.</p>



<p class="wp-block-paragraph">Nice.</p>



<p class="wp-block-paragraph">So now one more thing.</p>



<p class="wp-block-paragraph">I just want to show you how to return an integer to the caller in a basic YASM x86 assembly function.</p>



<p class="wp-block-paragraph">Again, this will not cover floats.</p>



<p class="wp-block-paragraph">This does not show you how to return floats.</p>



<p class="wp-block-paragraph">With floats, you have to use a different register called xmm0.</p>



<p class="wp-block-paragraph">That&#8217;s going to be a different video.</p>



<p class="wp-block-paragraph">But for now, I&#8217;m going to say all integer or pointer return values, like longs is going to be what we&#8217;re using,</p>



<p class="wp-block-paragraph">that is the register that is designated for return values.</p>



<p class="wp-block-paragraph">So what about this?</p>



<p class="wp-block-paragraph">Instead of just returning the exit success code</p>



<p class="wp-block-paragraph">that I&#8217;ve defined up above,</p>



<p class="wp-block-paragraph">we&#8217;ll return whatever some function call gives us.</p>



<p class="wp-block-paragraph">Maybe I&#8217;ll make another function down here</p>



<p class="wp-block-paragraph">and then I&#8217;ll say a function</p>



<p class="wp-block-paragraph">that just returns a simple long value.</p>



<p class="wp-block-paragraph">And we&#8217;ll call this function gimme long.</p>



<p class="wp-block-paragraph">We&#8217;ll call this function gimme long.</p>



<p class="wp-block-paragraph">It&#8217;ll take no arguments because we&#8217;re just keeping this simple for now.</p>



<p class="wp-block-paragraph">And instead of having a void return type, it&#8217;ll have a long return type because we expect</p>



<p class="wp-block-paragraph">RAX to be loaded up.</p>



<p class="wp-block-paragraph">So then I&#8217;m going to say gimme long and immediately do a return because it is a function.</p>



<p class="wp-block-paragraph">And then I&#8217;m just going to load up a value from somewhere.</p>



<p class="wp-block-paragraph">Obviously, it&#8217;s better if you&#8217;re doing something complicated to stick with registers as much</p>



<p class="wp-block-paragraph">or defines and hit memory only if you need to.</p>



<p class="wp-block-paragraph">And try not to hard code immediate into your code</p>



<p class="wp-block-paragraph">if you can possibly avoid it.</p>



<p class="wp-block-paragraph">It&#8217;s probably a lot better to define a value up at the top</p>



<p class="wp-block-paragraph">in the data area than to just hard code it here.</p>



<p class="wp-block-paragraph">But I&#8217;m gonna just hard code something here.</p>



<p class="wp-block-paragraph">No, I always second guess myself.</p>



<p class="wp-block-paragraph">I&#8217;m gonna say exit gimme.</p>



<p class="wp-block-paragraph">I&#8217;m gonna define something up here.</p>



<p class="wp-block-paragraph">So it&#8217;s easy to find what value I&#8217;m gonna return</p>



<p class="wp-block-paragraph">for the gimme.</p>



<p class="wp-block-paragraph">I&#8217;m just going to say 88, 89, 90, I don&#8217;t even know.</p>



<p class="wp-block-paragraph">About 33, I don&#8217;t even know.</p>



<p class="wp-block-paragraph">So we&#8217;re defining exitGimme as a symbol</p>



<p class="wp-block-paragraph">to just be a replacement of just the number 33.</p>



<p class="wp-block-paragraph">So down here, I&#8217;m going to say,</p>



<p class="wp-block-paragraph">let&#8217;s have gimme long simply return exitGimme into RAX.</p>



<p class="wp-block-paragraph">Nice, okay.</p>



<p class="wp-block-paragraph">And so that means it&#8217;s the official return value</p>



<p class="wp-block-paragraph">once you call gimme long.</p>



<p class="wp-block-paragraph">call gimme long so that means up here get our whoops get out it&#8217;s coming from inside the house</p>



<p class="wp-block-paragraph">get our return value from gimme long and again imagine gimme long might be a big complicated</p>



<p class="wp-block-paragraph">function that decides what a return value is going to be or does something so i&#8217;ll start with saying</p>



<p class="wp-block-paragraph">call gimme long because gimme long does not take any arguments i don&#8217;t have to load up any registers</p>



<p class="wp-block-paragraph">load up any registers nor do i have to worry about preserving them or setting them up or doing</p>



<p class="wp-block-paragraph">anything i just call but then let&#8217;s see inside of this function we&#8217;ll use rax in order to you know</p>



<p class="wp-block-paragraph">load up the system call to let it know what function or sorry what function we want from</p>



<p class="wp-block-paragraph">the system call so we got to give rax system exit so that means down here we can&#8217;t actually use rax</p>



<p class="wp-block-paragraph">those other registers again I&#8217;m going to use I&#8217;m going to use the r12 register</p>



<p class="wp-block-paragraph">I&#8217;m going to do like a little comment here that says register usage and I&#8217;ll say r12</p>



<p class="wp-block-paragraph">maybe I&#8217;ll do a tab r12 is going to be old return value</p>



<p class="wp-block-paragraph">or the syscall to exit you know whatever and that&#8217;s all we&#8217;re going to do so in the prologue</p>



<p class="wp-block-paragraph">I&#8217;m going to push R12.</p>



<p class="wp-block-paragraph">You should probably even preserve registers in the start function</p>



<p class="wp-block-paragraph">because something might, or in your entry point,</p>



<p class="wp-block-paragraph">because something that called you might be counting on those registers.</p>



<p class="wp-block-paragraph">This is going to be probably especially true if you&#8217;re using GCC</p>



<p class="wp-block-paragraph">to have a main entry point within a hybrid program.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to go prolog, push R12,</p>



<p class="wp-block-paragraph">and then pop R12 at the very end.</p>



<p class="wp-block-paragraph">Pop R12.</p>



<p class="wp-block-paragraph">That&#8217;s not going to work because by the time we get there,</p>



<p class="wp-block-paragraph">because by the time we get there,</p>



<p class="wp-block-paragraph">it&#8217;s gonna exit the program.</p>



<p class="wp-block-paragraph">I&#8217;ll do this here.</p>



<p class="wp-block-paragraph">I&#8217;ll just try to be a good citizen.</p>



<p class="wp-block-paragraph">I&#8217;m not actually sure if we need this</p>



<p class="wp-block-paragraph">for a pure assembly function,</p>



<p class="wp-block-paragraph">but it&#8217;s just a really good habit to get into.</p>



<p class="wp-block-paragraph">And what is it gonna cost you?</p>



<p class="wp-block-paragraph">Just a touch memory,</p>



<p class="wp-block-paragraph">just one time right before the program exits.</p>



<p class="wp-block-paragraph">It&#8217;s not gonna be a giant for loop or anything.</p>



<p class="wp-block-paragraph">Okay, so epilog,</p>



<p class="wp-block-paragraph">probably I might be showing you bad practice</p>



<p class="wp-block-paragraph">by preserving things in the underscore start function.</p>



<p class="wp-block-paragraph">But again, you will definitely wanna do this</p>



<p class="wp-block-paragraph">Again, you will definitely want to do this for the main when you return from main.</p>



<p class="wp-block-paragraph">So we do prolog R12, epilog R12.</p>



<p class="wp-block-paragraph">I made a comment there.</p>



<p class="wp-block-paragraph">And then we&#8217;re going to store the return value in R12.</p>



<p class="wp-block-paragraph">So that means here we&#8217;re going to move something into R12.</p>



<p class="wp-block-paragraph">It&#8217;s getting precarious because we restored it right before the system call,</p>



<p class="wp-block-paragraph">but I think it&#8217;ll be okay.</p>



<p class="wp-block-paragraph">system call but i think it&#8217;ll be okay we&#8217;re going to grab rax because rax is supposed to be the</p>



<p class="wp-block-paragraph">return value register so when we called gimme long it loaded up rax with our return value</p>



<p class="wp-block-paragraph">we then send rax into r12 so we can use r12 uh even though our rax has been you know destroyed</p>



<p class="wp-block-paragraph">we&#8217;ll still have the return value that we wanted to use so i&#8217;m just going to load r12 up into the</p>



<p class="wp-block-paragraph">first argument of the sys exit call let&#8217;s see if i if i got this to work or if i totally ruin this</p>



<p class="wp-block-paragraph">work or if i totally ruin this okay all right so let&#8217;s see here what was</p>



<p class="wp-block-paragraph">why is it not erroring gimme 33 exit gimme should i did i load the wrong value exit gimme what is</p>



<p class="wp-block-paragraph">it doing exit gimme it&#8217;s moving rex into exit gimme so you call gimme long and then you move</p>



<p class="wp-block-paragraph">r a x into r 12 and r 12 i wonder if that destroyed it</p>



<p class="wp-block-paragraph">hmm let me get rid of this push pop pair because it&#8217;s supposed to be giving me</p>



<p class="wp-block-paragraph">an ever message that the function or that the program did not exit maybe this is proof that</p>



<p class="wp-block-paragraph">you don&#8217;t need to preserve inside of the underscore start function let&#8217;s see here</p>



<p class="wp-block-paragraph">yeah okay i guess that proves it hey don&#8217;t take my word for it i&#8217;m wrong all the time</p>



<p class="wp-block-paragraph">For yourself, be a scientist, be a computer scientist.</p>



<p class="wp-block-paragraph">Try it out.</p>



<p class="wp-block-paragraph">Try to do something different.</p>



<p class="wp-block-paragraph">Okay, so I guess this is proof we don&#8217;t really need a prolog and epilog in just a pure assembly,</p>



<p class="wp-block-paragraph">at least the entry point for start.</p>



<p class="wp-block-paragraph">But if you were calling any other functions, then they definitely need to preserve.</p>



<p class="wp-block-paragraph">And again, if you&#8217;re using GCC, so you can have a main for your entry point, then you</p>



<p class="wp-block-paragraph">still have to preserve.</p>



<p class="wp-block-paragraph">you know print the message and then print our other three messages and then the return code</p>



<p class="wp-block-paragraph">is going to be 33 that was the gimme code that we did so now we know how to return a value from a</p>



<p class="wp-block-paragraph">function and um let me set this back to just zero so the make system thinks that we succeeded</p>



<p class="wp-block-paragraph">run it again succeeded all right i guess this concludes the video i hope you had a little bit</p>



<p class="wp-block-paragraph">and I hope you learn a little bit of stuff and I&#8217;ll see you in the next video.</p>



<p class="wp-block-paragraph">Hey everybody, thanks for watching this video again from the bottom of my heart.</p>



<p class="wp-block-paragraph">I really appreciate it.</p>



<p class="wp-block-paragraph">I do hope you did learn something and have some fun.</p>



<p class="wp-block-paragraph">If you could do me a please, a small little favor, could you please subscribe and follow</p>



<p class="wp-block-paragraph">this channel or these videos or whatever it is you do on the current social media website</p>



<p class="wp-block-paragraph">that you&#8217;re looking at right now.</p>



<p class="wp-block-paragraph">world to me and it&#8217;ll help make more videos and grow this community so we&#8217;ll</p>



<p class="wp-block-paragraph">be able to do more videos longer videos better videos or just I&#8217;ll be able to</p>



<p class="wp-block-paragraph">keep making videos in general so please do do me a kindness and and subscribe</p>



<p class="wp-block-paragraph">you know sometimes I&#8217;m sleeping in the middle of the night and I just wake up</p>



<p class="wp-block-paragraph">because I know somebody subscribed or followed it just wakes me up and I get</p>



<p class="wp-block-paragraph">filled with joy that&#8217;s exactly what happens every single time so you could</p>



<p class="wp-block-paragraph">do it as a nice favor to me or you could you control me if you want to just wake</p>



<p class="wp-block-paragraph">just subscribe and then I&#8217;ll just wake up. I promise that&#8217;s what will happen.</p>



<p class="wp-block-paragraph">Also, if you look at the middle of the screen right now, you should see a QR code, which you</p>



<p class="wp-block-paragraph">can scan in order to go to the website, which I think is also named somewhere at the bottom of</p>



<p class="wp-block-paragraph">this video. And it&#8217;ll take you to my main website where you can just kind of like see</p>



<p class="wp-block-paragraph">all the videos I published and the services and tutorials and things that I offer and all that</p>



<p class="wp-block-paragraph">clarifications or errata or just future videos that you want to see please leave a comment or</p>



<p class="wp-block-paragraph">if you just want to say hey what&#8217;s up what&#8217;s going on you know just send me a comment whatever</p>



<p class="wp-block-paragraph">i also wake up for those in the middle of the night i get i wake up in a cold sweat and i&#8217;m like</p>



<p class="wp-block-paragraph">it would really it really mean the world to me i would really appreciate it so</p>



<p class="wp-block-paragraph">again thank you so much for watching this video and um enjoy the cool music as as i fade into the</p>



<p class="wp-block-paragraph">which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/mastering-x86-64-yasm-assembly-functions-made-simple/">Mastering x86-64 YASM Assembly: Functions Made Simple</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/mastering-x86-64-yasm-assembly-functions-made-simple/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
