<?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 language Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/assembly-language/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/assembly-language/</link>
	<description></description>
	<lastBuildDate>Sun, 22 Mar 2026 01:28:32 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.2</generator>

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>assembly language Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/assembly-language/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>YASM x86-64 Assembly Functions Tutorial &#8211; Integers, Floats, Pointers, ABI, C++ Interoperability</title>
		<link>https://www.NeuralLantern.com/yasm-x86-64-assembly-functions-tutorial-integers-floats-pointers-abi-c-interoperability/</link>
					<comments>https://www.NeuralLantern.com/yasm-x86-64-assembly-functions-tutorial-integers-floats-pointers-abi-c-interoperability/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 22 Mar 2026 01:28:30 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[abi x86-64]]></category>
		<category><![CDATA[amd64]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[C++ assembly interop]]></category>
		<category><![CDATA[call ret assembly]]></category>
		<category><![CDATA[extern C]]></category>
		<category><![CDATA[floating point assembly]]></category>
		<category><![CDATA[hybrid C++ assembly]]></category>
		<category><![CDATA[linux assembly]]></category>
		<category><![CDATA[passing arguments assembly]]></category>
		<category><![CDATA[rax return]]></category>
		<category><![CDATA[system v abi]]></category>
		<category><![CDATA[ubuntu assembly]]></category>
		<category><![CDATA[writing functions assembly]]></category>
		<category><![CDATA[x64 assembly]]></category>
		<category><![CDATA[x86-64]]></category>
		<category><![CDATA[xmm registers]]></category>
		<category><![CDATA[xmm0 return]]></category>
		<category><![CDATA[yasm]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=321</guid>

					<description><![CDATA[<p>Learn how to write callable functions in YASM x86-64 assembly on Ubuntu. Covers passing integers, pointers, and floating-point values, using the correct registers (RDI, RSI, XMM0, etc.), returning values in RAX/XMM0, extern \"C\" for C++ interop, and building a hybrid C++/Assembly program.</p>
<p>The post <a href="https://www.NeuralLantern.com/yasm-x86-64-assembly-functions-tutorial-integers-floats-pointers-abi-c-interoperability/">YASM x86-64 Assembly Functions Tutorial &#8211; Integers, Floats, Pointers, ABI, C++ Interoperability</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="YASM x86-64 Assembly Functions Tutorial - Integers, Floats, Pointers, ABI, C++ Interoperability" width="1380" height="776" src="https://www.youtube.com/embed/fXf3Gpf-fyM?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">Quick but complete guide to writing proper functions in YASM x86-64 assembly on Linux. See how to pass integers, pointers &amp; floats, return values, follow the ABI, disable C++ name mangling with extern &#8220;C&#8221;, and call back and forth between C++ and assembly in a real working example.</p>



<p class="wp-block-paragraph">Great for people moving from NASM/GAS or trying to mix assembly with higher-level code.</p>



<p class="wp-block-paragraph">00:00 Introduction and Video Overview<br>00:28 What Are Functions in Programming<br>00:56 Why Functions Are Harder in Assembly<br>01:24 Topics Covered in This Video<br>01:59 About the Makefile and Prerequisites<br>02:28 Hybrid C++ and Assembly Program Plan<br>03:01 Using extern &#8220;C&#8221; to Disable Name Mangling<br>04:03 Main Driver Function in C++<br>05:00 Adding a Callable C++ Function for Assembly<br>05:42 Explaining extern &#8220;C&#8221; Placement<br>06:16 Assembly File Skeleton and Data Section<br>06:41 Creating Null-Terminated Strings<br>07:49 Section .text and External Symbols<br>08:52 Declaring my_cpp_function as extern<br>09:20 Defining my_assembly_function<br>09:40 Labels vs Real Functions<br>10:31 The call Instruction and Return Address<br>11:16 Why Jumping Instead of Calling Crashes<br>11:47 Global Directive for Exporting Functions<br>12:32 Basic Function Structure<br>13:20 Implementing my_assembly_function Prologue<br>14:50 Receiving Arguments in ABI Registers<br>16:30 Printing Received Integer Arguments<br>18:10 Handling Pointer Arguments (C Strings)<br>19:40 Passing Floating-Point Arguments in XMM<br>21:15 Printing Floats from Assembly<br>23:00 Calling Back to C++ Function<br>25:40 Preparing Arguments for my_cpp_function<br>27:20 Loading XMM0 and XMM1 for Floats<br>29:10 Making the Call to C++ Function<br>30:50 Receiving Double Return Value in XMM0<br>32:30 Saving Returned Float to Memory<br>34:10 Printing the Returned Value<br>36:00 Final Messages and Program Flow<br>38:20 Fixing String Pointer Crash Issue<br>40:00 Correcting Argument Loading<br>42:10 Passing String Owned by Assembly<br>44:00 Observing Successful Output<br>45:47 Saving and Restoring XMM0 Safely<br>47:14 Printing Final Returned Float<br>48:32 Importance of Following the ABI<br>50:29 Summary of Covered Topics<br>51:03 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 the Blog: https://www.NeuralLantern.com</li>



<li>Watch 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;m going to teach you how to write functions in YASM x86-64 assembly inside</p>



<p class="wp-block-paragraph">of Ubuntu, although probably any YASM assembler will be fine.</p>



<p class="wp-block-paragraph">I&#8217;ve covered these topics before in other videos, but I thought it would be nice to</p>



<p class="wp-block-paragraph">put them all here in one single video to make it a little bit easier to understand.</p>



<p class="wp-block-paragraph">Okay, so first off, what the heck am I talking about?</p>



<p class="wp-block-paragraph">Well, you know, when you have a program, let&#8217;s say we have a higher level language program</p>



<p class="wp-block-paragraph">and we have like, you know, void or actually probably not void main, let&#8217;s say int main</p>



<p class="wp-block-paragraph">and we&#8217;ll forget about the arguments and inside of main, it just sort of calls f and then</p>



<p class="wp-block-paragraph">we have an f function over here and it, you know, does stuff, right?</p>



<p class="wp-block-paragraph">So that&#8217;s the basic idea of having a function that you can call and calling it.</p>



<p class="wp-block-paragraph">We sort of take the whole process for granted in higher level languages, but in assembly,</p>



<p class="wp-block-paragraph">in assembly we have to do a lot more from scratch so I&#8217;m going to show you about how to pass</p>



<p class="wp-block-paragraph">arguments how to pass integer arguments pointer arguments floating point arguments return types</p>



<p class="wp-block-paragraph">you know double like floating return types integer return types pointer return types</p>



<p class="wp-block-paragraph">how to use special registers to actually pass the arguments into the functions how to write a</p>



<p class="wp-block-paragraph">so that&#8217;s basically what we&#8217;re going to do before before i get any further here i just want to point</p>



<p class="wp-block-paragraph">out that um this video is not about the basics of assembly nor is it about the basics of um</p>



<p class="wp-block-paragraph">make files or some of the other related technologies that i&#8217;m going to show</p>



<p class="wp-block-paragraph">i&#8217;ve covered everything in this video in a previous video um so if you if you find yourself</p>



<p class="wp-block-paragraph">getting lost and you don&#8217;t understand what i&#8217;m saying or what i&#8217;m doing you probably want to</p>



<p class="wp-block-paragraph">look at my previous videos because this is sort of a summary video to kind of help you</p>



<p class="wp-block-paragraph">of help you help you really lock down on the concepts of writing functions okay so for starters</p>



<p class="wp-block-paragraph">this right here that you&#8217;re looking at is a make file this is not a make file video if you want to</p>



<p class="wp-block-paragraph">learn how to make make files or why they&#8217;re awesome see my other videos but i&#8217;m just going to assume</p>



<p class="wp-block-paragraph">at this point you&#8217;re either willing to research my other video or you already know or you don&#8217;t care</p>



<p class="wp-block-paragraph">how to make a make file so you know we&#8217;re just going to skip this i&#8217;m going to close it</p>



<p class="wp-block-paragraph">function or sorry a driver module and what I&#8217;m going to do is I&#8217;m going to write this program</p>



<p class="wp-block-paragraph">that I&#8217;m showing you now as a as a hybrid program a program with modules of different languages so</p>



<p class="wp-block-paragraph">this module right here is going to be c++ and then we&#8217;re going to have an assembly module and</p>



<p class="wp-block-paragraph">then they&#8217;re going to be able to call each other so the first thing that I&#8217;m going to do is I&#8217;m</p>



<p class="wp-block-paragraph">going to name some functions that are going to be inside of my assembly module and also name some</p>



<p class="wp-block-paragraph">module and also name some functions that are going to well name a function that&#8217;s</p>



<p class="wp-block-paragraph">going to be just inside of the C++ module but it should be callable by the</p>



<p class="wp-block-paragraph">assembly module so for that we use a little block called extern C and long</p>



<p class="wp-block-paragraph">story short that disables name mangling which C++ does in order to provide</p>



<p class="wp-block-paragraph">overloading functionality so we&#8217;re basically going to disable overload</p>



<p class="wp-block-paragraph">functionality in order to have simple function names that way assembly can</p>



<p class="wp-block-paragraph">and also when we attempt to call a function inside of assembly we&#8217;ll just call it by its</p>



<p class="wp-block-paragraph">simple name rather than try to mangle the name based on the the arguments and return type and</p>



<p class="wp-block-paragraph">such so that that&#8217;s something we have to do for compatibility at least for this video and I&#8217;m just</p>



<p class="wp-block-paragraph">going to move on so basically you just take you just take the keyword extern and you put a c in</p>



<p class="wp-block-paragraph">a quote and then you make a block right a little braces scope and then you just list prototypes of</p>



<p class="wp-block-paragraph">types of either the functions inside of the current module which you would like to demangle</p>



<p class="wp-block-paragraph">or other modules functions that you would like to be able to call in a demangled or</p>



<p class="wp-block-paragraph">I guess a non-mangled way.</p>



<p class="wp-block-paragraph">Mangled meaning name mangling.</p>



<p class="wp-block-paragraph">Okay, so then I&#8217;m going to copy paste the main function that I&#8217;m going to do.</p>



<p class="wp-block-paragraph">So this is a hybrid program, which means you know there are several different modules of</p>



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



<p class="wp-block-paragraph">driver which just launches the program and it contains like the entry point</p>



<p class="wp-block-paragraph">like see how there&#8217;s like the the main entry point which if you know how to do</p>



<p class="wp-block-paragraph">other languages this is usually where you start in C++ or like the GCC</p>



<p class="wp-block-paragraph">libraries so this is the entry point for our whole program and then once we&#8217;re</p>



<p class="wp-block-paragraph">inside of here we&#8217;re just kind of print out a message to the user we&#8217;re gonna</p>



<p class="wp-block-paragraph">make a C string and then we&#8217;re gonna call on the assembly function we&#8217;re</p>



<p class="wp-block-paragraph">gonna give it you know some numbers some integers and some floats and we&#8217;re</p>



<p class="wp-block-paragraph">gonna give it our C string and then when we&#8217;re inside of the assembly</p>



<p class="wp-block-paragraph">and then when we&#8217;re inside of the assembly function we&#8217;re just going to print those things to prove</p>



<p class="wp-block-paragraph">that we know how to call and we know how to receive data from c or c plus plus into assembly</p>



<p class="wp-block-paragraph">and then we&#8217;re just going to print you know goodbye basically okay so uh i&#8217;m going to make</p>



<p class="wp-block-paragraph">another uh let&#8217;s see another function in the driver so that assembly has something to call</p>



<p class="wp-block-paragraph">because i don&#8217;t just want to be able to call assembly from c i also want to be able to call</p>



<p class="wp-block-paragraph">put in something that we can call on from assembly here.</p>



<p class="wp-block-paragraph">I&#8217;m going to call it my CPP function.</p>



<p class="wp-block-paragraph">And it&#8217;s just going to have like some arguments,</p>



<p class="wp-block-paragraph">just some nonsense arguments.</p>



<p class="wp-block-paragraph">And all it&#8217;s going to do is just print out that it&#8217;s entering,</p>



<p class="wp-block-paragraph">print out the arguments and then just sort of say goodbye.</p>



<p class="wp-block-paragraph">And then just return a value to the caller.</p>



<p class="wp-block-paragraph">We&#8217;re going to return a floating point value to the call.</p>



<p class="wp-block-paragraph">And we&#8217;re going to return a floating point value to the caller.</p>



<p class="wp-block-paragraph">floating point value to the color okay so notice how at the top here i have uh in the extern c my</p>



<p class="wp-block-paragraph">cpp function it&#8217;s the same thing that we see at the top is now at the bottom and that just means</p>



<p class="wp-block-paragraph">uh this normally would be compiled and linked with name mangling and so because it&#8217;s in the</p>



<p class="wp-block-paragraph">extern c block there&#8217;s no name mangling and the reason that works with an extern c block is</p>



<p class="wp-block-paragraph">because there&#8217;s no name mangling in c that&#8217;s sort of like a c plus plus thing with overloads and like</p>



<p class="wp-block-paragraph">whatever okay so we have like the basics for our driver it&#8217;s a simple module the hard work is going</p>



<p class="wp-block-paragraph">to be inside of assembly so uh maybe before we write assembly oh you know what let&#8217;s write the</p>



<p class="wp-block-paragraph">skeleton of the assembly and then i&#8217;ll start explaining uh what it means to call back and</p>



<p class="wp-block-paragraph">forth and to make functions and things like that okay so for starters i&#8217;m just going to make a data</p>



<p class="wp-block-paragraph">section with a bunch of stuff inside of it and i&#8217;ll explain it just very briefly again this video is</p>



<p class="wp-block-paragraph">basics of assembly in Yasm. If you want to learn the basics, go see my other videos.</p>



<p class="wp-block-paragraph">But for now, I&#8217;ll just assume that you understand why we&#8217;re making variables in the data section</p>



<p class="wp-block-paragraph">and kind of how to do it. So I&#8217;m just, you can see here, I&#8217;m making a bunch of null terminated</p>



<p class="wp-block-paragraph">strings. They&#8217;re null because they have a zero at the end. And I&#8217;m just saying, well, here&#8217;s a</p>



<p class="wp-block-paragraph">message when we begin the program. Here&#8217;s a message to announce we&#8217;re about to print some integers,</p>



<p class="wp-block-paragraph">to print a C string, to print some floats. Here&#8217;s another message for this, another message for</p>



<p class="wp-block-paragraph">messages right then a crlf that&#8217;s just you know taking the cursor down to the</p>



<p class="wp-block-paragraph">next line and then back to the very beginning so it&#8217;s like a basically a new</p>



<p class="wp-block-paragraph">line and then I&#8217;m gonna have some floating point numbers and I&#8217;m gonna</p>



<p class="wp-block-paragraph">have an a and a B number and then I&#8217;m gonna have a B and a D that I&#8217;ll send</p>



<p class="wp-block-paragraph">somewhere else and so I&#8217;m gonna populate them with numbers from the start the B</p>



<p class="wp-block-paragraph">and the D that just are not being sent they start at zero because we&#8217;re gonna</p>



<p class="wp-block-paragraph">we&#8217;re going to receive those numbers from C and then we&#8217;re going to get a return value as a</p>



<p class="wp-block-paragraph">floating point from the C function that we call so we&#8217;re just going to store it there so I&#8217;m just</p>



<p class="wp-block-paragraph">like making a bunch of room to store some from some variables and then here we&#8217;re just going</p>



<p class="wp-block-paragraph">to do system right and we&#8217;re going to send to standard output that&#8217;s covered in other videos</p>



<p class="wp-block-paragraph">but basically we&#8217;re just going to be printing stuff okay so section text now this is where</p>



<p class="wp-block-paragraph">actually starts. Well, sort of. This is where the program contains instructions to execute.</p>



<p class="wp-block-paragraph">So section text. And the first thing I&#8217;m going to do is I&#8217;m going to name some external symbols</p>



<p class="wp-block-paragraph">that I have for myself just to help me print integers and floats. So, you know, if you&#8217;ve</p>



<p class="wp-block-paragraph">seen my other videos, you know that you can do a wide variety of things to sort of print characters</p>



<p class="wp-block-paragraph">and numbers if you were clever. And you can also call on C library functions like printf.</p>



<p class="wp-block-paragraph">functions like printf if you really wanted to print floating point numbers</p>



<p class="wp-block-paragraph">and signed integers and stuff so you don&#8217;t have access to this library so</p>



<p class="wp-block-paragraph">don&#8217;t worry about putting this part in your code if you&#8217;re working at this from</p>



<p class="wp-block-paragraph">home unless you&#8217;re someone that has taken a class that I&#8217;ve taught somewhere</p>



<p class="wp-block-paragraph">and for now just just assume that this just lets me print very easily no</p>



<p class="wp-block-paragraph">problem you don&#8217;t really need that if you&#8217;re just kind of experimenting with</p>



<p class="wp-block-paragraph">function this is the name of the function that we made in the driver my cpp function notice how it&#8217;s</p>



<p class="wp-block-paragraph">over here my cpp function we&#8217;re just letting our assembly module know that there&#8217;s a function in</p>



<p class="wp-block-paragraph">another module that we should be able to call on even though all these modules are going to be</p>



<p class="wp-block-paragraph">linked into the same program we still have to do this just to let the assembly module know</p>



<p class="wp-block-paragraph">okay so now we need to make a function called my assembly function if you look at the top right here</p>



<p class="wp-block-paragraph">We had this other D name mingled prototype, my assembly function.</p>



<p class="wp-block-paragraph">So we have to actually write that somewhere.</p>



<p class="wp-block-paragraph">It&#8217;s not in the driver.</p>



<p class="wp-block-paragraph">Let&#8217;s write it inside of assembly.</p>



<p class="wp-block-paragraph">So my assembly function is going to start right here.</p>



<p class="wp-block-paragraph">Boom, right there.</p>



<p class="wp-block-paragraph">So this is when we kind of start on the basics of writing a function.</p>



<p class="wp-block-paragraph">So for starters, hopefully you understand labels.</p>



<p class="wp-block-paragraph">If you don&#8217;t understand labels by now, you might want to see my other videos.</p>



<p class="wp-block-paragraph">just some sort of an alphanumeric symbol that you just kind of write as sort of</p>



<p class="wp-block-paragraph">sort of like a variable name.</p>



<p class="wp-block-paragraph">You can&#8217;t use every symbol available.</p>



<p class="wp-block-paragraph">I would just start with using letters and numbers in the underscore.</p>



<p class="wp-block-paragraph">Of course, you can always experiment if you want to see what&#8217;s allowed and what&#8217;s</p>



<p class="wp-block-paragraph">not just type something and see if it compiles.</p>



<p class="wp-block-paragraph">So the label starts with just, you know, this name and then a colon, right?</p>



<p class="wp-block-paragraph">So that&#8217;s a label.</p>



<p class="wp-block-paragraph">You can jump into labels.</p>



<p class="wp-block-paragraph">Like if we were somewhere else in the program right now, we could like, if we</p>



<p class="wp-block-paragraph">we could like if we wanted to do an infinite loop we could say jump and then the name of the label</p>



<p class="wp-block-paragraph">and what would happen is execution would go back up to this label and then it would jump again</p>



<p class="wp-block-paragraph">it would go back up it would be an infinite loop but we wouldn&#8217;t technically be calling a function</p>



<p class="wp-block-paragraph">we&#8217;re just sort of jumping so labels are sort of the first step we have to do a little bit more to</p>



<p class="wp-block-paragraph">actually make a function so we have an assembly function which right now is just a label and in</p>



<p class="wp-block-paragraph">side we have to mark it as global so remember when we marked my CPP function</p>



<p class="wp-block-paragraph">as extern that means we can call a function that lives somewhere else now</p>



<p class="wp-block-paragraph">we&#8217;re marking this function as global which means it lives here but we want</p>



<p class="wp-block-paragraph">other modules to be able to call on our function or our label okay so how do we</p>



<p class="wp-block-paragraph">actually make a function let&#8217;s see the first thing that you should do is try to</p>



<p class="wp-block-paragraph">understand that if that all functions return even if they don&#8217;t return any any</p>



<p class="wp-block-paragraph">Under the hood, what this is actually going to do is crash the program if you just jumped</p>



<p class="wp-block-paragraph">into this label and didn&#8217;t call it like a function, because the return instruction will</p>



<p class="wp-block-paragraph">look onto the call stack and try to find a return address for wherever this function</p>



<p class="wp-block-paragraph">was called from and then go jump, do a jump instruction to return there.</p>



<p class="wp-block-paragraph">And so if you just jumped into this label and then there&#8217;s a return, it&#8217;s going to pop</p>



<p class="wp-block-paragraph">something up the stack that&#8217;s not actually a return address and probably crash the program.</p>



<p class="wp-block-paragraph">So that means now that we have a return statement, we definitely have to call this like a function.</p>



<p class="wp-block-paragraph">And I guess that&#8217;s pretty easy because, well, if we were inside of assembly and we wanted to call another function,</p>



<p class="wp-block-paragraph">let me just show you real fast.</p>



<p class="wp-block-paragraph">I&#8217;ll say some other function.</p>



<p class="wp-block-paragraph">If we were inside of this label, for some reason, we could call the above function by just saying call.</p>



<p class="wp-block-paragraph">And then the label.</p>



<p class="wp-block-paragraph">So now it&#8217;s being treated like a function.</p>



<p class="wp-block-paragraph">it&#8217;s being treated like a function.</p>



<p class="wp-block-paragraph">What the call instruction really does is it just looks at the address of the next instruction</p>



<p class="wp-block-paragraph">that would have been executed.</p>



<p class="wp-block-paragraph">So, you know, whatever, let&#8217;s say we have like a nope on the next line, whatever the</p>



<p class="wp-block-paragraph">memory location or the relative memory location is of that nope instruction, it&#8217;s going to</p>



<p class="wp-block-paragraph">push that onto the call stack.</p>



<p class="wp-block-paragraph">And then it&#8217;s going to just do a jump instruction to this label.</p>



<p class="wp-block-paragraph">So then later when we hit this return statement, then it&#8217;s going to look onto the call stack</p>



<p class="wp-block-paragraph">and it&#8217;s going to find that return address, which is going to correspond with the nope</p>



<p class="wp-block-paragraph">instruction and the return instruction is going to do a jump back to that address.</p>



<p class="wp-block-paragraph">So basically you have to use the call instruction to get there and then use a return instruction</p>



<p class="wp-block-paragraph">to return from there.</p>



<p class="wp-block-paragraph">And then now we can actually call it a function.</p>



<p class="wp-block-paragraph">But then there&#8217;s other things that functions have to do to behave themselves without crashing</p>



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



<p class="wp-block-paragraph">So for example, notice up here that I have copy pasted the let&#8217;s see call on assembly.</p>



<p class="wp-block-paragraph">Oh, I think I forgot to update that.</p>



<p class="wp-block-paragraph">I forgot to update that. Let&#8217;s see. Call on this. Yeah. It&#8217;s supposed to be called my assembly</p>



<p class="wp-block-paragraph">function now. Hold on. Let me update my, uh, my solution. Whoops. Hold on. Call on.</p>



<p class="wp-block-paragraph">There we go. My assembly. Okay. So I&#8217;ve got my solution up above on another monitor.</p>



<p class="wp-block-paragraph">So I have the prototype of the function here. Uh, it&#8217;s just going to return nothing. And it&#8217;s</p>



<p class="wp-block-paragraph">going to like take in a long and a double and a long and a double and a character pointer.</p>



<p class="wp-block-paragraph">And then my intention here is to save these arguments with some registers.</p>



<p class="wp-block-paragraph">So notice how I&#8217;m saying we&#8217;re going to use R12, 13, and 14 to save the arguments A, C, and E.</p>



<p class="wp-block-paragraph">And we&#8217;ll talk about why we&#8217;re not saving the other ones with those registers in a second.</p>



<p class="wp-block-paragraph">But basically we&#8217;re going to use those registers.</p>



<p class="wp-block-paragraph">So the thing about the registers is we have to do something called respecting the ABI.</p>



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



<p class="wp-block-paragraph">The ABI stands for the Application Binary Interface.</p>



<p class="wp-block-paragraph">interface and it&#8217;s just sort of like a standard that governs all of the things that you can do</p>



<p class="wp-block-paragraph">and you&#8217;re supposed to do when you&#8217;re working with x86 64 assembly so the abi is pretty cool</p>



<p class="wp-block-paragraph">hang on a second the abi is pretty cool because it actually standardizes things</p>



<p class="wp-block-paragraph">for instance if we didn&#8217;t have the abi and we didn&#8217;t respect it then we couldn&#8217;t actually call</p>



<p class="wp-block-paragraph">plus or vice versa because c plus plus the higher level languages they&#8217;re going to use the abi so if</p>



<p class="wp-block-paragraph">you try to do things your own way then all you&#8217;re going to end up doing is wasting time and energy</p>



<p class="wp-block-paragraph">coming up with two different ways of doing things when you could have just done it the abi way</p>



<p class="wp-block-paragraph">right so and one of those and one of those ways your way wouldn&#8217;t even work uh for cross you know</p>



<p class="wp-block-paragraph">module calling it would only work internally to your own program so we&#8217;re going to respect the abi</p>



<p class="wp-block-paragraph">does the abi say about these registers and again i&#8217;ve talked about this extensively in other videos</p>



<p class="wp-block-paragraph">but let me pull up a fun book that i love to uh to talk about uh so this book right here i did not</p>



<p class="wp-block-paragraph">write it uh the book here is written by a really wonderful uh professor dr ed jorgensen phd and um</p>



<p class="wp-block-paragraph">this book is totally free it&#8217;s not i&#8217;m not selling you anything uh this is literally you can just go</p>



<p class="wp-block-paragraph">to this this professor&#8217;s website and download his book for free and he&#8217;s already given me permission</p>



<p class="wp-block-paragraph">and he&#8217;s already given me permission to just tell everybody to just share it with everybody if you</p>



<p class="wp-block-paragraph">if you look in the license area it&#8217;s a copyleft license so it&#8217;s sort of like the spirit of open</p>



<p class="wp-block-paragraph">source just sort of like sharing knowledge and stuff so it&#8217;s awesome so I suggest everybody you</p>



<p class="wp-block-paragraph">know follow the link that I hopefully put in the video and and grab a copy of this book but</p>



<p class="wp-block-paragraph">basically what I&#8217;m going to do is I&#8217;m going to go to a special area and I&#8217;m going to search for</p>



<p class="wp-block-paragraph">here and so that would be I guess section 12.8.2 and it&#8217;s called register usage subsection 12.8.2</p>



<p class="wp-block-paragraph">notice how it lists all of the registers and kind of how they are typically used</p>



<p class="wp-block-paragraph">this falls under the scope of the abi so this is not all the abi is but this is one of the</p>



<p class="wp-block-paragraph">things you&#8217;re supposed to do when you&#8217;re respecting the abi is you&#8217;re supposed to sort of like</p>



<p class="wp-block-paragraph">are supposed to be used. Notice R12, which is one of the registers that we&#8217;re going to use,</p>



<p class="wp-block-paragraph">is designated as callee saved. So that means whoever is being called has to preserve that</p>



<p class="wp-block-paragraph">register if they intend to mess it up. So for example, if I just had this function here,</p>



<p class="wp-block-paragraph">move some value into, you know, R12, I have now basically broken the program. If any, you know,</p>



<p class="wp-block-paragraph">C function or other library that you didn&#8217;t write, just any anywhere else calls on this function,</p>



<p class="wp-block-paragraph">anywhere else calls on this function then their version of r12 is going to get destroyed by what</p>



<p class="wp-block-paragraph">i just did and so i&#8217;m not respecting the abi when this function returns to the caller they&#8217;re going</p>



<p class="wp-block-paragraph">to expect that their original data was intact and if it&#8217;s not program is not going to work it&#8217;s going</p>



<p class="wp-block-paragraph">to screw up so respecting the ai means you have to preserve any registered mark as callee saved or</p>



<p class="wp-block-paragraph">that&#8217;s one of the things that it means so notice how r12 through r15 are callee saved so we have</p>



<p class="wp-block-paragraph">the abi now and sort of preserve 12 13 and 14. we can do that pretty easily with some push and pop</p>



<p class="wp-block-paragraph">statements so i&#8217;m going to do push r12 push r13 push r14 and now what happens is those values</p>



<p class="wp-block-paragraph">are actually on the stack now and i can retrieve them later at the end of the function even if i</p>



<p class="wp-block-paragraph">destroy them while i&#8217;m inside of the function i&#8217;ll just restore them right before i return so</p>



<p class="wp-block-paragraph">because it happens before the function does anything.</p>



<p class="wp-block-paragraph">And then we have to make sure that we pop those values</p>



<p class="wp-block-paragraph">because you have to be careful with the stack.</p>



<p class="wp-block-paragraph">If you just start pushing values</p>



<p class="wp-block-paragraph">and you don&#8217;t restore the stack to its original state</p>



<p class="wp-block-paragraph">by the time you return,</p>



<p class="wp-block-paragraph">you&#8217;ve basically broken the program.</p>



<p class="wp-block-paragraph">It&#8217;s either going to crash right away</p>



<p class="wp-block-paragraph">or whoever called it is not going to function correctly anymore.</p>



<p class="wp-block-paragraph">So let&#8217;s do some pops.</p>



<p class="wp-block-paragraph">We&#8217;re going to do three pops</p>



<p class="wp-block-paragraph">and we&#8217;ll call this the epilogue,</p>



<p class="wp-block-paragraph">which means uh you know something we do right before we exit the function and keep in mind</p>



<p class="wp-block-paragraph">that the order of the pops should be reverse of the order of the pushes notice how we&#8217;re popping</p>



<p class="wp-block-paragraph">in reverse order from what i did before so we&#8217;re going to pop uh 14 13 and 12 where as we pushed</p>



<p class="wp-block-paragraph">12 13 and 14 before if you pop in the wrong order like if you try to do it in in the same order as</p>



<p class="wp-block-paragraph">the pushes then uh you&#8217;re still going to end up destroying data for the caller because you&#8217;re</p>



<p class="wp-block-paragraph">because you&#8217;re going to be restoring data to the wrong registers.</p>



<p class="wp-block-paragraph">So just keep that in mind.</p>



<p class="wp-block-paragraph">OK, so now the ABI is being respected.</p>



<p class="wp-block-paragraph">Let me see, by the way, do we have enough to actually even run the driver right now?</p>



<p class="wp-block-paragraph">We&#8217;re just calling it and then it didn&#8217;t really do anything.</p>



<p class="wp-block-paragraph">Yeah, I think we could probably this might compile.</p>



<p class="wp-block-paragraph">OK, let me let me just check this out.</p>



<p class="wp-block-paragraph">I want to say clear and make run.</p>



<p class="wp-block-paragraph">Yeah, it compiles.</p>



<p class="wp-block-paragraph">OK, so the driver printed its hello message.</p>



<p class="wp-block-paragraph">again it&#8217;s just sort of saying like hello from the driver and then when the driver comes</p>



<p class="wp-block-paragraph">back it says the driver regained control and nothing really happened because well we didn&#8217;t</p>



<p class="wp-block-paragraph">do anything in our assembly function yet but at least we&#8217;re calling multiple modules and again</p>



<p class="wp-block-paragraph">if you want to know how to do hybrid programs and linking and compiling and all that stuff</p>



<p class="wp-block-paragraph">see my other videos for now I&#8217;m just going to move on so the next thing we have to do is we have to</p>



<p class="wp-block-paragraph">have to try to understand like how are we going to receive these arguments so this is one of the</p>



<p class="wp-block-paragraph">other you know building blocks to making functions notice how the assembly function i&#8217;m sending in an</p>



<p class="wp-block-paragraph">integer and then a float and then another integer and then another floats i&#8217;m kind of mixing the</p>



<p class="wp-block-paragraph">arguments then at the very end i&#8217;m sending in a pointer if i look back up at the prototype here</p>



<p class="wp-block-paragraph">which matches what i&#8217;ve what how i&#8217;ve used it it&#8217;s a long a double a long a double and a character</p>



<p class="wp-block-paragraph">longs and doubles, they actually, they&#8217;re called mixed arguments and they don&#8217;t actually count</p>



<p class="wp-block-paragraph">against each other when you&#8217;re looking at the order of the registers to stuff them into.</p>



<p class="wp-block-paragraph">So for example, let me show you here. We have, what did I just do? I clicked on the wrong</p>



<p class="wp-block-paragraph">computer. Okay. I&#8217;m on the wrong computer. Okay. Let me close this real fast. So</p>



<p class="wp-block-paragraph">When you think about registers for incoming arguments, you basically start to think about this.</p>



<p class="wp-block-paragraph">RDI and RSI, those registers represent the first and the second integer arguments.</p>



<p class="wp-block-paragraph">If we look back to the book real fast, we can see in that same section, RDI is the first argument,</p>



<p class="wp-block-paragraph">and then RSI is the second argument, and then RDX is the third, and then the fourth.</p>



<p class="wp-block-paragraph">We can do up to six arguments with R9, and then after that, we have to start pushing arguments to the stack.</p>



<p class="wp-block-paragraph">stack i&#8217;m not going to go that far in this video i&#8217;ve actually done that already in a previous video</p>



<p class="wp-block-paragraph">but basically we for for now we can just use six registers to push arguments but if you think</p>



<p class="wp-block-paragraph">about it these registers these are not floating point registers these are general purpose registers</p>



<p class="wp-block-paragraph">they&#8217;re meant for integers and pointers the reason they&#8217;re used for in it for pointers also is because</p>



<p class="wp-block-paragraph">a pointer is just an integer a pointer is just a 64-bit integer unsigned which represents a memory</p>



<p class="wp-block-paragraph">can use these registers only for integers and pointers but we can&#8217;t use them for floats so that</p>



<p class="wp-block-paragraph">means the first integer argument is going to be RDI and the second integer argument is going to be</p>



<p class="wp-block-paragraph">RSI but if there was a float argument in between then RSI would still be the second integer argument</p>



<p class="wp-block-paragraph">the floats don&#8217;t count against the integers and vice versa so for example if we&#8217;re talking about</p>



<p class="wp-block-paragraph">float registers you know the first one available is XMM0 and then we have XMM1 and then we have</p>



<p class="wp-block-paragraph">we have XMM1 and we have XMM2 and it goes all the way up to I think XMM15 so we have 16 floating</p>



<p class="wp-block-paragraph">point registers. So we could pass in 16 floating point arguments just using these registers if we</p>



<p class="wp-block-paragraph">wanted to and then if we want to do even more than that we probably have to get you know funky with</p>



<p class="wp-block-paragraph">the stack or something or maybe hopefully you just have an array somewhere and you&#8217;re just going to</p>



<p class="wp-block-paragraph">pass in a pointer. But the way you have to think of these arguments is that even though they might</p>



<p class="wp-block-paragraph">they might be mixed in the prototype of the function that you&#8217;re calling from a higher level</p>



<p class="wp-block-paragraph">language you shouldn&#8217;t think of them as being mixed when you&#8217;re actually loading up registers so</p>



<p class="wp-block-paragraph">again if we just kind of like go back here to this prototype</p>



<p class="wp-block-paragraph">notice how that a variable we&#8217;re going to say that the a is rdi because a is along it&#8217;s an integer</p>



<p class="wp-block-paragraph">but then right after that there&#8217;s a double b we would not skip rsi or assign b to rsi</p>



<p class="wp-block-paragraph">b would just be the first float argument and then when we go back to long c long c is actually the</p>



<p class="wp-block-paragraph">second integer argument because that double doesn&#8217;t count against the integer so it&#8217;s going to be c</p>



<p class="wp-block-paragraph">is going to be rsi and then uh for double d uh same thing uh we&#8217;re not going to skip xmm1 just</p>



<p class="wp-block-paragraph">because there was a an integer we&#8217;re going to go straight to saying that xmm1 is the second float</p>



<p class="wp-block-paragraph">so it&#8217;s the d then for the last argument that we have i&#8217;m just going to erase this stuff down here</p>



<p class="wp-block-paragraph">it was going to be e and remember pointers are integers they&#8217;re just unsigned uh 64-bit integers</p>



<p class="wp-block-paragraph">we have to go on to the next one which i think i recall is like rdx let me just double check</p>



<p class="wp-block-paragraph">i don&#8217;t want to say this wrong rdx yeah the third argument so i&#8217;m going to do rdx and now</p>



<p class="wp-block-paragraph">that we&#8217;ve kind of like mapped this out we we know now what what registers we should be looking for</p>



<p class="wp-block-paragraph">we should be looking for when the function comes in in order to receive</p>



<p class="wp-block-paragraph">our data we should look at those registers for those variables another</p>



<p class="wp-block-paragraph">thing to keep in mind by the way is that usually when we return something in</p>



<p class="wp-block-paragraph">assembly we will move a value into RAX right like some value if you&#8217;ve been</p>



<p class="wp-block-paragraph">following my assembly videos so far but that only counts if you want to return</p>



<p class="wp-block-paragraph">an integer or a pointer if you instead wanted to return a floating point number</p>



<p class="wp-block-paragraph">use XMM zero and I&#8217;ll just you&#8217;re not allowed to you&#8217;re not allowed to hard code a floating</p>



<p class="wp-block-paragraph">point number in assembly like this or at least in the ASM so we&#8217;ll just pretend that there&#8217;s</p>



<p class="wp-block-paragraph">like a float somewhere and I&#8217;ll just load it from memory and I&#8217;ll say like the float</p>



<p class="wp-block-paragraph">something like that so notice how we&#8217;re using a different instruction we&#8217;re not using the regular</p>



<p class="wp-block-paragraph">move instruction that works with integers we&#8217;re instead using the floating point version we&#8217;re</p>



<p class="wp-block-paragraph">saying let&#8217;s move a single piece of data and let&#8217;s move a double precision floating point number</p>



<p class="wp-block-paragraph">xmm zero and then we&#8217;ll just grab from memory whatever whatever that variable has we&#8217;ll just</p>



<p class="wp-block-paragraph">take that floating point number and stick it into xmm zero so uh if you want to return an integer</p>



<p class="wp-block-paragraph">or float you use rax if you want to re sorry if you want to return an integer or a pointer</p>



<p class="wp-block-paragraph">you use rax if you want to return a float you use xmm zero you shouldn&#8217;t do that at the same time</p>



<p class="wp-block-paragraph">if you have like two assembly functions calling each other you might be tempted to do that and i</p>



<p class="wp-block-paragraph">you know very standard and it wouldn&#8217;t work very well with other people&#8217;s code or library code or</p>



<p class="wp-block-paragraph">higher level language code so only one or the other and it just has to match your prototype</p>



<p class="wp-block-paragraph">so notice how here inside of my cpp function notice how it&#8217;s going to return a double</p>



<p class="wp-block-paragraph">right so when the assembly module is done calling on this function it should expect xmm0 to be</p>



<p class="wp-block-paragraph">loaded up with that double precision loading point number okay so that&#8217;s the basic idea</p>



<p class="wp-block-paragraph">Okay, so that&#8217;s the basic idea.</p>



<p class="wp-block-paragraph">So now let&#8217;s maybe let me pin this.</p>



<p class="wp-block-paragraph">Let&#8217;s kind of fill this out a little bit more.</p>



<p class="wp-block-paragraph">So the first thing that we should do</p>



<p class="wp-block-paragraph">is we should save our integer or pointer arguments.</p>



<p class="wp-block-paragraph">So I&#8217;m gonna leave the respect ABI thing there.</p>



<p class="wp-block-paragraph">And notice how I&#8217;m just looking at RDI and RSI and RDX.</p>



<p class="wp-block-paragraph">And well, I guess, you know,</p>



<p class="wp-block-paragraph">we have it in a comment up here, A, C and E,</p>



<p class="wp-block-paragraph">but I&#8217;ll just make another comment here.</p>



<p class="wp-block-paragraph">here I&#8217;ll say like a and a or sorry a C and E and maybe we&#8217;ll specify the data types for fun</p>



<p class="wp-block-paragraph">it&#8217;s going to be long a and then long C and then the character pointer C even if this was a float</p>



<p class="wp-block-paragraph">pointer it would still be an integer because all pointers are integers no matter what they&#8217;re</p>



<p class="wp-block-paragraph">pointing to so just keep that in mind anyway so we&#8217;re going to save r12 r13 and 14 with the</p>



<p class="wp-block-paragraph">14 with the incoming arguments and the reason we want to save those right away is because rdi rsi</p>



<p class="wp-block-paragraph">and rdx those are not callee saved which means the moment we call in another function they&#8217;ll</p>



<p class="wp-block-paragraph">possibly be destroyed because we don&#8217;t really know what&#8217;s going on in other functions that we might</p>



<p class="wp-block-paragraph">call so it&#8217;s a good idea to just kind of save right away either to a global or the stack or</p>



<p class="wp-block-paragraph">in this case just registers being faster okay so we have that now we have to save our float arguments</p>



<p class="wp-block-paragraph">b and d were xmm0 and xmm1 so I&#8217;m just going to save both of those</p>



<p class="wp-block-paragraph">and if you&#8217;re wondering what this float b and float d are that&#8217;s just</p>



<p class="wp-block-paragraph">you know up here I just have a global variable so I can save them to memory</p>



<p class="wp-block-paragraph">easily and not worry about the stack this is not a stack video so much</p>



<p class="wp-block-paragraph">but yeah okay so I&#8217;m just saving all of the incoming arguments that&#8217;s all I&#8217;ve</p>



<p class="wp-block-paragraph">done so far and let&#8217;s see it should probably still</p>



<p class="wp-block-paragraph">Let me see if this works.</p>



<p class="wp-block-paragraph">I&#8217;m going to go make run.</p>



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



<p class="wp-block-paragraph">So nothing happens, but it at least worked.</p>



<p class="wp-block-paragraph">So I&#8217;m going to same as other windows for that so that we don&#8217;t have to look at it anymore.</p>



<p class="wp-block-paragraph">And now that we&#8217;re done saving our float arguments, let&#8217;s, let&#8217;s print a welcome message.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do, you know, welcome.</p>



<p class="wp-block-paragraph">And I&#8217;m going to use a special function that I&#8217;ve made in previous videos called print</p>



<p class="wp-block-paragraph">null terminated string, which means I should probably copy paste that into this program</p>



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



<p class="wp-block-paragraph">What is print null terminated string?</p>



<p class="wp-block-paragraph">It&#8217;s just another convenience function that I wrote.</p>



<p class="wp-block-paragraph">I&#8217;m not going to explain it too much because it&#8217;s in other videos and</p>



<p class="wp-block-paragraph">I&#8217;m already here trying to explain functions to you in general.</p>



<p class="wp-block-paragraph">So long story short, it takes in a C string and a file handle, you know, to like where</p>



<p class="wp-block-paragraph">you want to write, like if you want to write to a file or you want to write to standard</p>



<p class="wp-block-paragraph">output or standard error.</p>



<p class="wp-block-paragraph">And it just takes those arguments and then it sort of says, all right, how long is the</p>



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



<p class="wp-block-paragraph">And it uses another function called string length to figure out how long the string is.</p>



<p class="wp-block-paragraph">how long the string is and then it just uses a system call to actually print the</p>



<p class="wp-block-paragraph">string and again system calls are covered in other videos this one looks</p>



<p class="wp-block-paragraph">really convoluted because it&#8217;s like you know customized for this function but</p>



<p class="wp-block-paragraph">just trust me on this this prints a string next function I got a paste in</p>



<p class="wp-block-paragraph">real fast again explained in other videos is the string length function so</p>



<p class="wp-block-paragraph">all this does is it just takes a pointer to a string and it sort of scans the</p>



<p class="wp-block-paragraph">string is and as soon as it sees a zero like a null terminator then it just says all right that&#8217;s</p>



<p class="wp-block-paragraph">that&#8217;s the end of the string and it&#8217;ll just return the length to the caller so that&#8217;s all you need to</p>



<p class="wp-block-paragraph">know about this covered in other videos then i&#8217;m going to make a convenience function here called</p>



<p class="wp-block-paragraph">crlf and all that&#8217;s going to do is just print the new line that we talked about earlier so just</p>



<p class="wp-block-paragraph">just a bunch of convenience functions on top of the real part of the program so now that we have</p>



<p class="wp-block-paragraph">the convenience functions in there we should be able to see the welcome message let me just double</p>



<p class="wp-block-paragraph">yeah now inside assembly entry point oh what did I do wrong now inside I keep forgetting to change</p>



<p class="wp-block-paragraph">the strings on this now inside where&#8217;s that now inside my assembly function okay let me change</p>



<p class="wp-block-paragraph">that in my solution to you know I&#8217;m just kind of like writing these things and I&#8217;m having fun and</p>



<p class="wp-block-paragraph">I keep changing my mind about what they should be named and then I I get some inconsistencies okay</p>



<p class="wp-block-paragraph">all right so then uh we printed the welcome message and now let&#8217;s print the integer arguments</p>



<p class="wp-block-paragraph">but first let&#8217;s print a little introduction uh to the integers let&#8217;s just say hey we&#8217;re about</p>



<p class="wp-block-paragraph">to print the integer so that&#8217;s just this other string a message saying we&#8217;re about to print the</p>



<p class="wp-block-paragraph">integers nothing really that complicated so far it says now we&#8217;re printing all the integer arguments</p>



<p class="wp-block-paragraph">so now we can actually print the integer arguments so we have two integer arguments</p>



<p class="wp-block-paragraph">if you recall let&#8217;s see yeah we had like a and c those were integers I&#8217;m not going to talk about</p>



<p class="wp-block-paragraph">e right now because that was a pointer but you know right now we&#8217;re just saying a and c so that</p>



<p class="wp-block-paragraph">was r12 and r13 so I&#8217;m just going to paste some code here to actually print those and</p>



<p class="wp-block-paragraph">talk about this library in other videos but basically I&#8217;m moving r12 which is the first</p>



<p class="wp-block-paragraph">integer argument that functions typically receive and once that&#8217;s loaded up i&#8217;m just going to call</p>



<p class="wp-block-paragraph">on my special function to just print the integer and again you could use printf from the c libraries</p>



<p class="wp-block-paragraph">if you actually wanted to print it and not just experiment and stuff like that so i&#8217;m going to use</p>



<p class="wp-block-paragraph">r12 and r13 and i&#8217;m just going to print both of those integer arguments and then after each one</p>



<p class="wp-block-paragraph">is printed notice i&#8217;m calling the crlf function which is just the convenience function of just</p>



<p class="wp-block-paragraph">which is just the convenience function of just like doing a new line.</p>



<p class="wp-block-paragraph">So now we should see two numbers. Yeah.</p>



<p class="wp-block-paragraph">Now printing all integer arguments,</p>



<p class="wp-block-paragraph">we&#8217;ve got an 88 there and then that other giant number there.</p>



<p class="wp-block-paragraph">Let&#8217;s just double check that that&#8217;s actually what we&#8217;re supposed to be seeing.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do this and I&#8217;m going to say,</p>



<p class="wp-block-paragraph">the driver called the assembly module with these numbers.</p>



<p class="wp-block-paragraph">It gave it an 88 and then for the next integer,</p>



<p class="wp-block-paragraph">it gave it the 287 giant number. So great.</p>



<p class="wp-block-paragraph">so great we&#8217;re printing the integers now let&#8217;s print the floats so we should see</p>



<p class="wp-block-paragraph">like a 99 point something in a 32 point something next okay so let&#8217;s continue</p>



<p class="wp-block-paragraph">with printing oh sorry actually let&#8217;s print the the C string because that&#8217;s a</p>



<p class="wp-block-paragraph">pointer that&#8217;s still more closely related than the floats so the first</p>



<p class="wp-block-paragraph">thing I&#8217;m going to do is I&#8217;m going to</p>



<p class="wp-block-paragraph">print the received c string how about like announce that we will print the c string because</p>



<p class="wp-block-paragraph">that&#8217;s what we&#8217;re doing right here let me change my solution to match and then we&#8217;ll uh we&#8217;ll</p>



<p class="wp-block-paragraph">actually print the received c string next so same stuff as before first we call print null terminated</p>



<p class="wp-block-paragraph">string to print out a little welcome message or just like an intro message like we are going to</p>



<p class="wp-block-paragraph">print the c string and then we&#8217;ll use that print function again but we&#8217;ll give it the c string so</p>



<p class="wp-block-paragraph">it the c string so it just prints the whole c string out and this should prove to you that we</p>



<p class="wp-block-paragraph">are indeed receiving a pointer to some data owned by the c plus plus module so if we run this real</p>



<p class="wp-block-paragraph">fast it should just tell us two more things it&#8217;ll give us the announcement now printing the received</p>



<p class="wp-block-paragraph">c string and then on that same line it says hello this is the c string owned by main and if we just</p>



<p class="wp-block-paragraph">that&#8217;s exactly what string is inside of that variable so hello this is a c string owned by</p>



<p class="wp-block-paragraph">main and we gave it to the function by just kind of passing it in and we know that character arrays</p>



<p class="wp-block-paragraph">are basically character pointers or any array is just a pointer to the first item in the array so</p>



<p class="wp-block-paragraph">my c string is really a pointer to that h character so if we pass that in then a pointer</p>



<p class="wp-block-paragraph">based print function should be able to work and that&#8217;s what happened okay so we&#8217;ve done that</p>



<p class="wp-block-paragraph">So we&#8217;ve done that.</p>



<p class="wp-block-paragraph">And then the next thing we should do is let&#8217;s print the floats.</p>



<p class="wp-block-paragraph">So first let&#8217;s announce that we&#8217;re going to print the floats.</p>



<p class="wp-block-paragraph">Same thing we&#8217;d before we&#8217;re just printing like an announcement message.</p>



<p class="wp-block-paragraph">If we run the program again, it&#8217;s just like now printing the floats, but it doesn&#8217;t actually</p>



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



<p class="wp-block-paragraph">So then the next step is let&#8217;s, let&#8217;s grab the first float into XMM zero.</p>



<p class="wp-block-paragraph">And then let&#8217;s call a function to print it.</p>



<p class="wp-block-paragraph">so right here we have like that 99 number that we expected from before by</p>



<p class="wp-block-paragraph">the way so why am I doing it this way why am I not just keeping XMM zero</p>



<p class="wp-block-paragraph">because you remember before we had XMM zero had that had the float that we</p>



<p class="wp-block-paragraph">received and then we&#8217;re using it again down here but remember XMM zero and all</p>



<p class="wp-block-paragraph">the other float registers they&#8217;re not designated as callee saved which means</p>



<p class="wp-block-paragraph">the moment we call any other function we should expect that that data has been</p>



<p class="wp-block-paragraph">so I can&#8217;t actually count on XMM 0 surviving just this little simple function instead I have to save</p>



<p class="wp-block-paragraph">it somewhere to the stack to memory you know whatever so I&#8217;m just that&#8217;s why I put that into</p>



<p class="wp-block-paragraph">a global variable so it&#8217;s sitting in float underscore B right now and then we saved it at</p>



<p class="wp-block-paragraph">the beginning to float underscore B and if you just kind of look up to the data area well it was</p>



<p class="wp-block-paragraph">just float underscore B was just a little quad word you know eight bytes of memory that can hold</p>



<p class="wp-block-paragraph">hold our float. So we have like float allocations for B and D, the first and second float arguments.</p>



<p class="wp-block-paragraph">So we&#8217;re saving it there. And then we&#8217;re recalling it here. And remember, the first function argument</p>



<p class="wp-block-paragraph">is going to be XMM0, regardless of where that data originally came from. So if we look at the next</p>



<p class="wp-block-paragraph">one here, if we kind of like, let&#8217;s see, copy paste this, and we want to grab like the D float,</p>



<p class="wp-block-paragraph">still going to load it into XMM zero because right now it&#8217;s not about what we originally</p>



<p class="wp-block-paragraph">received as an argument.</p>



<p class="wp-block-paragraph">It&#8217;s what this function expects as an argument.</p>



<p class="wp-block-paragraph">This function only takes one argument.</p>



<p class="wp-block-paragraph">It just wants a float so that it can print it and that&#8217;s it.</p>



<p class="wp-block-paragraph">So both times we&#8217;re going to load it up into XMM zero and then we&#8217;re going to print a new</p>



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



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



<p class="wp-block-paragraph">So let me just run the program one more time and we should now see we&#8217;ve got two floats</p>



<p class="wp-block-paragraph">and they should match what the driver tried to send in.</p>



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



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



<p class="wp-block-paragraph">And then 32 point something, that&#8217;s the second one.</p>



<p class="wp-block-paragraph">So cool, we have received integers and pointers and floats.</p>



<p class="wp-block-paragraph">We&#8217;ve recalled them and then we&#8217;ve printed them.</p>



<p class="wp-block-paragraph">Pretty slick, what do you think?</p>



<p class="wp-block-paragraph">Anyway, so we&#8217;ve done that.</p>



<p class="wp-block-paragraph">And the next thing that we should probably do is…</p>



<p class="wp-block-paragraph">Well, at this point, we just maybe have to mess with return types.</p>



<p class="wp-block-paragraph">Even though I&#8217;ve told you about it, we&#8217;ll just mess with it a little bit.</p>



<p class="wp-block-paragraph">mess with it a little bit. But let&#8217;s call the C++ module. So what I&#8217;d like to do first is just sort</p>



<p class="wp-block-paragraph">of announce that we&#8217;re going to call on the C++ module. Again, typical design pattern, let&#8217;s just</p>



<p class="wp-block-paragraph">print a message saying what we&#8217;re about to do. And if we run this now, it&#8217;s going to say assembly</p>



<p class="wp-block-paragraph">module will now call on the C++ module. So nothing really too complicated. So now we&#8217;re going to call</p>



<p class="wp-block-paragraph">on that function. Let me let me paste the name of it here. Actually, I&#8217;m going to put this one right</p>



<p class="wp-block-paragraph">I want to put this one right here.</p>



<p class="wp-block-paragraph">It&#8217;s the my CPP function function.</p>



<p class="wp-block-paragraph">So if we look back at the driver</p>



<p class="wp-block-paragraph">and look at the signature for my CPP function,</p>



<p class="wp-block-paragraph">whoops, it has this signature.</p>



<p class="wp-block-paragraph">And if you wanted to look up higher,</p>



<p class="wp-block-paragraph">you totally can just look into the name mangling section.</p>



<p class="wp-block-paragraph">Whoops, the my CPP function, it returns a double.</p>



<p class="wp-block-paragraph">It takes in a long, a double, a long, a double,</p>



<p class="wp-block-paragraph">and a character pointer.</p>



<p class="wp-block-paragraph">Basically the same thing as the other one,</p>



<p class="wp-block-paragraph">as the other one except it returns a double so what&#8217;s going to happen is when it takes all of</p>



<p class="wp-block-paragraph">these in it&#8217;s just going to print all of them and then i&#8217;m just going to have it return just kind of</p>



<p class="wp-block-paragraph">like some random double that i decided to type because this is not an arithmetic video okay so</p>



<p class="wp-block-paragraph">cpp function and then um now how do we interpret that so if we uh let&#8217;s see let me maybe just for</p>



<p class="wp-block-paragraph">help this usually helps me when i&#8217;m trying to do this i&#8217;m going to take the prototype and just sort</p>



<p class="wp-block-paragraph">and just sort of like paste it right where I&#8217;m about to call the function.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do this just to remind myself of what I&#8217;m actually calling.</p>



<p class="wp-block-paragraph">Let me add that to my solution, by the way.</p>



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



<p class="wp-block-paragraph">So we&#8217;re going to call myCPP function,</p>



<p class="wp-block-paragraph">which means it&#8217;s expecting some registers to be loaded up with arguments.</p>



<p class="wp-block-paragraph">If we don&#8217;t actually load up anything right now,</p>



<p class="wp-block-paragraph">it&#8217;ll probably do some sort of nonsense.</p>



<p class="wp-block-paragraph">Let&#8217;s actually see what happens right now.</p>



<p class="wp-block-paragraph">If we don&#8217;t load up the appropriate registers,</p>



<p class="wp-block-paragraph">then C++ will still look at those registers expecting to see valid data.</p>



<p class="wp-block-paragraph">Let&#8217;s see what happens.</p>



<p class="wp-block-paragraph">I don&#8217;t know if it&#8217;s going to be good or bad.</p>



<p class="wp-block-paragraph">This would probably be something called undefined behavior,</p>



<p class="wp-block-paragraph">meaning you did something wrong,</p>



<p class="wp-block-paragraph">and probably sometimes your program will work,</p>



<p class="wp-block-paragraph">and sometimes it won&#8217;t.</p>



<p class="wp-block-paragraph">Sometimes you won&#8217;t understand what&#8217;s going on.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do this right now,</p>



<p class="wp-block-paragraph">and it&#8217;s saying, oh, it&#8217;s segfaulted.</p>



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



<p class="wp-block-paragraph">Why did it segfault?</p>



<p class="wp-block-paragraph">all right well I guess maybe because I did something naughty I don&#8217;t know</p>



<p class="wp-block-paragraph">if this say faults by the end we&#8217;re going to be in trouble I&#8217;ll have to debug on camera</p>



<p class="wp-block-paragraph">so uh we&#8217;re going to enter into my cpp function and then it says we got a variable a</p>



<p class="wp-block-paragraph">which was a long we didn&#8217;t give it a one a one was just probably sitting in there</p>



<p class="wp-block-paragraph">before we even called that function so like one is definitely not it and then b was like some</p>



<p class="wp-block-paragraph">floating point number and then C was just like another seemingly random value</p>



<p class="wp-block-paragraph">that kind of looks a little bit more like a pointer I&#8217;m not really sure it&#8217;s</p>



<p class="wp-block-paragraph">probably not but it&#8217;s just some junk data coming from somewhere and then D</p>



<p class="wp-block-paragraph">which I think D was supposed to be a character pointer it says not a number</p>



<p class="wp-block-paragraph">so we just got like a a really bad value for D let me upgrade this real fast</p>



<p class="wp-block-paragraph">because maybe we should be printing what printing it as a memory location so E is</p>



<p class="wp-block-paragraph">So E is, let me bring this down real fast.</p>



<p class="wp-block-paragraph">So E is supposed to be a character pointer.</p>



<p class="wp-block-paragraph">Let me, instead of printing the C string as just like itself, let&#8217;s first print the memory location.</p>



<p class="wp-block-paragraph">And I&#8217;ll just say memory.</p>



<p class="wp-block-paragraph">And then we&#8217;ll do a static cast.</p>



<p class="wp-block-paragraph">Oh, I wonder if this will actually change anything.</p>



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



<p class="wp-block-paragraph">Let&#8217;s static cast both of them just to see what happens.</p>



<p class="wp-block-paragraph">print e and uh c string here and then it&#8217;s going to be um</p>



<p class="wp-block-paragraph">hmm no i don&#8217;t think that&#8217;s actually going to change anything</p>



<p class="wp-block-paragraph">because if i cast it as a point as a character that&#8217;s definitely wrong</p>



<p class="wp-block-paragraph">and if i don&#8217;t cast it then it&#8217;s going to show up as its original data type how do i get the memory</p>



<p class="wp-block-paragraph">location hmm oh i know what to do i can static cast it maybe as a as an unsigned long okay</p>



<p class="wp-block-paragraph">and unsigned long. Okay, so a character pointer, we&#8217;ll leave that for the C string. Unsigned long,</p>



<p class="wp-block-paragraph">long, just in case. And I think I should see the memory location first and then the actual C</p>



<p class="wp-block-paragraph">string later. If not, then whatever. Let&#8217;s try one more time. Oh dear. Invalid static cast from</p>



<p class="wp-block-paragraph">a character pointer to a type long, long unsigned integer. What have I done? How about unsigned long?</p>



<p class="wp-block-paragraph">long see if that works character pointer long int i guess i forgot how to cast pointers to longs</p>



<p class="wp-block-paragraph">i&#8217;ll look that up and post another video in the future but i guess for now we&#8217;ll just</p>



<p class="wp-block-paragraph">we&#8217;ll deal with this humiliation and i&#8217;ll just print e by itself</p>



<p class="wp-block-paragraph">all right so i&#8217;m just going to run it one more time it should say fault again</p>



<p class="wp-block-paragraph">is wrong let&#8217;s uh let&#8217;s just fix the arguments so before we make that call we should load up some</p>



<p class="wp-block-paragraph">stuff right so i&#8217;m going to do maybe like a semicolon comment here and then i&#8217;m going to do</p>



<p class="wp-block-paragraph">what else do i have i&#8217;ve got three move instructions so the first argument is going to get</p>



<p class="wp-block-paragraph">77118 the second argument is going to get 1111 and the third argument looks like it&#8217;s going to</p>



<p class="wp-block-paragraph">get a pointer that i&#8217;ve defined inside of the assembly module so if you look back up at the</p>



<p class="wp-block-paragraph">look back up at the top let&#8217;s see message string inside asm message string inside asm so basically</p>



<p class="wp-block-paragraph">i&#8217;m going to be sending a character pointer to this t right here which is just this string says</p>



<p class="wp-block-paragraph">this string is owned by the assembly module and it&#8217;s a null terminated string which uh c definitely</p>



<p class="wp-block-paragraph">needs in order to print correctly if we took that zero off we&#8217;d probably get a bunch of junk data</p>



<p class="wp-block-paragraph">we might crash the program i don&#8217;t know so anyway basically we&#8217;re just going to be saying you know</p>



<p class="wp-block-paragraph">we&#8217;re just going to be saying, you know, here you go. Here&#8217;s a pointer. Where the heck am I?</p>



<p class="wp-block-paragraph">Okay. Here&#8217;s a pointer to a C string. And so we&#8217;re giving it one, two, three arguments. We&#8217;re giving</p>



<p class="wp-block-paragraph">it the A and the C and the E. Now we just have to load it up with the other two floats. So</p>



<p class="wp-block-paragraph">I&#8217;m going to do this real fast. Remember the first float argument is going to be XMM zero.</p>



<p class="wp-block-paragraph">And the second one is going to be XMM one. And you can see that send B corresponds to the first</p>



<p class="wp-block-paragraph">corresponds to the first float argument.</p>



<p class="wp-block-paragraph">That&#8217;s why it&#8217;s XMM zero.</p>



<p class="wp-block-paragraph">And then the float send D corresponds</p>



<p class="wp-block-paragraph">to the second argument.</p>



<p class="wp-block-paragraph">That&#8217;s why it&#8217;s XMM one.</p>



<p class="wp-block-paragraph">And that&#8217;s the D right here.</p>



<p class="wp-block-paragraph">And there&#8217;s no other floats.</p>



<p class="wp-block-paragraph">And if we just kind of look back up real fast,</p>



<p class="wp-block-paragraph">the float send B and D,</p>



<p class="wp-block-paragraph">I just defined those arbitrarily</p>



<p class="wp-block-paragraph">to just these two random numbers.</p>



<p class="wp-block-paragraph">So we should see like a two one nine</p>



<p class="wp-block-paragraph">and then a nine nine eight eight seven seven six</p>



<p class="wp-block-paragraph">sort of, you know, weird number.</p>



<p class="wp-block-paragraph">number then when we&#8217;re done loading all those things you know all of these registers up now</p>



<p class="wp-block-paragraph">the function should be able to receive something i bet you the reason it crashes because we</p>



<p class="wp-block-paragraph">we had like a bad address for the the string previously because we didn&#8217;t load it</p>



<p class="wp-block-paragraph">so let me go up here and now it doesn&#8217;t crash so if you just kind of like look we have hard-coded a</p>



<p class="wp-block-paragraph">and b sorry not not a and b a and also c so that&#8217;s the seven seven one and then the bunch of ones</p>



<p class="wp-block-paragraph">of ones and then the string which was rdx which was the e so this string is owned by the assembly</p>



<p class="wp-block-paragraph">module nice we&#8217;re now able to print a string that&#8217;s owned by assembly in another module in</p>



<p class="wp-block-paragraph">the c plus plus module and then xmm0 and xmm1 those are the two floats so if we look back up</p>



<p class="wp-block-paragraph">again that&#8217;s uh we should expect to see these two numbers right here whoops um bloat send b and d</p>



<p class="wp-block-paragraph">and then D. So the printing kind of turned it into scientific notation. That&#8217;s okay. You know,</p>



<p class="wp-block-paragraph">there&#8217;s other stuff you can do in C++ to just not print in scientific notation, but for now,</p>



<p class="wp-block-paragraph">I don&#8217;t really care. It&#8217;s basically the same value. So it&#8217;s fine. I just want to show you</p>



<p class="wp-block-paragraph">how to transport data. This is not a C++ video. Anyway, if we go back down to the end of that,</p>



<p class="wp-block-paragraph">so we called the function, but then this function, if you notice, it returns a double,</p>



<p class="wp-block-paragraph">we should expect to see something loaded into the XMM zero register.</p>



<p class="wp-block-paragraph">So I&#8217;m going to move a single piece of data and it&#8217;s going to be a double precision floating</p>



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



<p class="wp-block-paragraph">And I&#8217;m just going to store it into my float got return value global variable,</p>



<p class="wp-block-paragraph">which I defined up above.</p>



<p class="wp-block-paragraph">So let me scroll up real fast.</p>



<p class="wp-block-paragraph">So where is that?</p>



<p class="wp-block-paragraph">Where is that?</p>



<p class="wp-block-paragraph">Float got return value.</p>



<p class="wp-block-paragraph">I initialized it with a zero just because we&#8217;re going to receive something in there.</p>



<p class="wp-block-paragraph">I could have used the BSS section if I wanted to, probably would have been a little bit</p>



<p class="wp-block-paragraph">bit smarter and more performant but i don&#8217;t know i personally don&#8217;t like using the bss section in</p>



<p class="wp-block-paragraph">yasm unless uh i want to allocate an array if i&#8217;m just doing one variable here and there then i&#8217;m</p>



<p class="wp-block-paragraph">just going to put it in the globals or or the data section so float got return value i&#8217;m saving it in</p>



<p class="wp-block-paragraph">there and then uh i want to grab it from xmm zero so that&#8217;s where it comes from when uh my cpp</p>



<p class="wp-block-paragraph">return the double to us inside of XMM0.</p>



<p class="wp-block-paragraph">So I&#8217;m just moving that into my data section into the global variable.</p>



<p class="wp-block-paragraph">And then now that we have finally returned from the C++ module,</p>



<p class="wp-block-paragraph">I&#8217;m just going to announce that we have indeed returned from the C++ module.</p>



<p class="wp-block-paragraph">Let&#8217;s get that far real fast.</p>



<p class="wp-block-paragraph">So this string is owned and then my CPP function exiting.</p>



<p class="wp-block-paragraph">exiting and then now it says the assembly module received this return value from the</p>



<p class="wp-block-paragraph">C++ module and then it says the driver has regained control because we were supposed</p>



<p class="wp-block-paragraph">to print the float and do a line feed after that and we didn&#8217;t so basically assembly just</p>



<p class="wp-block-paragraph">printed this string that was the last part we just added and then it returned right away</p>



<p class="wp-block-paragraph">to the driver so let&#8217;s finish this up so next thing we&#8217;re going to do is we&#8217;re going to</p>



<p class="wp-block-paragraph">move the float value back into XMM0.</p>



<p class="wp-block-paragraph">Remember, anytime you call a function, all your float registers could be destroyed.</p>



<p class="wp-block-paragraph">So it makes sense that we saved XMM0 and then we loaded it only several instructions later</p>



<p class="wp-block-paragraph">because you don&#8217;t know what&#8217;s happening under the hood inside of print null terminated string</p>



<p class="wp-block-paragraph">or any of the system calls that it uses.</p>



<p class="wp-block-paragraph">Your XMM0 is likely destroyed, but you don&#8217;t know for sure, right?</p>



<p class="wp-block-paragraph">If you count on it not being destroyed, you might be introducing undefined behavior to your program.</p>



<p class="wp-block-paragraph">So that&#8217;s not good.</p>



<p class="wp-block-paragraph">Anyway, so we&#8217;re just loading back up from that global into XMM zero.</p>



<p class="wp-block-paragraph">And then we&#8217;re calling that function again to just sort of print the float.</p>



<p class="wp-block-paragraph">And it wants the float as argument zero.</p>



<p class="wp-block-paragraph">So we&#8217;re just sticking into XMM zero.</p>



<p class="wp-block-paragraph">And then we&#8217;re printing a new line.</p>



<p class="wp-block-paragraph">So then when we&#8217;re done here, run it.</p>



<p class="wp-block-paragraph">and now it says the assembly module received this value from the C++ module and notice how it&#8217;s that</p>



<p class="wp-block-paragraph">special value that I hard-coded 112222 whatever so that&#8217;s this value right here</p>



<p class="wp-block-paragraph">and again if we had this cpp function returning along then all we would have to do is look at</p>



<p class="wp-block-paragraph">the RAX register and if we had let&#8217;s let&#8217;s say for some reason that this assembly function we</p>



<p class="wp-block-paragraph">function we wanted to get something from it we could say you know double temp</p>



<p class="wp-block-paragraph">equals that then all we&#8217;d have to do is make sure that we returned sorry we</p>



<p class="wp-block-paragraph">loaded XMM zero with our return value in the assembly module because XMM zero is</p>



<p class="wp-block-paragraph">the return register for floats or if we wanted to do like a long then just load</p>



<p class="wp-block-paragraph">RAX with something at the end of my assembly function and that&#8217;s how C would</p>



<p class="wp-block-paragraph">C and C++ they&#8217;re following the ABI so if you personally don&#8217;t follow the ABI</p>



<p class="wp-block-paragraph">you&#8217;re just going to end up memorizing two different ways of doing things and</p>



<p class="wp-block-paragraph">your way is not going to be compatible with all the other modules written in</p>



<p class="wp-block-paragraph">higher level languages so it&#8217;s kind of a waste of time and your modules won&#8217;t be</p>



<p class="wp-block-paragraph">compatible with other people&#8217;s libraries and functions it&#8217;s just a huge waste of</p>



<p class="wp-block-paragraph">time so just follow the ABI in the first place I&#8217;ve literally known people who</p>



<p class="wp-block-paragraph">said you know what I don&#8217;t want to learn the ABI that&#8217;s dumb then as they wrote</p>



<p class="wp-block-paragraph">they wrote a bunch of assembly they just started thinking like oh this is too confusing because i</p>



<p class="wp-block-paragraph">keep forgetting what registers i was going to pass back and forth between my functions</p>



<p class="wp-block-paragraph">and like a month goes by i come back to an old program i can&#8217;t remember i have to like look</p>



<p class="wp-block-paragraph">through all my code again so then they started accidentally inventing their own little scheme</p>



<p class="wp-block-paragraph">like oh i know what to do i&#8217;ll put the first argument in this register and i&#8217;ll put the</p>



<p class="wp-block-paragraph">second argument in this all they were doing is just reinventing the abi from scratch and then</p>



<p class="wp-block-paragraph">got to that point and then later wasting more time when they realized they just needed to learn the</p>



<p class="wp-block-paragraph">ABI and forget about their way so don&#8217;t let that happen to you and you should probably just try to</p>



<p class="wp-block-paragraph">do it the right way the first time right um a lot of things in coding and computer science in general</p>



<p class="wp-block-paragraph">are just time savers that feel like wastes of time at first okay what else we have anything</p>



<p class="wp-block-paragraph">that I wanted to show you that was actually I think the entirety of that program let&#8217;s run it</p>



<p class="wp-block-paragraph">while I check to see if there&#8217;s any other stuff that I was supposed to show you.</p>



<p class="wp-block-paragraph">So let&#8217;s do this.</p>



<p class="wp-block-paragraph">Everything seems to be working.</p>



<p class="wp-block-paragraph">Let&#8217;s see, I wanted to tell you about labels, return, call,</p>



<p class="wp-block-paragraph">the stack, the return address, prologue and epilog,</p>



<p class="wp-block-paragraph">pushing and popping, respecting the ABI.</p>



<p class="wp-block-paragraph">I wanted to show you that textbook function arguments, mixed arguments,</p>



<p class="wp-block-paragraph">int or pointer return types and float return types.</p>



<p class="wp-block-paragraph">I guess I got through everything without forgetting something huge, which, hey,</p>



<p class="wp-block-paragraph">that&#8217;s probably a first not that i recall at the moment but it probably is</p>



<p class="wp-block-paragraph">anyway so uh i guess that&#8217;s it i hope you feel like function experts now in yasm x86-64 assembly</p>



<p class="wp-block-paragraph">also known as a amd64 assembly in ubuntu okay thank you so much for watching this video i</p>



<p class="wp-block-paragraph">hope you learned a little bit and had a little bit of fun 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. 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 whatever</p>



<p class="wp-block-paragraph">it is you do on the current social media website 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 and grow this community.</p>



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



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



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



<p class="wp-block-paragraph">with joy. That&#8217;s exactly what happens every single time. So you could do it as a nice favor to me or</p>



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



<p class="wp-block-paragraph">and then I&#8217;ll just wake up. I promise that&#8217;s what will happen. Also, if you look at the middle of</p>



<p class="wp-block-paragraph">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</p>



<p class="wp-block-paragraph">somewhere at the bottom of this video and it&#8217;ll take you to my main website</p>



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



<p class="wp-block-paragraph">and tutorials and things that I offer and all that good stuff and if you have</p>



<p class="wp-block-paragraph">a suggestion for clarifications or errata or just future videos that you</p>



<p class="wp-block-paragraph">comment or if you just want to say hey what&#8217;s up what&#8217;s going on you know just send me a comment</p>



<p class="wp-block-paragraph">whatever 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</p>



<p class="wp-block-paragraph">like it would really it really mean the world to me i would really appreciate it so again thank you</p>



<p class="wp-block-paragraph">so much for watching this video and um enjoy the cool music as as i fade into the darkness</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/yasm-x86-64-assembly-functions-tutorial-integers-floats-pointers-abi-c-interoperability/">YASM x86-64 Assembly Functions Tutorial &#8211; Integers, Floats, Pointers, ABI, C++ Interoperability</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/yasm-x86-64-assembly-functions-tutorial-integers-floats-pointers-abi-c-interoperability/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>x86-64 Assembly: Local Variables, Stack Frames &#038; Alignment Explained</title>
		<link>https://www.NeuralLantern.com/x86-64-assembly-local-variables-stack-frames-alignment-explained/</link>
					<comments>https://www.NeuralLantern.com/x86-64-assembly-local-variables-stack-frames-alignment-explained/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 16 Mar 2026 06:39:32 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[16 byte alignment]]></category>
		<category><![CDATA[assembly array on stack]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[assembly stack allocation]]></category>
		<category><![CDATA[calling convention]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[local variables stack]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[NASM tutorial]]></category>
		<category><![CDATA[printf crash assembly]]></category>
		<category><![CDATA[rbp rbp stack]]></category>
		<category><![CDATA[reverse engineering]]></category>
		<category><![CDATA[stack alignment]]></category>
		<category><![CDATA[stack frame assembly]]></category>
		<category><![CDATA[sub rsp assembly]]></category>
		<category><![CDATA[systems programming]]></category>
		<category><![CDATA[x64 assembly]]></category>
		<category><![CDATA[x86 assembly tutorial]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=317</guid>

					<description><![CDATA[<p>Learn how local variables are allocated on the stack in x86-64 assembly using YASM. See exactly how to reserve space, address variables with offsets, understand downward stack growth, and fix mysterious printf segfaults caused by 16-byte stack misalignment. Includes two alignment solutions and a complete working example with a local integer array.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-local-variables-stack-frames-alignment-explained/">x86-64 Assembly: Local Variables, Stack Frames &amp; Alignment Explained</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: Local Variables, Stack Frames &amp; Alignment Explained" width="1380" height="776" src="https://www.youtube.com/embed/woCCcOlopNo?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">Quick but deep look at local variables on the stack in x86-64 assembly (YASM). We cover allocation with sub rsp, accessing via offsets, why the stack must be 16-byte aligned when calling libc functions like printf, and two practical ways to fix alignment crashes. Includes live segfault debugging and a full working example with a local array.</p>



<p class="wp-block-paragraph">Great for anyone studying systems programming, computer architecture, or just trying to figure out why their assembly program randomly crashes on a library call.</p>



<p class="wp-block-paragraph">Like + subscribe if you want more clear, practical assembly tutorials!</p>



<p class="wp-block-paragraph">00:00 Introduction to Local Variables on the Stack<br>00:28 C++ Example of Function and Local Variables<br>01:06 Incoming Arguments in RDI vs Stack Variables<br>03:24 Pointers as Local Variables on Stack<br>04:34 Why the Stack – Recursion and Multiple Calls<br>05:18 Visualizing Multiple Stack Frames<br>09:24 How Function Returns Adjust RSP<br>10:35 Stack Grows Downward in Memory<br>11:33 Program Setup – Hybrid C++/Assembly<br>12:46 Assembly Module Overview<br>14:00 Function Prologue – Register Push &amp; Alignment<br>15:30 Allocating Stack Space for Local Array<br>17:45 Initializing Array in Loop<br>20:10 Printing Loop with printf<br>49:48 First Run – Segfault Observed<br>51:00 16-Byte Stack Alignment Requirement<br>51:55 Fix 1 – Extra Push/Pop in Prologue/Epilogue<br>53:15 Fix 2 – Push/Pop Around Each printf Call<br>55:04 Testing Different Array Sizes<br>56:52 Debugging Alignment Behavior<br>58:54 Summary – Creating Any Local Data on Stack<br>59:59 Closing Remarks &amp; Subscribe Call</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 the Blog: https://www.NeuralLantern.com</li>



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



<p class="wp-block-paragraph">Hi there! Today I&#8217;d like to talk to you about local variables on the stack</p>



<p class="wp-block-paragraph">in an x8664 assembly program written in YASM.</p>



<p class="wp-block-paragraph">If you don&#8217;t understand assembly or local variables or other things like that you might</p>



<p class="wp-block-paragraph">want to see my other videos but I&#8217;m just going to give you a quick example. So what am I talking</p>



<p class="wp-block-paragraph">about with local variables on the stack? For starters forget what you&#8217;re seeing here for a</p>



<p class="wp-block-paragraph">blank code page and pretend that we&#8217;re coding in c plus plus just for a moment this is an assembly</p>



<p class="wp-block-paragraph">video but pretend this is c plus plus so suppose you have c plus plus you have a main function here</p>



<p class="wp-block-paragraph">and at the end of it you know this is your entry point for your program we return zero and maybe</p>



<p class="wp-block-paragraph">above it there&#8217;s a function called f i&#8217;m putting it above because i don&#8217;t want to use prototypes</p>



<p class="wp-block-paragraph">in this video but you should probably use prototypes um maybe main calls on f and so then</p>



<p class="wp-block-paragraph">what happens? I don&#8217;t know. Maybe we have some arguments. We&#8217;ll call this integer argument A</p>



<p class="wp-block-paragraph">that comes in. If you&#8217;ve watched my other videos, hopefully you have by now,</p>



<p class="wp-block-paragraph">you&#8217;ll know that A comes into the function as RDI because that&#8217;s the first integer argument</p>



<p class="wp-block-paragraph">register. But then when we start creating local variables, we&#8217;ll say integer B equals, let&#8217;s say,</p>



<p class="wp-block-paragraph">a five and then integer C is equal to an eight for whatever reason. Maybe there will be an array.</p>



<p class="wp-block-paragraph">So I&#8217;ll call this a int array, I guess.</p>



<p class="wp-block-paragraph">We&#8217;ll say that we have 100 integers in our array.</p>



<p class="wp-block-paragraph">We could also have a pointer, int pointer p,</p>



<p class="wp-block-paragraph">and then allocate it to some kind of new memory just to prove a point.</p>



<p class="wp-block-paragraph">And then, you know, later in your function,</p>



<p class="wp-block-paragraph">you&#8217;re probably going to want to do something with your data.</p>



<p class="wp-block-paragraph">So I don&#8217;t know, maybe a, how about like b is plus equal to a,</p>



<p class="wp-block-paragraph">of C++ and then oh C++ C is plus equal to B for some reason I&#8217;m just making random nonsense up</p>



<p class="wp-block-paragraph">honestly I&#8217;m just showing you that we use our variables and then maybe you want to build an</p>



<p class="wp-block-paragraph">array so we just declared the array up here on line six but maybe you want to actually do something</p>



<p class="wp-block-paragraph">with it you want to fill it with data maybe so I&#8217;ll do size type I is zero keep going until I</p>



<p class="wp-block-paragraph">is less than as long as I is less than 100 I plus plus and now I&#8217;m going through every single index</p>



<p class="wp-block-paragraph">array maybe I&#8217;ll say the array at index i is equal to c and then we&#8217;ll just say c is plus equal to b</p>



<p class="wp-block-paragraph">and then we&#8217;ll do b plus plus just to have something in there okay so let me explain the</p>



<p class="wp-block-paragraph">parts of your program real fast if you haven&#8217;t watched my other videos please do because this</p>



<p class="wp-block-paragraph">should help a little bit anyway so a is an incoming argument like I said before that&#8217;s</p>



<p class="wp-block-paragraph">usually coming to you in the RDI register because it&#8217;s the first integer argument so we don&#8217;t need</p>



<p class="wp-block-paragraph">to worry too much about that we know that a is actually just a register in your CPU and b that&#8217;s</p>



<p class="wp-block-paragraph">created on the stack and c is created on the stack also and the array is created on the stack by the</p>



<p class="wp-block-paragraph">way I could name the array anything if I wanted to like just v I&#8217;m using the name array because</p>



<p class="wp-block-paragraph">Notice also that we have a pointer here. I just wanted to make a point that the pointers we make in our function are considered local variables.</p>



<p class="wp-block-paragraph">And that means they do sit on the stack because what I&#8217;m trying to say is that local variables that are not arguments, they sit on the stack.</p>



<p class="wp-block-paragraph">But the memory we allocated and then gave to the pointer, that&#8217;s sitting in the heap somewhere.</p>



<p class="wp-block-paragraph">So when you use the new operator or the malloc operator, or you&#8217;re just like making dynamic memory,</p>



<p class="wp-block-paragraph">that dynamic memory sits in the heap, but the pointer itself, since we just declared it here,</p>



<p class="wp-block-paragraph">that sits on the stack. That means later on when your function ends, the p pointer itself will get</p>



<p class="wp-block-paragraph">cleaned up automatically and itself won&#8217;t be leaked memory. But if you forget to clean up your memory</p>



<p class="wp-block-paragraph">here, like I did, you&#8217;ll have a memory leak. But I&#8217;m going to talk about memory leaks probably in</p>



<p class="wp-block-paragraph">some other videos somewhere else at another time. Anyway, so then we have our local variables here.</p>



<p class="wp-block-paragraph">remember B and C and array are all on the stack and well we just have our</p>



<p class="wp-block-paragraph">for loop here where we just kind of start modifying data so why would we use</p>



<p class="wp-block-paragraph">the stack let me just do a quick tutorial this is not a stack video I&#8217;m</p>



<p class="wp-block-paragraph">gonna make a stack video in the future but I&#8217;ll just do like a quick little</p>



<p class="wp-block-paragraph">rundown of what is the stack and why is it really good for function calls</p>



<p class="wp-block-paragraph">consider that sometimes F might want to call itself maybe it&#8217;s a recursive</p>



<p class="wp-block-paragraph">be like a long chain of function calls like f calls g and g calls h and h calls you know whatever</p>



<p class="wp-block-paragraph">and it just goes on and on and on as long as it&#8217;s not infinite recursion like all the functions are</p>



<p class="wp-block-paragraph">calling themselves in one circle that never ends you should be allowed to do that we should be</p>



<p class="wp-block-paragraph">allowed to have a function f that sometimes calls itself or other functions or maybe it&#8217;s called</p>



<p class="wp-block-paragraph">multiple times within our giant call stack our call graph and this will work because these</p>



<p class="wp-block-paragraph">you know a b and c and array are sitting on the stack and the stack allows you to have sort of</p>



<p class="wp-block-paragraph">different instances of variables so for example and this is just a quick thing i&#8217;m not this is</p>



<p class="wp-block-paragraph">not supposed to be a full stack tutorial here but um you know if we have like the function f</p>



<p class="wp-block-paragraph">and let&#8217;s say we call it at some point maybe uh maybe main calls f uh and then let&#8217;s let&#8217;s pretend</p>



<p class="wp-block-paragraph">f and then f calls h and then maybe h calls f and um did i write an h there no i think that&#8217;s</p>



<p class="wp-block-paragraph">supposed to be a parenthesis i&#8217;m sorry i have bad penmanship oh god it&#8217;s even worse hang on let me</p>



<p class="wp-block-paragraph">let me do another h here i&#8217;ll just do it i&#8217;ll just do a g about that f g and then uh</p>



<p class="wp-block-paragraph">F again. Let&#8217;s just pretend that we can have a call graph where F is sometimes called by something</p>



<p class="wp-block-paragraph">and sometimes called something else or maybe sometimes F is called by a different thing. So</p>



<p class="wp-block-paragraph">like I guess this was supposed to be the G function. So maybe that&#8217;s why down here was the H.</p>



<p class="wp-block-paragraph">Should I delete this video? I don&#8217;t know. You know what? I&#8217;m going to start. I&#8217;m going to delete this</p>



<p class="wp-block-paragraph">and then G calls H and then H calls F and then F calls I I guess and then</p>



<p class="wp-block-paragraph">eventually these functions start returning right oops how come my greens</p>



<p class="wp-block-paragraph">not working green there we go too late now so we can have like a really</p>



<p class="wp-block-paragraph">complicated call graph and F might appear in there multiple times and the</p>



<p class="wp-block-paragraph">Each call to F along the call graph should have its own copy, unique copy of local variables.</p>



<p class="wp-block-paragraph">So the B for this first F call, let me just do an arrow here, will have a B.</p>



<p class="wp-block-paragraph">And you can imagine it as B subscript 1.</p>



<p class="wp-block-paragraph">And down here when F executes, you can imagine that it&#8217;s got its own copy of B.</p>



<p class="wp-block-paragraph">So we can imagine this as B subscript 2.</p>



<p class="wp-block-paragraph">So these are two totally different Bs.</p>



<p class="wp-block-paragraph">If you tried to use global variables for this, it would be really, really, really hard to get the code to work.</p>



<p class="wp-block-paragraph">get the code to work and it would be really, really hard to debug.</p>



<p class="wp-block-paragraph">So that&#8217;s kind of why we have local variables.</p>



<p class="wp-block-paragraph">The stack allows us to do this.</p>



<p class="wp-block-paragraph">So what is the stack itself?</p>



<p class="wp-block-paragraph">Again, this is not a full tutorial on the stack,</p>



<p class="wp-block-paragraph">but I just want you to kind of see what&#8217;s happening.</p>



<p class="wp-block-paragraph">Imagine a data structure that kind of grows upward.</p>



<p class="wp-block-paragraph">I&#8217;ll say that here&#8217;s the floor at the bottom.</p>



<p class="wp-block-paragraph">And when you put an item onto the stack,</p>



<p class="wp-block-paragraph">the items kind of stack on top of each other.</p>



<p class="wp-block-paragraph">So imagine that this is the call to F.</p>



<p class="wp-block-paragraph">is the call to f and embedded within this little stack frame area we&#8217;ll call it a stack frame but</p>



<p class="wp-block-paragraph">actually the stack continues to grow as you create and destroy local variables within a function call</p>



<p class="wp-block-paragraph">we&#8217;ll just imagine that there&#8217;s i don&#8217;t know a mini stack sitting inside of the stack that</p>



<p class="wp-block-paragraph">contains all the local variables i&#8217;ll just put i&#8217;m not going to put a because a is an argument so</p>



<p class="wp-block-paragraph">it&#8217;s sitting in a register but we could do dude i got to learn how to how to draw with this tablet</p>



<p class="wp-block-paragraph">We got to do B and C and the array.</p>



<p class="wp-block-paragraph">I&#8217;ll put AR for array.</p>



<p class="wp-block-paragraph">Sitting in, you know, their own little spots on the stack within the major portion of the stack that is designated for that first F call.</p>



<p class="wp-block-paragraph">And then, you know, maybe F calls G and some other stuff happens.</p>



<p class="wp-block-paragraph">I&#8217;ll just pretend that a bunch of other stuff happened.</p>



<p class="wp-block-paragraph">And then eventually F is called again.</p>



<p class="wp-block-paragraph">But there&#8217;s a different, this is a different instance of the call to F.</p>



<p class="wp-block-paragraph">it&#8217;s got its own little area that is separate from the the previous call to f and again we&#8217;ll</p>



<p class="wp-block-paragraph">also have variables that we can create locally that are supposed to be separate from the original</p>



<p class="wp-block-paragraph">variables oh god that&#8217;s awful and ugly i need to maybe decrease the size of my eraser or something</p>



<p class="wp-block-paragraph">but imagine this is uh you know these are two separate copies so that&#8217;s like what i said before</p>



<p class="wp-block-paragraph">when we have like a b1 and a b2 basically they&#8217;re not going to be called b1 and b2 but they&#8217;re just</p>



<p class="wp-block-paragraph">They&#8217;re not going to be called B1 and B2, but they&#8217;re just two separate instances.</p>



<p class="wp-block-paragraph">And I just want you to know.</p>



<p class="wp-block-paragraph">So then when we start returning from functions, like when this F eventually returns, and by</p>



<p class="wp-block-paragraph">the way, I know that what I&#8217;m drawing on the right doesn&#8217;t match the code because this</p>



<p class="wp-block-paragraph">is not a function that calls itself, but just suppose that your code is a lot more complicated</p>



<p class="wp-block-paragraph">than what I drew up.</p>



<p class="wp-block-paragraph">When it eventually returns, all that happens is those items on the stack just sort of get</p>



<p class="wp-block-paragraph">not necessarily deallocated, but ignored.</p>



<p class="wp-block-paragraph">We&#8217;ll just say that they&#8217;re gone.</p>



<p class="wp-block-paragraph">They&#8217;re still sitting as junk data in system RAM somewhere.</p>



<p class="wp-block-paragraph">And in assembly talk, we know that we have a stack pointer called RSP.</p>



<p class="wp-block-paragraph">We have a register called RSP that just sort of points to the location in the</p>



<p class="wp-block-paragraph">stack that is considered the top of the stack, like the most recent piece of data</p>



<p class="wp-block-paragraph">that we have available.</p>



<p class="wp-block-paragraph">So all the other data is actually still kind of above, but we&#8217;re not pointing to it</p>



<p class="wp-block-paragraph">anymore, so we consider that it doesn&#8217;t exist.</p>



<p class="wp-block-paragraph">So then when G eventually returns, you know, we just change the stack pointer,</p>



<p class="wp-block-paragraph">Rsp, to point to that other piece of data.</p>



<p class="wp-block-paragraph">The G data frame and the other F data frame are still sitting above somewhere in memory,</p>



<p class="wp-block-paragraph">but we just ignore them, right?</p>



<p class="wp-block-paragraph">So that&#8217;s how the stack works.</p>



<p class="wp-block-paragraph">And that&#8217;s how we have local function call copies of all of our local variables.</p>



<p class="wp-block-paragraph">Something to note.</p>



<p class="wp-block-paragraph">Something to note, this is not a stack video, but you know, just something to note that</p>



<p class="wp-block-paragraph">even though I draw the stack visually as growing vertically up, when you actually manipulate</p>



<p class="wp-block-paragraph">the stack in assembly or just like in any language, the stack grows downward in terms</p>



<p class="wp-block-paragraph">of memory locations.</p>



<p class="wp-block-paragraph">So you can imagine, I&#8217;m trying so hard not to make this like a huge stack video.</p>



<p class="wp-block-paragraph">Imagine this is a memory location 80, we&#8217;ll say.</p>



<p class="wp-block-paragraph">You would think that memory location 81 would be the next item of the stack, or I guess</p>



<p class="wp-block-paragraph">the stack or i guess if you if you&#8217;re considering the fact that the items on the stack are quad words</p>



<p class="wp-block-paragraph">we would say it goes up to 88 but that&#8217;s not true it goes down to 72 so the memory location goes down</p>



<p class="wp-block-paragraph">even though we imagine the stack growing uh upward vertically just so you know that&#8217;s the kind of</p>



<p class="wp-block-paragraph">thing we&#8217;re going to do so what i&#8217;m going to do is just show you an assembly program where we can</p>



<p class="wp-block-paragraph">create local variables and i&#8217;m just going to show you how to create an array because this array is</p>



<p class="wp-block-paragraph">just like a bunch of integers and you can imagine it would be really easy to create only one integer</p>



<p class="wp-block-paragraph">by just imagining an example where the array is a size of one so keep that in mind and i&#8217;m not going</p>



<p class="wp-block-paragraph">to show you malloc or anything like that we&#8217;re just going to look at the local variables okay</p>



<p class="wp-block-paragraph">so for starters i have a make file here that&#8217;s just going to compile a hybrid program if you don&#8217;t</p>



<p class="wp-block-paragraph">know make files or you don&#8217;t know hybrid programs that&#8217;s okay just see my other videos i&#8217;ve explained</p>



<p class="wp-block-paragraph">The first source code file here is just driver.cpp.</p>



<p class="wp-block-paragraph">Again, this is a hybrid program,</p>



<p class="wp-block-paragraph">so I&#8217;m going to mix my C++ modules with my assembly modules,</p>



<p class="wp-block-paragraph">which is pretty cool.</p>



<p class="wp-block-paragraph">The whole point of the driver is just to contain the entry point, you know, main.</p>



<p class="wp-block-paragraph">And I&#8217;m just going to print a hello message.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to call the real function that I&#8217;m interested in,</p>



<p class="wp-block-paragraph">which I&#8217;ve named local underscore varrs.</p>



<p class="wp-block-paragraph">And that&#8217;s going to be all the assembly stuff that we talked about.</p>



<p class="wp-block-paragraph">block so that C++ can call an assembly module that&#8217;s explained in other videos. And then</p>



<p class="wp-block-paragraph">here&#8217;s the real heart of what we got to do. Let&#8217;s write up an assembly module</p>



<p class="wp-block-paragraph">that can do local variables. Okay. So again, if you don&#8217;t know assembly, that&#8217;s okay,</p>



<p class="wp-block-paragraph">but you need to watch my other videos before you can understand this one. So I&#8217;m going to just</p>



<p class="wp-block-paragraph">copy paste some starter code here. This is Yasm assembly in x86-64. So I&#8217;ve got a data section up</p>



<p class="wp-block-paragraph">So I&#8217;ve got a data section up top and I&#8217;m just going to define some messages.</p>



<p class="wp-block-paragraph">So, you know, I&#8217;ve got like an intro message that just says, hello, I&#8217;m so and so.</p>



<p class="wp-block-paragraph">And that&#8217;s not my name, but I like those kinds of names.</p>



<p class="wp-block-paragraph">And then over here, I&#8217;m going to do some printf formatted strings.</p>



<p class="wp-block-paragraph">That&#8217;s why I&#8217;m using a hybrid program for this video.</p>



<p class="wp-block-paragraph">I don&#8217;t want to import my own personal library.</p>



<p class="wp-block-paragraph">I want you to be able to do this at home with just the GCC libraries.</p>



<p class="wp-block-paragraph">link a hybrid library you know linking against gcc instead of linking against ld again if you don&#8217;t</p>



<p class="wp-block-paragraph">know that stuff check my other videos then we&#8217;re allowed to call c functions in this case we&#8217;re</p>



<p class="wp-block-paragraph">going to call printf and we&#8217;re just going to give it the string percent lu meaning i would like you</p>



<p class="wp-block-paragraph">to print just you know an unsigned long integer so i&#8217;m going to give it a value at some point on the</p>



<p class="wp-block-paragraph">stack representing a local variable and then i want it to print as just like a long like a string</p>



<p class="wp-block-paragraph">along like a string that a human can read then after that this is the carriage return line feed</p>



<p class="wp-block-paragraph">the crlf printf won&#8217;t flush its output unless that is sitting at the very end of the string so</p>



<p class="wp-block-paragraph">i&#8217;m just going to use printf to also print my new lines and then i&#8217;m going to null terminate the</p>



<p class="wp-block-paragraph">string so that printf doesn&#8217;t freak out and try to print a bunch of stuff after the crlf and uh</p>



<p class="wp-block-paragraph">oh i this was from another video let me get rid of that we don&#8217;t really need crlf in this video</p>



<p class="wp-block-paragraph">CRLF in this video because we&#8217;re just putting it directly inside of the printf string we&#8217;re not</p>



<p class="wp-block-paragraph">making our own function to do that so then I&#8217;m going to make some defines I&#8217;m going to define</p>



<p class="wp-block-paragraph">that we&#8217;re going to have 50 integers so I&#8217;m calling this define 50 I&#8217;m calling it num integers</p>



<p class="wp-block-paragraph">and I&#8217;m saying that it has a value of 50 so I want to make an array that has 50 integers I don&#8217;t</p>



<p class="wp-block-paragraph">know maybe if you want to imagine 100 you know like the example that I just showed I&#8217;m going to</p>



<p class="wp-block-paragraph">going to define what is the integer size so i&#8217;m going to use quad words which are 64 bit integers</p>



<p class="wp-block-paragraph">so i&#8217;m just going to say that there are eight bytes per integer that will help me multiply later</p>



<p class="wp-block-paragraph">and then i&#8217;m going to decide to fill up the array on the stack with just some numbers just to prove</p>



<p class="wp-block-paragraph">that i can just to prove that we can like uh you know manipulate and and fetch the values</p>



<p class="wp-block-paragraph">of all this stuff going on in the stack and i&#8217;m going to say that the starting value is seven so</p>



<p class="wp-block-paragraph">with this we should expect to see like an array of numbers that starts with seven and it just</p>



<p class="wp-block-paragraph">kind of increases somehow then i&#8217;m going to do some system call codes we talked about that in</p>



<p class="wp-block-paragraph">a different video and then some file descriptors i don&#8217;t think we actually need anything but standard</p>



<p class="wp-block-paragraph">output but i put it in there anyway then the next thing we&#8217;re going to add is the text section</p>



<p class="wp-block-paragraph">let me just do copy paste on my solution here so here&#8217;s the text section section text</p>



<p class="wp-block-paragraph">section text in Yasm and I&#8217;m going to let my module know that I want to be able to call printf</p>



<p class="wp-block-paragraph">which is a function sitting in the GCC libraries when I link against a GCC I have the ability to</p>



<p class="wp-block-paragraph">do that that way I don&#8217;t have to come up with like a complicated printing method or use one</p>



<p class="wp-block-paragraph">of my own shared libraries or something so we can just ask printf to do everything so now here&#8217;s the</p>



<p class="wp-block-paragraph">entry point for the module it&#8217;s just a function called local VAERS I mark it as global so it&#8217;s</p>



<p class="wp-block-paragraph">it&#8217;s accessible to outside modules ie or eg driver dot cpp and then so here&#8217;s the label saying the</p>



<p class="wp-block-paragraph">function starts and here&#8217;s the return statement saying that we&#8217;re done with the function i&#8217;m not</p>



<p class="wp-block-paragraph">going to manipulate any registers inside of the function so i don&#8217;t really need to do any push pop</p>



<p class="wp-block-paragraph">to preserve them first thing i&#8217;m going to do is call a welcome let me comment this part out by</p>



<p class="wp-block-paragraph">the way i&#8217;m going to call a welcome a function and the whole job of the welcome function is just to</p>



<p class="wp-block-paragraph">is just to, you know, print a welcome message to the user.</p>



<p class="wp-block-paragraph">So nothing that I haven&#8217;t talked about before in other videos.</p>



<p class="wp-block-paragraph">So it&#8217;s just we&#8217;re using a system call to print a string.</p>



<p class="wp-block-paragraph">Okay, so with that in mind, let me open this up here and see if this is going to work.</p>



<p class="wp-block-paragraph">I just want to basically print the welcome message at this point.</p>



<p class="wp-block-paragraph">Clear and make run.</p>



<p class="wp-block-paragraph">And again, if you don&#8217;t know make files or anything like that, see my other videos.</p>



<p class="wp-block-paragraph">So this is the driver, I think, that prints.</p>



<p class="wp-block-paragraph">think that Prince maybe I should change the driver&#8217;s message to make it more</p>



<p class="wp-block-paragraph">clear hello about this is the driver and my name is whatever I&#8217;m gonna do it again</p>



<p class="wp-block-paragraph">and now it says hello this is the driver okay so that&#8217;s the CPP module and then</p>



<p class="wp-block-paragraph">here is the assembly module and then finally the driver gets control back and</p>



<p class="wp-block-paragraph">then the program exits so nothing really happened so now let&#8217;s upgrade the</p>



<p class="wp-block-paragraph">So now let&#8217;s upgrade the assembly module a little bit.</p>



<p class="wp-block-paragraph">Next thing I want to add is the actual demo function, which is going to be absolutely huge.</p>



<p class="wp-block-paragraph">So first I&#8217;m going to start off with, how about just the signature here?</p>



<p class="wp-block-paragraph">Let&#8217;s go right after the welcome module.</p>



<p class="wp-block-paragraph">And I&#8217;m just going to copy paste the signature, put a return at the end of it.</p>



<p class="wp-block-paragraph">So we&#8217;ll consider this a function that can be called.</p>



<p class="wp-block-paragraph">instruction on line 47 of the entry point and then now we have a demo</p>



<p class="wp-block-paragraph">function that is being called but it does nothing for starters you know I in</p>



<p class="wp-block-paragraph">my comments I like to put the signature of the function and I like to remind</p>



<p class="wp-block-paragraph">myself of how I&#8217;m using my registers hopefully I&#8217;m not using the same</p>



<p class="wp-block-paragraph">register in two different ways but you know sometimes it happens if it does and</p>



<p class="wp-block-paragraph">I&#8217;m able to break my function up into multiple parts I&#8217;ll probably do it</p>



<p class="wp-block-paragraph">you know with modular thinking modular programming but in this case I&#8217;m just</p>



<p class="wp-block-paragraph">using these registers it&#8217;s fine to use them they are all designated per the ABI</p>



<p class="wp-block-paragraph">as callee saved so that means the callee which is the demo function is</p>



<p class="wp-block-paragraph">responsible for preserving them so if you don&#8217;t remember that or if you don&#8217;t</p>



<p class="wp-block-paragraph">know about that see my other videos this is not an ABI video so I&#8217;m just going to</p>



<p class="wp-block-paragraph">push all of them to make sure that they&#8217;re preserved I&#8217;m gonna call this</p>



<p class="wp-block-paragraph">call this the prolog then at the end of my function i&#8217;m going to call this the epilog where</p>



<p class="wp-block-paragraph">i just restore them in reverse order because that&#8217;s the way the stack returns data to you</p>



<p class="wp-block-paragraph">the stack returns data to you in reverse order so i have to pop in the reverse register order to</p>



<p class="wp-block-paragraph">un-reverse the reversal if that makes sense pop r13 pop r12 okay so i think 15 14 13 okay</p>



<p class="wp-block-paragraph">so i got that next thing we&#8217;ll do is um let&#8217;s remember where the stack pointer started because</p>



<p class="wp-block-paragraph">started because we have our register here that we&#8217;re going to mess with let me just type rsp</p>



<p class="wp-block-paragraph">real fast so this is the stack pointer register rsp this helps all programs know where where</p>



<p class="wp-block-paragraph">they&#8217;re looking at in the stack all of your functions have to be really really careful about</p>



<p class="wp-block-paragraph">messing with the stack pointer if you do it wrong you will crash the entire program because not only</p>



<p class="wp-block-paragraph">will your local function not really know where its local variables end and begin it probably also</p>



<p class="wp-block-paragraph">It&#8217;s return address when you try to return from the function because that is also sitting on the stack</p>



<p class="wp-block-paragraph">And even if you were lucky enough to be able to jump back correctly to whoever called you</p>



<p class="wp-block-paragraph">If you messed up the stack pointer then you&#8217;ve also messed it up for any caller of you and any of their callers</p>



<p class="wp-block-paragraph">So the whole program is ruined. So we&#8217;ll start off by trying to remember where the stack pointer was</p>



<p class="wp-block-paragraph">We&#8217;ll move</p>



<p class="wp-block-paragraph">The stack pointer into the base pointer</p>



<p class="wp-block-paragraph">didn&#8217;t do here that I that I want to do we should since we&#8217;re messing with the</p>



<p class="wp-block-paragraph">base pointer and other programs sorry other functions or modules might also</p>



<p class="wp-block-paragraph">rely on the base pointer and it&#8217;s considered callie saved we probably also</p>



<p class="wp-block-paragraph">want to preserve that too so I&#8217;m going to do push RBP to basically say I would</p>



<p class="wp-block-paragraph">like to restore I would like to preserve the base pointer so I don&#8217;t mess it up</p>



<p class="wp-block-paragraph">for my callers so that means I have to restore it with the pop so RBP the base</p>



<p class="wp-block-paragraph">The base pointer isn&#8217;t necessarily a pointer to the stack, but it&#8217;s often used as kind of like a bookmark.</p>



<p class="wp-block-paragraph">So we have RBP at the front and the back there.</p>



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



<p class="wp-block-paragraph">Next thing I want to add is, so now that we&#8217;ve restored it, we&#8217;re allowed to just overwrite it because, you know, we&#8217;re kind of like keeping its value at the top.</p>



<p class="wp-block-paragraph">Then we&#8217;re restoring its value at the bottom.</p>



<p class="wp-block-paragraph">And that means we can actually mess it up in the middle if we want to, and it&#8217;ll be fine.</p>



<p class="wp-block-paragraph">let me show you real fast what happens what happens is nothing the program is</p>



<p class="wp-block-paragraph">still okay because we we restored it so now we&#8217;re using the base pointer to</p>



<p class="wp-block-paragraph">remember where the stack was now we&#8217;ve got to sort of calculate how much room</p>



<p class="wp-block-paragraph">we want to make on the stack let me show you what I&#8217;m talking about here</p>



<p class="wp-block-paragraph">remember all of our local variables are going to be on the stack and before we</p>



<p class="wp-block-paragraph">drew this thing where it was like well we&#8217;ve got like a stack sitting here and</p>



<p class="wp-block-paragraph">let&#8217;s just pretend that there&#8217;s some kind of data sitting on the stack data</p>



<p class="wp-block-paragraph">data data right if the stack pointer dude green green there we go oh I erased my</p>



<p class="wp-block-paragraph">green RSP if the stack pointer is currently pointing to this frame then</p>



<p class="wp-block-paragraph">in order for us to make room on the stack to hold our array well if the</p>



<p class="wp-block-paragraph">whole array is going to be sitting on the stack that just basically means</p>



<p class="wp-block-paragraph">have five integers suppose five suppose that we want to make five integers on the stack that just</p>



<p class="wp-block-paragraph">means we need to do five extra slots let me draw it in red here well let&#8217;s see can we get a green</p>



<p class="wp-block-paragraph">no how about a yellow my green is just having a hard time we can do it in red even though this is</p>



<p class="wp-block-paragraph">not a bad thing to do so i&#8217;ll just draw like five extra frames on top of on top of the stack here</p>



<p class="wp-block-paragraph">imagine these are all 64-bit integers and so they take eight bytes on the stack even though in our</p>



<p class="wp-block-paragraph">previous example we were using just regular ints which are 32 we&#8217;ll just say we&#8217;re going to make</p>



<p class="wp-block-paragraph">five 64-bit integers because that&#8217;s easier so they&#8217;re quad words so every frame is actually</p>



<p class="wp-block-paragraph">eight bytes and not just four bytes and it&#8217;s definitely not one byte so we make five of those</p>



<p class="wp-block-paragraph">how do we make five slots it&#8217;s pretty easy we we literally just move the stack pointer let&#8217;s see</p>



<p class="wp-block-paragraph">we just move the stack pointer to or the you know the rsp register to just say let&#8217;s let&#8217;s point you</p>



<p class="wp-block-paragraph">know further out and how do we get that number we&#8217;re just going to multiply the size of one</p>



<p class="wp-block-paragraph">integer so you know the size of one integer here we know it&#8217;s going to be eight bytes we just</p>



<p class="wp-block-paragraph">multiply that by the number of integers that we want you know that&#8217;s going to be 40 so we&#8217;ll just</p>



<p class="wp-block-paragraph">increase uh sorry decrease the stack pointers value by 40 because remember again when the</p>



<p class="wp-block-paragraph">vertically, it&#8217;s actually growing downward in memory. So we&#8217;re going to decrease by 40 there,</p>



<p class="wp-block-paragraph">at least in this drawing example. And that gives us a bunch of junk data, you know, because there</p>



<p class="wp-block-paragraph">is always going to be some kind of a value sitting at every memory location in your computer.</p>



<p class="wp-block-paragraph">It&#8217;s impossible that there is literally nothing at some memory location,</p>



<p class="wp-block-paragraph">unless you&#8217;re trying to go beyond your RAM stick. But then the system will still acknowledge that,</p>



<p class="wp-block-paragraph">you know, you&#8217;ve done that and it won&#8217;t just give you back nothing if you try to read</p>



<p class="wp-block-paragraph">nothing if you try to read.</p>



<p class="wp-block-paragraph">So there&#8217;s going to be junk data sitting on there and then we&#8217;ll loop</p>



<p class="wp-block-paragraph">through all those slots on the stack and we&#8217;ll just modify the data one by one</p>



<p class="wp-block-paragraph">so that we can control what it is instead of just printing whatever junk data we end</p>



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



<p class="wp-block-paragraph">So really, we&#8217;re just moving the stack pointer, just making room and then just</p>



<p class="wp-block-paragraph">remembering where our array is.</p>



<p class="wp-block-paragraph">We could put, you know, another frame on top if we wanted to make just like one</p>



<p class="wp-block-paragraph">integer as a local variable.</p>



<p class="wp-block-paragraph">You just got to remember where it is.</p>



<p class="wp-block-paragraph">You know, what is it?</p>



<p class="wp-block-paragraph">What is its offset?</p>



<p class="wp-block-paragraph">is it what is its offset okay so i&#8217;m going to erase this because we&#8217;re going to do a lot more than</p>



<p class="wp-block-paragraph">than five uh integers on the stack but i just want you to understand what we&#8217;re doing before we do it</p>



<p class="wp-block-paragraph">okay so the next thing i&#8217;m going to grab is a move instruction and i&#8217;m going to put it right here so</p>



<p class="wp-block-paragraph">to move the stack pointer rsp so the first thing i&#8217;m going to do is i&#8217;m going to use a temporary</p>



<p class="wp-block-paragraph">register we don&#8217;t need to preserve this in the push and pop because it&#8217;s marked as temp so we&#8217;re</p>



<p class="wp-block-paragraph">not responsible for preserving it and so i&#8217;m going to say r10 is just going to be the number of</p>



<p class="wp-block-paragraph">integers that we want to create if you recall at the top of our program here num integers is just</p>



<p class="wp-block-paragraph">50 okay so then the next thing that i&#8217;m going to grab is well i&#8217;m not going to grab it i&#8217;m going</p>



<p class="wp-block-paragraph">it I&#8217;m going to straight multiply by integer size so again if you look at integer size that&#8217;s going</p>



<p class="wp-block-paragraph">to be eight because we&#8217;re using quad words for our integers so we&#8217;re really just going to take</p>



<p class="wp-block-paragraph">50 times 8 whatever number that is is that 400 tell me in the comments if that&#8217;s a right or wrong</p>



<p class="wp-block-paragraph">and so uh you may or may not know if you don&#8217;t see more videos see more textbooks you may or may not</p>



<p class="wp-block-paragraph">instruction just multiplies two numbers.</p>



<p class="wp-block-paragraph">If you use the three operand format,</p>



<p class="wp-block-paragraph">then the last two operands get multiplied</p>



<p class="wp-block-paragraph">and the results stored in the first operand.</p>



<p class="wp-block-paragraph">But if we use the two operand format like I&#8217;ve done here,</p>



<p class="wp-block-paragraph">then both of those operands get multiplied</p>



<p class="wp-block-paragraph">and then the result gets stored in the first operand.</p>



<p class="wp-block-paragraph">So basically at this point,</p>



<p class="wp-block-paragraph">R10 should hold the number of memory locations</p>



<p class="wp-block-paragraph">that we should move the stack pointer</p>



<p class="wp-block-paragraph">in order to make room for all those integers</p>



<p class="wp-block-paragraph">like I showed you a second ago.</p>



<p class="wp-block-paragraph">showed you a second ago. So then we&#8217;re going to move the stack pointer.</p>



<p class="wp-block-paragraph">And maybe I&#8217;ll leave a little comment here. Remember, the stack grows downward</p>



<p class="wp-block-paragraph">in memory. And so all I&#8217;m doing is subtracting the stack pointer. Remember the stack pointer</p>



<p class="wp-block-paragraph">register, it just holds a number, which is a memory location. So if you subtract some numbers</p>



<p class="wp-block-paragraph">from it, you&#8217;re really having it go downward in memory. And that&#8217;s what we want to do</p>



<p class="wp-block-paragraph">memory and that&#8217;s what we want to do to you know grow the stack for a local variable so I&#8217;m going</p>



<p class="wp-block-paragraph">to say well I should also say that we&#8217;re using the two operand version so just like I&#8217;m all if we</p>



<p class="wp-block-paragraph">had the three operand version then the last two operands would have one subtracted from the other</p>



<p class="wp-block-paragraph">and the result will be stored in the first one but since I&#8217;m using the two operand version</p>



<p class="wp-block-paragraph">basically it&#8217;s taking RSP minus R10 and then storing it in RSP so this this instruction just</p>



<p class="wp-block-paragraph">this instruction just says let&#8217;s move the stack pointer downward in memory</p>



<p class="wp-block-paragraph">enough times that we have room for all of our integers okay no problem next thing that we&#8217;re</p>



<p class="wp-block-paragraph">going to do is um we&#8217;re going to move r12 we&#8217;re going to move into r12 the current value of rsp</p>



<p class="wp-block-paragraph">word in memory right less okay so the first integer you know it&#8217;s up to you</p>



<p class="wp-block-paragraph">how you style this because once we do the subtraction then RSP is actually</p>



<p class="wp-block-paragraph">going to be pointing towards an integer you could consider that to be the first</p>



<p class="wp-block-paragraph">integer or the last integer because all we have is an array of integers so you</p>



<p class="wp-block-paragraph">you know rsp wherever it&#8217;s sitting when we&#8217;re finished we could say that&#8217;s pointing to the</p>



<p class="wp-block-paragraph">first integer or we could say it&#8217;s pointing to the last integer but um if we decide to say that it is</p>



<p class="wp-block-paragraph">pointing to the let&#8217;s see yeah if we&#8217;re deciding that it points to the first integer let me just do</p>



<p class="wp-block-paragraph">a little comment here first integer it just makes it a little bit easier for me to write our loop</p>



<p class="wp-block-paragraph">You could start by pointing to the one that RSP is pointing to,</p>



<p class="wp-block-paragraph">or you could start by pointing to the other one that was like the first one that you added onto the stack.</p>



<p class="wp-block-paragraph">You could call either one of those the first integer, as long as you remember where you started.</p>



<p class="wp-block-paragraph">So you can increase or decrease the memory location to get to the next integer.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to do it in this style.</p>



<p class="wp-block-paragraph">But keep in mind, as long as the only thing that you modify and read is within that range, it&#8217;s okay.</p>



<p class="wp-block-paragraph">So let&#8217;s remember where is RSP.</p>



<p class="wp-block-paragraph">So that&#8217;s like the top of the stack.</p>



<p class="wp-block-paragraph">We&#8217;re going to say R12 holds the stack pointer so that we can use R12 as sort of a running</p>



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



<p class="wp-block-paragraph">I think that&#8217;s the way I&#8217;m going to use it.</p>



<p class="wp-block-paragraph">Let me just double check my solution.</p>



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



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



<p class="wp-block-paragraph">a calculation where we remember where the last integer is so we know where the first integer</p>



<p class="wp-block-paragraph">we&#8217;re just going to call this the first integer i&#8217;ll put it in quotes just to remind you that</p>



<p class="wp-block-paragraph">this is just the style i happen to be using so here we&#8217;re going to say that the first integer</p>



<p class="wp-block-paragraph">is wherever rsp is pointing and then in r13 we&#8217;re going to remember where the other side of the</p>



<p class="wp-block-paragraph">array is in this case we&#8217;re calling it the last integer and pretty much it&#8217;s just r12 which is</p>



<p class="wp-block-paragraph">And then we add to it a memory location.</p>



<p class="wp-block-paragraph">So remember we said before that the stack grows downward in memory.</p>



<p class="wp-block-paragraph">So if we consider the top of the stack to be the first integer,</p>



<p class="wp-block-paragraph">then that means previous items, maybe I should draw this,</p>



<p class="wp-block-paragraph">are going to be increased memory locations.</p>



<p class="wp-block-paragraph">So that&#8217;s kind of like the backwards of what you imagine a stack is doing,</p>



<p class="wp-block-paragraph">but it&#8217;s kind of the way that I like it sometimes.</p>



<p class="wp-block-paragraph">So imagine just in a very simple stack,</p>



<p class="wp-block-paragraph">let&#8217;s pretend that the stack has one byte values,</p>



<p class="wp-block-paragraph">one byte of values, which it doesn&#8217;t, but let&#8217;s pretend if you, if you do it,</p>



<p class="wp-block-paragraph">I got to learn how to draw. I can&#8217;t get that five right. Okay. I&#8217;m defeated. So let&#8217;s pretend</p>



<p class="wp-block-paragraph">that that address on the thing sitting at the bottom of the stack is five. So then that means</p>



<p class="wp-block-paragraph">the next address would be four, right? But if we decided, let&#8217;s say that the, well, let&#8217;s do like</p>



<p class="wp-block-paragraph">like a few more just to make it a little bit more interesting.</p>



<p class="wp-block-paragraph">Let&#8217;s do a total of five.</p>



<p class="wp-block-paragraph">So we&#8217;ll say four, three, two, one.</p>



<p class="wp-block-paragraph">Maybe I should just add some numbers in front of those values.</p>



<p class="wp-block-paragraph">So it doesn&#8217;t feel like we&#8217;re hitting zero.</p>



<p class="wp-block-paragraph">But so just pretend we have a memory location of 15.</p>



<p class="wp-block-paragraph">That&#8217;s not going to be the case in real life.</p>



<p class="wp-block-paragraph">Pretend that we have one byte integers on the stack, which is not going to be the case.</p>



<p class="wp-block-paragraph">We&#8217;re going to have quad words.</p>



<p class="wp-block-paragraph">But you can see the memory locations go downward, right?</p>



<p class="wp-block-paragraph">to the stack pointer is pointing to the topmost location we have to remember that anything up here</p>



<p class="wp-block-paragraph">might exist in system ram but it&#8217;s not considered valid data because we didn&#8217;t</p>



<p class="wp-block-paragraph">you know make it part of the stack by by growing the stack pointer so that means uh these</p>



<p class="wp-block-paragraph">these items well maybe i&#8217;ll do a check instead of an x because x looks bad i&#8217;ll do a check</p>



<p class="wp-block-paragraph">check and a check and a check and a check these items are okay to use so if I have</p>



<p class="wp-block-paragraph">RSP or in this case we just remembered where RSP was by storing r12 if we have</p>



<p class="wp-block-paragraph">that memory location 11 and we want to get an additional integer like somewhere</p>



<p class="wp-block-paragraph">else like the next integer well that would be you know this one down here we</p>



<p class="wp-block-paragraph">wouldn&#8217;t go in the other direction we wouldn&#8217;t go you know up we would just go</p>



<p class="wp-block-paragraph">know up we would just go down in the stack but down visually is actually growing upwards in memory</p>



<p class="wp-block-paragraph">because remember when we grow upwards visually we&#8217;re growing downward in memory so and you can</p>



<p class="wp-block-paragraph">see here too if we&#8217;re if we&#8217;re increasing 11 to 12 that means we&#8217;re adding memory locations to get</p>



<p class="wp-block-paragraph">to the next uh integer that we have in the stack so that&#8217;s why here my green is just like frustrating</p>



<p class="wp-block-paragraph">fix this yeah so that&#8217;s why here we&#8217;re adding a little formula instead of subtracting because</p>



<p class="wp-block-paragraph">the rsp started there we&#8217;re saying that the top of the stack is the first integer</p>



<p class="wp-block-paragraph">just so we can add in a more convenient nice way so what are we adding to it we&#8217;re just adding to it</p>



<p class="wp-block-paragraph">the number of integers minus one and then so that&#8217;s that&#8217;s the number of slots that</p>



<p class="wp-block-paragraph">size. So if you imagine, you know, if we had 10 integers, then, you know, 10 minus one is nine</p>



<p class="wp-block-paragraph">slots. So if you imagine that zero is the first integer, let&#8217;s say the memory location again,</p>



<p class="wp-block-paragraph">then that means one, two, three, four, five, six, seven, eight, nine, 10 minus one is nine. So if we</p>



<p class="wp-block-paragraph">added the number of integers minus one from the start, which we&#8217;ll consider zero here, that means</p>



<p class="wp-block-paragraph">directly so that&#8217;s just like a little math because sometimes when you when you</p>



<p class="wp-block-paragraph">think of adding two numbers together or taking the difference or including the</p>



<p class="wp-block-paragraph">first number or not including the first number it&#8217;s a little confusing right so</p>



<p class="wp-block-paragraph">keep in mind for this particular calculation we are adding to it the</p>



<p class="wp-block-paragraph">number of integers minus one so that will be sitting on the last integer</p>



<p class="wp-block-paragraph">rather than going past it so start with the first integers memory location and</p>



<p class="wp-block-paragraph">the number of integers minus one or size minus one times the integer size,</p>



<p class="wp-block-paragraph">because remember, every integer is going to be eight bytes.</p>



<p class="wp-block-paragraph">And that will give us the memory location of the last integer in R13.</p>



<p class="wp-block-paragraph">And then I&#8217;ve said this in other videos,</p>



<p class="wp-block-paragraph">but basically you&#8217;re only allowed to make these sorts of calculations in Yasm</p>



<p class="wp-block-paragraph">when you have the calculation inside of brackets,</p>



<p class="wp-block-paragraph">but brackets will automatically dereference the value on the inside.</p>



<p class="wp-block-paragraph">It&#8217;ll consider it as a pointer that needs to be dereferenced,</p>



<p class="wp-block-paragraph">but we don&#8217;t want to dereference anything.</p>



<p class="wp-block-paragraph">anything we don&#8217;t want to go to a memory location and take a value the memory location is the value</p>



<p class="wp-block-paragraph">so these dereferencing brackets which are required for the formula are kind of bad so that&#8217;s why we</p>



<p class="wp-block-paragraph">use the lea instruction instead of the move instruction if i put move there it would</p>



<p class="wp-block-paragraph">definitely dereference the memory location and give me a value in r13 so r13 wouldn&#8217;t actually</p>



<p class="wp-block-paragraph">be a pointer it would just be the value of the junk data of the last integer keep that in mind</p>



<p class="wp-block-paragraph">keep that in mind okay so uh where&#8217;s RSP so we got that so now let&#8217;s do a loop</p>



<p class="wp-block-paragraph">I&#8217;m gonna start off with a label called demo loop init so for me personally when</p>



<p class="wp-block-paragraph">I&#8217;m looping you know I like to make my labels inside of my loops start with a</p>



<p class="wp-block-paragraph">prefix that matches the function so it&#8217;s like demo and then everything else is</p>



<p class="wp-block-paragraph">gonna be demo underscore something so uh init loop init I&#8217;m gonna make a loop</p>



<p class="wp-block-paragraph">to make a loop where i initialize the values of the array so i&#8217;m calling this loop the init loop</p>



<p class="wp-block-paragraph">and then the last part is just like this is the initialization part of the loop this is where we</p>



<p class="wp-block-paragraph">sort of like set up the initial values to loop so we have r12 and 13 that point to the first</p>



<p class="wp-block-paragraph">and last integers now we&#8217;re going to set up r14 and 15 where r14 is the running pointer i think</p>



<p class="wp-block-paragraph">before i might have accidentally said that r13 is the running pointer it just points to the last</p>



<p class="wp-block-paragraph">it just points to the last integer but if you look back up at my comments r14 is the running pointer</p>



<p class="wp-block-paragraph">to the current integer what is a running pointer it&#8217;s just a pointer that runs it&#8217;s just a pointer</p>



<p class="wp-block-paragraph">that just keeps increasing so we can look at different data values so i&#8217;m going to start it</p>



<p class="wp-block-paragraph">by looking at the first integer so now r14 is pointing to the first integer and then r15 is</p>



<p class="wp-block-paragraph">going to be the value that i want to put into that position in the array so like the first integer</p>



<p class="wp-block-paragraph">integer I want to put some kind of a starting value into it you can put the</p>



<p class="wp-block-paragraph">number zero or whatever you want I just wanted to have a start value so that it</p>



<p class="wp-block-paragraph">sort of looks more like I&#8217;m putting data and less like I have a loop counter so</p>



<p class="wp-block-paragraph">remember the integer start value up here is just seven so I&#8217;m just going to start</p>



<p class="wp-block-paragraph">at the number seven and now I&#8217;m done initializing my loop then the next thing</p>



<p class="wp-block-paragraph">I&#8217;m going to add is the top of my loop so you can imagine this as the top of a</p>



<p class="wp-block-paragraph">top of a while loop where you start comparing some sort of an expression maybe i&#8217;ll say uh</p>



<p class="wp-block-paragraph">you know expr to say that we&#8217;re comparing some sort of an expression</p>



<p class="wp-block-paragraph">and if that expression evaluates to true the loop continues if it evaluates to false</p>



<p class="wp-block-paragraph">then the loop does not continue let&#8217;s see so i&#8217;m going to compare r14 with r13 inside of the</p>



<p class="wp-block-paragraph">and 13 remember R14 is the running pointer and R13 is the last integer so basically I&#8217;m trying to</p>



<p class="wp-block-paragraph">figure out am I looking at or like let&#8217;s compare the running pointer with the pointer of the last</p>



<p class="wp-block-paragraph">integer then I&#8217;m going to say if the running pointer has a greater memory location than than</p>



<p class="wp-block-paragraph">the last integer that means I&#8217;ve gone beyond the last integer and again the way I arranged the first</p>



<p class="wp-block-paragraph">last integers just makes it easier for me to think of them as having increasing</p>



<p class="wp-block-paragraph">memory locations so I&#8217;m going to jump if the running pointer has already</p>



<p class="wp-block-paragraph">surpassed the last integer by saying let&#8217;s jump if it&#8217;s greater than so you</p>



<p class="wp-block-paragraph">can imagine maybe in the expression here I should probably say while not</p>



<p class="wp-block-paragraph">r14 is greater than r13 not a great expression but it&#8217;ll do so that&#8217;s what</p>



<p class="wp-block-paragraph">up there at the top let me just put that into my notes too so that my notes match</p>



<p class="wp-block-paragraph">the video okay so we are comparing and then we&#8217;re jumping to the end of the</p>



<p class="wp-block-paragraph">loop if we end up you know going beyond the last integer so that labels not</p>



<p class="wp-block-paragraph">created yet I&#8217;ll create that in a moment but pretty much that&#8217;s a label that&#8217;s</p>



<p class="wp-block-paragraph">just going to be below the loop just to say like we&#8217;re finished with the loop</p>



<p class="wp-block-paragraph">the loop body I like to put comments here to help myself remember oh this is</p>



<p class="wp-block-paragraph">the part of the while loop that I&#8217;m currently inside of just makes things a</p>



<p class="wp-block-paragraph">little bit easier to understand you know you put a block comment up top of every</p>



<p class="wp-block-paragraph">label or every you know chunk of instructions just to let you know the</p>



<p class="wp-block-paragraph">general idea and then you know sometimes you put comments also on the right side</p>



<p class="wp-block-paragraph">to help you remember what each instruction is actually doing so then</p>



<p class="wp-block-paragraph">what am I going to do here remember R14 is the running pointer if I D ref R14</p>



<p class="wp-block-paragraph">ref r14 that means i want to move an actual value into that memory location rather than changing the</p>



<p class="wp-block-paragraph">memory location that r14 points to so r15 is going to be the uh the integer that we want to</p>



<p class="wp-block-paragraph">write into the array so all i&#8217;m doing here is i&#8217;m saying let&#8217;s take that value seven which is what</p>



<p class="wp-block-paragraph">it starts as and just move it in to you know the ram stick at that memory location so i&#8217;m setting</p>



<p class="wp-block-paragraph">seven right now then I&#8217;m going to increase our 15 so that means every time</p>



<p class="wp-block-paragraph">we iterate the loop we should see that the value increases so the first integer</p>



<p class="wp-block-paragraph">should be seven the second one should be eight next one should be nine and so</p>



<p class="wp-block-paragraph">forth so just a simple loop where I&#8217;m just writing data into my array so now</p>



<p class="wp-block-paragraph">that we&#8217;ve ended the loop body let&#8217;s write the bottom of the loop which is</p>



<p class="wp-block-paragraph">just going to increase the running pointer and jump back up to the top and</p>



<p class="wp-block-paragraph">And this is not necessarily the only style for translating while loops.</p>



<p class="wp-block-paragraph">I&#8217;m just doing it.</p>



<p class="wp-block-paragraph">And, you know, I&#8217;m going to make another video in the future where we talk about, you know, for loops and while loops and all that stuff.</p>



<p class="wp-block-paragraph">But this video is just really about local variables on the stack.</p>



<p class="wp-block-paragraph">So I&#8217;m not going to go over all the different ways you can do it.</p>



<p class="wp-block-paragraph">Anyway, so R14 is the running pointer.</p>



<p class="wp-block-paragraph">So I&#8217;m going to just make the running pointer jump to the next integer.</p>



<p class="wp-block-paragraph">And we can do that by increasing its memory location by the size of one integer.</p>



<p class="wp-block-paragraph">Again, this is another benefit of the first and last pointers that I chose at the beginning.</p>



<p class="wp-block-paragraph">I can just increase to go to the next integer.</p>



<p class="wp-block-paragraph">So we&#8217;re going to increase by 8 bytes to just go to the next integer.</p>



<p class="wp-block-paragraph">If you increase by 1 byte, you&#8217;ll probably have a huge corrupted mess because you&#8217;re</p>



<p class="wp-block-paragraph">messing with 8 byte integers but you&#8217;re only increasing by 1 byte.</p>



<p class="wp-block-paragraph">And then after we increase, we&#8217;re just going to jump to the top of the loop.</p>



<p class="wp-block-paragraph">So notice how I have a jump statement here.</p>



<p class="wp-block-paragraph">It&#8217;s going to go just to loop top.</p>



<p class="wp-block-paragraph">loop top. So now this part is here is basically just going to execute over and over and over again</p>



<p class="wp-block-paragraph">until we finally scan through all of the integers in our array. So that&#8217;s the bottom of the loop.</p>



<p class="wp-block-paragraph">And then I&#8217;ll make the label for the loop being done. It&#8217;s not really going to do anything</p>



<p class="wp-block-paragraph">except just be done. And do you, you know, I don&#8217;t know, depending on your style, maybe you can let</p>



<p class="wp-block-paragraph">drop through if the loop&#8217;s done rather than always jumping to the top but i&#8217;m just going to say when</p>



<p class="wp-block-paragraph">we&#8217;re done we jump to the loop done label and therefore there&#8217;s no more looping of that</p>



<p class="wp-block-paragraph">initialization loop okay so we got that done let me just run the program real fast to make sure</p>



<p class="wp-block-paragraph">that i haven&#8217;t screwed it up we actually should not see anything right now oh what did i do</p>



<p class="wp-block-paragraph">must have done something naughty maybe if i finish</p>



<p class="wp-block-paragraph">uh this program then everything will be okay oh my gosh what did i even do</p>



<p class="wp-block-paragraph">well i&#8217;ve got a working solution in the other window so hopefully when i paste all the extra</p>



<p class="wp-block-paragraph">steps everything will be fine you never know subtract the stack pointer oh did i forget to</p>



<p class="wp-block-paragraph">oh did i forget to restore something at the very end print body move the base pointer into the</p>



<p class="wp-block-paragraph">RSP stack pointer oh yeah okay that&#8217;s definitely what you got that&#8217;s why I</p>



<p class="wp-block-paragraph">crashed okay so um remember I said you got to be very careful about the stack</p>



<p class="wp-block-paragraph">this is a great lesson so I did preserve the base pointer but I didn&#8217;t actually</p>



<p class="wp-block-paragraph">preserve the stack pointer notice how right here I subtracted from the stack</p>



<p class="wp-block-paragraph">pointer but I did not restore the stack pointer anywhere so that means I</p>



<p class="wp-block-paragraph">corrupted the stack for anyone that called me and also for my return address</p>



<p class="wp-block-paragraph">address. So I&#8217;m kind of trying to copy paste my instructions from top to bottom, but I think I&#8217;m</p>



<p class="wp-block-paragraph">just going to copy paste something else to make sure that we can actually run this. So I&#8217;m going</p>



<p class="wp-block-paragraph">to copy paste into the epilog a restoration of the stack pointer. And that&#8217;s why we saved the</p>



<p class="wp-block-paragraph">stack pointer in the base pointer, just to remember where it was when we originally started our</p>



<p class="wp-block-paragraph">function. So now on line 128, it should be restored and the program should work. Let me just double</p>



<p class="wp-block-paragraph">yeah it worked okay nothing happened that we can see but it did write values</p>



<p class="wp-block-paragraph">into the array now let&#8217;s do another loop where we just print the array so let&#8217;s</p>



<p class="wp-block-paragraph">see loop in it done looping it bottom and looping it done okay so now there&#8217;s</p>



<p class="wp-block-paragraph">like gonna be another loop here we&#8217;re gonna call this the print loop and so</p>



<p class="wp-block-paragraph">the print loop is gonna be kind of the same pattern we&#8217;re just gonna loop</p>



<p class="wp-block-paragraph">going to loop through all the integers in the array but instead of modifying them we&#8217;re just</p>



<p class="wp-block-paragraph">going to print them so now you know the first thing we&#8217;ll do is we&#8217;ll set r12 we&#8217;ll store that</p>



<p class="wp-block-paragraph">inside of r14 in order to start the loop at the first integer what was r14 again that was the</p>



<p class="wp-block-paragraph">running pointer remember let&#8217;s see where is that yeah it was the running pointer so now we&#8217;re</p>



<p class="wp-block-paragraph">resetting the r14 running pointer to the very beginning of the array and we know where the</p>



<p class="wp-block-paragraph">thing we&#8217;ll do is we will set up the top of the loop and the body so I&#8217;m just</p>



<p class="wp-block-paragraph">going to copy paste again this stuff right here</p>



<p class="wp-block-paragraph">right there okay so the top of the loop we&#8217;re asking ourselves you know we&#8217;re</p>



<p class="wp-block-paragraph">gonna compare I&#8217;m not gonna put all the extra while stuff that I put in the</p>



<p class="wp-block-paragraph">previous loop because hopefully by now you understand loops a little bit better</p>



<p class="wp-block-paragraph">and if we&#8217;re beyond the last integer because the running point of r14 is beyond the memory</p>



<p class="wp-block-paragraph">location of r13 that means we&#8217;re totally done so we should jump if it&#8217;s greater than</p>



<p class="wp-block-paragraph">now i feel bad let&#8217;s uh let&#8217;s put a comment in here on the top let&#8217;s go uh</p>



<p class="wp-block-paragraph">basically that you know if uh keep going as long as r14 is not greater than r13 so if it is</p>



<p class="wp-block-paragraph">in the body and in the body all i&#8217;m going to do is use r14 the running pointer to print uh you know</p>



<p class="wp-block-paragraph">whatever value is sitting in that particular integer so how do we do that i&#8217;m just going to</p>



<p class="wp-block-paragraph">use the printf statement or sorry the printf function which is provided by the c libraries</p>



<p class="wp-block-paragraph">that&#8217;s why we&#8217;re doing you know a modular or a hybrid program with multiple modules and c linking</p>



<p class="wp-block-paragraph">very quickly there is a function called printf which I can call it it takes multiple arguments</p>



<p class="wp-block-paragraph">but the first two arguments that I can give it are the string that represents the formatting</p>



<p class="wp-block-paragraph">that I want to print like I could do like a regular string message I could do tokens</p>



<p class="wp-block-paragraph">to format some data inside of them and then the second argument is going to be the piece of data</p>



<p class="wp-block-paragraph">that I actually want to use let me see if I can just type that up for you real fast</p>



<p class="wp-block-paragraph">So, you know, the printf instruction, or sorry, this is not an instruction.</p>



<p class="wp-block-paragraph">This is a function in C.</p>



<p class="wp-block-paragraph">We would typically, you know, give it some kind of string.</p>



<p class="wp-block-paragraph">The string should be null terminated, and it should have a new line at the very end of it</p>



<p class="wp-block-paragraph">to make sure that printf actually flushes.</p>



<p class="wp-block-paragraph">It won&#8217;t flush if you don&#8217;t have a new line, so the program will look really weird.</p>



<p class="wp-block-paragraph">But I guess it&#8217;s more performant if you have a way to delay the flushing,</p>



<p class="wp-block-paragraph">flushing and you know that you can flush it later at the very end but for now i&#8217;m just going to</p>



<p class="wp-block-paragraph">flush every time and then every argument after that is some sort of you know data that we can</p>



<p class="wp-block-paragraph">print so imagine we have a long and we&#8217;ll call it a and we&#8217;ll say that it has like some giant value</p>



<p class="wp-block-paragraph">so that means we would give that long as the next argument the rsi argument and then for the string</p>



<p class="wp-block-paragraph">what I&#8217;m using right here %lu so you can imagine instead of this string it is this</p>



<p class="wp-block-paragraph">string right here whoops too many too many quotes it&#8217;s just this string right</p>



<p class="wp-block-paragraph">here and then instead of a 10 13 that&#8217;s the same thing as just doing an or if</p>



<p class="wp-block-paragraph">you want to be you know more of a windows windows person slash r slash n</p>



<p class="wp-block-paragraph">it&#8217;s all good and the zero is not needed because the string if you put a string</p>



<p class="wp-block-paragraph">literal it&#8217;s automatically going to be null terminated which means there&#8217;s just</p>



<p class="wp-block-paragraph">null terminated which means there&#8217;s just a zero at the end of the string in memory so this is</p>



<p class="wp-block-paragraph">basically what I&#8217;m doing I&#8217;m making a an integer in the case of the assembly program it&#8217;s going to</p>



<p class="wp-block-paragraph">grab an integer from that position in the array that we&#8217;re looking at and it&#8217;s going to give it</p>



<p class="wp-block-paragraph">as an argument and then the first argument is going to say let&#8217;s just print this as a unsigned</p>



<p class="wp-block-paragraph">long so that&#8217;s why I have that string here let me search for it and go down a little bit again so</p>



<p class="wp-block-paragraph">I&#8217;m saying first argument is this the format that I want to be printed second argument is the actual</p>



<p class="wp-block-paragraph">is the actual value and then I&#8217;m gonna make a call to printf why do I have this</p>



<p class="wp-block-paragraph">weird push and pop pair sitting around printf so this is not a video about</p>



<p class="wp-block-paragraph">stack alignment in GCC but basically the GCC libraries expect that your stack is</p>



<p class="wp-block-paragraph">aligned to I think 16 bytes but since we use 8 byte integers every single time we</p>



<p class="wp-block-paragraph">address to the stack which is eight bytes and then every time we do one single push or pop</p>



<p class="wp-block-paragraph">we&#8217;re modifying the alignment of the stack by eight bytes so if you think about it when we&#8217;re</p>



<p class="wp-block-paragraph">programming in assembly for the most part the stack is going in and out of alignment because</p>



<p class="wp-block-paragraph">every time we modify it by eight bytes it it might line up with a 16 byte alignment or it might not</p>



<p class="wp-block-paragraph">it&#8217;s just kind of like oscillating right so when I first wrote this solution I wasn&#8217;t doing the</p>



<p class="wp-block-paragraph">what happened oh actually maybe i guess i don&#8217;t need to jump to the top right now i don&#8217;t need</p>



<p class="wp-block-paragraph">to finish the loop let&#8217;s see if this prints just one number well let me let me let me see if this</p>



<p class="wp-block-paragraph">prints one number certainly and if it&#8217;s an okay assembly program just to print one number is it</p>



<p class="wp-block-paragraph">going to work okay we got to do the done symbol okay so i&#8217;ll show you in a minute why we need</p>



<p class="wp-block-paragraph">that for stack alignment but i guess i&#8217;ll just finish the loop so demo print loop done so we</p>



<p class="wp-block-paragraph">just did print loop top and that means we need the bottom and the done so i&#8217;m just going to copy paste</p>



<p class="wp-block-paragraph">into program here i&#8217;ll just say nada because we&#8217;re not really doing anything and then at the bottom</p>



<p class="wp-block-paragraph">you know that&#8217;s the epilogue that&#8217;s separate from the the other label so basically now let me finish</p>



<p class="wp-block-paragraph">know we already know the loops but I&#8217;ll just I&#8217;ll just say it at the bottom of</p>



<p class="wp-block-paragraph">the loop we do the same thing that we did with the initialization loop we just</p>



<p class="wp-block-paragraph">increase the running pointer you know we move it along to the next integer and</p>



<p class="wp-block-paragraph">then we jump to the top of the loop that&#8217;s it and then the done label we</p>



<p class="wp-block-paragraph">don&#8217;t really do anything we&#8217;re just letting execution drop through down to</p>



<p class="wp-block-paragraph">that point so that the loop doesn&#8217;t continue so now we should be able to run</p>



<p class="wp-block-paragraph">the program don&#8217;t get excited oh actually you know what get excited I</p>



<p class="wp-block-paragraph">was gonna say don&#8217;t get excited because it was gonna totally work now but now</p>



<p class="wp-block-paragraph">because it was going to totally work now,</p>



<p class="wp-block-paragraph">but now I think we can just say that it&#8217;s going to crash.</p>



<p class="wp-block-paragraph">So if I run it, notice how there&#8217;s a segpult.</p>



<p class="wp-block-paragraph">So the GCC libraries, many functions expect your stack</p>



<p class="wp-block-paragraph">to be aligned to 16 bytes.</p>



<p class="wp-block-paragraph">So if you see mysterious crashes</p>



<p class="wp-block-paragraph">and you are absolutely sure that you&#8217;re not ruining</p>



<p class="wp-block-paragraph">the stack pointer or ruining something else,</p>



<p class="wp-block-paragraph">you&#8217;re doing everything correctly,</p>



<p class="wp-block-paragraph">but the program still crashes, it might be stack alignment.</p>



<p class="wp-block-paragraph">So one way to get around stack alignment</p>



<p class="wp-block-paragraph">is just to move the stack pointer.</p>



<p class="wp-block-paragraph">move the stack pointer like at the top here we could have said oh we&#8217;ve got one two three four</p>



<p class="wp-block-paragraph">five we&#8217;ve got five pushes and then here we&#8217;re moving the stack by I don&#8217;t know how many other</p>



<p class="wp-block-paragraph">addresses the stack might be out of alignment somehow so we could add an extra push up here</p>



<p class="wp-block-paragraph">and then add a corresponding pop down at the bottom like we could easily do this let me just</p>



<p class="wp-block-paragraph">show you real fast we could push r15 twice for no reason I acknowledge and then at the bottom</p>



<p class="wp-block-paragraph">and then at the bottom we pop R15 twice,</p>



<p class="wp-block-paragraph">that would change the alignment because that&#8217;s one more 8 byte push.</p>



<p class="wp-block-paragraph">But in my case, and actually that would be a little bit smarter</p>



<p class="wp-block-paragraph">because if you have our loop where it&#8217;s constantly calling on printf,</p>



<p class="wp-block-paragraph">this is a lot of hits to memory, right?</p>



<p class="wp-block-paragraph">This is like 100 hits to memory because every single time we do a push-pop pair</p>



<p class="wp-block-paragraph">around a call to printf, we&#8217;re like touching memory.</p>



<p class="wp-block-paragraph">Whereas if I did it at the beginning and the end,</p>



<p class="wp-block-paragraph">Maybe I should just do it this way.</p>



<p class="wp-block-paragraph">I want to do it both ways so you understand, but it&#8217;s more efficient, I think, if we do</p>



<p class="wp-block-paragraph">it this way.</p>



<p class="wp-block-paragraph">Anyway, so we&#8217;ll do pop twice at the bottom and then push twice at the top.</p>



<p class="wp-block-paragraph">And so then we don&#8217;t really need to surround it with a push-pop pair.</p>



<p class="wp-block-paragraph">I think I haven&#8217;t tested this.</p>



<p class="wp-block-paragraph">We&#8217;ll hope now that the stack is in alignment at all times in our functions so that it doesn&#8217;t</p>



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



<p class="wp-block-paragraph">Yeah, so now see how the program works.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do it the other way now, which is the less efficient way, because here</p>



<p class="wp-block-paragraph">now which is the less efficient way because here we just have one extra push</p>



<p class="wp-block-paragraph">pop pair but if we do it the other way it&#8217;ll still work but we&#8217;ll be hitting</p>



<p class="wp-block-paragraph">memory much more often so I&#8217;m just gonna do it this way just to show you you can</p>



<p class="wp-block-paragraph">surround any call because sometimes in your programs you might have the stack</p>



<p class="wp-block-paragraph">like you know modified throughout the function many different times so it</p>



<p class="wp-block-paragraph">wouldn&#8217;t make too much sense for you to add an extra push pop pair in the prologue</p>



<p class="wp-block-paragraph">and epilogue because that might not solve it for all of your calls to all of</p>



<p class="wp-block-paragraph">to all of your seed library functions.</p>



<p class="wp-block-paragraph">So in that case,</p>



<p class="wp-block-paragraph">where you can&#8217;t really predict the stack well enough,</p>



<p class="wp-block-paragraph">you can just surround your call with a push-pop pair.</p>



<p class="wp-block-paragraph">It hits memory more, but it&#8217;ll work.</p>



<p class="wp-block-paragraph">So this is basically, you can imagine,</p>



<p class="wp-block-paragraph">by the time we get to line 139,</p>



<p class="wp-block-paragraph">the stack is out of alignment.</p>



<p class="wp-block-paragraph">So I just do a push that puts it into alignment.</p>



<p class="wp-block-paragraph">And then after the call comes back,</p>



<p class="wp-block-paragraph">I just pop it so it&#8217;s back out of alignment again,</p>



<p class="wp-block-paragraph">but I don&#8217;t have extra erroneous data sitting on the stack.</p>



<p class="wp-block-paragraph">Because if I only had push and not pop,</p>



<p class="wp-block-paragraph">then it&#8217;s going to push it more and more out of alignment.</p>



<p class="wp-block-paragraph">It&#8217;s going to push it in and out and in and out,</p>



<p class="wp-block-paragraph">but it&#8217;s going to add a bunch of junk data to the stack</p>



<p class="wp-block-paragraph">that I&#8217;ll never recover from.</p>



<p class="wp-block-paragraph">Or actually, I guess I will recover at the very end</p>



<p class="wp-block-paragraph">when I restore the stack pointer, but it&#8217;s pointless.</p>



<p class="wp-block-paragraph">It&#8217;s going to consume too much memory.</p>



<p class="wp-block-paragraph">What if I was writing like a billion</p>



<p class="wp-block-paragraph">or like a million items on the stack, right?</p>



<p class="wp-block-paragraph">a billion iterations of the loop would probably be a bad idea to start adding onto the stack</p>



<p class="wp-block-paragraph">we&#8217;ll probably end up stack overflowing probably with far less than a billion items so anyway i&#8217;m</p>



<p class="wp-block-paragraph">going to surround the call with the push pop pair and then at the bottom we just do the same thing</p>



<p class="wp-block-paragraph">you know increase the integer and then go to the top and so now we you know you already can see it</p>



<p class="wp-block-paragraph">it starts off printing well it starts off initializing the array but then the thing that we</p>



<p class="wp-block-paragraph">can see starts off printing the first integer and then the next iteration of the loop prints the</p>



<p class="wp-block-paragraph">second integer and the third and the fourth and so forth until we get all the way down just to</p>



<p class="wp-block-paragraph">prove to you maybe we can increase it by two instead of one each time I&#8217;ll just modify that</p>



<p class="wp-block-paragraph">Oh no, that&#8217;s the print loop. Let&#8217;s do the init loop.</p>



<p class="wp-block-paragraph">Init loop bottom.</p>



<p class="wp-block-paragraph">Oh, right there, when we&#8217;re in the body.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to increase R15 twice.</p>



<p class="wp-block-paragraph">Just to show you, you know, we can kind of control what&#8217;s going inside of the local array.</p>



<p class="wp-block-paragraph">So see how it goes from 7 to 9 to 11 instead of 7, 8, 9.</p>



<p class="wp-block-paragraph">So I&#8217;m going to take that out and then just show you that we can control</p>



<p class="wp-block-paragraph">how many integers we have in our local array with just the number of integers.</p>



<p class="wp-block-paragraph">just the number of integers so I&#8217;m going to change the 50 to a 5 and run it again and you can see</p>



<p class="wp-block-paragraph">what the dang did I do number of integers is 5 and then it was 50 did I put the stack out of</p>



<p class="wp-block-paragraph">alignment no I I have a push put pair there number 50 is not hard-coded anywhere anywhere</p>



<p class="wp-block-paragraph">there. Oh, I have a bug that I can debug. That&#8217;s nice. I guess the bug debugging is not for this</p>



<p class="wp-block-paragraph">video. I wonder if some other value would work for that. There&#8217;s a number of things that could</p>



<p class="wp-block-paragraph">be the problem. It could be like stack alignment somehow. It could also be, let&#8217;s see, am I making</p>



<p class="wp-block-paragraph">any calls here inside of my function demo? I&#8217;m saying sub and LEA and then start and then jump</p>



<p class="wp-block-paragraph">and start and then jump and increase and jump and then RDI.</p>



<p class="wp-block-paragraph">Not really doing anything else.</p>



<p class="wp-block-paragraph">So I don&#8217;t think it&#8217;s stack alignment.</p>



<p class="wp-block-paragraph">I must have miscalculated somewhere somehow for changing that.</p>



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



<p class="wp-block-paragraph">Well, if I figured it out, then I will release another video.</p>



<p class="wp-block-paragraph">But basically, this is the idea.</p>



<p class="wp-block-paragraph">100 works, 50 works.</p>



<p class="wp-block-paragraph">What about 99?</p>



<p class="wp-block-paragraph">Would that work?</p>



<p class="wp-block-paragraph">Save faults on 99.</p>



<p class="wp-block-paragraph">faults on 99 and then 98. 98 works so like every two seems to work but that is</p>



<p class="wp-block-paragraph">two quad words or 16 oh right so like a hundred integers if we assume that</p>



<p class="wp-block-paragraph">that&#8217;s in alignment then 99 would be eight bytes less that we&#8217;re moving the</p>



<p class="wp-block-paragraph">98, notice how it is okay.</p>



<p class="wp-block-paragraph">And then 97, it&#8217;s going to segfault again.</p>



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



<p class="wp-block-paragraph">This is a great stack alignment video, I guess.</p>



<p class="wp-block-paragraph">96, it won&#8217;t segfault.</p>



<p class="wp-block-paragraph">So it&#8217;s going in and out of alignment.</p>



<p class="wp-block-paragraph">I think I figured it out.</p>



<p class="wp-block-paragraph">So if we use the number 100, it&#8217;s in alignment.</p>



<p class="wp-block-paragraph">Sorry, it&#8217;s out of alignment.</p>



<p class="wp-block-paragraph">If we use the number 100, the program works because the number 100 throws it out of alignment.</p>



<p class="wp-block-paragraph">number 100 throws it out of alignment but then we have this push pop pair here around the call to</p>



<p class="wp-block-paragraph">printf which puts it back in alignment so if I change the number of integers I&#8217;m actually</p>



<p class="wp-block-paragraph">changing the number of memory locations that I modify the stack pointer so I have to do it by</p>



<p class="wp-block-paragraph">twos if I wanted to do 99 here then that means the stack is in alignment by the time I call</p>



<p class="wp-block-paragraph">by the time I&#8217;m getting ready to call printf which means the push pop pair around it throws</p>



<p class="wp-block-paragraph">push pop pair around it throws the stack out of alignment so just watch here I&#8217;ll</p>



<p class="wp-block-paragraph">prove it okay so it&#8217;s sake false I comment that out now it won&#8217;t throw it</p>



<p class="wp-block-paragraph">out of alignment okay should have known that before I recorded the video it&#8217;s</p>



<p class="wp-block-paragraph">fun to guess sometimes though you know I get a little nervous oh no my program</p>



<p class="wp-block-paragraph">broke on camera can I debug it live well I guess I can but kind of slowly</p>



<p class="wp-block-paragraph">anyway so we I think we&#8217;ve gone over every single part that I wanted to show you</p>



<p class="wp-block-paragraph">we know how to create a local array on the stack and and therefore you also know how to create like</p>



<p class="wp-block-paragraph">any other data type on the stack if you want you want to create a long a 64-bit integer</p>



<p class="wp-block-paragraph">just move it by eight bytes instead of moving it by eight times however many integers we were doing</p>



<p class="wp-block-paragraph">in this video you want to I don&#8217;t know put a character on the stack you can do that if you</p>



<p class="wp-block-paragraph">Just, you know, move it by one memory location instead of eight memory locations.</p>



<p class="wp-block-paragraph">So, you know, one byte instead of eight bytes.</p>



<p class="wp-block-paragraph">You want to store a short, you know, a two byte integer?</p>



<p class="wp-block-paragraph">Well, just move it by two bytes instead of eight bytes, right?</p>



<p class="wp-block-paragraph">So you can do this as many times as you want.</p>



<p class="wp-block-paragraph">You want to have several local variables?</p>



<p class="wp-block-paragraph">Just move it one time for every local variable.</p>



<p class="wp-block-paragraph">Same thing for accessing.</p>



<p class="wp-block-paragraph">You just have to remember where everything is.</p>



<p class="wp-block-paragraph">offset of the of the first variable local variable and then the offset of the second</p>



<p class="wp-block-paragraph">local variable you can store those in globals or store those in registers if you can you just</p>



<p class="wp-block-paragraph">got to remember somehow where everything starts but it&#8217;s all sitting on the stack if it&#8217;s a local</p>



<p class="wp-block-paragraph">variable okay I guess that&#8217;s everything that I really have to say I hope you enjoyed this video</p>



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



<p class="wp-block-paragraph">have a good one</p>



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



<p class="wp-block-paragraph">Hey everybody, thanks for watching this video again from the bottom of my heart. 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 or whatever it is?</p>



<p class="wp-block-paragraph">You do on the current social media website 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 and grow this community</p>



<p class="wp-block-paragraph">more videos, longer videos, better videos, 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. 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 just wakes</p>



<p class="wp-block-paragraph">me up and I get filled with joy. That&#8217;s exactly what happens every single time. So you could do</p>



<p class="wp-block-paragraph">it as a nice favor to me or you could troll me if you want to just wake me up in the middle of</p>



<p class="wp-block-paragraph">the night, 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 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</p>



<p class="wp-block-paragraph">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">If you have a suggestion for</p>



<p class="wp-block-paragraph">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</p>



<p class="wp-block-paragraph">I get I wake up in a cold sweat. 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 again, thank you so much for watching this video and</p>



<p class="wp-block-paragraph">Enjoy the cool music 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/x86-64-assembly-local-variables-stack-frames-alignment-explained/">x86-64 Assembly: Local Variables, Stack Frames &amp; Alignment Explained</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-local-variables-stack-frames-alignment-explained/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Mixed Function Arguments in x86-64 Assembly &#8211; Integers &#038; Floats Explained</title>
		<link>https://www.NeuralLantern.com/mixed-function-arguments-in-x86-64-assembly-integers-floats-explained/</link>
					<comments>https://www.NeuralLantern.com/mixed-function-arguments-in-x86-64-assembly-integers-floats-explained/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 01 Mar 2026 02:14:39 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[abi calling convention]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[Ed Jorgensen]]></category>
		<category><![CDATA[floating point registers]]></category>
		<category><![CDATA[function calling convention]]></category>
		<category><![CDATA[integer float arguments]]></category>
		<category><![CDATA[linux assembly]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[mixed arguments]]></category>
		<category><![CDATA[nasm]]></category>
		<category><![CDATA[rax return]]></category>
		<category><![CDATA[rdi rsi xmm0]]></category>
		<category><![CDATA[system v abi]]></category>
		<category><![CDATA[ubuntu assembly]]></category>
		<category><![CDATA[x86 assembly]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[xmm registers]]></category>
		<category><![CDATA[xmm0 return]]></category>
		<category><![CDATA[yasm]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=310</guid>

					<description><![CDATA[<p>In the System V x86-64 ABI, integer and floating-point arguments use separate register assignment sequences. Integer arguments go into rdi, rsi, rdx, rcx, r8, r9 (then stack), while floating-point arguments use xmm0 through xmm7 independently. This means the first integer you see — even if it appears after several doubles — always goes into rdi, and the second float always goes into xmm1 regardless of how many integers came before it.</p>
<p>The post <a href="https://www.NeuralLantern.com/mixed-function-arguments-in-x86-64-assembly-integers-floats-explained/">Mixed Function Arguments in x86-64 Assembly &#8211; Integers &amp; Floats Explained</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="Mixed Function Arguments in x86-64 Assembly - Integers &amp; Floats Explained" width="1380" height="776" src="https://www.youtube.com/embed/ZAY3srLRx8c?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">Quick guide to passing mixed int + float arguments in x86-64 assembly (System V ABI). See why rdi can still be the first integer after several doubles, why xmm registers count separately, and how return values switch between rax and xmm0. Perfect for YASM/NASM programmers interfacing with C/C++.</p>



<p class="wp-block-paragraph">Intro to Mixed Arguments 00:00<br>Simple Integer-Only Functions 00:46<br>Adding Arguments and Return Values 01:01<br>Integer Arguments in RDI and RSI 01:30<br>Pointers Treated as Integers 02:06<br>Introducing Floating-Point Returns 02:31<br>Returning Double in XMM0 03:06<br>First Float Argument in XMM0 03:36<br>Float Registers Count Separately 04:03<br>Integer Register Order Explained 04:16<br>Separate Counting for Integers and Floats 05:21<br>RSI as First Integer After Float 05:38<br>Reference to Ed Jorgensen&#8217;s Book 07:00<br>Callee-Saved Registers Overview 07:56<br>Complex Mixed Argument Examples 09:48<br>Inserting Integer Among Floats 10:57<br>Skipping Float Registers on Integer 11:18<br>Calling C from Assembly Notes 11:56<br>Name Mangling Reminder 12:21<br>Closing Remarks and Thanks 12:30<br>Call to Subscribe and Support 13:04<br>Website and QR Code Mention 13:38<br>Final Thanks and Outro Music 14:19</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 the Blog: https://www.NeuralLantern.com</li>



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



<p class="wp-block-paragraph">Hey everybody! In this video I&#8217;m going to talk to you a little bit about using functions with</p>



<p class="wp-block-paragraph">mixed arguments in a YASM x86-64 assembly language program. Although this</p>



<p class="wp-block-paragraph">video will probably still be useful to you if you&#8217;re using NASM or MASM because the</p>



<p class="wp-block-paragraph">mixed arguments and their order and how they work is governed by the ABI which is a system</p>



<p class="wp-block-paragraph">so what the heck am i talking about okay so the first thing that i want to show you is uh</p>



<p class="wp-block-paragraph">imagine we have a function here let&#8217;s say void f this function doesn&#8217;t take any arguments and it</p>



<p class="wp-block-paragraph">doesn&#8217;t return anything congratulations you&#8217;re done that was the easiest thing that you&#8217;ve ever</p>



<p class="wp-block-paragraph">done in your entire life but as soon as you start adding arguments to it you have to uh</p>



<p class="wp-block-paragraph">start understanding how to uh how to call registers and stuff so imagine we have like a</p>



<p class="wp-block-paragraph">a long a and a long b so we give it two arguments and maybe we want it to return a long</p>



<p class="wp-block-paragraph">this is kind of like level one of writing functions in assembly right we just have</p>



<p class="wp-block-paragraph">a bunch of integers and we realize okay um this long the return value is going to come to the</p>



<p class="wp-block-paragraph">Maybe I&#8217;ll do a comment up here, we&#8217;ll do rax, we&#8217;ll say the long is actually coming</p>



<p class="wp-block-paragraph">back to the user in rax and the long is coming into the function with rdi, the long a and</p>



<p class="wp-block-paragraph">the long b is coming into the function with rsi.</p>



<p class="wp-block-paragraph">So if you understand this then you can write a function in assembly where basically you</p>



<p class="wp-block-paragraph">two incoming integer arguments and then you return to the caller a long value using rax</p>



<p class="wp-block-paragraph">keep in mind also that if this second argument was a pointer long pointer then it would be the</p>



<p class="wp-block-paragraph">same thing rsi because pointers are integers they&#8217;re 64-bit unsigned integers even if the</p>



<p class="wp-block-paragraph">pointer was not to a long even if the pointer was to a double or a character or whatever it would</p>



<p class="wp-block-paragraph">pointers are integers. It starts getting a little bit more complicated when you want to mix between</p>



<p class="wp-block-paragraph">floats and numeric arguments. So what I&#8217;ve been saying so far, it&#8217;s all just integers.</p>



<p class="wp-block-paragraph">Even if we&#8217;re mixing pointers with regular longs, it&#8217;s just, you know, they&#8217;re all longs basically,</p>



<p class="wp-block-paragraph">right? So, but as soon as we start introducing floats, then things get a little bit more</p>



<p class="wp-block-paragraph">complicated. So for starters, what if we did a function that was, that returned a double?</p>



<p class="wp-block-paragraph">that returned a double. So let&#8217;s say we have a function G and it returns a double. Maybe it</p>



<p class="wp-block-paragraph">still takes in a long and a double pointer. If it returns a double, let&#8217;s see, is this still</p>



<p class="wp-block-paragraph">lining up? I don&#8217;t think it&#8217;s lining up anymore. There we go. If it&#8217;s returning a double, then</p>



<p class="wp-block-paragraph">that means we have to return to the caller with the XMM0 register. So XMM0 is always designated as</p>



<p class="wp-block-paragraph">the well the first float argument but also the float return value so keep in</p>



<p class="wp-block-paragraph">mind if I&#8217;m returning a double to the caller I&#8217;m not going to use our a X at</p>



<p class="wp-block-paragraph">all I&#8217;m just going to use XMM zero load it up with the return value and then</p>



<p class="wp-block-paragraph">return to the caller and that&#8217;s it if I wanted to do another function let&#8217;s say</p>



<p class="wp-block-paragraph">hmm how about H and then we&#8217;ll say I don&#8217;t know maybe the first argument is a</p>



<p class="wp-block-paragraph">argument is a float we call it a double a that means rdi is not going to be the argument that</p>



<p class="wp-block-paragraph">or the register that we look at to see our incoming double it has to be a float register</p>



<p class="wp-block-paragraph">xmm0 is the float register that we look at for the first argument the float registers are really</p>



<p class="wp-block-paragraph">easy you just kind of go in order like xmm0 is the first argument xmm1 is the next argument xmm2</p>



<p class="wp-block-paragraph">the general purpose uh integer registers they&#8217;re a little more complicated you have to remember</p>



<p class="wp-block-paragraph">their their labels uh you know like the letters have an ordering to it uh suppose for the sake</p>



<p class="wp-block-paragraph">of argument that we&#8217;re going to return a double from this also uh in that case let&#8217;s see did i</p>



<p class="wp-block-paragraph">just mess the formatting up again i think that&#8217;s okay um again we&#8217;re going to return xmm0</p>



<p class="wp-block-paragraph">notice how we&#8217;re returning xmm0 oh that&#8217;s why i spaced it forward notice how we are</p>



<p class="wp-block-paragraph">forward notice how we are returning xmm0 and we&#8217;re also taking xmm0 these float registers tend to be</p>



<p class="wp-block-paragraph">reused all over the place none of them are designated as callee saved within the abi</p>



<p class="wp-block-paragraph">so you always have to stash their values somewhere if you&#8217;re ever going to make a function call or</p>



<p class="wp-block-paragraph">anything or a system call the other thing to keep in mind is notice how rsi i still have rsi there</p>



<p class="wp-block-paragraph">for the second incoming argument you would imagine that rsi is supposed to be the second argument</p>



<p class="wp-block-paragraph">supposed to be the second argument even if it uh you know comes after a double but that&#8217;s not</p>



<p class="wp-block-paragraph">actually true because the the order i guess like the the ordering of the uh of the registers they</p>



<p class="wp-block-paragraph">only count against their own class like the float registers the float arguments they only count</p>



<p class="wp-block-paragraph">against the position of the float and the uh of the float registers and and and the general purpose</p>



<p class="wp-block-paragraph">they only count against themselves.</p>



<p class="wp-block-paragraph">What I&#8217;m trying to say is that RSI is the second integer argument,</p>



<p class="wp-block-paragraph">but if you look at the signature, we only have one integer argument, right?</p>



<p class="wp-block-paragraph">So RSI is not the second integer argument.</p>



<p class="wp-block-paragraph">It&#8217;s actually the first integer argument.</p>



<p class="wp-block-paragraph">So I&#8217;m going to put an RDI there.</p>



<p class="wp-block-paragraph">You probably can infer that the second float argument should be XMM1 at this point, I hope.</p>



<p class="wp-block-paragraph">and that&#8217;s true if we just say that there&#8217;s going to be another double</p>



<p class="wp-block-paragraph">that this function takes then it&#8217;s going to be xmm1</p>



<p class="wp-block-paragraph">not xmm2 because if you&#8217;re just counting the arguments</p>



<p class="wp-block-paragraph">and you&#8217;re sort of grouping the integers with the floats together</p>



<p class="wp-block-paragraph">you might think well first argument is xmm0 second argument would have been xmm1</p>



<p class="wp-block-paragraph">third argument would have been xmm2 therefore if I see</p>



<p class="wp-block-paragraph">a float coming into the third argument position it&#8217;ll be xmm2</p>



<p class="wp-block-paragraph">No, the integers don&#8217;t count against the floats and the floats don&#8217;t count against the integers.</p>



<p class="wp-block-paragraph">That&#8217;s why RDI is the first argument for integers, even though it&#8217;s in the second position,</p>



<p class="wp-block-paragraph">it&#8217;s still the first integer that we see.</p>



<p class="wp-block-paragraph">And XMM1 is the second float argument that we see.</p>



<p class="wp-block-paragraph">That&#8217;s why it&#8217;s XMM1 instead of XMM2, because we&#8217;re looking at it in the third position.</p>



<p class="wp-block-paragraph">So you&#8217;re not really looking at, whoops, you&#8217;re not looking at overall position.</p>



<p class="wp-block-paragraph">at position only counting that type of register let me pull open my favorite book to get a little</p>



<p class="wp-block-paragraph">bit more clarity on this um this book is written by a wonderful professor dr ed jorgensen phd</p>



<p class="wp-block-paragraph">this book i did not write it ed jorgensen wrote this book it&#8217;s called x86 64 assembly language</p>



<p class="wp-block-paragraph">programming with ubuntu this book can turn you into an expert with yasm assembly so uh</p>



<p class="wp-block-paragraph">I recommend everybody grab a copy. It&#8217;s also free and it&#8217;s got a copy left license.</p>



<p class="wp-block-paragraph">You can see like the license here. But anyway, I wanted to open up this book real fast</p>



<p class="wp-block-paragraph">just to show you the registers. Okay. I&#8217;m going to search for Kali saved so I don&#8217;t have to fumble</p>



<p class="wp-block-paragraph">around the book too long. Kali saved and that brings us to section 12.8.2 register usage.</p>



<p class="wp-block-paragraph">let&#8217;s see callee saved you&#8217;ll see rbx is callee saved rbp is callee saved probably shouldn&#8217;t</p>



<p class="wp-block-paragraph">mess with that one too much r12 13 14 15 all callee saved there&#8217;s a couple temporary registers</p>



<p class="wp-block-paragraph">but if you look carefully rdi is designated as the first argument but it should say really</p>



<p class="wp-block-paragraph">the first integer argument so rdi is the first integer argument that we see that&#8217;s why</p>



<p class="wp-block-paragraph">argument that we see that&#8217;s why in all of these examples the first integer argument that we</p>



<p class="wp-block-paragraph">actually see is rdi so like for function f long a that was the first integer argument</p>



<p class="wp-block-paragraph">and then in the function g it&#8217;s also the first integer argument and then in function h rdi is</p>



<p class="wp-block-paragraph">the double pointer b because that is the first integer argument that we actually see</p>



<p class="wp-block-paragraph">so keep that in mind you would just repeat that pattern you know the second integer argument you</p>



<p class="wp-block-paragraph">second integer argument you see that&#8217;s RSI. That&#8217;s why RSI is the pointer here because</p>



<p class="wp-block-paragraph">pointers are integers and the pointer here and we don&#8217;t have anything else but if I guess if we</p>



<p class="wp-block-paragraph">added another you know let&#8217;s say a long D then it would be RSI right there. Hopefully that makes</p>



<p class="wp-block-paragraph">sense and you can you can carry this logic forward for the first argument the second argument the</p>



<p class="wp-block-paragraph">and then R8 for the fifth argument R9 for the sixth argument and then you can</p>



<p class="wp-block-paragraph">look for further arguments on the stack if you need more than that but basically</p>



<p class="wp-block-paragraph">that&#8217;s how you count them up and then the float registers are just a lot</p>



<p class="wp-block-paragraph">easier they always start at zero and they go all the way up to 15 and so I</p>



<p class="wp-block-paragraph">don&#8217;t know maybe I should just copy paste well maybe I&#8217;ll start it from</p>



<p class="wp-block-paragraph">scratch we&#8217;ll say dub no let&#8217;s let&#8217;s mix it up a little bit more we&#8217;ll say long</p>



<p class="wp-block-paragraph">And the long is going to be returned with the RAX.</p>



<p class="wp-block-paragraph">And I guess I have to space this forward a little bit.</p>



<p class="wp-block-paragraph">And then if we have a bunch of floats, say double A.</p>



<p class="wp-block-paragraph">Can I maybe just copy paste this a little bit so I don&#8217;t have to do so much typing?</p>



<p class="wp-block-paragraph">Okay, so we&#8217;ll do double A, double B, double C, double D, double E.</p>



<p class="wp-block-paragraph">and then this is going to be xmm zero why did i put the extra comment there i don&#8217;t know</p>



<p class="wp-block-paragraph">and then we&#8217;re going to have xmm one and then we&#8217;re going to have xmm two xmm three and so forth</p>



<p class="wp-block-paragraph">and just to make sure you&#8217;re paying attention ask yourself this real fast if i were to insert</p>



<p class="wp-block-paragraph">z what register would we use for that would it be the one two three four five the fifth argument</p>



<p class="wp-block-paragraph">would it be uh would it be r8 or would it be something else hopefully you understand now</p>



<p class="wp-block-paragraph">let&#8217;s say we do a character pointer that it would be rdi because that&#8217;s the first integer argument</p>



<p class="wp-block-paragraph">that we actually see all right and then you know to return we would return in rax and then the</p>



<p class="wp-block-paragraph">return in RAX and then the floats just keep counting notice how it skipped from XMM2 or it</p>



<p class="wp-block-paragraph">went from XMM2 to XMM3 even though we crossed over an integer argument in between because the</p>



<p class="wp-block-paragraph">float arguments don&#8217;t count against the integers and the ints don&#8217;t count against the floats</p>



<p class="wp-block-paragraph">let&#8217;s see I think</p>



<p class="wp-block-paragraph">well if you want you can look at section 18.2 which just sort of talks about the floating</p>



<p class="wp-block-paragraph">which just sort of talks about the floating point registers we have 0 through 15 but I don&#8217;t think</p>



<p class="wp-block-paragraph">we really need to do that here I think we pretty much have everything we need to know to to call</p>



<p class="wp-block-paragraph">on functions with mixed arguments and to have a function within assembly that supports mixed</p>



<p class="wp-block-paragraph">arguments so again if your C or C++ modules are expecting some kind of signature like this now you</p>



<p class="wp-block-paragraph">know how to calculate what registers they&#8217;re actually going to populate by the time execution</p>



<p class="wp-block-paragraph">execution jumps into your assembly function.</p>



<p class="wp-block-paragraph">And if you want to call a C or a C++ function from assembly,</p>



<p class="wp-block-paragraph">now you know which registers to populate so that the the other function that</p>



<p class="wp-block-paragraph">you&#8217;re calling can receive the right data from you.</p>



<p class="wp-block-paragraph">Don&#8217;t forget about name mangling, which I talked about in another video.</p>



<p class="wp-block-paragraph">Okay, I think that&#8217;s all I really have to say.</p>



<p class="wp-block-paragraph">This video was pretty short.</p>



<p class="wp-block-paragraph">Thank you so much for watching.</p>



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



<p class="wp-block-paragraph">I&#8217;ll see you in the next video.</p>



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



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



<p class="wp-block-paragraph">and subscribe you know sometimes i&#8217;m sleeping in the middle of the night and i just wake up because</p>



<p class="wp-block-paragraph">i know somebody subscribed or followed it just wakes me up and i get filled with joy that&#8217;s</p>



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



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



<p class="wp-block-paragraph">and then I&#8217;ll just wake up. I promise that&#8217;s what will happen. Also, if you look at the middle of</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 if you have a suggestion for</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 i also</p>



<p class="wp-block-paragraph">wake up for those in the middle of the night i get i wake up in a cold sweat and i&#8217;m like it would</p>



<p class="wp-block-paragraph">really it really mean the world to me i would really appreciate it so again thank you so much</p>



<p class="wp-block-paragraph">for watching this video and um enjoy the cool music as as i fade into the darkness which is</p>



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



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/mixed-function-arguments-in-x86-64-assembly-integers-floats-explained/">Mixed Function Arguments in x86-64 Assembly &#8211; Integers &amp; Floats Explained</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/mixed-function-arguments-in-x86-64-assembly-integers-floats-explained/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>x86-64 Assembly: Integer Data Movement, Pointers, and Dereferencing Explained (YASM on Ubuntu)</title>
		<link>https://www.NeuralLantern.com/x86-64-assembly-integer-data-movement-pointers-and-dereferencing-explained-yasm-on-ubuntu/</link>
					<comments>https://www.NeuralLantern.com/x86-64-assembly-integer-data-movement-pointers-and-dereferencing-explained-yasm-on-ubuntu/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Tue, 23 Dec 2025 13:22:17 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[dereferencing assembly]]></category>
		<category><![CDATA[gdb debugging assembly]]></category>
		<category><![CDATA[general purpose registers]]></category>
		<category><![CDATA[integer data movement]]></category>
		<category><![CDATA[lea instruction]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[mov instruction assembly]]></category>
		<category><![CDATA[pointer arithmetic assembly]]></category>
		<category><![CDATA[pointers in assembly]]></category>
		<category><![CDATA[systems programming]]></category>
		<category><![CDATA[ubuntu assembly programming]]></category>
		<category><![CDATA[x86 assembly tutorial]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=269</guid>

					<description><![CDATA[<p>This practical x86-64 assembly language tutorial explains integer data movement between registers and memory, the importance of data size specifiers (byte, word, dword, qword), the difference between pointers and dereferenced values, pointer arithmetic for array access, and using LEA for address calculation - all demonstrated with YASM on Ubuntu Linux and inspected via GDB.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-integer-data-movement-pointers-and-dereferencing-explained-yasm-on-ubuntu/">x86-64 Assembly: Integer Data Movement, Pointers, and Dereferencing Explained (YASM on Ubuntu)</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="x86-64 Assembly: Integer Data Movement, Pointers, and Dereferencing Explained (YASM on Ubuntu)" width="1380" height="776" src="https://www.youtube.com/embed/zGrnEPrpmeg?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 hands-on x86-64 assembly tutorial, we dive deep into moving integer data between registers and memory, working with pointers, dereferencing, and pointer arithmetic using YASM on Ubuntu.</p>



<p class="wp-block-paragraph">You&#8217;ll see practical examples of:</p>



<ul class="wp-block-list">
<li>Moving immediates and data between general-purpose registers</li>



<li>Loading different sized integers (byte, word, dword, qword) from memory</li>



<li>Why data size specifiers matter and what happens with overflow/underflow</li>



<li>The difference between moving a pointer vs dereferencing it</li>



<li>Pointer arithmetic to access array elements</li>



<li>Using LEA for address calculation without dereferencing</li>



<li>Debugging with GDB to inspect registers step-by-step</li>
</ul>



<p class="wp-block-paragraph">Perfect for anyone learning low-level programming, systems programming, or wanting to truly understand how pointers work at the assembly level. No prior expert knowledge required &#8211; we build it up with clear examples.</p>



<p class="wp-block-paragraph">Code and examples are shown live, compiled with YASM, linked with LD, and debugged with GDB.</p>



<p class="wp-block-paragraph">If you&#8217;re into assembly, reverse engineering, operating systems, or just love understanding how computers really work, this one is for you.</p>



<p class="wp-block-paragraph">Introduction to Integer Data Movement and Pointers 00:00:00<br>Program Setup and Makefile 00:01:16<br>Data Section Definitions 00:01:38<br>System Calls for Output and Exit 00:01:52<br>Text Section and Entry Point 00:04:21<br>Printing Hello String 00:05:05<br>Moving Immediates to Registers 00:06:04<br>Copying Between Registers 00:06:27<br>Dereferencing Pointers from Memory 00:07:16<br>Specifying Data Sizes 00:08:01<br>Register Size Variants 00:09:45<br>Reference Book Explanation 00:10:50<br>Loading Words and Bytes 00:13:25<br>Debugger Breakpoint and Registers 00:14:28<br>Overflow Demonstration 00:16:25<br>Makefile Adjustments for Warnings 00:17:44<br>Proving Data Size Importance 00:20:32<br>Underflow and Overflow Examples 00:21:34<br>Symbols as Pointers 00:24:16<br>Array Definitions and Access 00:25:20<br>Pointer to Array Items 00:26:08<br>LEA Instruction for Addresses 00:27:32<br>Dereferencing Manipulated Pointers 00:29:01<br>Final Register Inspection 00:29:50<br>Conclusion and Subscribe Request 00:33:26</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 everybody! In this video I&#8217;m going to talk to you about integer data movement and pointers</p>



<p class="wp-block-paragraph">in an x86-64 YASM assembly program within Ubuntu but this should work for all of you x86 YASM people.</p>



<p class="wp-block-paragraph">Okay so what am I talking about for starters? I&#8217;m just talking about moving data pretty much. If you</p>



<p class="wp-block-paragraph">from one integer register to another or from one general purpose register to another.</p>



<p class="wp-block-paragraph">And if you know how to manipulate pointers in assembly and all that stuff,</p>



<p class="wp-block-paragraph">you probably don&#8217;t need this video.</p>



<p class="wp-block-paragraph">But if you are not an expert, then this will probably help you.</p>



<p class="wp-block-paragraph">Let me start off by saying, of course, that there are going to be lots of concepts in this video</p>



<p class="wp-block-paragraph">that I don&#8217;t actually explain because I&#8217;ve explained them in other videos.</p>



<p class="wp-block-paragraph">For example, this is not a Hello World tutorial where I show you how to actually,</p>



<p class="wp-block-paragraph">you know, build a basic Yasm assembly program.</p>



<p class="wp-block-paragraph">a basic yasm assembly program you can probably infer that just from this video but i&#8217;m not going</p>



<p class="wp-block-paragraph">to explain it also this is not a debugging video so i&#8217;m not really going to explain too much about</p>



<p class="wp-block-paragraph">gdb which is something i&#8217;m going to use to look at the registers but it might be nice for you to</p>



<p class="wp-block-paragraph">get a little preview i will record a full gdb tutorial in another video anyway so let&#8217;s see</p>



<p class="wp-block-paragraph">here uh for starters i&#8217;ve just kind of like created a little sample program here um well</p>



<p class="wp-block-paragraph">a make file which will just compile and execute my program again this is not a</p>



<p class="wp-block-paragraph">make file video tutorial check my other videos for that I&#8217;ve already made one</p>



<p class="wp-block-paragraph">for now I&#8217;m just gonna start making a program and I&#8217;m gonna give it a data</p>



<p class="wp-block-paragraph">section so first thing I&#8217;m gonna give it is like well let&#8217;s say I&#8217;ll give it a</p>



<p class="wp-block-paragraph">bunch of data then I&#8217;ll kind of explain what I gave it again this is not a</p>



<p class="wp-block-paragraph">basics Yasum tutorial so see my other videos but you know just simply put I&#8217;m</p>



<p class="wp-block-paragraph">You know just simply put I&#8217;m gonna write to standard output so I do I remember the system call code for for writing</p>



<p class="wp-block-paragraph">And then I&#8217;m gonna exit because this is a pure assembly program that I&#8217;m compiling. There&#8217;s this is not a hybrid program</p>



<p class="wp-block-paragraph">We&#8217;re not doing GCC. We&#8217;re just doing LD</p>



<p class="wp-block-paragraph">So I&#8217;m just gonna say a call code 60 to exit a file descriptor for stdin actually I don&#8217;t really need that</p>



<p class="wp-block-paragraph">Let me just double check that I don&#8217;t need that in my solution</p>



<p class="wp-block-paragraph">Yeah, I like to reuse these things so we don&#8217;t really need standard input</p>



<p class="wp-block-paragraph">standard input. We&#8217;re just going to do standard output, you know, printing to the terminal.</p>



<p class="wp-block-paragraph">We&#8217;re going to exit with a success code of zero. And then I made a couple of, well, just one C</p>



<p class="wp-block-paragraph">string where I&#8217;m just basically saying, hello, my name is so-and-so. So you can know that the</p>



<p class="wp-block-paragraph">program actually started in case it crashes later. That&#8217;s not my name. I just love those kinds of</p>



<p class="wp-block-paragraph">names. And then I&#8217;m just going to make a bunch of integers of different data sizes. So this is kind</p>



<p class="wp-block-paragraph">sizes but just real fast because these are going to be directly used in our video we uh we have a</p>



<p class="wp-block-paragraph">quad word here so i&#8217;m defining an integer called my long int and i&#8217;m saying that it&#8217;s a quad word</p>



<p class="wp-block-paragraph">with dq q for quad word and then i&#8217;m just giving a pretty big integer same thing for a regular d</p>



<p class="wp-block-paragraph">word which is just a double word so that&#8217;s half of a quad word and then another thing with a whoops</p>



<p class="wp-block-paragraph">w not a q let me just fix that in my solution oh dear then i&#8217;m going to do a word which is a six two</p>



<p class="wp-block-paragraph">one one one a word is two bytes of data we&#8217;re starting to get dangerously into overflow territory</p>



<p class="wp-block-paragraph">when i was originally writing this solution i had like a bunch of nines up here and the number that</p>



<p class="wp-block-paragraph">i ended up seeing on the screen was totally different and i forgot oh a word is just two bytes</p>



<p class="wp-block-paragraph">so even if it&#8217;s unsigned the highest thing that it can actually represent is um</p>



<ol start="65535" class="wp-block-list">
<li>So that&#8217;s not good. Then I&#8217;m going to just store a byte here with DB and put some twos there.</li>
</ol>



<p class="wp-block-paragraph">And then I&#8217;m going to make an array of integers. Putting an array, making an array in the regular</p>



<p class="wp-block-paragraph">data section, that&#8217;s not as good as putting it in the BSS section if you want a lot of integers.</p>



<p class="wp-block-paragraph">But I&#8217;m just going to specify a small number of integers so I can just put it directly into the</p>



<p class="wp-block-paragraph">BSS is for like giant arrays of uninitialized data and the data section is for whatever you can handle typing pretty much of initialized data.</p>



<p class="wp-block-paragraph">So now I&#8217;m going to start my text section and my program&#8217;s entry point.</p>



<p class="wp-block-paragraph">Again, this is not a Yasm basics tutorial. See my other videos.</p>



<p class="wp-block-paragraph">And so we&#8217;re going to start by just saying here&#8217;s my entry point where the operating system can sort of jump into my program.</p>



<p class="wp-block-paragraph">to just you know exit the program let&#8217;s actually see if that works so far it</p>



<p class="wp-block-paragraph">should do nothing but but at least exit with success I&#8217;m gonna go clear and make</p>



<p class="wp-block-paragraph">run and notice how it just kind of like ran it and then nothing happened okay so</p>



<p class="wp-block-paragraph">that&#8217;s great how come it didn&#8217;t print though what did I do wrong oh I forgot</p>



<p class="wp-block-paragraph">to copy paste the code that prints something okay let me get my solution up</p>



<p class="wp-block-paragraph">here so the first thing we&#8217;re really going to do is just use a system call to</p>



<p class="wp-block-paragraph">call to print the word hello again this is not a system call video see my other videos</p>



<p class="wp-block-paragraph">and um so now we&#8217;re just we&#8217;re just going to print our hello string</p>



<p class="wp-block-paragraph">with a system call let me run it one more time we should see like a quick hello</p>



<p class="wp-block-paragraph">yeah okay my name is uh chaplain mondover it&#8217;s not maybe i should change that to it&#8217;s not my name is</p>



<p class="wp-block-paragraph">not no i don&#8217;t want to do that sometimes i like to pretend that my name is chaplain mondover</p>



<p class="wp-block-paragraph">name is check lane Mondover you should too it&#8217;s fun anyway so the first thing that I&#8217;m going to</p>



<p class="wp-block-paragraph">do here is I&#8217;m going to move an immediate well let me copy paste a whole chunk of code and then</p>



<p class="wp-block-paragraph">I&#8217;ll explain it one by one I&#8217;m basically going to use data movement instructions to move a bunch of</p>



<p class="wp-block-paragraph">data into a bunch of registers and then I&#8217;m going to hit a break point or I&#8217;m going to I&#8217;m going to</p>



<p class="wp-block-paragraph">sort of do like a non-operation piece of code so that I can easily break on it in my debugger so</p>



<p class="wp-block-paragraph">the results. Okay so first off I&#8217;m going to move an immediate into R12. The</p>



<p class="wp-block-paragraph">instruction for moving data into general purpose registers or the integer</p>



<p class="wp-block-paragraph">registers is just MOV so that&#8217;s fine probably you should know that by now but</p>



<p class="wp-block-paragraph">I&#8217;m going to move an immediate into R10 and I&#8217;m going to move 12876 into R10.</p>



<p class="wp-block-paragraph">So that&#8217;s how you move an immediate no problem. Then I&#8217;m going to first clear</p>



<p class="wp-block-paragraph">out R11 and then I&#8217;m going to move R10 into it. The reason I&#8217;m clearing out R11</p>



<p class="wp-block-paragraph">reason i&#8217;m clearing out r11 first is just to prove to you that we are actually moving data from one</p>



<p class="wp-block-paragraph">register into another because if i just i don&#8217;t know if i didn&#8217;t do that you might be tempted to</p>



<p class="wp-block-paragraph">to think well um maybe r11 already had that data in it so first it&#8217;s going to be just a zero and</p>



<p class="wp-block-paragraph">then it&#8217;s going to actually be whatever r10 had so we should see r10 and 11 both have one two eight</p>



<p class="wp-block-paragraph">register or a pointer register from one to another. And by the way, integers, those are</p>



<p class="wp-block-paragraph">pointers, or rather, should I say, pointers are integers. So if you&#8217;re moving an integer or you&#8217;re</p>



<p class="wp-block-paragraph">moving a pointer, the system just kind of sees that as a number either way. So you can use the</p>



<p class="wp-block-paragraph">move instruction for it. So now let&#8217;s use the pointer that was assigned to our long integer</p>



<p class="wp-block-paragraph">to move that number into R12. So let me just go up real fast. My long int up here,</p>



<p class="wp-block-paragraph">my long int up here it&#8217;s like a gigantic number and remember I said before that my long int is</p>



<p class="wp-block-paragraph">actually just a pointer to the memory location that begins to hold the quad word so my long</p>



<p class="wp-block-paragraph">int is really a pointer to one byte and there&#8217;s an eight byte allocation because it&#8217;s a quad word</p>



<p class="wp-block-paragraph">so when you sort of put this into your code and you let the system know that you want to move a</p>



<p class="wp-block-paragraph">then the system will scan eight bytes and interpret that as an integer so</p>



<p class="wp-block-paragraph">putting the the keyword quad word here I don&#8217;t think that&#8217;s necessary I usually</p>



<p class="wp-block-paragraph">do it anyway but you&#8217;ll see in the next few instructions that it&#8217;s a really</p>



<p class="wp-block-paragraph">really good idea to specify the data size because if you don&#8217;t you&#8217;re gonna</p>



<p class="wp-block-paragraph">actually get something wrong in other words if for some reason I moved I don&#8217;t</p>



<p class="wp-block-paragraph">into R12 I could do it if I if I wrote it correctly but then I would be taking</p>



<p class="wp-block-paragraph">part of a quad word and trying to interpret that as the entire number and</p>



<p class="wp-block-paragraph">it&#8217;s probably going to be totally wrong we&#8217;ll have an example for that in a</p>



<p class="wp-block-paragraph">moment but anyway so that&#8217;s how we move a quad word we just specify keyword right</p>



<p class="wp-block-paragraph">before the memory location and we use the brackets to dereference if we</p>



<p class="wp-block-paragraph">didn&#8217;t dereference then we would actually be moving a pointer to the long</p>



<p class="wp-block-paragraph">integer into R12 we&#8217;ll do that soon too I&#8217;m going to try to do everything in this</p>



<p class="wp-block-paragraph">to do everything in this video then we&#8217;ll move a d word from uh from memory into r13 this is a</p>



<p class="wp-block-paragraph">little bit different notice how uh here on line 60 i kind of have to specify d word because that&#8217;s</p>



<p class="wp-block-paragraph">the data size that i&#8217;m trying to move into the r13 register so this is telling the system that</p>



<p class="wp-block-paragraph">there&#8217;s about four bytes that i need to scan not eight bytes and interpret those four bytes as an</p>



<p class="wp-block-paragraph">Moving it into R13, you know, I could have sworn a long time ago, you could just move that sort of thing directly into R13.</p>



<p class="wp-block-paragraph">But when I tried my solution before recording this video, I kept getting assembler errors until I added the D at the end.</p>



<p class="wp-block-paragraph">And so I just want you to know, oh, you know what, let&#8217;s look at a book real fast.</p>



<p class="wp-block-paragraph">Let&#8217;s look at my favorite book.</p>



<p class="wp-block-paragraph">I just want you to know that there are other forms of every register that can specify the register&#8217;s data size.</p>



<p class="wp-block-paragraph">What do I mean by that?</p>



<p class="wp-block-paragraph">itself then the system thinks you&#8217;re talking about all 64 bits of the 13 register the r13 register</p>



<p class="wp-block-paragraph">but if you type whoops if you type r13d then the system thinks you&#8217;re only talking about the lowest</p>



<p class="wp-block-paragraph">32 bits of that register so you&#8217;re also able to specify the data size of a register again i thought</p>



<p class="wp-block-paragraph">you should should a long time ago i thought i remembered moving a d word directly into r13 and</p>



<p class="wp-block-paragraph">it would just clear out the extra bits but i got warnings this time maybe that&#8217;s for the best</p>



<p class="wp-block-paragraph">that&#8217;s for the best forcing us to be more precise, but this will solve it. R13 just says,</p>



<p class="wp-block-paragraph">let&#8217;s only talk about a D word. So let me go, let me go to my favorite book here.</p>



<p class="wp-block-paragraph">Where the heck is my favorite book? Okay. I guess I have to like, there we go.</p>



<p class="wp-block-paragraph">Okay. There&#8217;s my favorite book. So, um, I&#8217;m going to go to, uh, let&#8217;s, let me just search for,</p>



<p class="wp-block-paragraph">just search for I can&#8217;t remember where it is our 13 D yeah there it is okay so</p>



<p class="wp-block-paragraph">what we&#8217;re looking for is 2.3.1.1 let me show you real fast the top of the book</p>



<p class="wp-block-paragraph">just to give a shout out to the person who wrote this book I did not write this</p>



<p class="wp-block-paragraph">book this is a free and open source book that you can download for free</p>



<p class="wp-block-paragraph">everybody should get a copy to turn yourselves into an expert it&#8217;s called</p>



<p class="wp-block-paragraph">x86 64 assembly language programming with Ubuntu it&#8217;s written by a brilliant</p>



<p class="wp-block-paragraph">professor. This is like a semi recent version. There&#8217;s probably a newer one now, but yeah,</p>



<p class="wp-block-paragraph">it&#8217;s a great book. I love it. So I&#8217;m going to go back to what the heck was I just doing?</p>



<p class="wp-block-paragraph">Was it 2.3.1.1? Yeah. Okay. So in this book section 2.3.1.1, the section entitled general</p>



<p class="wp-block-paragraph">purpose registers, notice how there are a bunch of different versions of each register. So if you</p>



<p class="wp-block-paragraph">So if you look in this column right here, we have, oh, I get to use my annotator.</p>



<p class="wp-block-paragraph">We have a, what were we just looking at?</p>



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



<p class="wp-block-paragraph">Notice how R12 here is a designated as a 64 bit register.</p>



<p class="wp-block-paragraph">But if you wanted to use the R12 register and only use 32 bits of it, the lowest 32</p>



<p class="wp-block-paragraph">bits, then this is the form you would use for the R12 register.</p>



<p class="wp-block-paragraph">So 32 bits.</p>



<p class="wp-block-paragraph">And if you wanted to use only a word&#8217;s worth, you know, 16 bits.</p>



<p class="wp-block-paragraph">Well there you go.</p>



<p class="wp-block-paragraph">two bytes on these systems or 16 bits and then if you just wanted to use one</p>



<p class="wp-block-paragraph">byte of the register well you can you just put a B at the end of it it&#8217;s</p>



<p class="wp-block-paragraph">pretty sweet and convenient it&#8217;s not too hard to remember that D is for D word and</p>



<p class="wp-block-paragraph">W is for word and B is for bytes it just gets a little confusing when you&#8217;re</p>



<p class="wp-block-paragraph">using the other registers like the the RAX register it&#8217;s got EAX as its 32-bit</p>



<p class="wp-block-paragraph">version and then AX as its 16-bit version and AL as its 8-bit version if</p>



<p class="wp-block-paragraph">it&#8217;s a 8-bit version if you can remember that there&#8217;s an L I guess that&#8217;s not too bad but you</p>



<p class="wp-block-paragraph">know remembering the X and then it changes to an I over here and a P over here I&#8217;m personally not</p>



<p class="wp-block-paragraph">a fan so I always come back to this book to try and remember what I&#8217;m supposed to be putting for</p>



<p class="wp-block-paragraph">these registers because I can&#8217;t remember all the time or rather I can almost never remember but we</p>



<p class="wp-block-paragraph">So, I want to take a D word from memory and load it up into the R13 register.</p>



<p class="wp-block-paragraph">So I got to specify the R13D.</p>



<p class="wp-block-paragraph">And, you know, maybe I&#8217;ll take that out and show you that it doesn&#8217;t compile later, if I can remember.</p>



<p class="wp-block-paragraph">But I&#8217;m going to now take a D word, do the same thing into R13.</p>



<p class="wp-block-paragraph">Oh, sorry, no.</p>



<p class="wp-block-paragraph">I&#8217;m going to take a regular word and do the same thing basically into R14.</p>



<p class="wp-block-paragraph">But it&#8217;s just going to be a word instead of a D word.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to take a single byte and load it up into R15.</p>



<p class="wp-block-paragraph">single byte and load it up into R15. Then I&#8217;m going to call Nope, which is an instruction that</p>



<p class="wp-block-paragraph">just does nothing. And it&#8217;s useful sometimes if you want to have a place where you can stop your</p>



<p class="wp-block-paragraph">program and sort of inspect the results of your program in your debugger. So there&#8217;s a lot more</p>



<p class="wp-block-paragraph">to this program. I&#8217;m going to go ahead and just run it right now, just to make sure that it compiles.</p>



<p class="wp-block-paragraph">And then maybe I&#8217;ll add a breakpoint here. Let&#8217;s go clear and make run. Okay, so it ran,</p>



<p class="wp-block-paragraph">Okay, so it ran, it compiled, it was fine.</p>



<p class="wp-block-paragraph">And now I&#8217;m going to open up a new little window and I&#8217;m going to launch the program.</p>



<p class="wp-block-paragraph">Notice how the program that has been compiled is called main.</p>



<p class="wp-block-paragraph">So I&#8217;m going to launch the program into the GDB debugger.</p>



<p class="wp-block-paragraph">This is not a GDB video.</p>



<p class="wp-block-paragraph">So I&#8217;m going to be skimming over this, check future videos for a full GDB tutorial.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do a break point right there at line 72.</p>



<p class="wp-block-paragraph">So that I can just kind of inspect the state of the register.</p>



<p class="wp-block-paragraph">break at main.asm line 72 and then just to be sure I did that correctly I&#8217;m going to say info</p>



<p class="wp-block-paragraph">breakpoints or just a b is fine and it&#8217;s like looks like I set it up correctly then I can type</p>



<p class="wp-block-paragraph">run to see what&#8217;s up notice how it starts to run and immediately stops at that breakpoint</p>



<p class="wp-block-paragraph">you can tell it stops there because it&#8217;s at line 72 then I&#8217;m just going to do info registers or</p>



<p class="wp-block-paragraph">info r just to print out all my registers and we should see a confirmation of what we were hoping</p>



<p class="wp-block-paragraph">so if I kind of like maybe I&#8217;ll pin this to the top for a second always on top how about that</p>



<p class="wp-block-paragraph">so first we moved an immediate into r10 so notice how 12876 is in r10 right there and then on line</p>



<p class="wp-block-paragraph">53 we basically copied r10 into r11 so that means in r11 we see the same value</p>



<p class="wp-block-paragraph">and then we&#8217;ll move a quad word from the long integer we&#8217;ll dereference that pointer</p>



<p class="wp-block-paragraph">at R12 it&#8217;s like this gigantic number if I scroll all the way up it&#8217;s the same number here as in our</p>



<p class="wp-block-paragraph">source code so that seemed to work and then we&#8217;re going to move the D word next I guess into R13</p>



<p class="wp-block-paragraph">so that&#8217;s this number right here so that seemed to have worked it&#8217;s this number right here the</p>



<p class="wp-block-paragraph">D word and I&#8217;m going to get lost so fast scrolling up and down like this now we&#8217;re going to move a</p>



<p class="wp-block-paragraph">one one and there it is right there and then r15 is just gonna take a bite and</p>



<p class="wp-block-paragraph">it&#8217;s gonna take a bite oh now I&#8217;m hungry now I&#8217;m hungry that activated it it&#8217;s</p>



<p class="wp-block-paragraph">only been like think four or five hours since I ate I guess that is usually when</p>



<p class="wp-block-paragraph">I need to eat again even though I gorge and fall asleep on the floor because I</p>



<p class="wp-block-paragraph">home r15 anyway holds the correct value it&#8217;s 222 i just want to show you overflow real fast</p>



<p class="wp-block-paragraph">we look at r14 uh we&#8217;re using the word version so it&#8217;s only going to read you know uh two bytes</p>



<p class="wp-block-paragraph">from that memory location and it&#8217;s only going to consider uh you know 16 bits as being valid in uh</p>



<p class="wp-block-paragraph">in that register so if i use a number that&#8217;s too big it&#8217;ll overflow two bytes the maximum number</p>



<p class="wp-block-paragraph">three, five or from zero to that number or six, five, five, three, six combinations.</p>



<p class="wp-block-paragraph">So if I just stick a nine in front of that right now, that should overflow the system.</p>



<p class="wp-block-paragraph">So let me try it one more time.</p>



<p class="wp-block-paragraph">I want to do Q because that was the last thing we needed to look at.</p>



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



<p class="wp-block-paragraph">Oh gosh, what&#8217;s happening here?</p>



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



<p class="wp-block-paragraph">How about I do clear and make build, which is in my make file and GDB main.</p>



<p class="wp-block-paragraph">So I don&#8217;t have to type all this stuff out again every single time.</p>



<p class="wp-block-paragraph">have to type all this stuff out again every single time what did i just do oh nice okay you know what</p>



<p class="wp-block-paragraph">i swear it didn&#8217;t used to do this before it&#8217;s giving me a nice warning saying that value does</p>



<p class="wp-block-paragraph">not fit in a 16-bit field i guess if i turned warnings off which i don&#8217;t want to do then the</p>



<p class="wp-block-paragraph">program would run and it would overflow eh do you want to turn it off let&#8217;s turn off warnings okay</p>



<p class="wp-block-paragraph">much attention to it. Um, where the heck are the warnings? Where&#8217;s the assembler running? There it</p>



<p class="wp-block-paragraph">is. Yasum flags. And that&#8217;s, so that&#8217;s my variable up there. There we go. Okay. So I&#8217;m just going to</p>



<p class="wp-block-paragraph">make a copy of this line, comment out the original, and then I&#8217;m going to take out, uh, the command</p>



<p class="wp-block-paragraph">that says, or the flag that says convert warnings into errors. Your compiler is your friend. It was</p>



<p class="wp-block-paragraph">So I am now going to do a breakpoint at 72.</p>



<p class="wp-block-paragraph">So break at main.asm972 and then run and then it breaks.</p>



<p class="wp-block-paragraph">And then I go input registers.</p>



<p class="wp-block-paragraph">Then if I look at, what was I just talking about?</p>



<p class="wp-block-paragraph">R14, was that it?</p>



<p class="wp-block-paragraph">Notice how 44607 for the word, that&#8217;s a totally different value, right?</p>



<p class="wp-block-paragraph">So this is what happens when you overflow or underflow.</p>



<p class="wp-block-paragraph">You got to be careful about your data sizes.</p>



<p class="wp-block-paragraph">I&#8217;m just going to do this again one more time just to make sure.</p>



<p class="wp-block-paragraph">one more time just to make sure that it&#8217;s fixed now that I moved it and I&#8217;m</p>



<p class="wp-block-paragraph">gonna I&#8217;m gonna read that part about warnings because I love converting</p>



<p class="wp-block-paragraph">warnings to errors as just a way to help myself write better code okay so let&#8217;s</p>



<p class="wp-block-paragraph">do that and you know what I&#8217;m gonna do too I added another target here again</p>



<p class="wp-block-paragraph">this is not a Yasm or sorry this is not this is kind of a Yasm video this is not</p>



<p class="wp-block-paragraph">extra file called gdb.txt where I can stick gdb commands and so I don&#8217;t want</p>



<p class="wp-block-paragraph">to continue to type my breakpoints over and over over again every single time so</p>



<p class="wp-block-paragraph">I know for sure I&#8217;m not going to change the program anymore at least I&#8217;m not</p>



<p class="wp-block-paragraph">going to shift the lines up and down before 72 so I&#8217;m going to do breakpoint</p>



<p class="wp-block-paragraph">you can type like break or I think you can type either break or the full word</p>



<p class="wp-block-paragraph">breakpoint but I just type B name of the source code file a.asm at line 72 and so</p>



<p class="wp-block-paragraph">And so this file right here, again, this is not a GDB tutorial, but in my make file, I have it set up so that GDB will execute whatever commands I put into this text file.</p>



<p class="wp-block-paragraph">So it just kind of saves me typing.</p>



<p class="wp-block-paragraph">So let&#8217;s put a breakpoint there and then we&#8217;ll say info breakpoints and then we&#8217;ll just run the program in that way.</p>



<p class="wp-block-paragraph">I can have a little bit more information.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do make build instead of make build and then running GDB.</p>



<p class="wp-block-paragraph">I&#8217;m going to say make debug.</p>



<p class="wp-block-paragraph">That&#8217;s another target in my make file.</p>



<p class="wp-block-paragraph">make file so then you can see it added the breakpoint for me and then it</p>



<p class="wp-block-paragraph">immediately broke on that and you know what yeah I guess I&#8217;ll just leave it in</p>



<p class="wp-block-paragraph">where I have to type info registers okay so we&#8217;re just making sure I think that</p>



<p class="wp-block-paragraph">R14 is still a valid value and that it was fixed okay so we&#8217;ve gotten this far</p>



<p class="wp-block-paragraph">the next thing we need to do is prove that data size matters by specifying a</p>



<p class="wp-block-paragraph">specifying a bad data size for our long and then for our byte. So I&#8217;m going to copy paste a little</p>



<p class="wp-block-paragraph">proof here that is followed by another non-operation. So I&#8217;m going to do this. And so</p>



<p class="wp-block-paragraph">I&#8217;m going to try to prove to you that the data size matters by specifying a bad data size. I</p>



<p class="wp-block-paragraph">just proved to you that it kind of mattered or it definitely mattered in terms of overflow and</p>



<p class="wp-block-paragraph">underflow, but I&#8217;m trying to prove to you now that it matters when you&#8217;re reading. So I&#8217;m going to</p>



<p class="wp-block-paragraph">I&#8217;m going to clear out r12 and then I&#8217;m going to move a byte from the long integer into r12b</p>



<p class="wp-block-paragraph">and you know this would be a warning or I think it this wouldn&#8217;t compile or wouldn&#8217;t assemble if</p>



<p class="wp-block-paragraph">I didn&#8217;t have the b there so I&#8217;m basically telling the computer all right I want you to use only a</p>



<p class="wp-block-paragraph">byte of r12 and I want you to take one byte from memory and just put it in there but I&#8217;m taking it</p>



<p class="wp-block-paragraph">awful. And then right after that, I&#8217;m going to do another naughty thing. I&#8217;m going to move</p>



<p class="wp-block-paragraph">something into R15 and I&#8217;m going to word, I&#8217;m going to move a keyword of memory into R15,</p>



<p class="wp-block-paragraph">but I&#8217;m going to be taking it from the bytes memory. So remember a byte is just one byte.</p>



<p class="wp-block-paragraph">And so the next seven bytes that come after it definitely have nothing to do with that byte,</p>



<p class="wp-block-paragraph">but I&#8217;m going to interpret them all as a quad word. So we should get nonsense data</p>



<p class="wp-block-paragraph">also R15. I don&#8217;t really need to zero out R15 because the fact that I&#8217;m specifying R15 means</p>



<p class="wp-block-paragraph">I want to use all 64 bits. So it is going to definitely zero it out. And whatever we see in</p>



<p class="wp-block-paragraph">R15 is going to be the real thing. But in terms of R12B, we&#8217;re only going to be loading up one</p>



<p class="wp-block-paragraph">byte&#8217;s worth of that register. So that&#8217;s why I&#8217;m zeroing out the rest of it just to prove to you</p>



<p class="wp-block-paragraph">that we are truly getting nonsense. So now there&#8217;s going to be another break point at 82.</p>



<p class="wp-block-paragraph">I&#8217;m not going to break anymore at 72. I&#8217;m going to break at 82. So let&#8217;s do that.</p>



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



<p class="wp-block-paragraph">Oh gosh. Okay. Quit. I guess that means I need to comment this out. Let me do that.</p>



<p class="wp-block-paragraph">We&#8217;ll do break at main.asm at 82. And you could, you could do both of these two. Actually,</p>



<p class="wp-block-paragraph">let me do it first. We could, we could do multiple break points. Yeah, I&#8217;ll just do it first,</p>



<p class="wp-block-paragraph">So notice how it first breaks at the 72 and then I do C to continue and then suddenly</p>



<p class="wp-block-paragraph">it breaks at the 82.</p>



<p class="wp-block-paragraph">But I don&#8217;t want to have to hit continue a bunch of times so I&#8217;m going to comment that</p>



<p class="wp-block-paragraph">out for the future.</p>



<p class="wp-block-paragraph">Let me quit real fast to make sure I remember what GDB expects as a comment.</p>



<p class="wp-block-paragraph">Yeah, okay.</p>



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



<p class="wp-block-paragraph">Okay, so then I&#8217;m going to print the registers info registers.</p>



<p class="wp-block-paragraph">And we&#8217;re looking at R12 and R15.</p>



<p class="wp-block-paragraph">You can see R12 has the number 144, which definitely doesn&#8217;t make sense because there&#8217;s</p>



<p class="wp-block-paragraph">no 144 anywhere in our original data.</p>



<p class="wp-block-paragraph">in our original data so hopefully that&#8217;s proof to you that we&#8217;ve grabbed some junk</p>



<p class="wp-block-paragraph">sometimes maybe when you&#8217;re doing this at home if you&#8217;re just going to grab one bite</p>



<p class="wp-block-paragraph">maybe accidentally um no no that would definitely still look bad but for the r15</p>



<p class="wp-block-paragraph">if you&#8217;re going to grab a q words worth of data i don&#8217;t know maybe the junk data is like</p>



<p class="wp-block-paragraph">accidentally correct in a way where it would it would still make it look like the intended value</p>



<p class="wp-block-paragraph">the intended value no it wouldn&#8217;t there&#8217;s no way that would even happen no i guess the junk data</p>



<p class="wp-block-paragraph">could could work that way anyway so we&#8217;ll look at r15 two eight and a bunch of fours and then a two</p>



<p class="wp-block-paragraph">three eight um that value does not exist up here so again junk data mixed in with real data is is</p>



<p class="wp-block-paragraph">bad news okay so now let&#8217;s do another proof i want to prove to you that symbols are pointers</p>



<p class="wp-block-paragraph">you know like I&#8217;ve been saying and they need to be dereferenced so let&#8217;s first start off by</p>



<p class="wp-block-paragraph">moving the raw pointer for my long int into r10 and then into r11 we will dereference that</p>



<p class="wp-block-paragraph">pointer so in r10 we should see something that looks like a memory location or at least a relative</p>



<p class="wp-block-paragraph">memory location and then in r11 we should see the actual long integer like we saw before</p>



<p class="wp-block-paragraph">And then I&#8217;m going to mess around with some arrays.</p>



<p class="wp-block-paragraph">So remember up here we made an array.</p>



<p class="wp-block-paragraph">Let me minimize that real fast.</p>



<p class="wp-block-paragraph">We made an array, a very small array.</p>



<p class="wp-block-paragraph">I just specified a bunch of ones and then specified a bunch of twos.</p>



<p class="wp-block-paragraph">So it should be contiguous memory since it&#8217;s been allocated as an array with that comma</p>



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



<p class="wp-block-paragraph">We should see if we looked inside of the system inside of memory, we should see eight bytes</p>



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



<p class="wp-block-paragraph">And then right next to that, we should see eight bytes representing the twos.</p>



<p class="wp-block-paragraph">easy for us to sort of move our pointer around a little bit. So what else do I need to copy paste</p>



<p class="wp-block-paragraph">here? We got that long int. Okay. So I&#8217;m going to grab the first item and the second item in those</p>



<p class="wp-block-paragraph">arrays just using some pointer manipulation. So you know how to do this already. You just take</p>



<p class="wp-block-paragraph">like the pointer to some number and you just dereference it and you end up getting a number.</p>



<p class="wp-block-paragraph">I understand your feeling because you might be thinking, wait a minute, wait a minute.</p>



<p class="wp-block-paragraph">I understood it when it was dereferencing just one number, but didn&#8217;t you say this is an array?</p>



<p class="wp-block-paragraph">Why is it we can dereference the array and just get one number still?</p>



<p class="wp-block-paragraph">Well, remember, a pointer to an array is really a pointer to the first item in the array.</p>



<p class="wp-block-paragraph">That&#8217;s kind of how it works.</p>



<p class="wp-block-paragraph">So either way, whether you consider that to be a pointer to one number or a pointer to array,</p>



<p class="wp-block-paragraph">when we dereference it, we&#8217;ll get one number.</p>



<p class="wp-block-paragraph">If you want to get the other numbers, then you have to start manipulating the pointer</p>



<p class="wp-block-paragraph">or derefing in different ways.</p>



<p class="wp-block-paragraph">So here&#8217;s how we can get the second item.</p>



<p class="wp-block-paragraph">We deref the original pointer plus 8.</p>



<p class="wp-block-paragraph">8 because it&#8217;s a quad word.</p>



<p class="wp-block-paragraph">So, you know, it probably would be a little smarter to put that 8 up into a define somewhere</p>



<p class="wp-block-paragraph">so you&#8217;re not hard coding numbers, but I&#8217;m not going to do it.</p>



<p class="wp-block-paragraph">So basically in R12, we should see the 1s.</p>



<p class="wp-block-paragraph">In R13, we should see the 2s.</p>



<p class="wp-block-paragraph">And then a couple more things.</p>



<p class="wp-block-paragraph">Next, we will grab a pointer to the first item in the small array.</p>



<p class="wp-block-paragraph">And we&#8217;ll just stick that into R14.</p>



<p class="wp-block-paragraph">So, you know, we kind of did that with R10 already.</p>



<p class="wp-block-paragraph">But now I just want a pointer to the small array.</p>



<p class="wp-block-paragraph">And I&#8217;m going to keep it in R14 just so you can see the memory location.</p>



<p class="wp-block-paragraph">And then after that, we&#8217;re going to grab an actual pointer to the second item.</p>



<p class="wp-block-paragraph">hey how do you get a pointer to the second item well it&#8217;s that but then what</p>



<p class="wp-block-paragraph">if you didn&#8217;t want to dereference the pointer like what if R13 you know here</p>



<p class="wp-block-paragraph">clearly because we have these brackets for dereferencing you&#8217;re allowed to put</p>



<p class="wp-block-paragraph">the formulas inside of the brackets what if I wanted to get the address to the</p>



<p class="wp-block-paragraph">second item and not actually the second item we would not be allowed to remove</p>



<p class="wp-block-paragraph">the brackets the assembler wouldn&#8217;t like that so without another instruction</p>



<p class="wp-block-paragraph">way to actually get a pointer with a complicated mathematical formula. You know, you can do like</p>



<p class="wp-block-paragraph">multiplication inside of here and other things in parentheses. So we wouldn&#8217;t be able to do that</p>



<p class="wp-block-paragraph">without the other instruction that I&#8217;m introducing now called LEA. LEA just means, yeah, sure, we&#8217;re</p>



<p class="wp-block-paragraph">still going to use the brackets to figure out where the pointer is, like figure out what memory</p>



<p class="wp-block-paragraph">location we want with some sort of a formula. But then what will be stored in the first operand is</p>



<p class="wp-block-paragraph">This LEA won&#8217;t dereference just because there are brackets.</p>



<p class="wp-block-paragraph">LEA will keep the pointer.</p>



<p class="wp-block-paragraph">And then finally, once we actually have a pointer to the second item in R15,</p>



<p class="wp-block-paragraph">then we can move the second item into another register.</p>



<p class="wp-block-paragraph">And this is like an LOL because I ran out of registers already.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to use RDI, which is usually the first argument.</p>



<p class="wp-block-paragraph">It doesn&#8217;t matter. It&#8217;s fine.</p>



<p class="wp-block-paragraph">So if we dereference R15, it&#8217;s the same thing as dereferencing this.</p>



<p class="wp-block-paragraph">We could have also just dereferenced R14,</p>



<p class="wp-block-paragraph">and it would have been the same thing as just getting the value of the first item.</p>



<p class="wp-block-paragraph">But I hope you can see now that, you know,</p>



<p class="wp-block-paragraph">that expression is going to be a pointer to the second item in the array,</p>



<p class="wp-block-paragraph">and that&#8217;s going to be stuck into R15.</p>



<p class="wp-block-paragraph">So if we dereference that, we can treat R15 like a pointer.</p>



<p class="wp-block-paragraph">It&#8217;s just, you know, it&#8217;s a general purpose register.</p>



<p class="wp-block-paragraph">it doesn&#8217;t only need to hold integer values it can also hold pointers because pointers are integers</p>



<p class="wp-block-paragraph">they&#8217;re just unsigned so we deref the pointer that is stored inside of r15 now put it inside</p>



<p class="wp-block-paragraph">of rdi and then we should see that rdi actually holds the second item which should be a bunch of</p>



<p class="wp-block-paragraph">twos so the last thing that i&#8217;m going to copy paste in here is another nope so i&#8217;m going to</p>



<p class="wp-block-paragraph">break at 108 just so we can see the rest of it so i&#8217;m going to open up this right here and i&#8217;m</p>



<p class="wp-block-paragraph">open up this right here and I&#8217;m going to comment that out and I&#8217;m gonna say break at 108.</p>



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



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



<p class="wp-block-paragraph">There we go.</p>



<p class="wp-block-paragraph">But, but, but quitsies.</p>



<p class="wp-block-paragraph">Now I&#8217;m just going to run make debug again so that I can enter GDB.</p>



<p class="wp-block-paragraph">And we&#8217;ve, we&#8217;ve, we&#8217;ve hit our break points.</p>



<p class="wp-block-paragraph">And now I&#8217;m going to go info registers and just talk about the stuff that we&#8217;re seeing.</p>



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



<p class="wp-block-paragraph">So the first thing I think this is all on one page, right?</p>



<p class="wp-block-paragraph">I don&#8217;t need to scroll.</p>



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



<p class="wp-block-paragraph">one page right I don&#8217;t need to scroll yeah okay this is all on one page first thing we see is r10</p>



<p class="wp-block-paragraph">holds this value right here that&#8217;s a pointer to the my long int that kind of looks like a pointer</p>



<p class="wp-block-paragraph">if you consider that it could be relative and then r11 holds the actual integer and if you recall</p>



<p class="wp-block-paragraph">that is the gigantic long that we put in there great so now that&#8217;s the difference between a</p>



<p class="wp-block-paragraph">pointer and its value or in its dereference value so then we&#8217;re going to grab the first item in the</p>



<p class="wp-block-paragraph">So then we&#8217;re going to grab the first item in the small array.</p>



<p class="wp-block-paragraph">So R12 should have the ones.</p>



<p class="wp-block-paragraph">If we look at R12 here, it&#8217;s got the ones.</p>



<p class="wp-block-paragraph">Again, just trying to prove to you that if you dereference a pointer to an array,</p>



<p class="wp-block-paragraph">you&#8217;re actually dereferencing a pointer to the first item.</p>



<p class="wp-block-paragraph">And then on R13, we&#8217;re going to make a pointer to the second item.</p>



<p class="wp-block-paragraph">Then with the brackets and the move instruction, we dereference that and it should become the twos.</p>



<p class="wp-block-paragraph">So if we look at R13, notice how it&#8217;s the twos.</p>



<p class="wp-block-paragraph">Then we&#8217;re going to grab a pointer to the first item in the small array.</p>



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



<p class="wp-block-paragraph">What did I want to say about that?</p>



<p class="wp-block-paragraph">In the small array to R14.</p>



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



<p class="wp-block-paragraph">So again, we&#8217;re just going to grab an actual pointer to the small array.</p>



<p class="wp-block-paragraph">And so the difference between R12 and R14 is the dereft value or the pointer.</p>



<p class="wp-block-paragraph">So if we look at R12, that&#8217;s the actual value that we&#8217;re pointing at.</p>



<p class="wp-block-paragraph">And then R14 is the memory location that we have in there.</p>



<p class="wp-block-paragraph">And then notice how it&#8217;s kind of similar.</p>



<p class="wp-block-paragraph">Notice how it&#8217;s kind of similar.</p>



<p class="wp-block-paragraph">Maybe this proves a little bit more that these are pointers that we&#8217;re looking at.</p>



<p class="wp-block-paragraph">R10 was supposed to be a pointer and now R14 is also a pointer.</p>



<p class="wp-block-paragraph">Notice how those numbers are pretty close together.</p>



<p class="wp-block-paragraph">I&#8217;m sure if we looked back up in the data section and we calculated an offset based</p>



<p class="wp-block-paragraph">on how big each data item was, we could probably predict what the memory location of R14 should</p>



<p class="wp-block-paragraph">have been based on R10 because you can see it&#8217;s like only increased by a little bit,</p>



<p class="wp-block-paragraph">by a little bit right it was like 534 to 49 what is that 10 plus 5 so that&#8217;s like 15 is that correct</p>



<p class="wp-block-paragraph">oh I guess I had a byte in there so maybe it&#8217;s that&#8217;s why it&#8217;s kind of odd but yeah we can we</p>



<p class="wp-block-paragraph">can just compute the offset and then we&#8217;ll grab a pointer to the second item using the LEA</p>



<p class="wp-block-paragraph">instruction remember we have to use brackets if we&#8217;re going to do an expression but then the</p>



<p class="wp-block-paragraph">dereferencing in the move instruction so we just use LEA to not dereference.</p>



<p class="wp-block-paragraph">So we should see that R15 is about 8 bytes further along in RAM than R14 because both of those items</p>



<p class="wp-block-paragraph">are contiguous in memory and they should only be about you know a quad word apart 8 bytes.</p>



<p class="wp-block-paragraph">So if we look at R14 it&#8217;s got this number so it&#8217;s a pointer and then if we look at R15</p>



<p class="wp-block-paragraph">you know what&#8217;s 49 plus 8 it&#8217;s 57 so you can see that the pointer to the second item is only</p>



<p class="wp-block-paragraph">pointer to the second item is only eight bytes further in memory yay contiguousness and then</p>



<p class="wp-block-paragraph">sorry i can&#8217;t resist so then finally we will uh we&#8217;ll look at the dereference to r15 because it&#8217;s</p>



<p class="wp-block-paragraph">now a pointer what was it it was the it was a pointer to the second item that should be a bunch</p>



<p class="wp-block-paragraph">of twos right because remember in our array we had a bunch of twos here as our second item</p>



<p class="wp-block-paragraph">have a bunch of twos inside of it so if i look at rdi where the heck is that right there has a</p>



<p class="wp-block-paragraph">bunch of twos inside of it rdi baby oh my gosh why did i say that anyway so that&#8217;s our non-operation</p>



<p class="wp-block-paragraph">instruction and i think that&#8217;s everything that i wanted to talk to you about in this video so now</p>



<p class="wp-block-paragraph">i hope you feel like you&#8217;re an expert in moving integers and moving pointers and</p>



<p class="wp-block-paragraph">dereferencing pointers and manipulating pointers so you can get a pointer to something else and</p>



<p class="wp-block-paragraph">pointer to something else and de-referencing those pointers or not whenever you choose.</p>



<p class="wp-block-paragraph">Thank you so much for watching this video. I hope you learned a little bit of stuff</p>



<p class="wp-block-paragraph">and you had a little bit of fun. I&#8217;ll see you in the next video. Limit out. I&#8217;ll never say that</p>



<p class="wp-block-paragraph">again. 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">could do me a please a small little favor could you please subscribe and</p>



<p class="wp-block-paragraph">follow this channel or these videos or whatever it is you do on the current</p>



<p class="wp-block-paragraph">social media website that you&#8217;re looking at right now it would really mean the</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">night and I just wake up because I know somebody subscribed or followed it just</p>



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



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



<p class="wp-block-paragraph">want to just wake me up in the middle of night just subscribe and then I&#8217;ll just</p>



<p class="wp-block-paragraph">wake up I promise that&#8217;s what will happen also if you look at the middle of</p>



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



<p class="wp-block-paragraph">to the website which I think is also named somewhere at the bottom of this</p>



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



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



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



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



<p class="wp-block-paragraph">or 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 mean the world to me.</p>



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



<p class="wp-block-paragraph">So again, thank you so much for watching this video.</p>



<p class="wp-block-paragraph">And enjoy the cool music 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/x86-64-assembly-integer-data-movement-pointers-and-dereferencing-explained-yasm-on-ubuntu/">x86-64 Assembly: Integer Data Movement, Pointers, and Dereferencing Explained (YASM on Ubuntu)</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-integer-data-movement-pointers-and-dereferencing-explained-yasm-on-ubuntu/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 loading="lazy" 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>Master File Copying with System Calls in x86-64 YASM Assembly</title>
		<link>https://www.NeuralLantern.com/master-file-copying-with-system-calls-in-x86-64-yasm-assembly/</link>
					<comments>https://www.NeuralLantern.com/master-file-copying-with-system-calls-in-x86-64-yasm-assembly/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 05 Oct 2025 11:22:39 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[file copying]]></category>
		<category><![CDATA[file handling]]></category>
		<category><![CDATA[file I/O]]></category>
		<category><![CDATA[file permissions]]></category>
		<category><![CDATA[Linux system calls]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[MD5 checksum]]></category>
		<category><![CDATA[programming tutorial]]></category>
		<category><![CDATA[software development]]></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=237</guid>

					<description><![CDATA[<p>Learn file copying in x86-64 YASM assembly with system calls. This tutorial covers opening files, using looping buffers, and verifying with MD5 checksums.</p>
<p>The post <a href="https://www.NeuralLantern.com/master-file-copying-with-system-calls-in-x86-64-yasm-assembly/">Master File Copying with System Calls in x86-64 YASM Assembly</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 File Copying with System Calls in x86-64 YASM Assembly" width="1380" height="776" src="https://www.youtube.com/embed/hvq4VGqA6Ks?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">Join me as I break down a simple yet powerful x86-64 YASM assembly program to copy files using system calls! Learn how to open input files, create output files, and use a looping buffer for efficient data transfer. I’ll demo the code, explain file handles, permissions, and error handling, and even verify the copy with MD5 checksums. Perfect for intermediate assembly programmers or anyone curious about low-level file operations. Check out my other videos for more assembly tips, and don’t forget to subscribe!</p>



<p class="wp-block-paragraph">Introduction to File Copy Program 00:00:00<br>System Calls in YASM Assembly 00:00:06<br>Opening Input File 00:00:17<br>Creating Output File 00:00:22<br>Using Looping Buffer 00:00:28<br>Assembly Program Prerequisites 00:00:37<br>Overview of Source File 00:00:53<br>Data Section and Strings 00:01:06<br>Copy Buffer Length 00:01:21<br>File Permissions Explanation 00:02:05<br>System Call Codes 00:02:57<br>File Descriptors and Exit Codes 00:03:24<br>Text Section and Entry Point 00:03:45<br>Welcome Message Function 00:04:01<br>Print Null Terminated String 00:04:16<br>Running Initial Program 00:06:26<br>MD5 Checksum Explanation 00:07:00<br>File Tests Function Introduction 00:07:28<br>Open File Read Function 00:09:52<br>Checking File Handle 00:14:00<br>File Handle Concept 00:14:25<br>Error Handling Importance 00:15:24<br>Testing File Open 00:23:00<br>Create File Function 00:31:16<br>Testing File Creation 00:32:36<br>Copy File Function 00:36:14<br>Stack Buffer Creation 00:38:16<br>While Loop for Copying 00:40:14<br>Read System Call 00:40:47<br>Write System Call 00:45:03<br>Checking Read/Write Operations 00:46:51<br>Final Program Run 00:49:00<br>Verifying File Copy with MD5 00:49:22<br>Testing with Larger Input 00:51:20<br>Optimizing Buffer Size 00:53:41<br>Conclusion and Call to Subscribe 00:54:41</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 everybody in this video i&#8217;m going to show and explain a simple program that copies a file</p>



<p class="wp-block-paragraph">using system calls in an x8664 yasm assembly program</p>



<p class="wp-block-paragraph">we&#8217;re going to use the system calls to open an input file and read characters from it we&#8217;re</p>



<p class="wp-block-paragraph">going to use another system call to create a destination file and write characters to it</p>



<p class="wp-block-paragraph">we&#8217;re going to use a looping buffer,</p>



<p class="wp-block-paragraph">which should be kind of fun in the middle.</p>



<p class="wp-block-paragraph">And I&#8217;m just going to do my best to explain as much as I can.</p>



<p class="wp-block-paragraph">I should say though that before you can watch this video,</p>



<p class="wp-block-paragraph">you probably already need to know how to program an assembly,</p>



<p class="wp-block-paragraph">at least in a basic level, Yasm assembly.</p>



<p class="wp-block-paragraph">And so if you don&#8217;t know how to do that yet,</p>



<p class="wp-block-paragraph">this video is probably going to be confusing for you.</p>



<p class="wp-block-paragraph">You probably need to look at my other videos dealing with Yasm assembly and</p>



<p class="wp-block-paragraph">system calls and so forth.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to show you real fast what&#8217;s actually going on in this source</p>



<p class="wp-block-paragraph">actually going on in this source file so far.</p>



<p class="wp-block-paragraph">So this is my assembly program.</p>



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



<p class="wp-block-paragraph">I&#8217;m going to write it on screen for you.</p>



<p class="wp-block-paragraph">For the most part, you can see I&#8217;ve got a data section here and then I just have a</p>



<p class="wp-block-paragraph">bunch of null terminated strings.</p>



<p class="wp-block-paragraph">So I&#8217;ve got a string saying, hey, the module started.</p>



<p class="wp-block-paragraph">We&#8217;re about to open the file.</p>



<p class="wp-block-paragraph">We&#8217;re about to create the file.</p>



<p class="wp-block-paragraph">We failed to do something.</p>



<p class="wp-block-paragraph">We&#8217;re done copying.</p>



<p class="wp-block-paragraph">You know, we terminated the program, you know, whatever.</p>



<p class="wp-block-paragraph">Right. So I just have some strings.</p>



<p class="wp-block-paragraph">No big deal.</p>



<p class="wp-block-paragraph">Down here, I have something called the copy buffer length, which is which is just</p>



<p class="wp-block-paragraph">length with just which is just the size of the buffer that I&#8217;m going to use</p>



<p class="wp-block-paragraph">between the input file and the output file so a two byte buffer is really</p>



<p class="wp-block-paragraph">inefficient it&#8217;s too small but I made it two bytes just to show to you that are</p>



<p class="wp-block-paragraph">looping you know a read area is going to actually work because if I make a buffer</p>



<p class="wp-block-paragraph">that&#8217;s too big if it&#8217;s bigger than the file then we won&#8217;t actually know if the</p>



<p class="wp-block-paragraph">if the buffer loop works or not so I&#8217;m just gonna put two here could change</p>



<p class="wp-block-paragraph">that later if we wanted to we&#8217;re gonna be reading from a file called input and</p>



<p class="wp-block-paragraph">called input and writing to a file called output so that&#8217;s no big deal in order to open a file with</p>



<p class="wp-block-paragraph">a system call for read mode we&#8217;re just going to use a zero as the flags that just means read mode</p>



<p class="wp-block-paragraph">and then for the creation of the file we&#8217;re going to use some standard permissions this is not a</p>



<p class="wp-block-paragraph">permissions video I&#8217;m going to probably release a permissions video at some later point in time</p>



<p class="wp-block-paragraph">not too important right now but basically this 640 is the heart of what permissions we&#8217;re actually</p>



<p class="wp-block-paragraph">what permissions we&#8217;re actually looking at.</p>



<p class="wp-block-paragraph">Q is quad word.</p>



<p class="wp-block-paragraph">And six just means that the owner can read</p>



<p class="wp-block-paragraph">and write to the file.</p>



<p class="wp-block-paragraph">Four means that anyone who is in the same group</p>



<p class="wp-block-paragraph">as the file has been assigned to,</p>



<p class="wp-block-paragraph">which is usually the owner,</p>



<p class="wp-block-paragraph">can just read it but not write to it.</p>



<p class="wp-block-paragraph">And then everybody else,</p>



<p class="wp-block-paragraph">people who are not the owner</p>



<p class="wp-block-paragraph">and who are not in the right group,</p>



<p class="wp-block-paragraph">they have no access to it.</p>



<p class="wp-block-paragraph">Just a simple security feature of Linux</p>



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



<p class="wp-block-paragraph">You can go a lot further than this in the terminal,</p>



<p class="wp-block-paragraph">in the terminal, but for this assembly program,</p>



<p class="wp-block-paragraph">we&#8217;re just gonna use basics.</p>



<p class="wp-block-paragraph">We&#8217;re not gonna use ACLs.</p>



<p class="wp-block-paragraph">I don&#8217;t even know how to do that in assembly.</p>



<p class="wp-block-paragraph">System call codes.</p>



<p class="wp-block-paragraph">So again, if you don&#8217;t understand system calls,</p>



<p class="wp-block-paragraph">you gotta watch my other videos first,</p>



<p class="wp-block-paragraph">but we have a code of zero to read from a file,</p>



<p class="wp-block-paragraph">one to write to a file, two to open a file,</p>



<p class="wp-block-paragraph">three to close and open file.</p>



<p class="wp-block-paragraph">Probably should have put that one before create,</p>



<p class="wp-block-paragraph">but I don&#8217;t really care.</p>



<p class="wp-block-paragraph">85 is the code to create a file for writing,</p>



<p class="wp-block-paragraph">to create a file for writing and then 60 is the code to exit from the program.</p>



<p class="wp-block-paragraph">So then we have file descriptors.</p>



<p class="wp-block-paragraph">Descriptor number one is a standard output.</p>



<p class="wp-block-paragraph">Descriptor number two is standard error.</p>



<p class="wp-block-paragraph">And it&#8217;s always one and two, no matter what program you&#8217;re dealing with.</p>



<p class="wp-block-paragraph">Unless you have some kind of really crazy non-standard thing going on.</p>



<p class="wp-block-paragraph">And then for the exit codes, we&#8217;re just going to say exit zero for success and exit one</p>



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



<p class="wp-block-paragraph">Now to the actual tech section where our instructions are in the assembly program.</p>



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



<p class="wp-block-paragraph">So you can see section text here.</p>



<p class="wp-block-paragraph">We&#8217;ve got a global entry point called underscore start.</p>



<p class="wp-block-paragraph">So this is not a GCC program that requires a main entry point.</p>



<p class="wp-block-paragraph">This is a pure assembly program that requires</p>



<p class="wp-block-paragraph">an underscore start entry point.</p>



<p class="wp-block-paragraph">Again, see my other videos for more assembly explanation.</p>



<p class="wp-block-paragraph">I&#8217;m gonna call on a method,</p>



<p class="wp-block-paragraph">well, not a method of regular function called welcome.</p>



<p class="wp-block-paragraph">Just to print a little welcome message,</p>



<p class="wp-block-paragraph">you can see right here, that&#8217;s all that&#8217;s doing.</p>



<p class="wp-block-paragraph">It&#8217;s just loading, you know, a string,</p>



<p class="wp-block-paragraph">sending it to standard output, and calling on a helper function that I made called print null</p>



<p class="wp-block-paragraph">terminated string. When it&#8217;s done with that, it just uses the call code to exit the program.</p>



<p class="wp-block-paragraph">No big deal. Nothing too advanced so far. I have the tests function commented out because I want</p>



<p class="wp-block-paragraph">to write that in front of you. And if you look at what I have inside of print null terminated string,</p>



<p class="wp-block-paragraph">this is not the point of the video, so I&#8217;m just going to skim it. It&#8217;s not super important.</p>



<p class="wp-block-paragraph">called print null terminated string that takes in a pointer to a character array</p>



<p class="wp-block-paragraph">and a file handle for where you want to print it and what it&#8217;ll do is it&#8217;ll come</p>



<p class="wp-block-paragraph">in grab the incoming arguments it&#8217;s only got two arguments so it uses r12 and r13</p>



<p class="wp-block-paragraph">to store those arguments and then r14 is the result of calling another helper</p>



<p class="wp-block-paragraph">called string length you can imagine my string length function just kind of</p>



<p class="wp-block-paragraph">scans the string until it finds a null terminator aka the number zero not the</p>



<p class="wp-block-paragraph">zero but actually a zero in order to figure out how long the string is and then it can use a</p>



<p class="wp-block-paragraph">regular system call to print the string to the right file descriptor so we&#8217;re going to be doing</p>



<p class="wp-block-paragraph">most mostly this kind of this exact same thing when we copy the file so I&#8217;m just definitely not</p>



<p class="wp-block-paragraph">going to explain it if you don&#8217;t know pushes and pops and epilogues and prologues see my other</p>



<p class="wp-block-paragraph">videos crlf is just going to print a carriage return new line so it or sorry carriage return</p>



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



<p class="wp-block-paragraph">So that&#8217;s just like making the cursor go to the next line.</p>



<p class="wp-block-paragraph">Not a big deal.</p>



<p class="wp-block-paragraph">We just print a string basically.</p>



<p class="wp-block-paragraph">And then I have a custom die function</p>



<p class="wp-block-paragraph">that allows us to die with a failure code of one.</p>



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



<p class="wp-block-paragraph">Let&#8217;s see, 206 exit fail.</p>



<p class="wp-block-paragraph">I&#8217;m just gonna put that down here.</p>



<p class="wp-block-paragraph">Yeah, instead of hard coding values,</p>



<p class="wp-block-paragraph">it&#8217;s a lot better to use variables if you can or defines.</p>



<p class="wp-block-paragraph">So all that&#8217;s gonna do is just kind of print</p>



<p class="wp-block-paragraph">an error message and then exit</p>



<p class="wp-block-paragraph">then exit with the you know the appropriate</p>



<p class="wp-block-paragraph">Exit code which is just going to be one because this is a simple program</p>



<p class="wp-block-paragraph">So CRLF print an alternated string dying string length a welcome message all this fun pretty stuff</p>



<p class="wp-block-paragraph">That doesn&#8217;t really do anything except make the program more fun to look at so I&#8217;m going to run it here and make sure that actually works</p>



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



<p class="wp-block-paragraph">Nice, okay, so if I run it you can see that the the make file system again</p>



<p class="wp-block-paragraph">file system again this is not a make file video so I&#8217;m not going to show you</p>



<p class="wp-block-paragraph">my make file see my other videos if you want to learn more about make files but</p>



<p class="wp-block-paragraph">you can see that the assembly program says that it started and then it just</p>



<p class="wp-block-paragraph">exited then the make file continues to run these extra commands that I have</p>



<p class="wp-block-paragraph">set up so this is not part of the assembly program this is just taking a</p>



<p class="wp-block-paragraph">checksum an MD5 checksum of the input file and then an MD5 checksum of the</p>



<p class="wp-block-paragraph">what empty five checksums are i&#8217;ll probably release a video sometime in the future if you&#8217;re</p>



<p class="wp-block-paragraph">interested on all my platforms just talking about why we why you would use a checksum but for now</p>



<p class="wp-block-paragraph">just imagine this is a fingerprint so if the fingerprints don&#8217;t match that means the files</p>



<p class="wp-block-paragraph">don&#8217;t match right now the output file doesn&#8217;t even exist so it just is an error and that&#8217;s why the</p>



<p class="wp-block-paragraph">make file thinks there&#8217;s an error because the output file didn&#8217;t exist so that&#8217;s the basics</p>



<p class="wp-block-paragraph">of kind of getting started with this you know bare bones program now let&#8217;s start looking at running</p>



<p class="wp-block-paragraph">program. Now let&#8217;s start looking at running the file tests. So file tests, we got to make a new</p>



<p class="wp-block-paragraph">method. I&#8217;m going to stick it down here. Let&#8217;s see. I got a solution up so that I don&#8217;t have to</p>



<p class="wp-block-paragraph">spend too much time typing. I&#8217;m going to try my best to balance between copy pasting and just</p>



<p class="wp-block-paragraph">typing very quickly. But let&#8217;s see, where&#8217;s the file test? So we got this. I&#8217;m going to put the</p>



<p class="wp-block-paragraph">I&#8217;m feeling pretty lazy.</p>



<p class="wp-block-paragraph">I&#8217;m just going to copy paste the whole thing.</p>



<p class="wp-block-paragraph">Copy paste the whole thing.</p>



<p class="wp-block-paragraph">So now this method right here is going to get called the file tests.</p>



<p class="wp-block-paragraph">I keep saying method because I teach C++ a lot too.</p>



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



<p class="wp-block-paragraph">If we go down further, this function is called file tests.</p>



<p class="wp-block-paragraph">Here&#8217;s the signature.</p>



<p class="wp-block-paragraph">It doesn&#8217;t take any arguments.</p>



<p class="wp-block-paragraph">It just kind of does stuff.</p>



<p class="wp-block-paragraph">It doesn&#8217;t return anything.</p>



<p class="wp-block-paragraph">And here are the registers that I&#8217;m going to use.</p>



<p class="wp-block-paragraph">the input file handle, the output file handle, and then the count of bytes read from the input</p>



<p class="wp-block-paragraph">file at any given time inside of our looping buffer reader section. So the first thing I&#8217;ve</p>



<p class="wp-block-paragraph">got to do is I&#8217;ve got to open a file to read. The second thing I&#8217;ve got to do is create a file to</p>



<p class="wp-block-paragraph">write. And then I&#8217;m going to copy the input file to the output file. And then I&#8217;m going to print</p>



<p class="wp-block-paragraph">a message saying, hey, everything was successful. And then I&#8217;m going to close both files. Notice how</p>



<p class="wp-block-paragraph">anything i&#8217;m just sort of calling other functions do the work for me again if you don&#8217;t know the</p>



<p class="wp-block-paragraph">prologue and epilogue stuff or calling functions and returning you should see my other videos</p>



<p class="wp-block-paragraph">but i just like to use helper functions assembly is like so unwieldy right it&#8217;s it just gets out</p>



<p class="wp-block-paragraph">of control so quick and so confusing so fast so anytime you can just you know take a chunk out</p>



<p class="wp-block-paragraph">of your assembly code and move it somewhere to another module or to another function</p>



<p class="wp-block-paragraph">going to make your life a lot easier and make debugging a lot easier people who try to write</p>



<p class="wp-block-paragraph">the entire portion of their program in just one gigantic function those are the people who usually</p>



<p class="wp-block-paragraph">end up spending 10 times longer debugging for no reason at all so i believe in the power of modular</p>



<p class="wp-block-paragraph">thinking so anyway what are we going to do inside of the open file read function it&#8217;s not too bad</p>



<p class="wp-block-paragraph">to be honest let&#8217;s see if i can find it real quick open file read i&#8217;m going to copy paste it because</p>



<p class="wp-block-paragraph">Again, I&#8217;m pretty lazy.</p>



<p class="wp-block-paragraph">I&#8217;ll just explain what it is though.</p>



<p class="wp-block-paragraph">So down here we need a function called open file read.</p>



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



<p class="wp-block-paragraph">And you can see the signature that I&#8217;ve chosen for this is, you know, I like to</p>



<p class="wp-block-paragraph">write all my functions in C++, uh, kind of lingo or prototypes so that I can have a</p>



<p class="wp-block-paragraph">better understanding of what the assembly is actually supposed to do.</p>



<p class="wp-block-paragraph">So you can see the function called open file read.</p>



<p class="wp-block-paragraph">I want it to take two arguments.</p>



<p class="wp-block-paragraph">two arguments the first argument should be a pointer to a string that represents the file</p>



<p class="wp-block-paragraph">name that i want to open and it should be null terminated again if you look back at my uh at</p>



<p class="wp-block-paragraph">my strings up here they all have little zeros at the end so they are all null terminated anyway uh</p>



<p class="wp-block-paragraph">so that&#8217;s the first argument that&#8217;s going to show up as rdi in assembly and then the second argument</p>



<p class="wp-block-paragraph">is the flags for opening the file so i think probably that was redundant if i if i name this</p>



<p class="wp-block-paragraph">that was redundant if i if i name this open file read then i think it should be obvious that the</p>



<p class="wp-block-paragraph">flags are just going to be the read flags only so i probably didn&#8217;t even need to provide this</p>



<p class="wp-block-paragraph">that was bad design on my part you could write a better one on your own where it&#8217;s just one argument</p>



<p class="wp-block-paragraph">and if the name is read then just use the read flags but if the maybe if you want to leave the</p>



<p class="wp-block-paragraph">flags in there you could just say open file only it&#8217;s up to you anyway so it attempts to open a</p>



<p class="wp-block-paragraph">successful it&#8217;ll return the file handle that&#8217;s the long return type right here</p>



<p class="wp-block-paragraph">long file handle if it fails then it will just basically complain and exit</p>



<p class="wp-block-paragraph">the program you probably want a more sophisticated way of handling errors in</p>



<p class="wp-block-paragraph">your program I just decided to complain and exit the entire program because this</p>



<p class="wp-block-paragraph">is you know not supposed to be super complicated I just want to show you how</p>



<p class="wp-block-paragraph">to copy files and then for me I like to leave comments that just sort of explain</p>



<p class="wp-block-paragraph">And once my functions get so complicated that I actually run out of registers,</p>



<p class="wp-block-paragraph">then that tells me I probably need to just make another function,</p>



<p class="wp-block-paragraph">you know, split the work up in some way.</p>



<p class="wp-block-paragraph">So far, so good.</p>



<p class="wp-block-paragraph">I only end up reusing the same registers for multiple purposes occasionally.</p>



<p class="wp-block-paragraph">I&#8217;m not a hardcore assembly programmer.</p>



<p class="wp-block-paragraph">I&#8217;m just, you know, I&#8217;m like medium.</p>



<p class="wp-block-paragraph">So I&#8217;m going to use R12 and 13 and 14.</p>



<p class="wp-block-paragraph">That&#8217;s why we have the prolog pushing all those registers to preserve them</p>



<p class="wp-block-paragraph">pushing all those registers to preserve them because they&#8217;re callee saved and then the epilogue</p>



<p class="wp-block-paragraph">epilogue that just kind of pops them i&#8217;ve got a label here for the function remember a function</p>



<p class="wp-block-paragraph">is just a label and then a return statement as long as you obey and respect the abi like preserving</p>



<p class="wp-block-paragraph">certain registers then you should be okay first thing i&#8217;m going to do is grab the incoming</p>



<p class="wp-block-paragraph">arguments so i&#8217;m going to grab the file name which is a pointer which means it&#8217;s just a 64-bit</p>



<p class="wp-block-paragraph">integer i can stick that into a register and then the flags same thing so i&#8217;m going to grab</p>



<p class="wp-block-paragraph">rdi and rsi the incoming arguments i&#8217;m going to stick them into r12 and r13 so the character</p>



<p class="wp-block-paragraph">pointer in the flags and then i&#8217;m going to attempt to open the file with a system call so</p>



<p class="wp-block-paragraph">system call is right there line 168 if you look up the table for system calls in my favorite book</p>



<p class="wp-block-paragraph">that i usually recommend or any table that knows the system call codes for x86-64 assembly</p>



<p class="wp-block-paragraph">call code to open a file is some number i&#8217;ve assigned that to sys open if we look up here</p>



<p class="wp-block-paragraph">open is code 2 so that&#8217;s why i you know i don&#8217;t want to remember the numbers it&#8217;s bad to hard</p>



<p class="wp-block-paragraph">code number so i just i just put it as a define and then the first argument that it wants in rdi</p>



<p class="wp-block-paragraph">is the name of the file so i just gave it r12 i guess i probably could have just used rdi</p>



<p class="wp-block-paragraph">directly in the system call but that tends to make me nervous reusing the argument registers</p>



<p class="wp-block-paragraph">registers, I like to have them somewhere where they&#8217;re not going to be destroyed.</p>



<p class="wp-block-paragraph">If I like, let&#8217;s say I accidentally added some code here on line 163.</p>



<p class="wp-block-paragraph">If I wanted to reuse RDI and I accidentally added some code there,</p>



<p class="wp-block-paragraph">then RDI would have been destroyed.</p>



<p class="wp-block-paragraph">Would have cost me a bunch of time debugging my program.</p>



<p class="wp-block-paragraph">Although I admit it&#8217;s not super efficient to do it that way.</p>



<p class="wp-block-paragraph">Then we&#8217;re going to do the file status</p>



<p class="wp-block-paragraph">flags as the second argument, and then we just do a system call right away.</p>



<p class="wp-block-paragraph">register gives you the result of your system call.</p>



<p class="wp-block-paragraph">Notice how we use RAX to send in the call code.</p>



<p class="wp-block-paragraph">And then the system call sends us back its return result also in the RAX</p>



<p class="wp-block-paragraph">register. So I&#8217;m just going to save that right away to R14.</p>



<p class="wp-block-paragraph">You can see up here, it&#8217;s just the file handle.</p>



<p class="wp-block-paragraph">So we can assume maybe at this point that we have a file handle sitting in R14.</p>



<p class="wp-block-paragraph">You know, what is a file handle when you ask the system to open a file for you?</p>



<p class="wp-block-paragraph">The operating system under the hood is just going to do a bunch of stuff to</p>



<p class="wp-block-paragraph">bunch of stuff to actually open a file it&#8217;s going to go it&#8217;s going to take the string</p>



<p class="wp-block-paragraph">that you sent it and it&#8217;s going to parse it and figure out you know how do i how do i interpret</p>



<p class="wp-block-paragraph">where that actually is on disk i&#8217;m going to look at like the file system in the past that you</p>



<p class="wp-block-paragraph">provided i&#8217;m going to look at the mount points and i&#8217;m going to figure out like where exactly on disk</p>



<p class="wp-block-paragraph">does that file start and then the operating system stores that the operating system stores the file</p>



<p class="wp-block-paragraph">name it stores where you&#8217;re looking at the file currently stores a bunch of stuff that you don&#8217;t</p>



<p class="wp-block-paragraph">a bunch of stuff that you don&#8217;t want to have to remember you know it creates data structures under</p>



<p class="wp-block-paragraph">the hood and all it&#8217;ll give you back in return is a file handle for simplicity because then later</p>



<p class="wp-block-paragraph">you can use that file handle to just sort of say I would like to write some bytes to a file or I</p>



<p class="wp-block-paragraph">would like to read some bytes from an open file here&#8217;s the handle you gave me previously and then</p>



<p class="wp-block-paragraph">it&#8217;ll just work assuming you have a valid handle so the handle is kind of the most important part</p>



<p class="wp-block-paragraph">failed because it&#8217;s a mistake i&#8217;ve said this in other videos it&#8217;s a mistake that new programmers</p>



<p class="wp-block-paragraph">make or lazy programmers make uh let&#8217;s suppose for the sake of argument uh uh file open sys call</p>



<p class="wp-block-paragraph">pretend we&#8217;re in c plus plus and there is some sort of an api function that we can call either</p>



<p class="wp-block-paragraph">directly to the system or some person&#8217;s library and it&#8217;ll open a file for us so maybe like a</p>



<p class="wp-block-paragraph">just say we call this right so i&#8217;m going to do like a little comment if inside of your program you just</p>



<p class="wp-block-paragraph">call it with the you know some some path and then you assume that what you have is a valid you know</p>



<p class="wp-block-paragraph">handle if you assume that what you got back is a valid handle or maybe this is not a function</p>



<p class="wp-block-paragraph">just assume that the call succeeded, your program is probably going to have errors when you least</p>



<p class="wp-block-paragraph">expect it. And it&#8217;s not going to look good. Especially if you release a function like this</p>



<p class="wp-block-paragraph">to the public, or if you have like a professor who&#8217;s like grading your code and they are testing</p>



<p class="wp-block-paragraph">to see if you&#8217;re checking for return codes and stuff like that. That&#8217;s not a smart idea, right?</p>



<p class="wp-block-paragraph">Like you shouldn&#8217;t, should not, shouldn&#8217;t proceed as if everything went according to plan. What,</p>



<p class="wp-block-paragraph">just proceeding as if everything went according to plan.</p>



<p class="wp-block-paragraph">You want to use an if statement, right?</p>



<p class="wp-block-paragraph">You want to say if the handle has some value that seems to be valid,</p>



<p class="wp-block-paragraph">like for example, more than zero in the case of opening a file,</p>



<p class="wp-block-paragraph">I would say probably more than two because all of our programs</p>



<p class="wp-block-paragraph">always have automatically assigned file handles of zero and one and two</p>



<p class="wp-block-paragraph">to represent standard input, standard output, and standard error.</p>



<p class="wp-block-paragraph">But I think usually people just say like,</p>



<p class="wp-block-paragraph">if it&#8217;s greater than zero, then it&#8217;s fine.</p>



<p class="wp-block-paragraph">zero then it&#8217;s fine uh you know for me i might put greater than two but more than zero is fine</p>



<p class="wp-block-paragraph">the point is check to make sure that it actually succeeded</p>



<p class="wp-block-paragraph">do i have spell check on this thing oh god you&#8217;re all going to see my true spelling let me see if i</p>



<p class="wp-block-paragraph">can get it on real fast plug-ins what how come this oh okay now it&#8217;s highlighted</p>



<p class="wp-block-paragraph">If we&#8217;ve succeeded based on some kind of a comparison of the return result,</p>



<p class="wp-block-paragraph">then we&#8217;ll proceed in one way.</p>



<p class="wp-block-paragraph">Otherwise we&#8217;ll respond to the error by, you know, doing something else.</p>



<p class="wp-block-paragraph">Somehow, like writing a log file, sending an email, complaining to the user,</p>



<p class="wp-block-paragraph">doing any number of things where you can actually respond to the error.</p>



<p class="wp-block-paragraph">Maybe you probably want to change your execution path.</p>



<p class="wp-block-paragraph">Like if the file successfully opened, then go ahead and start writing to it</p>



<p class="wp-block-paragraph">or reading from it or whatever.</p>



<p class="wp-block-paragraph">But if the file did not successfully open,</p>



<p class="wp-block-paragraph">want to do something else in the program and not just start trying to read from the file so anyway</p>



<p class="wp-block-paragraph">super super good idea and so that&#8217;s why i&#8217;m going to implement that inside of assembly 2. so instead</p>



<p class="wp-block-paragraph">of just immediately using the file handle i&#8217;m going to check it i&#8217;m going to say let&#8217;s compare</p>



<p class="wp-block-paragraph">it to the number zero if we succeeded then let&#8217;s go to another label called uh read success i</p>



<p class="wp-block-paragraph">personally when i&#8217;m doing branching logic i like to say open file read is the name of the function</p>



<p class="wp-block-paragraph">read is the name of the function and I&#8217;ll just I&#8217;ll just uh uh append some kind of a suffix</p>



<p class="wp-block-paragraph">to the original function name that way it&#8217;s it&#8217;s easier to avoid collisions when you have lots of</p>



<p class="wp-block-paragraph">functions and lots of labels and things like that so I&#8217;m basically saying if we succeeded I&#8217;m going</p>



<p class="wp-block-paragraph">to jump to this label which is down here and so uh this video also is not about branching logic</p>



<p class="wp-block-paragraph">and and how to implement those instructions you can probably infer it by looking at my code but</p>



<p class="wp-block-paragraph">it by looking at my code but you know see another video in the future for that topic anyway if we</p>



<p class="wp-block-paragraph">succeeded in opening the file handle then we&#8217;re just going to say oh we were successful and then</p>



<p class="wp-block-paragraph">we&#8217;re going to uh um let&#8217;s see we&#8217;re going to print the name of the file that we just successfully</p>



<p class="wp-block-paragraph">opened and then we&#8217;re going to send the file handle into rax so that this function has a return</p>



<p class="wp-block-paragraph">So when you open a file to read successfully, the caller will receive the file handle.</p>



<p class="wp-block-paragraph">You might be wondering yourself, wait a minute, wait a minute, wait a minute.</p>



<p class="wp-block-paragraph">It just gave us the file handle in RAX.</p>



<p class="wp-block-paragraph">Remember, we got to respect the ABI.</p>



<p class="wp-block-paragraph">Anytime we jump anywhere or call another function or call another syscall,</p>



<p class="wp-block-paragraph">which a lot of these things do,</p>



<p class="wp-block-paragraph">we will probably lose the value of registers that are not callee preserved.</p>



<p class="wp-block-paragraph">And that&#8217;s definitely RAX.</p>



<p class="wp-block-paragraph">I mean, just doing any system call like this print null terminated string function does.</p>



<p class="wp-block-paragraph">print null terminated string function does that&#8217;s going to destroy rex so that&#8217;s why i saved it away</p>



<p class="wp-block-paragraph">first in r14 then at the very end of the function right before i return i&#8217;m just going to grab r14</p>



<p class="wp-block-paragraph">and send it into rex again respect the abi do not return data as a return value in r14 or any other</p>



<p class="wp-block-paragraph">register you have to use the designated registers and in this case an integer that you&#8217;re returning</p>



<p class="wp-block-paragraph">x anyway so that&#8217;s the gist of that let me go back up for a second uh so notice how</p>



<p class="wp-block-paragraph">we&#8217;re we&#8217;re sort of comparing oh gosh i just i just reconfigured my annotator and i bought a</p>



<p class="wp-block-paragraph">new drawpad i wonder if this is going to work ah it works there&#8217;s a bunch of stuff i added too</p>



<p class="wp-block-paragraph">so notice how uh we try to open the file and then we sort of compare the file handle to see if we</p>



<p class="wp-block-paragraph">off to the success area down here oh my green&#8217;s not working oh it&#8217;s tricky it&#8217;s tricky i gotta</p>



<p class="wp-block-paragraph">hit it in a certain way there we go if it succeeds we go down here to the success label so we</p>



<p class="wp-block-paragraph">basically at that point um totally ignore all of the fail code right we&#8217;re just like branching</p>



<p class="wp-block-paragraph">around on the other hand if it fails then execution falls through because it&#8217;s not going to jump</p>



<p class="wp-block-paragraph">if uh if if r14 is not greater than or equal to zero so if we fail maybe i should put this in a</p>



<p class="wp-block-paragraph">red if we fail then it&#8217;s going to fall through to the next label and then the next instructions</p>



<p class="wp-block-paragraph">some people like to say let&#8217;s uh let&#8217;s jump to the success label and then if not let&#8217;s jump to</p>



<p class="wp-block-paragraph">the fail label that can buy you a little bit more uh i don&#8217;t know jump length if you have a giant</p>



<p class="wp-block-paragraph">program but in this case i&#8217;m just going to let it fall through it saves us one jump instruction</p>



<p class="wp-block-paragraph">and so if it fails then it&#8217;s going to say first off let&#8217;s print a message that we failed</p>



<p class="wp-block-paragraph">to standard error if you want a refresher on standard input standard output and standard error</p>



<p class="wp-block-paragraph">see my other videos and then it&#8217;s going to print the name of the file that failed so that&#8217;s this</p>



<p class="wp-block-paragraph">part right here it&#8217;s going to say hey we we failed to print or sorry we failed to open this file name</p>



<p class="wp-block-paragraph">for reading and it&#8217;s going to print a new line there with the crlf thing</p>



<p class="wp-block-paragraph">and then it&#8217;s actually going to exit the program at that point it&#8217;s going to say all right we</p>



<p class="wp-block-paragraph">failed and so the whole program just just quits and that&#8217;s my die function that i showed you</p>



<p class="wp-block-paragraph">earlier it&#8217;s just going to call the system call code for quitting and it&#8217;s going to give a return</p>



<p class="wp-block-paragraph">code of one to indicate to anyone automating our program including new make that well our program</p>



<p class="wp-block-paragraph">failed at least it failed okay so we got all that and then we got the success and so now you kind</p>



<p class="wp-block-paragraph">of know the idea behind opening a file let&#8217;s do that real fast just for fun so file tests</p>



<p class="wp-block-paragraph">um oh i know what to do i&#8217;ll just comment out these uh instructions just so that we&#8217;re only</p>



<p class="wp-block-paragraph">doing the file open yeah that&#8217;s pretty good and then we won&#8217;t close anything just yet</p>



<p class="wp-block-paragraph">so now let me run this in the terminal real fast make run it says it successfully opened</p>



<p class="wp-block-paragraph">it says it successfully opened the file input.txt</p>



<p class="wp-block-paragraph">and then the program exited.</p>



<p class="wp-block-paragraph">No problem, that error code one,</p>



<p class="wp-block-paragraph">that&#8217;s because the output file doesn&#8217;t actually exist.</p>



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



<p class="wp-block-paragraph">And let&#8217;s change the name of the input file</p>



<p class="wp-block-paragraph">just to show you that it can fail and we can detect it.</p>



<p class="wp-block-paragraph">I&#8217;m gonna put a two there</p>



<p class="wp-block-paragraph">so that the program will try to open an input file</p>



<p class="wp-block-paragraph">that doesn&#8217;t actually exist.</p>



<p class="wp-block-paragraph">And then I&#8217;ll run it again.</p>



<p class="wp-block-paragraph">Notice how it says fail to open file input2.txt.</p>



<p class="wp-block-paragraph">And then it says terminating program after failure to open file.</p>



<p class="wp-block-paragraph">And then this time, notice how the make file never got far enough</p>



<p class="wp-block-paragraph">so that it tried to print the MD5 sums of the input and output files.</p>



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



<p class="wp-block-paragraph">And so GNU make said, I&#8217;m not going to proceed any further.</p>



<p class="wp-block-paragraph">Kind of useful when your program gives good exit codes, right?</p>



<p class="wp-block-paragraph">Because then other programs know when to stop</p>



<p class="wp-block-paragraph">or maybe what to do depending on what&#8217;s happening.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to fix this real fast.</p>



<p class="wp-block-paragraph">down here and maybe the next thing I want to add is closing the files so we&#8217;ve got a function to</p>



<p class="wp-block-paragraph">open a file for reading and then we have a function down here that is not implemented yet</p>



<p class="wp-block-paragraph">for closing the files so I&#8217;m going to do let&#8217;s see what is r12 r12 is the input file so I&#8217;m</p>



<p class="wp-block-paragraph">going to close r12 so basically this function it&#8217;s going to it&#8217;s going to take one argument</p>



<p class="wp-block-paragraph">handle. So that&#8217;s why I&#8217;m giving the file handle of the input file as the first argument. And then</p>



<p class="wp-block-paragraph">I&#8217;m just going to call it. So let&#8217;s copy paste close file underneath this. So I&#8217;m going to do</p>



<p class="wp-block-paragraph">like some more space. And let me let me go get this from my solution close file.</p>



<p class="wp-block-paragraph">And it&#8217;s going to be a pretty simple one. Really nothing much to it. I&#8217;m just going to write my</p>



<p class="wp-block-paragraph">C++ prototype saying, well, it takes in one argument and it&#8217;s a handle. It doesn&#8217;t return</p>



<p class="wp-block-paragraph">argument and it&#8217;s a handle it doesn&#8217;t return anything it attempts to close a</p>



<p class="wp-block-paragraph">file and I&#8217;m just gonna use R12 to hold the incoming argument your programs at</p>



<p class="wp-block-paragraph">home should probably be a little bit better than mine you should check to see</p>



<p class="wp-block-paragraph">if the file successfully closed or not and respond in some kind of a way but</p>



<p class="wp-block-paragraph">for me I&#8217;m just saying I don&#8217;t really care I already showed you that we can</p>



<p class="wp-block-paragraph">check for a return value so I&#8217;m allowed to be lazy now and just sort of try to</p>



<p class="wp-block-paragraph">close it and then just assume it all went according to plan so grabbing the</p>



<p class="wp-block-paragraph">So grabbing the incoming arguments here.</p>



<p class="wp-block-paragraph">That&#8217;s why I&#8217;m using R12.</p>



<p class="wp-block-paragraph">That&#8217;s why I&#8217;m doing a push-pop pair.</p>



<p class="wp-block-paragraph">And then the system call code to close is pretty easy.</p>



<p class="wp-block-paragraph">You just say, here&#8217;s the code to close.</p>



<p class="wp-block-paragraph">Stick that in RAX.</p>



<p class="wp-block-paragraph">Let me go up real fast.</p>



<p class="wp-block-paragraph">Notice how sysclose is the call code number three</p>



<p class="wp-block-paragraph">on line 43 there.</p>



<p class="wp-block-paragraph">So I send it the call code three to say, let&#8217;s close a file.</p>



<p class="wp-block-paragraph">It only wants one argument, which is just,</p>



<p class="wp-block-paragraph">what is the file handle that you want me to close?</p>



<p class="wp-block-paragraph">what is the file handle that you want me to close remember before the operating system created a</p>



<p class="wp-block-paragraph">bunch of stuff under the hood and gave you a file handle you can then use the file handle</p>



<p class="wp-block-paragraph">to close a file read a file write to the file whatever to the file so it&#8217;s pretty easy once</p>



<p class="wp-block-paragraph">i&#8217;ve set up the incoming arguments to the system then i actually use the system call instruction</p>



<p class="wp-block-paragraph">syscall and then i can assume it&#8217;s probably closed at that point then a return statement at the end</p>



<p class="wp-block-paragraph">like any additional thing happening.</p>



<p class="wp-block-paragraph">It should just be, it said it successfully opened the file</p>



<p class="wp-block-paragraph">and then it just exited.</p>



<p class="wp-block-paragraph">Okay, now we&#8217;re ready to add a little bit more.</p>



<p class="wp-block-paragraph">Let&#8217;s create a file to write.</p>



<p class="wp-block-paragraph">So this is going to be the same thing basically</p>



<p class="wp-block-paragraph">as opening a file to read,</p>



<p class="wp-block-paragraph">except it&#8217;s going to be a different call code</p>



<p class="wp-block-paragraph">and we&#8217;ll give it initial file permissions</p>



<p class="wp-block-paragraph">rather than a read mode flag.</p>



<p class="wp-block-paragraph">But then we&#8217;ll just get a file handle in return.</p>



<p class="wp-block-paragraph">get a file handle in return.</p>



<p class="wp-block-paragraph">And I&#8217;ve also stuck this into another function, of course.</p>



<p class="wp-block-paragraph">So the file name to write line 109.</p>



<p class="wp-block-paragraph">If you just scroll up real fast here, or if I scroll up real fast,</p>



<p class="wp-block-paragraph">it&#8217;s a line 27 here, just output.txt.</p>



<p class="wp-block-paragraph">And then the file permissions that we want to use.</p>



<p class="wp-block-paragraph">I&#8217;m going to scroll up here.</p>



<p class="wp-block-paragraph">That&#8217;s the second argument to the system call code.</p>



<p class="wp-block-paragraph">That&#8217;s just the stuff that I talked about a little while ago where it&#8217;s like,</p>



<p class="wp-block-paragraph">we want the user to be able to read and write to the file.</p>



<p class="wp-block-paragraph">to be able to read and write to the file we want people in the same group to be able to read only</p>



<p class="wp-block-paragraph">and we want everyone else to not be able to do anything basic linux permissions not a big deal</p>



<p class="wp-block-paragraph">at the end you should probably maybe note that uh you know this q i think i might have said this</p>



<p class="wp-block-paragraph">before this q just means quad word and these zeros are always going to be the same so really it&#8217;s just</p>



<p class="wp-block-paragraph">like three numbers representing file permissions i&#8217;ll go over that in more detail in some other</p>



<p class="wp-block-paragraph">So for now, we know that we&#8217;re going to open a file name to we&#8217;re going to open a file</p>



<p class="wp-block-paragraph">for writing doesn&#8217;t have to exist yet.</p>



<p class="wp-block-paragraph">We&#8217;ll give it some default permissions.</p>



<p class="wp-block-paragraph">And those are the incoming arguments to the function that we&#8217;re going to call now called</p>



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



<p class="wp-block-paragraph">When that file when that function comes back, assuming it didn&#8217;t decide to exit the program</p>



<p class="wp-block-paragraph">because we failed to open the file, we will receive the file handle in our ex per usual.</p>



<p class="wp-block-paragraph">And so I&#8217;m just going to stash that away real fast into our 13.</p>



<p class="wp-block-paragraph">Remember R12 has the handle to the input file.</p>



<p class="wp-block-paragraph">R13 has the handle to the output file.</p>



<p class="wp-block-paragraph">And then, you know, for me personally,</p>



<p class="wp-block-paragraph">I put that in comments to help myself remember</p>



<p class="wp-block-paragraph">what I&#8217;m even doing,</p>



<p class="wp-block-paragraph">because things can get confusing really, really fast.</p>



<p class="wp-block-paragraph">And while we&#8217;re at it,</p>



<p class="wp-block-paragraph">before we even write the create file function,</p>



<p class="wp-block-paragraph">I might as well just uncomment these things at the bottom,</p>



<p class="wp-block-paragraph">just to say, let&#8217;s close both files properly.</p>



<p class="wp-block-paragraph">You always want to do that.</p>



<p class="wp-block-paragraph">let&#8217;s copy paste the create file function let&#8217;s see I didn&#8217;t do that already right yeah okay</p>



<p class="wp-block-paragraph">so let&#8217;s do that maybe like right here I&#8217;m going to go grab it from my solution real fast create</p>



<p class="wp-block-paragraph">file okay about as complicated as opening a file just because I put in some some checking logic to</p>



<p class="wp-block-paragraph">see if it successfully opened the file which is a good idea so here&#8217;s my function create file</p>



<p class="wp-block-paragraph">arguments. Oh shoot, file creation hand. Let&#8217;s see, am I using R13? Yeah, oh, I mislabeled that.</p>



<p class="wp-block-paragraph">Instead of saying flags, it should be permissions. So let me just do perms here. Long perms,</p>



<p class="wp-block-paragraph">file creation perms, perm. I wish I could get a perm, if you know what I mean. Anyway, so,</p>



<p class="wp-block-paragraph">you know, we just have like the file name that comes in and the permissions that come in,</p>



<p class="wp-block-paragraph">And it&#8217;s going to return a handle just like we did with the read file for opening function.</p>



<p class="wp-block-paragraph">But this time we&#8217;re going to do something slightly different.</p>



<p class="wp-block-paragraph">So the system call is going to be the code for creating a file.</p>



<p class="wp-block-paragraph">SYS create, which is not real unless you define it.</p>



<p class="wp-block-paragraph">So if we just look up, I define that as.</p>



<p class="wp-block-paragraph">Where is it?</p>



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



<p class="wp-block-paragraph">So like right here, code 85 for system create.</p>



<p class="wp-block-paragraph">Again, not a smart idea to hard code numbers.</p>



<p class="wp-block-paragraph">Defines are way better.</p>



<p class="wp-block-paragraph">numbers defines are way better. So then the first argument that it wants is the name of the file.</p>



<p class="wp-block-paragraph">That&#8217;s the incoming argument that I took into R12. It has to be a null terminated string. It&#8217;s going</p>



<p class="wp-block-paragraph">to be output.txt. Then the second argument is the file&#8217;s permissions. So that&#8217;s R13 that I took here</p>



<p class="wp-block-paragraph">from the second argument, long perms. Once I&#8217;ve set up those things, I can do the system call</p>



<p class="wp-block-paragraph">right away. The system, again, will try to open the file. It&#8217;ll try to create the file,</p>



<p class="wp-block-paragraph">set up some data structures under the hood. If it succeeds, it&#8217;ll give me a valid file handle</p>



<p class="wp-block-paragraph">in RAX, the return register. If not, then things have failed and I need to respond to that. So</p>



<p class="wp-block-paragraph">I&#8217;m just going to stash the file handle in R14 right away. That&#8217;s why I&#8217;m also preserving R14</p>



<p class="wp-block-paragraph">in the push pop pair that I have, R14 up at the top and the bottom.</p>



<p class="wp-block-paragraph">So now we got to check whether or not we successfully created the file. Again,</p>



<p class="wp-block-paragraph">logic is just opening up off a reading I&#8217;m going to compare our 14 to zero if</p>



<p class="wp-block-paragraph">it&#8217;s greater than zero I&#8217;m going to assume it&#8217;s a valid file handle so jump</p>



<p class="wp-block-paragraph">if it&#8217;s greater than or equal to to that label create file success I&#8217;m still</p>



<p class="wp-block-paragraph">using my appending can naming convention so the name of the function is create</p>



<p class="wp-block-paragraph">file so that means the success area is create file and then append underscore</p>



<p class="wp-block-paragraph">success and so then I&#8217;m just gonna you know print a cute message saying hey we</p>



<p class="wp-block-paragraph">successfully created the file. Yay.</p>



<p class="wp-block-paragraph">Same thing that we looked at before basically.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to return the file handle in RAX.</p>



<p class="wp-block-paragraph">If we fail, same thing that we did before when we were opening a read file,</p>



<p class="wp-block-paragraph">I&#8217;m just going to complain basically to the user and then call on my little</p>



<p class="wp-block-paragraph">die function to properly exit the assembly program with a</p>



<p class="wp-block-paragraph">exit code of one, you know,</p>



<p class="wp-block-paragraph">and I could enhance that to exit with different codes. Like maybe exit code.</p>



<p class="wp-block-paragraph">Maybe exit code one means the read file didn&#8217;t work and exit code two means the</p>



<p class="wp-block-paragraph">write file didn&#8217;t work and exit code three means we failed for some reason</p>



<p class="wp-block-paragraph">while we were copying the data, you know, whatever you want.</p>



<p class="wp-block-paragraph">I&#8217;m keeping it kind of simple.</p>



<p class="wp-block-paragraph">So we got all that.</p>



<p class="wp-block-paragraph">Again, this is not different than reading just, you know, the call codes pretty</p>



<p class="wp-block-paragraph">much and, you know, the arguments, but the idea is the same.</p>



<p class="wp-block-paragraph">So now we&#8217;re ready to uncomment.</p>



<p class="wp-block-paragraph">I think we actually already did that.</p>



<p class="wp-block-paragraph">We&#8217;re ready to let the program try to create the file.</p>



<p class="wp-block-paragraph">to create the file and the only thing we need to add after that is the copying portion and the</p>



<p class="wp-block-paragraph">successful message so let&#8217;s see if this works do clear and make run so you can see that the program</p>



<p class="wp-block-paragraph">starts running here and then it says okay the module started and then it says we successfully</p>



<p class="wp-block-paragraph">opened the read file and then we successfully created the output file notice how the make file</p>



<p class="wp-block-paragraph">doesn&#8217;t fail when it tries to call the md5 checksum of the output file because now it actually exists</p>



<p class="wp-block-paragraph">because now it actually exists if we list the program uh list the directory here you&#8217;ll see</p>



<p class="wp-block-paragraph">that the out file has actually been created it just has a length of zero because there&#8217;s nothing</p>



<p class="wp-block-paragraph">inside of it notice also that the permissions match what we intended the initial user can read</p>



<p class="wp-block-paragraph">and write or the owning user can read and write the group can only read and everybody else can do</p>



<p class="wp-block-paragraph">nothing to heck with you we could change that real fast just to show you i can get this r to</p>



<p class="wp-block-paragraph">show you i can get this r to turn into an rw just by modifying permissions up here</p>



<p class="wp-block-paragraph">you know giving permissions to a group uh just is a nice way of allowing multiple users</p>



<p class="wp-block-paragraph">to have a shared file location you know add them all to the same group and then set that group</p>



<p class="wp-block-paragraph">onto the file and then set the group permissions to allow people to do whatever you want them to</p>



<p class="wp-block-paragraph">do read or read and write or whatever so let&#8217;s see where the perms i&#8217;m going to change this</p>



<p class="wp-block-paragraph">So now the group people should be able to read and write.</p>



<p class="wp-block-paragraph">I can&#8217;t remember if this will work because the file already exists.</p>



<p class="wp-block-paragraph">Let me give it a try.</p>



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



<p class="wp-block-paragraph">I ran it one more time and it looks like it did not create the file because it already</p>



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



<p class="wp-block-paragraph">So let me just remove output.txt.</p>



<p class="wp-block-paragraph">There should be another system call code you could use to just check to see if a file</p>



<p class="wp-block-paragraph">exists or if you wanted to be kind of hacky, you could just try to open the file and see</p>



<p class="wp-block-paragraph">open the file and see if it succeeded and then close it right away.</p>



<p class="wp-block-paragraph">I wonder if there is a call code for just exists only.</p>



<p class="wp-block-paragraph">I don&#8217;t remember off the top of my head.</p>



<p class="wp-block-paragraph">So I&#8217;m going to remove it and then run the program again.</p>



<p class="wp-block-paragraph">And then we should see now.</p>



<p class="wp-block-paragraph">Yeah, now that the file didn&#8217;t exist and was newly created.</p>



<p class="wp-block-paragraph">Now those new permissions that we added are reflected.</p>



<p class="wp-block-paragraph">So the group, anybody who&#8217;s on the group can read and write.</p>



<p class="wp-block-paragraph">And obviously the group is just the same as my new user by default.</p>



<p class="wp-block-paragraph">But again, you could be more complicated than that.</p>



<p class="wp-block-paragraph">you were running a multi-user system</p>



<p class="wp-block-paragraph">and you wanted to share folders or whatever.</p>



<p class="wp-block-paragraph">Okay, so I&#8217;m gonna remove output real fast</p>



<p class="wp-block-paragraph">and I&#8217;m just gonna revert the permissions to 640</p>



<p class="wp-block-paragraph">so that the group really can&#8217;t do very much</p>



<p class="wp-block-paragraph">except just read it.</p>



<p class="wp-block-paragraph">And then if I, oh shoot.</p>



<p class="wp-block-paragraph">LS, okay, yeah.</p>



<p class="wp-block-paragraph">So now you can see it reverted.</p>



<p class="wp-block-paragraph">Okay, so we got the out file.</p>



<p class="wp-block-paragraph">I&#8217;m starting to get lost here.</p>



<p class="wp-block-paragraph">I&#8217;m starting to get lost here. What am I doing? I&#8217;m supposed to copy the data, I think. So file</p>



<p class="wp-block-paragraph">tests and we are copying the file. I think I just uncommented that. Really? Oh, no, no, no. I&#8217;m</p>



<p class="wp-block-paragraph">looking at my solution repo. Okay. That&#8217;s what I&#8217;m doing wrong. So we&#8217;ll look into your repo or your</p>



<p class="wp-block-paragraph">the copy file I was like what how did I how did that program run if I already uncommented that</p>



<p class="wp-block-paragraph">and we&#8217;ll we&#8217;ll uncomment this message for now no let&#8217;s leave it until we actually finish everything</p>



<p class="wp-block-paragraph">so now we got to do the copy file function so where&#8217;s that okay I&#8217;m going to copy paste this</p>



<p class="wp-block-paragraph">whole thing you know what this would have been like a five hour video if I actually had to type</p>



<p class="wp-block-paragraph">this by hand I can&#8217;t even remember how long it took me to write this program I think it was like</p>



<p class="wp-block-paragraph">off the top of my head. So this would be a nightmare to type, I think, on video, even if I</p>



<p class="wp-block-paragraph">kind of already know how to do it now. So let&#8217;s see, string length, print an alternated string.</p>



<p class="wp-block-paragraph">This is close file. This is create file. Okay, so I&#8217;m just going to do copy.</p>



<p class="wp-block-paragraph">File is going to be right before close file and right after create file. Okay, so copy file.</p>



<p class="wp-block-paragraph">Notice the signature that I&#8217;ve chosen for this one. It doesn&#8217;t return anything. So</p>



<p class="wp-block-paragraph">So that&#8217;s like not great design.</p>



<p class="wp-block-paragraph">You know, I need to check to see if the copying operations inside of my loop actually succeeded</p>



<p class="wp-block-paragraph">and maybe return something to indicate success or failure or at least exit the program.</p>



<p class="wp-block-paragraph">But I&#8217;m not doing that right now.</p>



<p class="wp-block-paragraph">I&#8217;m just keeping it a little bit more simple.</p>



<p class="wp-block-paragraph">It takes two arguments.</p>



<p class="wp-block-paragraph">The first thing is the input handle and the second thing is the output handle.</p>



<p class="wp-block-paragraph">Conveniently, we have both of those now at this point.</p>



<p class="wp-block-paragraph">And then here&#8217;s my register usage.</p>



<p class="wp-block-paragraph">through r15 to just sort of grab the incoming arguments here for r12 and 13 and then</p>



<p class="wp-block-paragraph">r14 grabs the beginning of the temporary buffer which i&#8217;m going to make on the stack because i</p>



<p class="wp-block-paragraph">think i&#8217;m so cool instead of making it as a global in the bss section and then uh</p>



<p class="wp-block-paragraph">r15 is going to hold the result from the copy operation or i guess the the write operation so</p>



<p class="wp-block-paragraph">Sorry, the read operation only.</p>



<p class="wp-block-paragraph">Oh, I think I&#8217;m only checking the read bytes instead of the read and the write bytes.</p>



<p class="wp-block-paragraph">I&#8217;ll talk about that in a second.</p>



<p class="wp-block-paragraph">So I could upgrade my program a little bit more if I wanted to.</p>



<p class="wp-block-paragraph">But basically R15 is going to be my temporary variable that looks at the return value to</p>



<p class="wp-block-paragraph">say, hey, did we do we read anything?</p>



<p class="wp-block-paragraph">Like how much did we read?</p>



<p class="wp-block-paragraph">So copy file.</p>



<p class="wp-block-paragraph">This is not a video about making local variables.</p>



<p class="wp-block-paragraph">So just trust me on this.</p>



<p class="wp-block-paragraph">so that I can use it to save the location of the stack pointer.</p>



<p class="wp-block-paragraph">Then I&#8217;m going to make a copy buffer on the stack</p>



<p class="wp-block-paragraph">just by subtracting the stack pointer</p>



<p class="wp-block-paragraph">because the stack grows downward in memory location numbers.</p>



<p class="wp-block-paragraph">And then the base pointer is going to help me remember</p>



<p class="wp-block-paragraph">where the stack was when I started.</p>



<p class="wp-block-paragraph">I&#8217;m just going to say that that&#8217;s going to be the first byte in my buffer.</p>



<p class="wp-block-paragraph">And I&#8217;m going to move that into R14.</p>



<p class="wp-block-paragraph">And let&#8217;s see, what did I do?</p>



<p class="wp-block-paragraph">Let&#8217;s see, what did I do?</p>



<p class="wp-block-paragraph">Think about from the stack.</p>



<p class="wp-block-paragraph">Oh, did I write like a good program or a bad program?</p>



<p class="wp-block-paragraph">I feel like I should have actually saved the base pointer and not the stack pointer there.</p>



<p class="wp-block-paragraph">Let me see if this runs.</p>



<p class="wp-block-paragraph">If it does run, I probably have a naughty program that might self-corrupt sometimes.</p>



<p class="wp-block-paragraph">And it might be a good idea to move the base pointer as the first.</p>



<p class="wp-block-paragraph">Because this is not a stack video, but basically the stack grows downward.</p>



<p class="wp-block-paragraph">that means you&#8217;re sort of like extending its reach you&#8217;re allocating like a free space</p>



<p class="wp-block-paragraph">if i take where the stack</p>



<p class="wp-block-paragraph">nope nope nope i got it right i&#8217;m sorry if i take where the stack ends up after i extend it then i</p>



<p class="wp-block-paragraph">have a lower address right because it grows downward to memory if i then say that the new</p>



<p class="wp-block-paragraph">tip where it grew not where it grew from but where it grew to if i say that&#8217;s the first bite in a</p>



<p class="wp-block-paragraph">in a buffer then when you&#8217;re actually using the buffer you increase memory locations as you&#8217;re</p>



<p class="wp-block-paragraph">filling up a buffer so then that&#8217;s going to grow back towards where the stack started so it should</p>



<p class="wp-block-paragraph">be fine i think if i if i did go ahead and reverse this if i used rbp instead of rsp there then i</p>



<p class="wp-block-paragraph">think i would be going in the wrong direction and just corrupting memory so just so you know</p>



<p class="wp-block-paragraph">this is not a stack video but just so you know so here&#8217;s a little label again i&#8217;m just kind of</p>



<p class="wp-block-paragraph">iteration label and at the bottom I&#8217;m just kind of jumping back up to that label so this is a loop</p>



<p class="wp-block-paragraph">you can imagine this is a while loop maybe I should draw that for fun I gotta find more excuses</p>



<p class="wp-block-paragraph">to use my drophead so this is kind of like a while loop I&#8217;ll say while true maybe and here&#8217;s like the</p>



<p class="wp-block-paragraph">body and then here&#8217;s like maybe the end of the body and so this jump statement just kind of goes back</p>



<p class="wp-block-paragraph">this thing&#8217;s going to frustrate me so the first thing that happens is we read a portion</p>



<p class="wp-block-paragraph">of the input file into the buffer so what are we doing we&#8217;re just using another system call</p>



<p class="wp-block-paragraph">we&#8217;re saying let&#8217;s use the call code for read I can&#8217;t remember what that was it might have been</p>



<p class="wp-block-paragraph">uh whoops oh I cleared the dang drawing oh hang on I have undo I think wait I have that I&#8217;m gonna</p>



<p class="wp-block-paragraph">Okay, I&#8217;m not going to mess with it because this will actually terminate my whole program.</p>



<p class="wp-block-paragraph">One of these keys, I forgot which one, will just kill the whole annotator.</p>



<p class="wp-block-paragraph">So if we go up to SYS read in the defines, you can see that it is just call code zero.</p>



<p class="wp-block-paragraph">So that means I&#8217;m telling the system I would like to read something.</p>



<p class="wp-block-paragraph">First argument it wants is a file descriptor for the input file.</p>



<p class="wp-block-paragraph">In a different video, I showed you how to use exactly this sort of thing to read standard</p>



<p class="wp-block-paragraph">of thing to read standard input from the user or from another program that launched your program</p>



<p class="wp-block-paragraph">but in this case r12 it&#8217;s not going to be the file handle zero for standard input it&#8217;s going</p>



<p class="wp-block-paragraph">to be the actual file handle of the file that you&#8217;re trying to read from whatever that may have</p>



<p class="wp-block-paragraph">been whatever the os gave you and then here here&#8217;s the address of where to store our characters and</p>



<p class="wp-block-paragraph">if you look up again we decided to remember where the first byte in our buffer on the stack was or</p>



<p class="wp-block-paragraph">like a local array like oh shoot let&#8217;s do it again let&#8217;s do more drawings you have like a function</p>



<p class="wp-block-paragraph">right in c and then here it&#8217;s like we declared a local variable maybe not int maybe a character</p>



<p class="wp-block-paragraph">array we&#8217;ll call it a or how about b for buffer and then we just gave it you know let&#8217;s say eight</p>



<p class="wp-block-paragraph">bytes or something for the buffer i think i still have it set to two bytes obviously you want to use</p>



<p class="wp-block-paragraph">more bytes for efficiency but just to prove that the loop actually works i&#8217;m keeping it small like</p>



<p class="wp-block-paragraph">keeping it small like I said before whoa what&#8217;s all that you see that it&#8217;s like smearing okay</p>



<p class="wp-block-paragraph">that&#8217;s not good so uh anyway we remembered where the uh buffer starts and it&#8217;s going to be r14</p>



<p class="wp-block-paragraph">and then the copy buffer length is being you know used here uh and I think I have that set up to uh</p>



<p class="wp-block-paragraph">from the input file and we want to read into our temporary buffer and we want to read at most</p>



<p class="wp-block-paragraph">this many characters and then we say system call and it does all the work for us to read that many</p>



<p class="wp-block-paragraph">characters and then we want to remember how many bytes were actually read because that could be</p>



<p class="wp-block-paragraph">different from the number of bytes we requested maybe we&#8217;re at the end of the file maybe the</p>



<p class="wp-block-paragraph">system is having like some kind of a buffer issue or something so we just want to remember how many</p>



<p class="wp-block-paragraph">this little token up there to remind myself the temporary bytes read should</p>



<p class="wp-block-paragraph">go there. Notice also that when we created the stack,</p>



<p class="wp-block-paragraph">this is not a stack video again,</p>



<p class="wp-block-paragraph">but like I made the stack buffer equal to the length of the buffer that I</p>



<p class="wp-block-paragraph">actually wanted to use. So I use the same symbol,</p>



<p class="wp-block-paragraph">copy buffer length and copy buffer length.</p>



<p class="wp-block-paragraph">So then it&#8217;s going to try to figure out, okay,</p>



<p class="wp-block-paragraph">how many bytes did we actually read? Let me,</p>



<p class="wp-block-paragraph">let me do that while loop thing again. Cause I&#8217;m, I&#8217;m feeling it.</p>



<p class="wp-block-paragraph">I&#8217;m feeling it.</p>



<p class="wp-block-paragraph">feeling it did i really have to write the word true especially my bad penmanship so we&#8217;re doing</p>



<p class="wp-block-paragraph">while true and then i&#8217;ll just say like read you know do some kind of a call to read maybe we&#8217;ll</p>



<p class="wp-block-paragraph">say n equals read like a long you know the the r15 uh register is like the number of bytes that we</p>



<p class="wp-block-paragraph">actually read and then we do a comparison here so we&#8217;re saying you know if you know n is um</p>



<p class="wp-block-paragraph">if it&#8217;s equal to zero then we&#8217;re going to jump to the position in the while loop where we&#8217;re</p>



<p class="wp-block-paragraph">um done which is actually past the while loop if you scroll down you&#8217;ll kind of see it so i&#8217;m just</p>



<p class="wp-block-paragraph">going to say break so basically if we read zero bytes then we&#8217;re done reading bytes we&#8217;re just</p>



<p class="wp-block-paragraph">finished so we just break the while loop and that&#8217;s what&#8217;s going to happen here when we say</p>



<p class="wp-block-paragraph">the end of the body and you&#8217;ll see that in a second so then otherwise if we&#8217;re not done that</p>



<p class="wp-block-paragraph">means execution is not going to jump it&#8217;s going to just fall through to the next statement here</p>



<p class="wp-block-paragraph">and that&#8217;s going to be another system call code to write to the output file and it&#8217;s going to be</p>



<p class="wp-block-paragraph">very similar we just load it up with the system call code for write let me just double check</p>



<p class="wp-block-paragraph">is code one and then we&#8217;re going to give it the target for output which is going to be</p>



<p class="wp-block-paragraph">a file handle so you know r13 r13 right here that&#8217;s a file handle that&#8217;s where we want to</p>



<p class="wp-block-paragraph">write and then the next argument that it wants is the buffer and that&#8217;s going to be r14 which</p>



<p class="wp-block-paragraph">is where we just what&#8217;s going on green grain there we go i&#8217;m having issues that&#8217;s uh the</p>



<p class="wp-block-paragraph">that&#8217;s uh the buffer that we just read into right so if you look here r14 green oh my gosh okay so</p>



<p class="wp-block-paragraph">green we uh read from uh the buffer pointed to by r14 and then we uh we read into the buffer</p>



<p class="wp-block-paragraph">pointed to by r14 and then we are using that same buffer pointed to by r14 in order to grab</p>



<p class="wp-block-paragraph">R15 says how many files or sorry, how many bytes?</p>



<p class="wp-block-paragraph">Green, I&#8217;m having such problems.</p>



<p class="wp-block-paragraph">R15 says how many bytes do you want to write to the file?</p>



<p class="wp-block-paragraph">That should be our return value from before, right?</p>



<p class="wp-block-paragraph">Because we want to read a certain number of bytes from the file</p>



<p class="wp-block-paragraph">and then write exactly that many bytes.</p>



<p class="wp-block-paragraph">If we did more, we would be writing some junk data probably.</p>



<p class="wp-block-paragraph">And if we wrote less, then we would be missing data in the output file</p>



<p class="wp-block-paragraph">that was originally in the input file.</p>



<p class="wp-block-paragraph">So you want to do it exactly.</p>



<p class="wp-block-paragraph">in the input file so you want to do it exactly and of course i&#8217;m just doing a system call here</p>



<p class="wp-block-paragraph">but like i said before your program should probably be a little bit smarter and check</p>



<p class="wp-block-paragraph">rax after you write to the file just to make sure that you know how many bytes were actually written</p>



<p class="wp-block-paragraph">like what if you what if you read a hundred bytes but then you wrote 90 bytes only right that would</p>



<p class="wp-block-paragraph">be like a bad situation so you&#8217;d want to do some branching logic there so that if you read 100 and</p>



<p class="wp-block-paragraph">100 and you wrote 90 you probably want to backtrack the position of the file by like 10.</p>



<p class="wp-block-paragraph">You know just to make sure that you can actually get all of the bytes into the output file</p>



<p class="wp-block-paragraph">and seeking backwards 10 that&#8217;s just another system call. It&#8217;s not in this program but it&#8217;s</p>



<p class="wp-block-paragraph">not too bad. You just make another system call give it the right call code tell it how far back</p>



<p class="wp-block-paragraph">you want to go no problem. So then when by the time we make it here we&#8217;re going to jump back</p>



<p class="wp-block-paragraph">We&#8217;re going to jump back up to the iterate label.</p>



<p class="wp-block-paragraph">Let&#8217;s see, where&#8217;s that?</p>



<p class="wp-block-paragraph">Yeah, right there.</p>



<p class="wp-block-paragraph">We&#8217;re going to jump back up to the iterate label.</p>



<p class="wp-block-paragraph">So basically this is how the while loop continues looping.</p>



<p class="wp-block-paragraph">So let me clear that real fast.</p>



<p class="wp-block-paragraph">Eventually when we&#8217;re out of data to read, you know, that&#8217;s because if we try to read</p>



<p class="wp-block-paragraph">and we end up with zero bytes, that means we&#8217;re done.</p>



<p class="wp-block-paragraph">We&#8217;ll break the while loop.</p>



<p class="wp-block-paragraph">Then we&#8217;re going to go down here.</p>



<p class="wp-block-paragraph">Notice how it says copy file done.</p>



<p class="wp-block-paragraph">What I was talking about before, just sort of jumping past the end of the body.</p>



<p class="wp-block-paragraph">so if we are done we jump to copy file done and all it does is just restore a</p>



<p class="wp-block-paragraph">bunch of registers for us and then return to the caller so copy file we have that</p>



<p class="wp-block-paragraph">handled now and if I go back up here file tests I guess I can I feel bad about</p>



<p class="wp-block-paragraph">this I can uncomment the success message but we probably should have done more</p>



<p class="wp-block-paragraph">checking on the reads and the rights just to make sure that we actually wrote</p>



<p class="wp-block-paragraph">to make sure that we actually wrote the correct amount of characters and everything succeeded</p>



<p class="wp-block-paragraph">every time and if not we exit the program and only if everything went well then we print a</p>



<p class="wp-block-paragraph">successful message at this point in this program the way it&#8217;s written it could totally fail and</p>



<p class="wp-block-paragraph">it&#8217;ll still say that it was successful so just keep that in mind that&#8217;s bad bad for your users</p>



<p class="wp-block-paragraph">okay i think we have everything that we need now i can probably just run the program and</p>



<p class="wp-block-paragraph">run. Notice now, oh shoot, I didn&#8217;t even show this to you before. Let me just emphasize this</p>



<p class="wp-block-paragraph">one more time real fast. So I&#8217;m going to comment out the part where we actually copy the file</p>



<p class="wp-block-paragraph">and I&#8217;m going to remove the output file. Then I&#8217;m going to run the whole program again. Notice how</p>



<p class="wp-block-paragraph">the two MD5 sums are different. So remember before I said that MD5 sums, they&#8217;re basically</p>



<p class="wp-block-paragraph">fingerprints. They&#8217;re not actually considered secure in the modern era. I just use them because</p>



<p class="wp-block-paragraph">and fun if you are interested in security you probably want to use a</p>



<p class="wp-block-paragraph">modern hashing algorithm so don&#8217;t use md5 but I am but don&#8217;t but I am and it&#8217;s</p>



<p class="wp-block-paragraph">basically saying the fingerprint here of input is different than the fingerprint</p>



<p class="wp-block-paragraph">here of the output quickly indicating to me that the files are different so if I</p>



<p class="wp-block-paragraph">list the contents of the directory obviously that&#8217;s true because the output</p>



<p class="wp-block-paragraph">file is empty still but if I uncomment this part here where I&#8217;m actually</p>



<p class="wp-block-paragraph">here where I&#8217;m actually copying the file then I should see that the fingerprints</p>



<p class="wp-block-paragraph">match and then if I look at what&#8217;s inside of the output file it should match</p>



<p class="wp-block-paragraph">what&#8217;s inside of the input file really fast let me open up a terminal and I&#8217;ll</p>



<p class="wp-block-paragraph">just cat the input file so this is all I added you know why hello there add some</p>



<p class="wp-block-paragraph">stuff this is definitely more than two characters so we can be sure that our</p>



<p class="wp-block-paragraph">buffer loop is actually working and so I&#8217;m gonna do clear and make run and now</p>



<p class="wp-block-paragraph">And now the output file, notice how it has a matching file size.</p>



<p class="wp-block-paragraph">It&#8217;s got 38 bytes in there.</p>



<p class="wp-block-paragraph">Same thing for the input.</p>



<p class="wp-block-paragraph">And then the thumbprints or, you know, the signatures, whatever you want to call it,</p>



<p class="wp-block-paragraph">the hashes, they match exactly, indicating that we probably have two identical files.</p>



<p class="wp-block-paragraph">Even with MD5, even though it&#8217;s old and not considered secure,</p>



<p class="wp-block-paragraph">the chances of two files kind of having random differences,</p>



<p class="wp-block-paragraph">not hacked differences, but just like random differences,</p>



<p class="wp-block-paragraph">and having the same fingerprint is like astronomically almost impossible so if i do</p>



<p class="wp-block-paragraph">cat input oops input.txt that&#8217;s what&#8217;s in there like we showed before and if i cat the output</p>



<p class="wp-block-paragraph">now it is uh the same thing why hello there added some stuff for both i could make this as big as i</p>



<p class="wp-block-paragraph">wanted to just for fun maybe let&#8217;s do uh let&#8217;s do a nano on the input file</p>



<p class="wp-block-paragraph">I&#8217;ll just add like I don&#8217;t even know what I&#8217;m doing I&#8217;m just going to type a bunch of stuff</p>



<p class="wp-block-paragraph">oh wait what is this remember that thing that people got taught a long time ago it was like</p>



<p class="wp-block-paragraph">the quick brown fox jumped over the lazy dog and this was supposed to be all the letters of the</p>



<p class="wp-block-paragraph">alphabet there was an there&#8217;s another one that I just heard about and I don&#8217;t remember exactly</p>



<p class="wp-block-paragraph">look it up on the internet it&#8217;s it&#8217;s pretty cool i think i need to memorize this and stop using the</p>



<p class="wp-block-paragraph">lazy dog it was um something like those sphinx of quartz hear my vow or something like that</p>



<p class="wp-block-paragraph">you know what i&#8217;m gonna look it up for you right now i don&#8217;t want to do the wrong thing</p>



<p class="wp-block-paragraph">it&#8217;s really cool uh i&#8217;ll type brown fox and then uh</p>



<p class="wp-block-paragraph">Hear my vow.</p>



<p class="wp-block-paragraph">Oh, Sphinx of black quartz.</p>



<p class="wp-block-paragraph">Judge my vow.</p>



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



<p class="wp-block-paragraph">Okay, so I&#8217;m going to go back to my little VM here.</p>



<p class="wp-block-paragraph">Sphinx of black quartz.</p>



<p class="wp-block-paragraph">Judge my…</p>



<p class="wp-block-paragraph">I didn&#8217;t even write vow correctly.</p>



<p class="wp-block-paragraph">Judge my vow.</p>



<p class="wp-block-paragraph">And I think that has all the letters in the alphabet too.</p>



<p class="wp-block-paragraph">probably less spaces. I wonder if that&#8217;s, I mean, that&#8217;s what the internet says. If this is true,</p>



<p class="wp-block-paragraph">and it has all the letters of the alphabet, that&#8217;s going to be awesome. I&#8217;m going to memorize that</p>



<p class="wp-block-paragraph">for sure. Sphinx of black quartz, judge my vow. Anyway, so I&#8217;m just kind of adding stuff into</p>



<p class="wp-block-paragraph">this file. And if I run, let me save that here. I&#8217;ll do a clear. And then I&#8217;ll do, um,</p>



<p class="wp-block-paragraph">I&#8217;ll cat the input file here, and then cat the output file. So you can see they&#8217;re different.</p>



<p class="wp-block-paragraph">again let me remove the output file just in case I can&#8217;t remember if I&#8217;m supposed</p>



<p class="wp-block-paragraph">to remove it manually or if I put that into the program we&#8217;ll just try it like</p>



<p class="wp-block-paragraph">this so now they match and then if I cat again the output file notice how it&#8217;s a</p>



<p class="wp-block-paragraph">perfect copy of the input file nice so I think that&#8217;s pretty much everything</p>



<p class="wp-block-paragraph">that I wanted to show you maybe um well maybe we can use a more efficient buffer</p>



<p class="wp-block-paragraph">we can use a more efficient buffer now that it&#8217;s done the copy buffer we could change this to like</p>



<p class="wp-block-paragraph">eight kilobytes or something we should end up with the same result let me run this just as is</p>



<p class="wp-block-paragraph">and see if it ends up being the same thing without erasing the file first yeah it looks good let me</p>



<p class="wp-block-paragraph">remove the output file and then run it one more time so make run and then uh cat the output file</p>



<p class="wp-block-paragraph">yeah okay so it still works but um you know whereas before we were just using a two byte buffer</p>



<p class="wp-block-paragraph">two byte buffer there&#8217;s like very little chance except maybe at the end of the file that we would</p>



<p class="wp-block-paragraph">request more data than the file had but using the return value of the read operation always told us</p>



<p class="wp-block-paragraph">exactly how much was read by the operating system on the other hand if we have a giant buffer we</p>



<p class="wp-block-paragraph">could request way more bytes than the file could ever have because that file is way less than eight</p>



<p class="wp-block-paragraph">kilobytes so again we still want to look at the return value to make sure we know how many bytes</p>



<p class="wp-block-paragraph">how many bytes should be sent into the right file.</p>



<p class="wp-block-paragraph">So I guess that&#8217;s everything that I wanted to tell you</p>



<p class="wp-block-paragraph">about reading and writing files using system calls.</p>



<p class="wp-block-paragraph">I hope you&#8217;ve learned a lot of stuff</p>



<p class="wp-block-paragraph">and you enjoyed this video and had a little bit of fun.</p>



<p class="wp-block-paragraph">Thank you so much for watching.</p>



<p class="wp-block-paragraph">I&#8217;m going to cut the video.</p>



<p class="wp-block-paragraph">I&#8217;ll see you, whoops.</p>



<p class="wp-block-paragraph">I&#8217;ll 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</p>



<p class="wp-block-paragraph">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</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 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</p>



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



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



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



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



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



<p class="wp-block-paragraph">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 you control me if you want</p>



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



<p class="wp-block-paragraph">wake up. I promise that&#8217;s what will happen. Also if you look at the middle of</p>



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



<p class="wp-block-paragraph">to the website which I think is also named somewhere at the bottom of this</p>



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



<p class="wp-block-paragraph">can just kind of like see all the videos I published and the services and tutorials and</p>



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



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



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



<p class="wp-block-paragraph">I wake up in a cold sweat and I&#8217;m like,</p>



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



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



<p class="wp-block-paragraph">So again, thank you so much for watching this video</p>



<p class="wp-block-paragraph">and enjoy the cool music as I fade into the darkness,</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/master-file-copying-with-system-calls-in-x86-64-yasm-assembly/">Master File Copying with System Calls in x86-64 YASM Assembly</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/master-file-copying-with-system-calls-in-x86-64-yasm-assembly/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Mastering System Services in x86-64 Assembly Programming</title>
		<link>https://www.NeuralLantern.com/mastering-system-services-in-x86-64-assembly-programming/</link>
					<comments>https://www.NeuralLantern.com/mastering-system-services-in-x86-64-assembly-programming/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 18 Aug 2025 14:42:23 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[coding tutorials]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[exit codes]]></category>
		<category><![CDATA[file descriptors]]></category>
		<category><![CDATA[file operations]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[standard output]]></category>
		<category><![CDATA[syscalls]]></category>
		<category><![CDATA[system calls]]></category>
		<category><![CDATA[system services]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=205</guid>

					<description><![CDATA[<p>Learn x86-64 assembly system services! Master syscalls, file descriptors, and program exits with clear examples in this coding tutorial. #AssemblyProgramming</p>
<p>The post <a href="https://www.NeuralLantern.com/mastering-system-services-in-x86-64-assembly-programming/">Mastering System Services in x86-64 Assembly Programming</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 System Services in x86-64 Assembly Programming" width="1380" height="776" src="https://www.youtube.com/embed/-ukGDzRNTxU?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">Ready to master x86-64 assembly? This video breaks down system services, showing you how to use syscalls to print messages, handle file descriptors, and exit programs cleanly. We’ll walk through a real assembly program, explain key concepts like standard output and file handles, and share tips from a top book on the subject. Whether you’re new to assembly or sharpening your skills, this tutorial is packed with clear examples and practical advice. Subscribe for more coding deep dives, and check out our upcoming file I/O video! #AssemblyProgramming #SystemCalls #x86_64 #CodingTutorials</p>



<p class="wp-block-paragraph">Introduction to System Services 00:00:00<br>System Services in x86-64 Assembly 00:00:04<br>Recommended Book on Assembly 00:00:35<br>What is a System Service? 00:01:01<br>Example Assembly Program 00:01:21<br>Syscall Instruction Explanation 00:02:52<br>Standard Output and File Descriptors 00:04:01<br>Printing a String with Syscall 00:05:16<br>Exiting a Program with Syscall 00:07:21<br>Exit Codes and Program Success 00:08:02<br>Book Reference for System Services 00:09:51<br>Detailed System Write Service 00:11:01<br>Checking System Call Return Values 00:12:08<br>File Operations with System Calls 00:14:35<br>Closing Files and Best Practices 00:15:50<br>Other System Call Codes 00:16:43<br>File Permissions and Modes 00:17:20<br>Handling System Call Results 00:19:03<br>Future Video Plans and Wrap-Up 00:20:40<br>Call to Subscribe and Outro 00:21:28</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 everybody in this video we&#8217;re going to talk a little bit about system services</p>



<p class="wp-block-paragraph">in an x86-64 machine while programming in assembly</p>



<p class="wp-block-paragraph">this video is going to focus on yasm assembly but this should also work for any other assembly</p>



<p class="wp-block-paragraph">language that you program as long as you have the right kind of cpu x86-64 is also known as</p>



<p class="wp-block-paragraph">that in mind okay so for starters let&#8217;s see here&#8217;s a wonderful book i love pushing this book i did</p>



<p class="wp-block-paragraph">not write it the person who wrote it is a genius it&#8217;s called x86 64 assembly language programming</p>



<p class="wp-block-paragraph">with the boot to it&#8217;s a free and open source book so anybody can go get it find his website</p>



<p class="wp-block-paragraph">and here&#8217;s the version i&#8217;m using and honestly a really old version of this book is still really</p>



<p class="wp-block-paragraph">good he just keeps you know making little improvements but it was it was good even a</p>



<p class="wp-block-paragraph">even a long time ago. Anyway, so what is a system service? For starters, before I go into this book</p>



<p class="wp-block-paragraph">a little bit more, what is the system service? Suppose for the sake of argument that I&#8217;ve</p>



<p class="wp-block-paragraph">already written an assembly program, a pure assembly program, not a hybrid module program</p>



<p class="wp-block-paragraph">or anything. If you want to learn assembly, see my other videos, but for now I&#8217;m just going to</p>



<p class="wp-block-paragraph">assume you kind of already know how, so I&#8217;m just going to open up a pre-made program here.</p>



<p class="wp-block-paragraph">I&#8217;m going to do nano so I can edit the assembly file just so you can see what&#8217;s inside of here.</p>



<p class="wp-block-paragraph">just so you can see what&#8217;s inside of here.</p>



<p class="wp-block-paragraph">Okay, so I hope the red is not too hard to see.</p>



<p class="wp-block-paragraph">Oh, the tabs got ruined on Nano.</p>



<p class="wp-block-paragraph">Hang on, let me see if I can just open this up in Genie.</p>



<p class="wp-block-paragraph">Should have done that to begin with, okay.</p>



<p class="wp-block-paragraph">Yeah, okay, I wrote it in Genie</p>



<p class="wp-block-paragraph">and I guess I don&#8217;t have the tabs set up in Nano very well.</p>



<p class="wp-block-paragraph">Okay, so here&#8217;s like my assembly program.</p>



<p class="wp-block-paragraph">You know, I just have a little string here</p>



<p class="wp-block-paragraph">string length and CRLF for like a line feed and some file descriptors and stuff</p>



<p class="wp-block-paragraph">that I talk about in other videos here&#8217;s the entry point of the program just like</p>



<p class="wp-block-paragraph">a little start entry point because it&#8217;s pure assembly and all I&#8217;m gonna do is</p>



<p class="wp-block-paragraph">I&#8217;m gonna print a message right I&#8217;m just gonna print a little hello message and</p>



<p class="wp-block-paragraph">then call on CRLF all CRLF does is just it just prints a new line so the program</p>



<p class="wp-block-paragraph">if i run it right now you&#8217;ll just see what happens if i say let&#8217;s see clear and make run</p>



<p class="wp-block-paragraph">then you can see there&#8217;s some stuff happening up here in the make file which i forgot to update</p>



<p class="wp-block-paragraph">the title ignore that basically the real action is this line right here hello i&#8217;m printing using</p>



<p class="wp-block-paragraph">a system service if you already know assembly then you have probably already seen this before</p>



<p class="wp-block-paragraph">and it seems a little boring that&#8217;s good but i just want you to know that we&#8217;re using a system</p>



<p class="wp-block-paragraph">system service to actually do the printing.</p>



<p class="wp-block-paragraph">Kind of take it for granted here.</p>



<p class="wp-block-paragraph">This little instruction called syscall.</p>



<p class="wp-block-paragraph">What is a syscall?</p>



<p class="wp-block-paragraph">Well, it&#8217;s just basically you set up some variables, some,</p>



<p class="wp-block-paragraph">some codes, some data, whatever in, in some special registers.</p>



<p class="wp-block-paragraph">One of the registers lets the system know what you want it to do.</p>



<p class="wp-block-paragraph">One of the registers is like, or I guess all the following registers</p>



<p class="wp-block-paragraph">if they&#8217;re needed, are just input to the system call that you&#8217;re trying to do.</p>



<p class="wp-block-paragraph">do so in this case rax is letting the system know i want you to do this this code i want you to do</p>



<p class="wp-block-paragraph">this certain thing i have a defined set up here called system right but if you look up in my</p>



<p class="wp-block-paragraph">little defines area it&#8217;s just a number one so that means if i set the number one in rax i&#8217;m letting</p>



<p class="wp-block-paragraph">the system know that i want it to write to some pipe to some file then you know depending on what</p>



<p class="wp-block-paragraph">code you&#8217;re using rdi rsi rdx and other things may be needed or maybe not be needed at all</p>



<p class="wp-block-paragraph">in this case when i&#8217;m writing somewhere it wants three arguments so i have to use rdi rsi and rdx</p>



<p class="wp-block-paragraph">because those are the three arguments the first argument that it wants is where to print</p>



<p class="wp-block-paragraph">see my other videos for a more in-depth explanation of file descriptors and pipes but just long story</p>



<p class="wp-block-paragraph">pipes standard input standard output and standard error standard out is a pipe</p>



<p class="wp-block-paragraph">number one let&#8217;s see where&#8217;s that yeah it&#8217;s just assigned a number one so</p>



<p class="wp-block-paragraph">every program has its own number one pipe which is just standard output and I</p>



<p class="wp-block-paragraph">guess I should say that it gets piped to file descriptor one it&#8217;s kind of like an</p>



<p class="wp-block-paragraph">ambiguous term sort of but the system right call will write data to a file</p>



<p class="wp-block-paragraph">file and if i give it a file handle of just one that lets the system know oh actually i want to</p>



<p class="wp-block-paragraph">write to this standard output for the one particular process that we&#8217;re inside of but</p>



<p class="wp-block-paragraph">you could open any file that you want and get like a file handle to it and the operating system will</p>



<p class="wp-block-paragraph">realize oh you know i i associate that file handle i gave you with this certain open file that you</p>



<p class="wp-block-paragraph">created or opened for reading or whatever um and so when you give the handle back it knows where</p>



<p class="wp-block-paragraph">knows where to write. So in this case,</p>



<p class="wp-block-paragraph">it&#8217;s treating the standard output just like a file,</p>



<p class="wp-block-paragraph">just a file with a special handle of just one.</p>



<p class="wp-block-paragraph">I&#8217;ll talk about that more in another video.</p>



<p class="wp-block-paragraph">Then the next two arguments it wants is just the,</p>



<p class="wp-block-paragraph">a pointer to the first character of the message that you want to print.</p>



<p class="wp-block-paragraph">And then just an integer representing the length of the message that you want</p>



<p class="wp-block-paragraph">to print. And if we just look up here again at my little hello,</p>



<p class="wp-block-paragraph">my hello symbol points to an array of bytes, just characters.</p>



<p class="wp-block-paragraph">bytes just characters there is no null terminator on there it&#8217;s just bytes only</p>



<p class="wp-block-paragraph">and so when I give it you know message hello by itself what I&#8217;m really giving</p>



<p class="wp-block-paragraph">it is a pointer to that H letter just a pointer to the memory look you know I&#8217;m</p>



<p class="wp-block-paragraph">pointing to the memory location of the very first character in that string and</p>



<p class="wp-block-paragraph">then for the length I&#8217;m just using a special token little shortcut thing</p>



<p class="wp-block-paragraph">where you just put a dollar sign minus and then the name of another string and</p>



<p class="wp-block-paragraph">and then the name of another string,</p>



<p class="wp-block-paragraph">and then the assembler will compute the length of that string.</p>



<p class="wp-block-paragraph">So this right here is the same thing as me</p>



<p class="wp-block-paragraph">just typing the exact length of the string.</p>



<p class="wp-block-paragraph">I don&#8217;t even know what it is.</p>



<p class="wp-block-paragraph">I don&#8217;t feel like counting it.</p>



<p class="wp-block-paragraph">You can at home if you want.</p>



<p class="wp-block-paragraph">One, two, three, four, I&#8217;m not gonna do it.</p>



<p class="wp-block-paragraph">Anyway, so we basically just say,</p>



<p class="wp-block-paragraph">you know, I would like to write somewhere.</p>



<p class="wp-block-paragraph">I would like to write to the standard output file,</p>



<p class="wp-block-paragraph">file handle, I would like to, you know, to that pipe.</p>



<p class="wp-block-paragraph">I would like to write, you know, this string,</p>



<p class="wp-block-paragraph">you know this string and then i would like to tell you that this is how long that string is</p>



<p class="wp-block-paragraph">and then after you set all that stuff up you just say system call and the system will go ahead and</p>



<p class="wp-block-paragraph">do all of the hard work for you it&#8217;ll go figure out how to actually write data to the file and</p>



<p class="wp-block-paragraph">you know figure out how to do everything that it&#8217;s supposed to do and all you have to do is</p>



<p class="wp-block-paragraph">a little system call here right after this you can see that i have a call crlf that&#8217;s just a function</p>



<p class="wp-block-paragraph">call i talked about functions in other videos but basically you know i&#8217;m just calling a function here</p>



<p class="wp-block-paragraph">you know i&#8217;m just calling a function here and it does the same thing it makes a system call but</p>



<p class="wp-block-paragraph">instead of printing the hello string it&#8217;s printing my crlf string and if you just kind of look at what</p>



<p class="wp-block-paragraph">my crlf is it&#8217;s just these two characters 13 and 10 so like you know slash r slash n for carriage</p>



<p class="wp-block-paragraph">return line feed that&#8217;s why i call it crlf so all this program does just print a message and then a</p>



<p class="wp-block-paragraph">new line and then at the very end it does another system call to properly exit the program if this</p>



<p class="wp-block-paragraph">hybrid program and I had like the entry point of main, you know, that the GCC libraries give you,</p>



<p class="wp-block-paragraph">then I would probably just want to return at the end of this function. But since this is pure</p>



<p class="wp-block-paragraph">assembly, I can actually just exit the program and be fine. So exit with success is a different</p>



<p class="wp-block-paragraph">system call code. Notice how I have the symbol system exit loaded into RAX. And if you look up</p>



<p class="wp-block-paragraph">here, system exit is just the code 60. So if I send a code of one into RAX, that means I want to write</p>



<p class="wp-block-paragraph">a code of 60 somewhere that means i want to exit the program the only argument that takes is rdi</p>



<p class="wp-block-paragraph">which is just what is the exit code that you want to return to the operating system so um</p>



<p class="wp-block-paragraph">if you recall from some of my other videos let&#8217;s see if we do echo hello echo always succeeds at</p>



<p class="wp-block-paragraph">least as far as i know there&#8217;s like it&#8217;s like really hard to make it fail if i echo hello then</p>



<p class="wp-block-paragraph">the program echo launches and it will succeed by just printing the word hello like see how it just</p>



<p class="wp-block-paragraph">like see how it just does that so that means after that command i could echo the special</p>



<p class="wp-block-paragraph">variable dollar sign question mark just to see what the echo command exited with right it should</p>



<p class="wp-block-paragraph">be a zero because it succeeded so you can see now a zero under the hello on the other hand</p>



<p class="wp-block-paragraph">if i concatenate um let&#8217;s say a file that exists i should also get you know an error code that is a</p>



<p class="wp-block-paragraph">So it&#8217;s a zero at the very end.</p>



<p class="wp-block-paragraph">But if I try to concatenate a file that doesn&#8217;t actually exist,</p>



<p class="wp-block-paragraph">I&#8217;ll put a OS release two because that doesn&#8217;t exist.</p>



<p class="wp-block-paragraph">Notice how the cat command fails because it couldn&#8217;t find the file and the exit</p>



<p class="wp-block-paragraph">code is a one.</p>



<p class="wp-block-paragraph">So all you&#8217;re doing when setting RDI here is just controlling what exit code you</p>



<p class="wp-block-paragraph">want your program to exit with.</p>



<p class="wp-block-paragraph">This isn&#8217;t very convenient for a human running a program.</p>



<p class="wp-block-paragraph">Maybe if you want to look at the exit code for some reason, but this is really,</p>



<p class="wp-block-paragraph">really convenient for programs that want to automate other programs.</p>



<p class="wp-block-paragraph">programs just keep in mind if you have a program that is executing another</p>



<p class="wp-block-paragraph">program and it wants to see if that program succeeded the exit code is one</p>



<p class="wp-block-paragraph">of the easiest ways to find out if the program succeeded so this is the whole</p>



<p class="wp-block-paragraph">idea of this entire program we&#8217;re just using two system calls to print</p>



<p class="wp-block-paragraph">something and then to exit the program hopefully this makes sense and now I</p>



<p class="wp-block-paragraph">want to go show you this wonderful wonderful book if I didn&#8217;t already say</p>



<p class="wp-block-paragraph">If I didn&#8217;t already say this before, sorry if I already did.</p>



<p class="wp-block-paragraph">This book is written by a genius.</p>



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



<p class="wp-block-paragraph">Here&#8217;s the guy who wrote the book.</p>



<p class="wp-block-paragraph">And here&#8217;s the version that I&#8217;m using, but old versions are good.</p>



<p class="wp-block-paragraph">You can get this book for free on his website if you go find it.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to go to this assembly book, which deals with a lot of Yasm assembly tips</p>



<p class="wp-block-paragraph">and tricks and tutorials and explanations and things.</p>



<p class="wp-block-paragraph">And I&#8217;m going to go to the section labeled after system services.</p>



<p class="wp-block-paragraph">So for me, this is a chapter 13 system services, just to give you a little explanation of what</p>



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



<p class="wp-block-paragraph">You know, this is basically what I said.</p>



<p class="wp-block-paragraph">It gives you, you know, a chance for your application to ask the operating system to</p>



<p class="wp-block-paragraph">do something for you that you don&#8217;t want to program from scratch in assembly or that you</p>



<p class="wp-block-paragraph">can&#8217;t program.</p>



<p class="wp-block-paragraph">But here&#8217;s the real juicy details here.</p>



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



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



<p class="wp-block-paragraph">C, which is section 23.0 system services.</p>



<p class="wp-block-paragraph">There&#8217;s a whole bunch of system services in here that you can, uh, that you can read about.</p>



<p class="wp-block-paragraph">So for starters, if we go down a little bit, notice how it says basic system services.</p>



<p class="wp-block-paragraph">If we go down a little bit, you can see right here, the system write service that we&#8217;ve</p>



<p class="wp-block-paragraph">been using in the sample assembly program.</p>



<p class="wp-block-paragraph">You know, when we&#8217;re just printing, we say code one is to write something.</p>



<p class="wp-block-paragraph">And so this is what the table says.</p>



<p class="wp-block-paragraph">you place inside of RAX before syscall.</p>



<p class="wp-block-paragraph">Call code 1 is going to be to write characters and then it tells you what arguments it needs.</p>



<p class="wp-block-paragraph">So like RDI is the file descriptor where do you want to write to.</p>



<p class="wp-block-paragraph">Just like I said before you could give it a file handle that you already received from</p>



<p class="wp-block-paragraph">opening a file or creating a file.</p>



<p class="wp-block-paragraph">Or you could give it 0 or 1 or 2 if you want to try to write to one of the pipes although</p>



<p class="wp-block-paragraph">actually file descriptor 0 is standard input I don&#8217;t think that would actually work.</p>



<p class="wp-block-paragraph">zero to reading if you wanted to read the user&#8217;s input or or you know any input that that program</p>



<p class="wp-block-paragraph">was given through its standard input rsi is just the address of the characters to write like we</p>



<p class="wp-block-paragraph">talked about before and then rdx is the count of the characters to write notice how there are no</p>



<p class="wp-block-paragraph">more arguments and that&#8217;s why i only gave it three arguments besides rex notice how it says here if</p>



<p class="wp-block-paragraph">unsuccessful it returns a negative value if successful it returns the count of characters</p>



<p class="wp-block-paragraph">count of characters actually written this is a really really good idea if you think about it</p>



<p class="wp-block-paragraph">um a lot of new programmers they don&#8217;t really look at return codes when they call system services</p>



<p class="wp-block-paragraph">or built-in c functions or built-in c++ functions they just kind of call it and hope for the best</p>



<p class="wp-block-paragraph">but imagine if you uh if you try to write a huge long string in uh using system write call code one</p>



<p class="wp-block-paragraph">and maybe some of the characters did write but the system decided to only write i don&#8217;t know half for</p>



<p class="wp-block-paragraph">reason maybe it ran out of buffer maybe you know it interrupted you know your right or something</p>



<p class="wp-block-paragraph">like that so it could happen what that would mean is that the return value it would be greater than</p>



<p class="wp-block-paragraph">zero indicating that some characters were written but it&#8217;ll tell you exactly how many characters</p>



<p class="wp-block-paragraph">were written so you&#8217;ll know that you got to keep calling the sys call until all the characters were</p>



<p class="wp-block-paragraph">successfully written why would you do this i don&#8217;t know maybe you have a gigantic string like maybe</p>



<p class="wp-block-paragraph">like a several gigabyte file and you wanted to copy it to another file so you&#8217;ll be calling</p>



<p class="wp-block-paragraph">system write over and over again and system write it&#8217;s only going to write so many bytes at the same</p>



<p class="wp-block-paragraph">time so you use the return value to figure out how far forward in the read buffer you need to advance</p>



<p class="wp-block-paragraph">or you know whatever so that you can well write the entire complete file or the entire complete</p>



<p class="wp-block-paragraph">string or whatever it is without any gaps without it being truncated and so forth and then of course</p>



<p class="wp-block-paragraph">And then of course, if it returns a negative value, then it totally failed.</p>



<p class="wp-block-paragraph">Like you tried to write to a bad file handle, like standard input or a file handle that</p>



<p class="wp-block-paragraph">was closed or a file handle that was open for reading only, something like that.</p>



<p class="wp-block-paragraph">And then you can use some if else, you know, branching logic.</p>



<p class="wp-block-paragraph">I mean, not if else in assembly, it&#8217;s just going to be comparison and conditional jumping</p>



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



<p class="wp-block-paragraph">But you can have more control over your program, right?</p>



<p class="wp-block-paragraph">Because you want your program to be able to respond to errors.</p>



<p class="wp-block-paragraph">Let me just quickly cruise through all of the other options and let you know that</p>



<p class="wp-block-paragraph">and let you know that in another video I&#8217;m gonna do a full tutorial for how to</p>



<p class="wp-block-paragraph">copy one file to another by opening one file for reading and then like using</p>



<p class="wp-block-paragraph">system calls to read from it and then open another file for writing and then</p>



<p class="wp-block-paragraph">you know use system calls to write to it and I&#8217;m gonna do that looping buffer</p>



<p class="wp-block-paragraph">stuff that I talked about before anyway so but for now I&#8217;m just gonna go through</p>



<p class="wp-block-paragraph">the rest of these system call codes let&#8217;s see so first off we got open well</p>



<p class="wp-block-paragraph">Well, if you want to open a file, then you just give it call code 2 and you pass in the</p>



<p class="wp-block-paragraph">address here, you know, RDI, you say, here&#8217;s the address of a null terminated file name.</p>



<p class="wp-block-paragraph">So that means somewhere in memory, you have to have the file name that you want to open</p>



<p class="wp-block-paragraph">with a null terminator, like a zero at the end of the string, either the full path or</p>



<p class="wp-block-paragraph">relative path to wherever the program is currently running.</p>



<p class="wp-block-paragraph">And then RSI is going to be file status flags.</p>



<p class="wp-block-paragraph">And what does that mean?</p>



<p class="wp-block-paragraph">And what does that mean? We can actually just search for that.</p>



<p class="wp-block-paragraph">I&#8217;m going to say control C to copy,</p>



<p class="wp-block-paragraph">and then I&#8217;m going to do control F to search for that.</p>



<p class="wp-block-paragraph">And let&#8217;s see what page of mine right now, 340.</p>



<p class="wp-block-paragraph">If I go down to the search results for that,</p>



<p class="wp-block-paragraph">it just explains what the file modes are. So if you put value zero,</p>



<p class="wp-block-paragraph">that means read only you&#8217;re opening the file and read only mode value one.</p>



<p class="wp-block-paragraph">That means a write only a value to allows reading and writing to the file.</p>



<p class="wp-block-paragraph">So if you don&#8217;t know what that means, well, there it was.</p>



<p class="wp-block-paragraph">What was I just on before?</p>



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



<p class="wp-block-paragraph">Oh, gosh.</p>



<p class="wp-block-paragraph">Completely lost my…</p>



<p class="wp-block-paragraph">Oh, yeah, 40.</p>



<p class="wp-block-paragraph">Okay, so opening pretty easy, right?</p>



<p class="wp-block-paragraph">Like if you didn&#8217;t know this before, you didn&#8217;t necessarily read me.</p>



<p class="wp-block-paragraph">You could just look at this table and go, what do I want to do?</p>



<p class="wp-block-paragraph">I want to open.</p>



<p class="wp-block-paragraph">So here&#8217;s the code.</p>



<p class="wp-block-paragraph">Here&#8217;s the address that it needs.</p>



<p class="wp-block-paragraph">When you&#8217;re done opening a file, when you&#8217;re done working with a file,</p>



<p class="wp-block-paragraph">you want to close that file unless the whole program is going to terminate.</p>



<p class="wp-block-paragraph">unless the whole program is going to terminate at that point you i mean maybe you have a function</p>



<p class="wp-block-paragraph">that gets called somewhat often and you kind of always have to open a file and sort of look at</p>



<p class="wp-block-paragraph">something or write something and maybe you want to close it when the function is over right you</p>



<p class="wp-block-paragraph">don&#8217;t want to have a file handle just open forever that&#8217;s a waste of memory and it might introduce</p>



<p class="wp-block-paragraph">bad behavior to your program if you just have a bunch of file handles floating around that you</p>



<p class="wp-block-paragraph">forgot about so it&#8217;s proper to close a file when you&#8217;re finished with it that&#8217;s just going to be</p>



<p class="wp-block-paragraph">and it only wants one argument.</p>



<p class="wp-block-paragraph">It just wants the file descriptor of the file that you wanted to close.</p>



<p class="wp-block-paragraph">So if you open a file for a reading or to create it,</p>



<p class="wp-block-paragraph">you get a file descriptor back.</p>



<p class="wp-block-paragraph">You should probably hang on to it somewhere on the stack</p>



<p class="wp-block-paragraph">or like in a global variable or like, you know, in a register or whatever.</p>



<p class="wp-block-paragraph">Then when you&#8217;re done doing something to that file</p>



<p class="wp-block-paragraph">and you&#8217;re sure that it&#8217;s time to close it,</p>



<p class="wp-block-paragraph">just give that handle right back when you call code three to close it.</p>



<p class="wp-block-paragraph">Then you can sort of like seek, you know,</p>



<p class="wp-block-paragraph">forward and backwards to the file if you want to.</p>



<p class="wp-block-paragraph">You can fork the current process.</p>



<p class="wp-block-paragraph">You can fork the current process that&#8217;s kind of advanced for this video.</p>



<p class="wp-block-paragraph">You can exit.</p>



<p class="wp-block-paragraph">Remember before we exited from that program with code 60.</p>



<p class="wp-block-paragraph">So that&#8217;s it. Call code 60.</p>



<p class="wp-block-paragraph">If you didn&#8217;t watch this video, you could have gone through this table and just said,</p>



<p class="wp-block-paragraph">Oh, you know, I want to terminate the executing process.</p>



<p class="wp-block-paragraph">I do a system call with code 60 and I&#8217;ll give the exit status to RDI,</p>



<p class="wp-block-paragraph">which is typically zero for success.</p>



<p class="wp-block-paragraph">Bunch of other stuffs create, get the time of day,</p>



<p class="wp-block-paragraph">and then the file modes that we talked about before.</p>



<p class="wp-block-paragraph">we talked about before and then file permissions. Let&#8217;s see, where&#8217;s the permission. I think it&#8217;s</p>



<p class="wp-block-paragraph">when you want to create a file. Yeah, right here. If we wanted to create a file, that&#8217;s code 85.</p>



<p class="wp-block-paragraph">RDI is the name of the file, you know, like a pointer to the string. RSI is the file mode flags.</p>



<p class="wp-block-paragraph">And then you&#8217;re thinking like, what are the file mode flags? Just go down here and then here you</p>



<p class="wp-block-paragraph">go. All the file modes that you could ever want. You just look at it and go, well, I guess I can</p>



<p class="wp-block-paragraph">I guess I can just copy paste this number right here and it&#8217;ll end up being, you know,</p>



<p class="wp-block-paragraph">whatever is described on the left and the right.</p>



<p class="wp-block-paragraph">Like the group has read, write and execute permissions and so forth.</p>



<p class="wp-block-paragraph">I mean, really, if you look at this long enough, you&#8217;ll kind of realize that this is a quad</p>



<p class="wp-block-paragraph">word because it has a Q on the end of it.</p>



<p class="wp-block-paragraph">And the rest is just an octal file permissions notation where this first number here, notice</p>



<p class="wp-block-paragraph">how the first two are zeros, but this first actual number here is something that applies</p>



<p class="wp-block-paragraph">to the user and then the next one applies to the group the next one applies to others who are not</p>



<p class="wp-block-paragraph">part of the user or group and this video is not about file permissions I&#8217;ll probably make one</p>



<p class="wp-block-paragraph">later if I think people will actually watch it but yeah you can just kind of like look it up here if</p>



<p class="wp-block-paragraph">you want to and just copy paste that into a define somewhere let&#8217;s see what else I just want to show</p>



<p class="wp-block-paragraph">you a little code snip just to illustrate a little bit more what I was talking about before</p>



<p class="wp-block-paragraph">for. People usually make this mistake, they&#8217;ll do see this is what people will usually do like new</p>



<p class="wp-block-paragraph">programmers. Suppose this is a function pretend we&#8217;re in C or C++. For the time being, you call</p>



<p class="wp-block-paragraph">some sort of a system function or an API of some other library. And you just you just hope that it</p>



<p class="wp-block-paragraph">works, you just call it and then your program continues underneath, right? It&#8217;s a bad idea,</p>



<p class="wp-block-paragraph">because maybe that failed, maybe there&#8217;s some action you need to take. So instead, it&#8217;s a really</p>



<p class="wp-block-paragraph">really good idea to check the return result or i should say an even better idea is check the</p>



<p class="wp-block-paragraph">documentation usually these types of you know c system calls or function calls or api calls or</p>



<p class="wp-block-paragraph">whatever they&#8217;ll return something indicating success or failure sometimes the documentation</p>



<p class="wp-block-paragraph">will tell you oh you&#8217;ve got to put like a pointer to some other data structure in the arguments</p>



<p class="wp-block-paragraph">and then the result comes there of course just read the documentation but usually it&#8217;s just</p>



<p class="wp-block-paragraph">So you want to grab the result and then check the result.</p>



<p class="wp-block-paragraph">In this case I&#8217;m assuming this is sort of a standard function that will return zero</p>



<p class="wp-block-paragraph">or greater on success like the system write call or the system read call and return less</p>



<p class="wp-block-paragraph">than zero if it fails.</p>



<p class="wp-block-paragraph">So here I&#8217;m just responding, you know, if it&#8217;s like if the result was greater than zero</p>



<p class="wp-block-paragraph">then it&#8217;s like, yay we can proceed.</p>



<p class="wp-block-paragraph">Maybe I&#8217;ll continue with my program in some way and if it&#8217;s not then I&#8217;ll print a complaint</p>



<p class="wp-block-paragraph">then I&#8217;ll print a complaint to the user and then I&#8217;ll take some sort of an action</p>



<p class="wp-block-paragraph">like I&#8217;ll write to a log file, send an email somewhere, you know,</p>



<p class="wp-block-paragraph">do something to try and recover from the error and alert admins to the error.</p>



<p class="wp-block-paragraph">And in this case, I&#8217;m just throwing an exception.</p>



<p class="wp-block-paragraph">This video is not about exceptions, but so I just want you to know,</p>



<p class="wp-block-paragraph">this is a good design pattern to not just discard the result of a call to</p>



<p class="wp-block-paragraph">something. You should check the result and see what you&#8217;re supposed to do.</p>



<p class="wp-block-paragraph">Let&#8217;s see, what else did I want to tip?</p>



<p class="wp-block-paragraph">system services, file handles.</p>



<p class="wp-block-paragraph">Yeah, okay, I think that&#8217;s everything that I wanted to tell you.</p>



<p class="wp-block-paragraph">In future videos, I&#8217;m going to talk about, you know, some of this stuff much more in-depth,</p>



<p class="wp-block-paragraph">like with my file.io video that I&#8217;m going to publish pretty soon, which is, you know,</p>



<p class="wp-block-paragraph">it lets you read and write to files, and it uses a loop to read a little bit at a time.</p>



<p class="wp-block-paragraph">Okay, thank you so much for watching this video.</p>



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



<p class="wp-block-paragraph">I&#8217;m outie.</p>



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



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



<p class="wp-block-paragraph">you&#8217;re looking at right now it would really mean the world to me and it&#8217;ll</p>



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



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



<p class="wp-block-paragraph">in general so please do do me a kindness and and subscribe you know sometimes</p>



<p class="wp-block-paragraph">I&#8217;m sleeping in the middle of the night and I just wake up because I know</p>



<p class="wp-block-paragraph">somebody subscribed or followed 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 so you could do it as a nice favor</p>



<p class="wp-block-paragraph">could you control 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 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</p>



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



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



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



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



<p class="wp-block-paragraph">if you have a suggestion for uh uh clarifications or errata or just future videos that you want to</p>



<p class="wp-block-paragraph">see please leave a comment or if you just want to say hey what&#8217;s up what&#8217;s going on you know just</p>



<p class="wp-block-paragraph">send me a comment whatever i also wake up for those in the middle of the night i get i wake</p>



<p class="wp-block-paragraph">up in a cold sweat and i&#8217;m like it would really it really mean the world to me i would really</p>



<p class="wp-block-paragraph">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 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/mastering-system-services-in-x86-64-assembly-programming/">Mastering System Services in x86-64 Assembly Programming</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/mastering-system-services-in-x86-64-assembly-programming/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How Your CPU Works: Fetch, Decode, Execute Explained</title>
		<link>https://www.NeuralLantern.com/how-your-cpu-works-fetch-decode-execute-explained/</link>
					<comments>https://www.NeuralLantern.com/how-your-cpu-works-fetch-decode-execute-explained/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 10 Aug 2025 06:23:48 +0000</pubDate>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[ALU]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[code cache]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[CPU]]></category>
		<category><![CDATA[CPU clock]]></category>
		<category><![CDATA[data cache]]></category>
		<category><![CDATA[fetch decode execute]]></category>
		<category><![CDATA[instruction pointer]]></category>
		<category><![CDATA[processor basics]]></category>
		<category><![CDATA[RIP register]]></category>
		<category><![CDATA[tech tutorials]]></category>
		<category><![CDATA[x86 processors]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=199</guid>

					<description><![CDATA[<p>Discover how your CPU fetches, decodes, and executes instructions in this beginner-friendly guide. Learn about the clock, RIP register, and ALU in a simple breakdown of computer architecture.</p>
<p>The post <a href="https://www.NeuralLantern.com/how-your-cpu-works-fetch-decode-execute-explained/">How Your CPU Works: Fetch, Decode, Execute Explained</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="How Your CPU Works: Fetch, Decode, Execute Explained" width="1380" height="776" src="https://www.youtube.com/embed/UXMDPnMfcAM?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 a clear, beginner-friendly dive into how your CPU works! This video explains the fetch-decode-execute cycle, the role of the clock, instruction pointer (RIP), and units like the ALU and cache. Learn why timing matters and how your CPU handles instructions step-by-step. Perfect for gamers, coders, or anyone curious about what makes computers tick. Inspired by Kip Irvine’s Assembly Language for x86 Processors, this is your gateway to understanding computer architecture. Subscribe for more tech insights and hit that QR code to explore more tutorials! #CPU #ComputerScience #TechTutorials</p>



<p class="wp-block-paragraph">Introduction to CPU Operations 00:00:00<br>CPU Clock Explanation 00:00:17<br>Clock Cycle and Stability 00:01:07<br>Instruction Fetch from RAM 00:03:24<br>Instruction Pointer (RIP) Role 00:03:48<br>Sequential and Conditional Execution 00:04:49<br>Code Cache Functionality 00:06:24<br>Instruction Decoding Process 00:07:51<br>Control Unit Operations 00:08:33<br>Arithmetic Logic Unit (ALU) 00:08:52<br>Floating Point Unit 00:11:03<br>Data Cache Usage 00:12:08<br>Summary of CPU Process 00:14:36<br>Closing and Subscription Call 00:15:07</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">CPU #ComputerScience #TechTutorials #ComputerArchitecture #AssemblyLanguage #TechExplained #CPUBasics #LearnTech #x86Processors #Coding</p>



<p class="wp-block-paragraph">Hello there. Let&#8217;s talk about how your CPU fetches, decodes and executes instructions.</p>



<p class="wp-block-paragraph">We&#8217;re going to just do a quick surface level overview, but I hope you like it.</p>



<p class="wp-block-paragraph">Okay. So for starters, your CPU has something called the clock.</p>



<p class="wp-block-paragraph">keep my pen onto one monitor here let me just double check that real fast</p>



<p class="wp-block-paragraph">okay is it working yeah okay so</p>



<p class="wp-block-paragraph">okay so notice how we have this line that goes up and down and up and down it kind of looks like a</p>



<p class="wp-block-paragraph">little binary line uh it actually is uh sometimes your cpu is outputting a zero to this special</p>



<p class="wp-block-paragraph">and it just oscillates between zero and one constantly it goes zero one zero one zero one</p>



<p class="wp-block-paragraph">it does so very very very very fast and the reason it does that is because something needs to control</p>



<p class="wp-block-paragraph">the timing of your cpu imagine this we kind of take this for granted but when you apply electricity</p>



<p class="wp-block-paragraph">somewhere as a human you kind of feel like the electricity goes to the other place like on the</p>



<p class="wp-block-paragraph">other side of your house or your room or whatever you kind of feel like it goes there instantaneously</p>



<p class="wp-block-paragraph">really fast but in reality electricity doesn&#8217;t go faster than the speed of light so there is a limit</p>



<p class="wp-block-paragraph">which means if you have a cpu that is executing instructions really really really really fast</p>



<p class="wp-block-paragraph">you know fetching instructions from your system ram really really fast</p>



<p class="wp-block-paragraph">there&#8217;s there&#8217;s there&#8217;s the problem of trying to um to know for sure that the electricity actually</p>



<p class="wp-block-paragraph">made it uh to the other place in time uh so like what if we said let&#8217;s load some instructions from</p>



<p class="wp-block-paragraph">load some instructions from memory, they come down the wire to your CPU.</p>



<p class="wp-block-paragraph">How do we know when it actually finished coming down the wire to the CPU?</p>



<p class="wp-block-paragraph">We just take it for granted.</p>



<p class="wp-block-paragraph">But in reality, physics has a limit.</p>



<p class="wp-block-paragraph">And so the electricity needs time to get there and it needs time to stabilize.</p>



<p class="wp-block-paragraph">So the clock cycle helps us with that timing.</p>



<p class="wp-block-paragraph">So, you know, if we make the clock go too fast, your CPU becomes unstable.</p>



<p class="wp-block-paragraph">Your gamers, you gamers probably know that.</p>



<p class="wp-block-paragraph">If we make it too slow, then it&#8217;ll work.</p>



<p class="wp-block-paragraph">it&#8217;ll work, but, um, you know, we&#8217;re not getting the fastest, you know, CPU, uh, that, that</p>



<p class="wp-block-paragraph">we, that we can, we&#8217;re not getting all the performance out of it that we can.</p>



<p class="wp-block-paragraph">So we make it, we make the clock cycle go as fast as we can, uh, without making the</p>



<p class="wp-block-paragraph">CPU, uh, instable.</p>



<p class="wp-block-paragraph">And so just keep that in mind.</p>



<p class="wp-block-paragraph">That&#8217;s one component of the CPU.</p>



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



<p class="wp-block-paragraph">So then the next thing I wanted to show you, oh, and by the way, let me give credit to</p>



<p class="wp-block-paragraph">the authors of this book.</p>



<p class="wp-block-paragraph">I&#8217;m just looking at a diagram, but, um, this book is written by someone named Kip</p>



<p class="wp-block-paragraph">This book is written by someone named Kip Irvine</p>



<p class="wp-block-paragraph">and it&#8217;s titled Assembly Language for X86 Processors.</p>



<p class="wp-block-paragraph">It&#8217;s written by Pearson or it&#8217;s published by Pearson</p>



<p class="wp-block-paragraph">and the year is 2014.</p>



<p class="wp-block-paragraph">So get a copy of this book.</p>



<p class="wp-block-paragraph">Anyway, so this is a simplified CPU block diagram.</p>



<p class="wp-block-paragraph">Let me get my notes here real fast</p>



<p class="wp-block-paragraph">so I don&#8217;t forget what I was supposed to say.</p>



<p class="wp-block-paragraph">Clock ticks and alu.</p>



<p class="wp-block-paragraph">Okay, so I guess that&#8217;s pretty much it.</p>



<p class="wp-block-paragraph">Imagine now that we have a little RAM stick or some place where we can get instructions for the CPU.</p>



<p class="wp-block-paragraph">So this is like system RAM, we&#8217;ll say.</p>



<p class="wp-block-paragraph">And we&#8217;ll just pretend for the sake of argument that we already have existing circuitry</p>



<p class="wp-block-paragraph">that will easily transfer data back and forth from the RAM stick to the CPU.</p>



<p class="wp-block-paragraph">So anytime the CPU wants to grab some kind of data or like an instruction or something off the RAM stick,</p>



<p class="wp-block-paragraph">it can just do it. And that&#8217;s the arrow right there.</p>



<p class="wp-block-paragraph">keep in mind after that is that there is a special register in the CPU called the instruction pointer</p>



<p class="wp-block-paragraph">RIP in x86-64 assembly the RIP register and the RIP register it basically just holds a memory</p>



<p class="wp-block-paragraph">location telling the CPU where is the next where&#8217;s the address of the next instruction that we want</p>



<p class="wp-block-paragraph">to execute so you know imagine you have like some kind of memory address 0x I don&#8217;t know 55 and then</p>



<p class="wp-block-paragraph">an instruction on the very next actually let&#8217;s do five zero so I can increase this by eight without</p>



<p class="wp-block-paragraph">doing hex in my head we&#8217;ll say this is like the address five zero in hexadecimal so that means</p>



<p class="wp-block-paragraph">that the next time the CPU wants to execute an instruction it&#8217;s just going to ask system ram</p>



<p class="wp-block-paragraph">for that address system ram is going to give it whatever ones and zeros it has and then it can be</p>



<p class="wp-block-paragraph">decoded and executed after that and then of course we have to execute a program you know step by step</p>



<p class="wp-block-paragraph">you know step by step we have to execute more instructions and we can&#8217;t just do one instruction</p>



<p class="wp-block-paragraph">so typically in a program you execute instructions one by one by one by one by one</p>



<p class="wp-block-paragraph">and then every once in a while there might be some decision logic like you have an if else</p>



<p class="wp-block-paragraph">branch like let&#8217;s say uh you know for the for the high level people i&#8217;ll put a question mark here</p>



<p class="wp-block-paragraph">you have like an if statement and you say like if that&#8217;s true then execute this i&#8217;ll put maybe i&#8217;ll</p>



<p class="wp-block-paragraph">then we&#8217;ll just execute this other logic down here. So you know very often we execute instructions</p>



<p class="wp-block-paragraph">sequentially but every once in a while we have to jump right because if if you executed this</p>



<p class="wp-block-paragraph">if statement and you realized that this condition was not true then you could not execute the body</p>



<p class="wp-block-paragraph">that came right after the comparison instructions you would have to jump down to wherever the else</p>



<p class="wp-block-paragraph">block is so there is circuitry inside of the CPU that allows you to either increase the instruction</p>



<p class="wp-block-paragraph">instruction pointer by 8 bytes, 8 bytes because 64 bits per instruction because that&#8217;s your CPU.</p>



<p class="wp-block-paragraph">So you just kind of go sequentially or to jump somewhere because you made a decision or you&#8217;re</p>



<p class="wp-block-paragraph">like making a function call or whatever. So just keep in mind that under the hood, under this</p>



<p class="wp-block-paragraph">diagram, there is a lot more logic even than the more detailed diagrams that you can find elsewhere.</p>



<p class="wp-block-paragraph">There&#8217;s just like a lot more detail. So anyway, the RIP register just kind of increases by 8</p>



<p class="wp-block-paragraph">every time we execute an instruction,</p>



<p class="wp-block-paragraph">sometimes it&#8217;ll drastically change its address</p>



<p class="wp-block-paragraph">so we can jump execution somewhere else</p>



<p class="wp-block-paragraph">in order to make a decision.</p>



<p class="wp-block-paragraph">So just keep that in mind.</p>



<p class="wp-block-paragraph">So suppose that we just fetched an instruction from memory.</p>



<p class="wp-block-paragraph">You know, it comes in here</p>



<p class="wp-block-paragraph">and we kind of send it down the line.</p>



<p class="wp-block-paragraph">We first give it to the code cache unit.</p>



<p class="wp-block-paragraph">Your CPU is just not a solid block of stuff.</p>



<p class="wp-block-paragraph">It&#8217;s got subunits inside of it.</p>



<p class="wp-block-paragraph">And one of these units is the code cache unit.</p>



<p class="wp-block-paragraph">it&#8217;s a lot more complicated than what you&#8217;re seeing here but basically</p>



<p class="wp-block-paragraph">system ram is very slow dealing with the cpu internally is very very very fast in comparison</p>



<p class="wp-block-paragraph">so if we have like a register or some unit that we&#8217;re accessing that&#8217;s inside of the cpu it&#8217;s</p>



<p class="wp-block-paragraph">lightning fast compared to accessing the memory so that means cpus have caches which just i&#8217;m not</p>



<p class="wp-block-paragraph">going to go too much into caches for this video but basically every time we get a new instruction</p>



<p class="wp-block-paragraph">we might put it into the cache, which is like incredibly fast because it&#8217;s hardware that&#8217;s part</p>



<p class="wp-block-paragraph">of the CPU. Then maybe sometime in the future, we might want to execute the same exact instruction</p>



<p class="wp-block-paragraph">at the same exact address. Again, that&#8217;s possible. Remember we have loops. We can execute the same</p>



<p class="wp-block-paragraph">instructions a bunch of times in a row. So if we come across an instruction that we have executed</p>



<p class="wp-block-paragraph">in the past and it&#8217;s already in the code cache, then we don&#8217;t actually need to wait for memory</p>



<p class="wp-block-paragraph">to fetch the instruction for us, we can just go directly to the code cache and it&#8217;s a dramatic</p>



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



<p class="wp-block-paragraph">Remember, when you access memory, execution has to stall for quite a long time usually</p>



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



<p class="wp-block-paragraph">Okay, so that&#8217;s what the code cache does.</p>



<p class="wp-block-paragraph">Once the memory and the code cache have figured out, okay, we got the instruction and everything&#8217;s</p>



<p class="wp-block-paragraph">good, then we just sort of decode the instruction.</p>



<p class="wp-block-paragraph">We decide, you know, what is in the instruction?</p>



<p class="wp-block-paragraph">it all like data or is it all like like is it half and half and part instruction</p>



<p class="wp-block-paragraph">part data part whatever there&#8217;s lots of different formats for instructions so</p>



<p class="wp-block-paragraph">the instruction decoder just sort of like you know decides how to separate</p>



<p class="wp-block-paragraph">it and it sends data into the control unit notice how there&#8217;s a little arrow</p>



<p class="wp-block-paragraph">pointing from the instruction decoder to the instruction pointer so we could use</p>



<p class="wp-block-paragraph">that information to decide how to modify the RIP register like if we&#8217;re gonna go</p>



<p class="wp-block-paragraph">or if we&#8217;re going to jump somewhere.</p>



<p class="wp-block-paragraph">And then the, let&#8217;s see,</p>



<p class="wp-block-paragraph">when we send the instruction to the control unit,</p>



<p class="wp-block-paragraph">the control unit is not necessarily the brain</p>



<p class="wp-block-paragraph">because the whole CPU is the brain of the computer,</p>



<p class="wp-block-paragraph">but the control unit is kind of like the decider of the CPU.</p>



<p class="wp-block-paragraph">At least that&#8217;s the way I see it.</p>



<p class="wp-block-paragraph">It decides what to do.</p>



<p class="wp-block-paragraph">There&#8217;s a lot of other subunits in the CPU.</p>



<p class="wp-block-paragraph">Notice how down here we have, you know, CPU registers</p>



<p class="wp-block-paragraph">and we also have the all you.</p>



<p class="wp-block-paragraph">the all you the all you is basically the arithmetic logical unit so it&#8217;s it&#8217;s a little subunit that</p>



<p class="wp-block-paragraph">will execute mathematical instructions or logical instructions like it&#8217;ll take some bits and it&#8217;ll</p>



<p class="wp-block-paragraph">it&#8217;ll move them to the left or the right or it&#8217;ll flip the bits or whatever or it&#8217;ll add two numbers</p>



<p class="wp-block-paragraph">together the registers you know they&#8217;re very very very fast they&#8217;re like you know the variables of</p>



<p class="wp-block-paragraph">the cpu um so the control unit will examine uh whatever it&#8217;s getting and it&#8217;ll decide all right</p>



<p class="wp-block-paragraph">want us to do it wants us to add two integers together or it wants us to</p>



<p class="wp-block-paragraph">write something to memory or it wants us to do whatever right so when</p>



<p class="wp-block-paragraph">the control unit receives input remember this is all just like ones and zeros</p>



<p class="wp-block-paragraph">right so imagine if we had like a bunch of ones and zeros coming in and they&#8217;re</p>



<p class="wp-block-paragraph">just wired directly into the control unit right so then the control unit has</p>



<p class="wp-block-paragraph">its own internal circuitry that that makes it so well if I receive this</p>



<p class="wp-block-paragraph">going to tell everybody else to add numbers together and if I see some other number I&#8217;m</p>



<p class="wp-block-paragraph">going to tell everybody else to multiply numbers together so the control unit is really responsible</p>



<p class="wp-block-paragraph">for deciding what the other units need to do in order to accomplish the current instruction</p>



<p class="wp-block-paragraph">so gosh I really need to fix this I just reconfigured this thing and now it&#8217;s just like</p>



<p class="wp-block-paragraph">drawing even when I don&#8217;t want it to draw so the control unit let&#8217;s say that we do that we have</p>



<p class="wp-block-paragraph">that we have like an addition instruction we want to add two registers together the control unit will</p>



<p class="wp-block-paragraph">send this down the wire to let the all you unit know that it needs to perform addition the all</p>



<p class="wp-block-paragraph">you unit also has inputs and based on what it receives it will decide to do something it&#8217;ll</p>



<p class="wp-block-paragraph">decide to add or subtract or multiply or just whatever so the control unit sends the right</p>



<p class="wp-block-paragraph">input into the all you unit so that it knows to add or do whatever we wanted it to do</p>



<p class="wp-block-paragraph">operands like it might receive data that just came from memory it might receive registers it might</p>



<p class="wp-block-paragraph">receive just like a raw immediate number that&#8217;s embedded into the instruction on the other hand</p>



<p class="wp-block-paragraph">we might also send something into the floating point unit so the control unit it&#8217;s always sending</p>



<p class="wp-block-paragraph">all of these signals to all of its you know connected units these wires it&#8217;s not like we</p>



<p class="wp-block-paragraph">can remove these wires so there&#8217;s always something even if it&#8217;s a zero going down the line</p>



<p class="wp-block-paragraph">so the control unit will basically send the right signals down to the all you to tell it you&#8217;re</p>



<p class="wp-block-paragraph">going to add and it&#8217;ll send the right signals down to the floating point unit to say you&#8217;re</p>



<p class="wp-block-paragraph">not going to do anything or just we&#8217;re not we&#8217;re going to ignore the result there&#8217;s like all these</p>



<p class="wp-block-paragraph">logical gates that we&#8217;re not showing on this diagram where we can just like ignore the output</p>



<p class="wp-block-paragraph">result of some unit that we&#8217;re not going to use so it&#8217;s sending all these signals to these units</p>



<p class="wp-block-paragraph">Just like a quick note here, the all you only deals with integer math and, you know, bitwise</p>



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



<p class="wp-block-paragraph">Floating point numbers in the machine are stored totally differently than integers.</p>



<p class="wp-block-paragraph">So we need a totally different unit in order to process them.</p>



<p class="wp-block-paragraph">So that&#8217;s why there are two units there.</p>



<p class="wp-block-paragraph">Well, I mean, I guess depending on, you know, what CPU we&#8217;re talking about, but like as</p>



<p class="wp-block-paragraph">far as the ones that I&#8217;m aware of and that I use.</p>



<p class="wp-block-paragraph">So then, you know, the data cache, kind of the same thing.</p>



<p class="wp-block-paragraph">kind of the same thing um you know there&#8217;s like a code cache up here and a data cache down there</p>



<p class="wp-block-paragraph">so sometimes we might want to fetch data that we&#8217;ve already fetched before and we don&#8217;t want</p>



<p class="wp-block-paragraph">to hit system ram so you know we don&#8217;t want that slow down so we&#8217;ll just take it from the cache</p>



<p class="wp-block-paragraph">and um we will also populate the cache when we have a result so when this like all you is done</p>



<p class="wp-block-paragraph">adding whatever it&#8217;s going to add it&#8217;ll send its result data to the data cache and then um</p>



<p class="wp-block-paragraph">two or three or whatever then we can reuse the result in another instruction or send it back to</p>



<p class="wp-block-paragraph">memory or do whatever we want to so this is like a basic idea right like you you start off by you</p>



<p class="wp-block-paragraph">know grabbing something from memory possibly if it&#8217;s already in the cache then you wouldn&#8217;t</p>



<p class="wp-block-paragraph">actually grab it and then the instruction decoder decides you know are we gonna we&#8217;re gonna jump</p>



<p class="wp-block-paragraph">are we gonna go to the next instruction after this is over so it&#8217;ll have the next address</p>



<p class="wp-block-paragraph">notice how there&#8217;s kind of like a little cycle that goes here between code cache instruction</p>



<p class="wp-block-paragraph">decoder and instruction pointer and so you know we&#8217;re always kind of deciding what&#8217;s the next</p>



<p class="wp-block-paragraph">instruction that we&#8217;re going to execute and then we like you know send the decoded instruction to</p>



<p class="wp-block-paragraph">the control unit the control unit decides you know are you going to do some addition are we</p>



<p class="wp-block-paragraph">going to use registers or not and then you know like are you not going to do anything or are maybe</p>



<p class="wp-block-paragraph">you are going to do something in this case I&#8217;m just saying the floating point won&#8217;t do anything</p>



<p class="wp-block-paragraph">integers. And then once that is all processed, you know, it comes to the data cache and, um,</p>



<p class="wp-block-paragraph">well, we can, we can basically just, uh, you know, write it back to memory. I think this</p>



<p class="wp-block-paragraph">diagram is not showing an arrow that will write it, write it back to memory. Um,</p>



<p class="wp-block-paragraph">yeah, the code cache is definitely not going to feed directly into memory. That&#8217;s just a cache,</p>



<p class="wp-block-paragraph">but you know, so, so imagine like there&#8217;s a, there&#8217;s like another wire here that goes</p>



<p class="wp-block-paragraph">so when we have a result we can just write it into system ram</p>



<p class="wp-block-paragraph">let me see if i forgot to say anything rip</p>



<p class="wp-block-paragraph">yeah that&#8217;s basically all i wanted to say okay so now you have a fairly good idea of at least</p>



<p class="wp-block-paragraph">one level deeper of what&#8217;s going on inside of your computer how the cpu in a general way loads</p>



<p class="wp-block-paragraph">and all these little subunits getting told what to do</p>



<p class="wp-block-paragraph">and the results getting piped back to the instruction pointer</p>



<p class="wp-block-paragraph">and also to system RAM and so forth.</p>



<p class="wp-block-paragraph">And it just keeps going like this again and again and again.</p>



<p class="wp-block-paragraph">And also just remember that at certain points</p>



<p class="wp-block-paragraph">during even one cycle, we wait for a clock tick.</p>



<p class="wp-block-paragraph">I&#8217;m not gonna say exactly when we wait</p>



<p class="wp-block-paragraph">because it&#8217;s kind of specific to what&#8217;s going on</p>



<p class="wp-block-paragraph">to be on the scope of this video.</p>



<p class="wp-block-paragraph">But yeah, the clock speed controls</p>



<p class="wp-block-paragraph">how fast these cycles go.</p>



<p class="wp-block-paragraph">And there&#8217;s much more advanced stuff</p>



<p class="wp-block-paragraph">And there&#8217;s much more advanced stuff that you could learn later,</p>



<p class="wp-block-paragraph">like pipelining and such to increase efficiency.</p>



<p class="wp-block-paragraph">But this is a basic idea of how your computer works.</p>



<p class="wp-block-paragraph">I hope you enjoyed this video.</p>



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



<p class="wp-block-paragraph">I&#8217;ll 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 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</p>



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



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



<p class="wp-block-paragraph">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 because I know somebody subscribed or followed</p>



<p class="wp-block-paragraph">subscribed or followed it just wakes me up and I get filled with joy that&#8217;s</p>



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



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



<p class="wp-block-paragraph">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</p>



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



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



<p class="wp-block-paragraph">website where you can just kind of like see all the videos I published and the</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 uh if you have a suggestion for uh uh 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</p>



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



<p class="wp-block-paragraph">really appreciate it. So again, thank you so much for watching this video and enjoy the cool music</p>



<p class="wp-block-paragraph">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/how-your-cpu-works-fetch-decode-execute-explained/">How Your CPU Works: Fetch, Decode, Execute Explained</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/how-your-cpu-works-fetch-decode-execute-explained/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Mastering x86-64 Assembly: A Beginner&#8217;s Guide to CPU Registers</title>
		<link>https://www.NeuralLantern.com/mastering-x86-64-assembly-a-beginners-guide-to-cpu-registers/</link>
					<comments>https://www.NeuralLantern.com/mastering-x86-64-assembly-a-beginners-guide-to-cpu-registers/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 27 Jul 2025 00:12:09 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[ABI]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[callee saved registers]]></category>
		<category><![CDATA[coding optimization]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[CPU registers]]></category>
		<category><![CDATA[general purpose registers]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[programming tutorial]]></category>
		<category><![CDATA[RAX register]]></category>
		<category><![CDATA[RBX register]]></category>
		<category><![CDATA[stack pointer]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=190</guid>

					<description><![CDATA[<p>Learn x86-64 assembly with this beginner’s guide to CPU registers. Master RAX, RBX, ABI rules, and optimize code. Perfect for low-level programming enthusiasts.</p>
<p>The post <a href="https://www.NeuralLantern.com/mastering-x86-64-assembly-a-beginners-guide-to-cpu-registers/">Mastering x86-64 Assembly: A Beginner&#8217;s Guide to CPU Registers</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 Assembly: A Beginner&#039;s Guide to CPU Registers" width="1380" height="776" src="https://www.youtube.com/embed/jPysL_y4rRA?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">Ready to dive into x86-64 assembly? This beginner-friendly video breaks down CPU registers like RAX, RBX, and more in Yasm assembly. Learn how to use general-purpose registers, respect the ABI to avoid debugging nightmares, and optimize code by minimizing RAM hits. We’ll cover callee-saved registers, function arguments, and why you shouldn’t mess with the stack pointer. Perfect for coders looking to master low-level programming. Grab the free book mentioned and subscribe for more assembly tips! #AssemblyProgramming #x86</p>



<p class="wp-block-paragraph">Introduction to Registers 00:00:00<br>x86-64 Yasm Assembly 00:00:04<br>Recommended Book 00:00:09<br>Move Instruction Example 00:01:04<br>64-bit Registers Overview 00:01:52<br>General Purpose Registers 00:02:24<br>Two&#8217;s Complement for Integers 00:03:37<br>Floating Point Registers Introduction 00:03:54<br>ABI Importance 00:04:55<br>Function Return Values 00:08:01<br>C++ to Assembly Translation 00:08:43<br>Avoiding System RAM 00:11:45<br>Optimizing with Registers 00:13:04<br>Callee Saved Registers 00:14:16<br>Preserving Registers with Push/Pop 00:16:40<br>Stack and Local Variables 00:22:00<br>Function Arguments in Registers 00:25:12<br>Stack Pointer and Base Pointer 00:31:29<br>Instruction Pointer (RIP) 00:36:30<br>Temporary Registers (R10, R11) 00:36:49<br>Accessing 32-bit Register Portions 00:38:29<br>Floating Point Registers Details 00:42:53<br>Conclusion and Call to Subscribe 00:45:35</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">Let&#8217;s talk about registers in assembly.</p>



<p class="wp-block-paragraph">More specifically, x8664 Yasm assembly.</p>



<p class="wp-block-paragraph">Okay, so first off, check out this book.</p>



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



<p class="wp-block-paragraph">The author is a genius.</p>



<p class="wp-block-paragraph">It&#8217;s called x8664 assembly language programming with Ubuntu.</p>



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



<p class="wp-block-paragraph">website and you know grab your copy it&#8217;s wonderful it&#8217;s released under a copy left license</p>



<p class="wp-block-paragraph">author&#8217;s a genius whenever i&#8217;m trying to remember things about cpu registers for assembly</p>



<p class="wp-block-paragraph">i always go to this section i say callie saved i search for callie saved i already had it there</p>



<p class="wp-block-paragraph">callie saved and it ends up being section 12.8.2 register usage so before we do that let me just</p>



<p class="wp-block-paragraph">that let me just give you a little quick example so you kind of know what I&#8217;m</p>



<p class="wp-block-paragraph">talking about here hopefully everybody&#8217;s seen this instruction by now this is a</p>



<p class="wp-block-paragraph">move instruction in Yasm x86 64 assembly it just moves data from one place to</p>



<p class="wp-block-paragraph">another it takes two operands so you know we have like operand one and</p>



<p class="wp-block-paragraph">operand two basically operand one is the destination operand so we could put</p>



<p class="wp-block-paragraph">destination there if we wanted to operand two is the source we could put</p>



<p class="wp-block-paragraph">that actually won&#8217;t, you know, compile or assemble,</p>



<p class="wp-block-paragraph">but I&#8217;m just saying that&#8217;s what those positions are for.</p>



<p class="wp-block-paragraph">So here&#8217;s an example of a register receiving the value 10.</p>



<p class="wp-block-paragraph">We use the move instruction and we say,</p>



<p class="wp-block-paragraph">we would like to move something into the REX register.</p>



<p class="wp-block-paragraph">We would like to move the value 10 into the REX register.</p>



<p class="wp-block-paragraph">And there you have it. There&#8217;s a move instruction. Okay.</p>



<p class="wp-block-paragraph">So REX is a register.</p>



<p class="wp-block-paragraph">that it is a 64-bit register in our 64-bit CPUs every single register or</p>



<p class="wp-block-paragraph">sorry every single general purpose register and the plot registers they</p>



<p class="wp-block-paragraph">have 64 bits available for us to use 64 bits like you know ones and zeros it&#8217;s</p>



<p class="wp-block-paragraph">like a very very very big number a long time ago we had 32 bit registers and so</p>



<p class="wp-block-paragraph">these are like you know twice as mathematically robust or whatever you</p>



<p class="wp-block-paragraph">allows the registers to address, you know, the 64 bit memory addressing space,</p>



<p class="wp-block-paragraph">which helps us go beyond four gigabytes of RAM in our modern computers,</p>



<p class="wp-block-paragraph">helps us go far beyond what we can even achieve right now.</p>



<p class="wp-block-paragraph">So we have a register called RAX and there&#8217;s a list of other registers on this</p>



<p class="wp-block-paragraph">page here. So basically, you know,</p>



<p class="wp-block-paragraph">we have the RAX register and the RBX register and the RCX register and so</p>



<p class="wp-block-paragraph">forth. I have a hard time remembering all these names,</p>



<p class="wp-block-paragraph">remembering all these names so I go to this book when I forget but otherwise</p>



<p class="wp-block-paragraph">you know you can kind of think of like ABCD and then just surround them with</p>



<p class="wp-block-paragraph">RNX you know our kind of for register and X for extra big maybe so we&#8217;ve got</p>



<p class="wp-block-paragraph">the RAX RBX RCX and RDX registers and then we have a bunch of other ones</p>



<p class="wp-block-paragraph">through R15 but first before we do that let me just emphasize that these are</p>



<p class="wp-block-paragraph">general purpose registers to be used for integers or data you can put</p>



<p class="wp-block-paragraph">characters in there for strings you can put just like you know whatever data you</p>



<p class="wp-block-paragraph">want inside the machine we use twos complement to represent integers so it&#8217;s</p>



<p class="wp-block-paragraph">it&#8217;s fine the instructions that you will do for addition and other mathematical</p>



<p class="wp-block-paragraph">but don&#8217;t put floating point numbers in these registers because the computer the</p>



<p class="wp-block-paragraph">system the machine won&#8217;t know how to operate on that data the thing is</p>



<p class="wp-block-paragraph">integers are stored as twos complement I&#8217;m going to talk about that more in</p>



<p class="wp-block-paragraph">another video and floats are stored in something called I triple E seven five</p>



<p class="wp-block-paragraph">four standard which is just like a crazy different way of storing floating point</p>



<p class="wp-block-paragraph">numbers that makes more sense for floating point numbers but because the</p>



<p class="wp-block-paragraph">differently in the machine you can&#8217;t really use normal instructions like you</p>



<p class="wp-block-paragraph">can&#8217;t add two floats together the same with the same instruction that you would</p>



<p class="wp-block-paragraph">add two integers together you would have to use special floating point registers</p>



<p class="wp-block-paragraph">and special floating point instructions I&#8217;ll talk about floats at the end of</p>



<p class="wp-block-paragraph">this video I hope but basically just keep in mind these are general purpose</p>



<p class="wp-block-paragraph">registers to be used for integers in data only okay so what am I talking</p>



<p class="wp-block-paragraph">Okay, so what am I talking about?</p>



<p class="wp-block-paragraph">What are we seeing here on the side here?</p>



<p class="wp-block-paragraph">Why does it say return value?</p>



<p class="wp-block-paragraph">What is going on with Kali saved and fourth argument and third argument and so forth?</p>



<p class="wp-block-paragraph">So there&#8217;s a standard that you&#8217;re supposed to use when you program.</p>



<p class="wp-block-paragraph">You can get yourself into a lot of hot water or you can dramatically increase your debugging</p>



<p class="wp-block-paragraph">time if you don&#8217;t do this.</p>



<p class="wp-block-paragraph">standard called the ABI which I think is short for application binary interface but basically I just</p>



<p class="wp-block-paragraph">like to say the ABI respect the ABI there&#8217;s a plan that people came up with that says this is how you</p>



<p class="wp-block-paragraph">should use these registers don&#8217;t use them in a different way and that way if everybody is writing</p>



<p class="wp-block-paragraph">you know modules and they&#8217;re all going to interact together like I write a module you write a module</p>



<p class="wp-block-paragraph">the abi then it&#8217;s it&#8217;s pretty much guaranteed that we can we can expect certain things like</p>



<p class="wp-block-paragraph">our registers won&#8217;t become corrupted after a function call or that we&#8217;re not going to corrupt</p>



<p class="wp-block-paragraph">someone else&#8217;s uh function or you know whatever right so you can also use this to benefit yourself</p>



<p class="wp-block-paragraph">even if you&#8217;re the only person who is writing your code and you&#8217;re not calling on anyone else&#8217;s</p>



<p class="wp-block-paragraph">function um then you know like how are you going to remember like what was i using this register</p>



<p class="wp-block-paragraph">Was I supposed to save that one?</p>



<p class="wp-block-paragraph">Was I supposed to preserve it?</p>



<p class="wp-block-paragraph">How was I going to return data from that function?</p>



<p class="wp-block-paragraph">Was I going to use this register or the other register?</p>



<p class="wp-block-paragraph">It&#8217;s hard to remember what you,</p>



<p class="wp-block-paragraph">what you yourself even did like a month ago.</p>



<p class="wp-block-paragraph">So if you respect this plan,</p>



<p class="wp-block-paragraph">then your functions will work not only</p>



<p class="wp-block-paragraph">with other people&#8217;s functions,</p>



<p class="wp-block-paragraph">but with your own functions from a month ago.</p>



<p class="wp-block-paragraph">The other thing too, is we have system calls, right?</p>



<p class="wp-block-paragraph">So you have like a system call.</p>



<p class="wp-block-paragraph">Let&#8217;s say we want to move something into REX.</p>



<p class="wp-block-paragraph">something into REX, maybe I want to exit the system or something.</p>



<p class="wp-block-paragraph">Just forget about this.</p>



<p class="wp-block-paragraph">This is not a system call video.</p>



<p class="wp-block-paragraph">Suppose I want to do a system call, right?</p>



<p class="wp-block-paragraph">The system call itself is also going to respect the ABI.</p>



<p class="wp-block-paragraph">So if you start calling other</p>



<p class="wp-block-paragraph">services in the system call instruction, like if you want to open a file,</p>



<p class="wp-block-paragraph">close a file, read a file, write a file, whatever.</p>



<p class="wp-block-paragraph">If you don&#8217;t respect the ABI and the system call is respecting the ABI,</p>



<p class="wp-block-paragraph">might have some of your data corrupted so it&#8217;s a really really good idea to respect the abi</p>



<p class="wp-block-paragraph">in fact think about it this way what if uh what if you wrote a bunch of functions for a long long</p>



<p class="wp-block-paragraph">time and then like a year later you couldn&#8217;t you couldn&#8217;t remember what you were doing you know</p>



<p class="wp-block-paragraph">last year oh what was the return register uh and it costs you a bunch of extra time debugging</p>



<p class="wp-block-paragraph">eventually you&#8217;ll probably decide to write your write your own standard okay from now on i&#8217;m always</p>



<p class="wp-block-paragraph">going to use this register for return values and i&#8217;m going to use this register for the first</p>



<p class="wp-block-paragraph">and i&#8217;m going to use this register for the first argument and i&#8217;m going to write you probably</p>



<p class="wp-block-paragraph">come up with a plan right that&#8217;s basically the abi the abi covers more than just how to use</p>



<p class="wp-block-paragraph">registers but um register usage is covered in the abi so why would you put yourself through</p>



<p class="wp-block-paragraph">a bunch of heartache and then eventually come up with your own plan when you could have just</p>



<p class="wp-block-paragraph">been following the correct plan in the first place right so everybody should respect the abi</p>



<p class="wp-block-paragraph">I wanted to talk next about the return value.</p>



<p class="wp-block-paragraph">Okay, so let me write up a little function here.</p>



<p class="wp-block-paragraph">Let me just say that we have a C++ function.</p>



<p class="wp-block-paragraph">And we&#8217;ll just say it&#8217;s like void f.</p>



<p class="wp-block-paragraph">And we&#8217;ll say it calls on g.</p>



<p class="wp-block-paragraph">And then we&#8217;ll have another function called g.</p>



<p class="wp-block-paragraph">And actually, maybe it&#8217;s not void.</p>



<p class="wp-block-paragraph">Maybe it actually does return a long.</p>



<p class="wp-block-paragraph">returns the number five or something. Okay.</p>



<p class="wp-block-paragraph">Hopefully you&#8217;ve seen some kind of a higher level language before like C++</p>



<p class="wp-block-paragraph">so that you can understand what&#8217;s going on here.</p>



<p class="wp-block-paragraph">We&#8217;re just making two functions and one is calling the other.</p>



<p class="wp-block-paragraph">And the second one is just returning a value to the first one.</p>



<p class="wp-block-paragraph">So in assembly, the equivalent of this would be,</p>



<p class="wp-block-paragraph">let&#8217;s make a label with the name of the function and then a little colon,</p>



<p class="wp-block-paragraph">and then just put a return instruction at the end of it. Wham,</p>



<p class="wp-block-paragraph">it, wham, you&#8217;ve got a function. It&#8217;s not going to work very well, but you do have a function at</p>



<p class="wp-block-paragraph">this point. So F, uh, I&#8217;m going to try to copy the C++, uh, function. So it&#8217;s a really good idea.</p>



<p class="wp-block-paragraph">Whenever you&#8217;re writing a function in assembly, try to imagine what the prototype would be for</p>



<p class="wp-block-paragraph">C++ and just put it as a comment at the top. So I know how the F function is going to behave in</p>



<p class="wp-block-paragraph">my assembly module because I put a comment up there just kind of reminding me what the prototype</p>



<p class="wp-block-paragraph">for C++. It doesn&#8217;t really do anything. It just sort of runs. So that&#8217;s okay. And then I&#8217;ll make</p>



<p class="wp-block-paragraph">another function down here. I&#8217;ll do the long G again in a comment, and then I&#8217;ll do the actual</p>



<p class="wp-block-paragraph">assembly version. I&#8217;ll say G colon, and then I&#8217;ll return. So just by putting that comment up,</p>



<p class="wp-block-paragraph">I kind of can remind myself now that the G function is supposed to return some kind of a value.</p>



<p class="wp-block-paragraph">up here, you know, do how about let&#8217;s say, I don&#8217;t want to start adding a bunch of local</p>



<p class="wp-block-paragraph">variables right now.</p>



<p class="wp-block-paragraph">Pretend that we&#8217;re going to print the return value of the call to G somehow we&#8217;re going</p>



<p class="wp-block-paragraph">to use C out or whatever.</p>



<p class="wp-block-paragraph">We&#8217;re going to send it to a variable, whatever.</p>



<p class="wp-block-paragraph">I&#8217;m just going to try to keep the assembly as simple as possible.</p>



<p class="wp-block-paragraph">But anyway, the point is G returns something.</p>



<p class="wp-block-paragraph">you know, a value in your assembly function.</p>



<p class="wp-block-paragraph">Well, remember that higher level languages,</p>



<p class="wp-block-paragraph">part of why they&#8217;re so awesome</p>



<p class="wp-block-paragraph">is they do a lot of extra work for us under the hood</p>



<p class="wp-block-paragraph">and they actually provide illusions to us</p>



<p class="wp-block-paragraph">that make programming easier.</p>



<p class="wp-block-paragraph">For example, there are no functions</p>



<p class="wp-block-paragraph">going on inside the actual machine.</p>



<p class="wp-block-paragraph">I mean, I guess from a certain point of view there are,</p>



<p class="wp-block-paragraph">but basically the machine is just sort of like moving data</p>



<p class="wp-block-paragraph">and jumping to instructions</p>



<p class="wp-block-paragraph">and jumping back from somewhere.</p>



<p class="wp-block-paragraph">And, you know, it&#8217;s just sort of like jumping around</p>



<p class="wp-block-paragraph">and executing instructions.</p>



<p class="wp-block-paragraph">and executing instructions there&#8217;s no actual function it&#8217;s so it&#8217;s like if you want to pretend</p>



<p class="wp-block-paragraph">that you have a function in assembly well there is a return instruction that will help you jump</p>



<p class="wp-block-paragraph">back to wherever you came from most recently but we have to we have to implement more is what i&#8217;m</p>



<p class="wp-block-paragraph">saying so how do we return a value in an assembly function we just have to load up the return value</p>



<p class="wp-block-paragraph">it as return value that just means we have to load rax up with something so for example we wanted to</p>



<p class="wp-block-paragraph">return the number five so i&#8217;m just going to load it up with the number five and then return</p>



<p class="wp-block-paragraph">now we&#8217;ve pretty much translated a c plus plus function into uh an assembly function i mean</p>



<p class="wp-block-paragraph">that&#8217;s really what&#8217;s happening under the hood when you compile your c plus plus program</p>



<p class="wp-block-paragraph">it&#8217;s um it&#8217;s just you know translating all the c plus plus into assembly</p>



<p class="wp-block-paragraph">Another thing that I should probably point out is that you should use registers as often as possible</p>



<p class="wp-block-paragraph">and you should try your best not to touch memory.</p>



<p class="wp-block-paragraph">Of course at some point you have to touch memory when you want to save your final result or send</p>



<p class="wp-block-paragraph">it off somewhere or whatever but you know imagine that you have an assembly function and you&#8217;re</p>



<p class="wp-block-paragraph">doing lots and lots of calculations you&#8217;re performing an algorithm or something your</p>



<p class="wp-block-paragraph">program will be so much more efficient by like a factor of a hundred or probably more if you don&#8217;t</p>



<p class="wp-block-paragraph">if you don&#8217;t hit system ram because every time you hit system ram like a global variable or the</p>



<p class="wp-block-paragraph">stack or something then um your cpu you know typically in the uncashed uh scenario your cpu</p>



<p class="wp-block-paragraph">has to go talk to the system bus on your motherboard and then you know send a message</p>



<p class="wp-block-paragraph">to system ram and then wait for the system ram to figure out what it&#8217;s doing and then get a response</p>



<p class="wp-block-paragraph">back so when your program is executing on the cpu you can encounter a stall which is like your</p>



<p class="wp-block-paragraph">like a hundred clock cycles or more like it&#8217;s a long time at least in the most basic case</p>



<p class="wp-block-paragraph">and how do you avoid that just use registers only when you&#8217;re doing lots of calculations the</p>



<p class="wp-block-paragraph">registers are built into the cpu they&#8217;re part of the cpu&#8217;s hardware so they are lightning fast</p>



<p class="wp-block-paragraph">compared to system ram or your disk or whatever</p>



<p class="wp-block-paragraph">i probably say this a lot but you know part of the reason you would even want to code an assembly</p>



<p class="wp-block-paragraph">Maybe you have a big program that does a lot of stuff and eventually you profile the program</p>



<p class="wp-block-paragraph">and you realize, Hey, this one part of the program is really slow because it gets called</p>



<p class="wp-block-paragraph">constantly and it&#8217;s not super efficient.</p>



<p class="wp-block-paragraph">That might be a good use case for writing a hybrid program where you have multiple modules.</p>



<p class="wp-block-paragraph">Some of your modules are in a higher level language and some of your modules are in assembly.</p>



<p class="wp-block-paragraph">So you take your most important function that slows down your program that gets called all</p>



<p class="wp-block-paragraph">like a big loop or something and you rewrite it to assembly so that you can</p>



<p class="wp-block-paragraph">have more control over how often you touch system RAM to try to make the</p>



<p class="wp-block-paragraph">whole thing more efficient and you know reduce the number of instructions and</p>



<p class="wp-block-paragraph">just like whatever and you can improve the product of the efficiency of your</p>



<p class="wp-block-paragraph">program so anyway you want to try to avoid hitting system RAM your registers</p>



<p class="wp-block-paragraph">are built in the CPU there&#8217;s 64 bits that gives us 64 bits of address space</p>



<p class="wp-block-paragraph">can reach you know back in the day we had 32 bit systems that means you could only have a memory</p>



<p class="wp-block-paragraph">stick that was about four gigabytes so now we can go much further so um yeah that&#8217;s what&#8217;s going on</p>



<p class="wp-block-paragraph">with our registers on the cpu back to the return value okay so we have return value here goes into</p>



<p class="wp-block-paragraph">the rax register and then the next thing here is it&#8217;s saying that the rbx register is something</p>



<p class="wp-block-paragraph">remember we said that uh we have to respect the abi right like you have to respect this convention</p>



<p class="wp-block-paragraph">uh because if you don&#8217;t you&#8217;re going to cost yourself debug time and your</p>



<p class="wp-block-paragraph">functions probably won&#8217;t be interoperable interoperable with other people&#8217;s functions</p>



<p class="wp-block-paragraph">or the system call uh instruction well so one of the things about the abi is some of the registers</p>



<p class="wp-block-paragraph">are designated as the function that is being called has to be the thing that saves the register&#8217;s</p>



<p class="wp-block-paragraph">registers value so that it doesn&#8217;t corrupt for the caller. Imagine this if I had a function</p>



<p class="wp-block-paragraph">called f I&#8217;m going to do like f and g here suppose the function f is going to move something into</p>



<p class="wp-block-paragraph">let&#8217;s say r12 or actually let&#8217;s do rbx move the value 10 into rbx and then it&#8217;ll call g for some</p>



<p class="wp-block-paragraph">reason maybe g doesn&#8217;t take any arguments maybe g is just kind of doing stuff let&#8217;s just say that</p>



<p class="wp-block-paragraph">g just moves a different value into rbx so the thing is when the call comes back like after we</p>



<p class="wp-block-paragraph">get to this next line let&#8217;s say we&#8217;re going to do something with do something do something with</p>



<p class="wp-block-paragraph">rbx by the time we get to this line dude can i get the line numbers on this oh yeah i forgot okay</p>



<p class="wp-block-paragraph">so by the time we get to line seven uh the register rbx is ruined these registers are not</p>



<p class="wp-block-paragraph">local variables they&#8217;re not like uh you know tied to any scope or function call these registers are</p>



<p class="wp-block-paragraph">just basically global variables that are sitting on the cpu for lightning fast performance but that</p>



<p class="wp-block-paragraph">means the rbx i use inside of f is the same rbx i use inside of g so that means if g messes up the</p>



<p class="wp-block-paragraph">value of rbx and then if f calls g then f now has a bad value for rbx this is a broken program</p>



<p class="wp-block-paragraph">broken program because we did not respect the ABI.</p>



<p class="wp-block-paragraph">The ABI says that RBX is callee saved,</p>



<p class="wp-block-paragraph">which means if I use RBX in the G,</p>



<p class="wp-block-paragraph">then I have to preserve the value so that by the time I return,</p>



<p class="wp-block-paragraph">the value is the same as it was when the call first came in.</p>



<p class="wp-block-paragraph">So how do we do that?</p>



<p class="wp-block-paragraph">We&#8217;ll do that with a push pop pair.</p>



<p class="wp-block-paragraph">I&#8217;ll probably make more videos in the future about pushing and popping,</p>



<p class="wp-block-paragraph">but I&#8217;m going to try to keep it simple for now.</p>



<p class="wp-block-paragraph">We just say, let&#8217;s push RBX onto the stack.</p>



<p class="wp-block-paragraph">at the beginning and then we&#8217;ll pop it off the stack at the very end.</p>



<p class="wp-block-paragraph">This basically means we&#8217;re going to take the value of RBX.</p>



<p class="wp-block-paragraph">We&#8217;re going to send it onto the stack.</p>



<p class="wp-block-paragraph">So, so yeah, we are hitting system RAM.</p>



<p class="wp-block-paragraph">It&#8217;s a little slower at that point.</p>



<p class="wp-block-paragraph">The ABI will save us a little time for the other registers.</p>



<p class="wp-block-paragraph">I&#8217;ll try to explain that in a second, but basically we&#8217;re preserving the value here</p>



<p class="wp-block-paragraph">with push and then we&#8217;re popping it back off.</p>



<p class="wp-block-paragraph">So we&#8217;re restoring it.</p>



<p class="wp-block-paragraph">So that means even though we ruined the value at line 14, when F does its line</p>



<p class="wp-block-paragraph">When F does its line 7, it&#8217;s going to have the correct value of RBX.</p>



<p class="wp-block-paragraph">It&#8217;s going to have the value 10.</p>



<p class="wp-block-paragraph">The book and I like to call this the prologue, just meaning this is a little section of code</p>



<p class="wp-block-paragraph">where we&#8217;re going to set things up.</p>



<p class="wp-block-paragraph">We&#8217;re going to start getting ready to do more instructions.</p>



<p class="wp-block-paragraph">And then this at the bottom is going to be called the epilogue.</p>



<p class="wp-block-paragraph">This is just like the finale.</p>



<p class="wp-block-paragraph">We&#8217;re like, we&#8217;re kind of cleaning up.</p>



<p class="wp-block-paragraph">We&#8217;re finishing up right before we return.</p>



<p class="wp-block-paragraph">If you wanted to return a value from G, then you could put, you know, the move inside of</p>



<p class="wp-block-paragraph">RAX somewhere else.</p>



<p class="wp-block-paragraph">Like you could put it like below the epilogue or just above the epilogue.</p>



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



<p class="wp-block-paragraph">As long as you&#8217;re sure that RAX is not getting trampled upon.</p>



<p class="wp-block-paragraph">So it&#8217;s important to note also that if I do a, let&#8217;s say a system call right after that,</p>



<p class="wp-block-paragraph">let&#8217;s say I call G and then I do a system call with, you know, like, you know, some</p>



<p class="wp-block-paragraph">you know some other number as the call code i want to open a file i want to close a file</p>



<p class="wp-block-paragraph">i want to read or write a file whatever i want to ask the system to do something for me the</p>



<p class="wp-block-paragraph">system is also going to respect the abi which means i&#8217;m guaranteed that rbx is not modified</p>



<p class="wp-block-paragraph">when the system call comes back if if the system call you know if the syscall instruction didn&#8217;t</p>



<p class="wp-block-paragraph">respect the abi then my rbx could be ruined i&#8217;d have to do a bunch of stuff to preserve it so this</p>



<p class="wp-block-paragraph">some of these registers let me go down a little bit here like this R10 the</p>



<p class="wp-block-paragraph">temporary register and also the RDI if you decided to use that in the body of</p>



<p class="wp-block-paragraph">your program which you&#8217;re allowed to these are not designated as callee saved</p>



<p class="wp-block-paragraph">which means system call could actually ruin the value of those registers so</p>



<p class="wp-block-paragraph">suppose for the sake of argument I really decided that I needed to use I</p>



<p class="wp-block-paragraph">10 because that&#8217;s marked as a temporary I&#8217;ll put a value in our 10 and then I&#8217;ll say you know</p>



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



<p class="wp-block-paragraph">With our 10 I&#8217;m sorry our 10</p>



<p class="wp-block-paragraph">Then that means by the time I&#8217;m done with my call to G</p>



<p class="wp-block-paragraph">I should assume that our 10 is ruined because even though you&#8217;re you can see right here that that G doesn&#8217;t actually do anything</p>



<p class="wp-block-paragraph">To our 10 we should assume that we have to respect the ABI which means the other function could have ruined it</p>



<p class="wp-block-paragraph">Ruined it. This is especially true if you call someone else&#8217;s module or system call or whatever same thing for the system call</p>



<p class="wp-block-paragraph">so if I call system call</p>



<p class="wp-block-paragraph">and I use</p>



<p class="wp-block-paragraph">R10 at some point before and then after then I have to assume R10 is destroyed by the time I get back in system call</p>



<p class="wp-block-paragraph">So that&#8217;s no good</p>



<p class="wp-block-paragraph">The cure to that when we&#8217;re talking about a temporary or something that is not designated as callee saved is that the caller has to save</p>



<p class="wp-block-paragraph">hit a system RAM just to make a call.</p>



<p class="wp-block-paragraph">Anything that we think we need when we&#8217;re finished.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do a push.</p>



<p class="wp-block-paragraph">This is not the only way to do it, but I&#8217;m going to do a push R10 here.</p>



<p class="wp-block-paragraph">And then afterwards, I&#8217;m going to do pop R10.</p>



<p class="wp-block-paragraph">So that sucks.</p>



<p class="wp-block-paragraph">And then I have to do the same thing for the system call.</p>



<p class="wp-block-paragraph">I can go push R10 and then pop R10.</p>



<p class="wp-block-paragraph">And of course, if you were clever, you probably would, you know, put the call to G inside</p>



<p class="wp-block-paragraph">of that system call push pop pair.</p>



<p class="wp-block-paragraph">So you could have one less push pop pair.</p>



<p class="wp-block-paragraph">push pop pair but I&#8217;m just saying there&#8217;s no guarantee r10 is going to survive so you always</p>



<p class="wp-block-paragraph">have to preserve it if you ever want to use it again so this is like another way that the abi</p>



<p class="wp-block-paragraph">kind of can help you save time notice how here in the uh in the g function which I probably should</p>



<p class="wp-block-paragraph">have prototyped let me just say it looks like to me it&#8217;s a void and it doesn&#8217;t take any arguments</p>



<p class="wp-block-paragraph">to prototype your functions in comments.</p>



<p class="wp-block-paragraph">Maybe let me do the same thing to F up here.</p>



<p class="wp-block-paragraph">So F looks like it&#8217;s not returning anything and it&#8217;s not taking anything.</p>



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



<p class="wp-block-paragraph">Those are C++ comments that would not compile.</p>



<p class="wp-block-paragraph">All right. So notice how I&#8217;m using RBX.</p>



<p class="wp-block-paragraph">So I decided to preserve RBX, but there&#8217;s also other registers that are marked as</p>



<p class="wp-block-paragraph">marked as Kali saved like R12 through 15 and RBP and whatever,</p>



<p class="wp-block-paragraph">but I&#8217;m not preserving those.</p>



<p class="wp-block-paragraph">The reason I don&#8217;t have to preserve those is because I&#8217;m not using them.</p>



<p class="wp-block-paragraph">So the, uh, the, uh, the ABI can save you a little time here.</p>



<p class="wp-block-paragraph">Like for example, I&#8217;m preserving R10 because I&#8217;m supposed to assume that I,</p>



<p class="wp-block-paragraph">that I, that they could be destroyed by the time the function comes back.</p>



<p class="wp-block-paragraph">What if instead of using R10, I used R12.</p>



<p class="wp-block-paragraph">have to surround any calls with a push pop pair because I can trust that whoever is going to</p>



<p class="wp-block-paragraph">modify R12 will preserve it for me. But then notice down here G is not actually using R12.</p>



<p class="wp-block-paragraph">So G doesn&#8217;t even do push pop on R12. So we save hits to memory. We don&#8217;t even have to touch memory</p>



<p class="wp-block-paragraph">to preserve R12 because the ABI is helping us sort of stay a little bit more efficient.</p>



<p class="wp-block-paragraph">So this is great, right? F is a broken function at this point though, because I&#8217;m using</p>



<p class="wp-block-paragraph">at this point though because I&#8217;m using two registers that are callee saved I</p>



<p class="wp-block-paragraph">can&#8217;t assume that whoever called F is gonna know that I&#8217;m modifying RBX and</p>



<p class="wp-block-paragraph">R12 so I should do a prolog and an epilog up here I&#8217;m gonna say push RBX</p>



<p class="wp-block-paragraph">and then I&#8217;m gonna push R12 and then down here at the bottom I&#8217;m gonna do an</p>



<p class="wp-block-paragraph">epilog epilog and I&#8217;m gonna pop RBX and I&#8217;m gonna pop R12</p>



<p class="wp-block-paragraph">but I&#8217;ve done something wrong.</p>



<p class="wp-block-paragraph">The thing is, I&#8217;m not going to talk about the stack too much in this video,</p>



<p class="wp-block-paragraph">but the stack is a particular type of data structure</p>



<p class="wp-block-paragraph">that will return to you data in a reverse order than you sent into it.</p>



<p class="wp-block-paragraph">This is great for helping us keep track of function calls and return addresses,</p>



<p class="wp-block-paragraph">as I&#8217;ll probably try to explain in this video.</p>



<p class="wp-block-paragraph">But basically, the data comes out backwards.</p>



<p class="wp-block-paragraph">The data comes out backwards. So if I do it like this</p>



<p class="wp-block-paragraph">Then actually by the time the return statement instruction gets executed</p>



<p class="wp-block-paragraph">RBX will have the value that was intended for R12 and R12 will have the value that was intended for RBX</p>



<p class="wp-block-paragraph">It&#8217;ll be backwards. So you have to do your push pops in the reverse order to notice how</p>



<p class="wp-block-paragraph">It&#8217;s kind of like a shell that goes outwards. It&#8217;s like R12 is first on the inside and then RBX is next on the outside</p>



<p class="wp-block-paragraph">That&#8217;s just what you have to do to preserve everything.</p>



<p class="wp-block-paragraph">So the F function works now.</p>



<p class="wp-block-paragraph">The G function, I think it works now.</p>



<p class="wp-block-paragraph">We have a prologue and epilogue.</p>



<p class="wp-block-paragraph">Okay, so we talked about the ABI.</p>



<p class="wp-block-paragraph">We talked about calls.</p>



<p class="wp-block-paragraph">We talked about the registers.</p>



<p class="wp-block-paragraph">Talked about callee saved.</p>



<p class="wp-block-paragraph">We talked about the fact that we don&#8217;t have to save the other ones.</p>



<p class="wp-block-paragraph">I think I can talk about…</p>



<p class="wp-block-paragraph">um so uh one thing that i should make sure to mention is that these are general purpose registers</p>



<p class="wp-block-paragraph">they&#8217;re to be used for integers integer data or like characters or just like regular data</p>



<p class="wp-block-paragraph">they use certain instructions that are not to be used with floating point data so you actually</p>



<p class="wp-block-paragraph">should not store floating point numbers inside of your general purpose registers you should only</p>



<p class="wp-block-paragraph">store floating points in special registers which i&#8217;ll talk about at the end of this video</p>



<p class="wp-block-paragraph">So keep that in mind.</p>



<p class="wp-block-paragraph">Floating point numbers are stored differently in the system.</p>



<p class="wp-block-paragraph">They&#8217;re stored with a scheme called IEEE 754 floating point.</p>



<p class="wp-block-paragraph">So like the numbers wouldn&#8217;t make sense.</p>



<p class="wp-block-paragraph">Integers and floats are stored differently.</p>



<p class="wp-block-paragraph">So the hardware is wired differently to operate on two different types of data.</p>



<p class="wp-block-paragraph">So anyway, I just want to say that this is only for integers and general purpose data.</p>



<p class="wp-block-paragraph">So now let&#8217;s look at the other types of registers.</p>



<p class="wp-block-paragraph">rc x and rd x and rsi and rdi are designated as arguments first second third and fourth</p>



<p class="wp-block-paragraph">okay so let me uh let me write like a quick c plus function here and this is going to be uh let&#8217;s</p>



<p class="wp-block-paragraph">say we have like a function that returns some value we&#8217;ll call it uh we&#8217;ll call it f and we&#8217;ll</p>



<p class="wp-block-paragraph">say that f takes in some arguments let&#8217;s say that it takes in three arguments um again uh</p>



<p class="wp-block-paragraph">integers right now because we&#8217;re only using integer registers.</p>



<p class="wp-block-paragraph">We can&#8217;t use floating point arguments or return values.</p>



<p class="wp-block-paragraph">Just keep that in mind.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to do like three arguments long A and B and C.</p>



<p class="wp-block-paragraph">OK, so we can take in three arguments and maybe we&#8217;re just going to return,</p>



<p class="wp-block-paragraph">you know, a plus B plus C.</p>



<p class="wp-block-paragraph">OK, simple C++ function.</p>



<p class="wp-block-paragraph">Let&#8217;s do this in assembly.</p>



<p class="wp-block-paragraph">We&#8217;ll make a label and we&#8217;ll do a little comment that just reminds us.</p>



<p class="wp-block-paragraph">of sorry of the</p>



<p class="wp-block-paragraph">Function that we&#8217;re trying to implement we stick a little return instruction at the end of it so that</p>



<p class="wp-block-paragraph">it will jump back to the caller and</p>



<p class="wp-block-paragraph">then we&#8217;ll say</p>



<p class="wp-block-paragraph">Well when the caller of this function</p>



<p class="wp-block-paragraph">Called us and gave us a and B and C. How do we get a and B and C?</p>



<p class="wp-block-paragraph">Well a is the first integer argument, so it&#8217;s just going to be the RDI register</p>



<p class="wp-block-paragraph">So we&#8217;ll literally just use a move instruction</p>



<p class="wp-block-paragraph">just use a move instruction.</p>



<p class="wp-block-paragraph">Let&#8217;s say we want to, for whatever reason, use the R12 register</p>



<p class="wp-block-paragraph">and we&#8217;ll do something with it later.</p>



<p class="wp-block-paragraph">We&#8217;ll print it, we&#8217;ll save it, we&#8217;ll do whatever.</p>



<p class="wp-block-paragraph">Let&#8217;s just grab the incoming argument.</p>



<p class="wp-block-paragraph">So we&#8217;ll just grab RDI.</p>



<p class="wp-block-paragraph">So I&#8217;m going to put a little comment up above here, you know, grab</p>



<p class="wp-block-paragraph">a and do something</p>



<p class="wp-block-paragraph">with it and I&#8217;ll just write down here more instructions just to just to denote</p>



<p class="wp-block-paragraph">Just to denote that we&#8217;re doing something with the a value, but I&#8217;m not going to write it down here because I don&#8217;t want this to get huge.</p>



<p class="wp-block-paragraph">So we&#8217;ll do the same thing with the b.</p>



<p class="wp-block-paragraph">Maybe I should remember that programming is case sensitive.</p>



<p class="wp-block-paragraph">We&#8217;ll do something with the b.</p>



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



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



<p class="wp-block-paragraph">So that&#8217;s actually RSI.</p>



<p class="wp-block-paragraph">And then we&#8217;ll do something else.</p>



<p class="wp-block-paragraph">We could have used R13 or other registers if we wanted to.</p>



<p class="wp-block-paragraph">I&#8217;m just showing you how to grab incoming arguments.</p>



<p class="wp-block-paragraph">So then we&#8217;ll grab the c and do something with it also.</p>



<p class="wp-block-paragraph">C and do something with it also it&#8217;s not going to be RDI it&#8217;s going to be RDX and you can do this</p>



<p class="wp-block-paragraph">up to like I think six arguments let me just double check here yeah so like R9 ends up being</p>



<p class="wp-block-paragraph">the sixth argument and if you want more than that then the caller will have to put stuff on the stack</p>



<p class="wp-block-paragraph">and in fact if you if you kind of like understand what&#8217;s happening here let me just do like a main</p>



<p class="wp-block-paragraph">Let&#8217;s say we do cout whatever f returns.</p>



<p class="wp-block-paragraph">You know, this is like a typical function call, right?</p>



<p class="wp-block-paragraph">So we&#8217;ll call f.</p>



<p class="wp-block-paragraph">f will jump in there.</p>



<p class="wp-block-paragraph">It&#8217;ll jump into its body.</p>



<p class="wp-block-paragraph">It&#8217;ll do some sort of a manipulation with the incoming arguments.</p>



<p class="wp-block-paragraph">It&#8217;ll return a final value.</p>



<p class="wp-block-paragraph">Then that final value gets printed out.</p>



<p class="wp-block-paragraph">So what&#8217;s 5 plus 6 plus 7?</p>



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



<p class="wp-block-paragraph">What is that, like 18?</p>



<p class="wp-block-paragraph">I&#8217;m bad at math.</p>



<p class="wp-block-paragraph">So let&#8217;s just say that&#8217;s 18 gets printed.</p>



<p class="wp-block-paragraph">So what happens is, um, we return, Oh, I forgot to return.</p>



<p class="wp-block-paragraph">I&#8217;m going to say compute the, uh,</p>



<p class="wp-block-paragraph">compute the sum of the things.</p>



<p class="wp-block-paragraph">Uh, and I&#8217;ll just say like block cause I don&#8217;t want to put a bunch of addition</p>



<p class="wp-block-paragraph">instructions here. Uh, and we&#8217;ll just like store in, uh,</p>



<p class="wp-block-paragraph">in R13, let&#8217;s say. So then when we have finally computed our result, I&#8217;ll just move R13 into the</p>



<p class="wp-block-paragraph">return value. And what I&#8217;m really trying to say here is if you imagine a function that behaves</p>



<p class="wp-block-paragraph">this way in C++, then what I&#8217;ve written down below is really what&#8217;s actually happening in</p>



<p class="wp-block-paragraph">assembly. Your compiler compiles the code down to assembly language first, and then it assembles it</p>



<p class="wp-block-paragraph">down to machine code. So really this is what&#8217;s happening under the hood. It&#8217;s not like a special</p>



<p class="wp-block-paragraph">under the hood it&#8217;s not like a special trick when you call a function in C++</p>



<p class="wp-block-paragraph">literally the A B and C have been loaded up just before the the jump</p>



<p class="wp-block-paragraph">instruction to go to that you know line 9 they&#8217;ve been loaded up with the</p>



<p class="wp-block-paragraph">appropriate values so that&#8217;s part of like you know the magic of what higher</p>



<p class="wp-block-paragraph">level languages give us it&#8217;s just makes things a little bit easier okay so I&#8217;ve</p>



<p class="wp-block-paragraph">If I had only used RDI and RSI and RDX, those are not designated as callee saved.</p>



<p class="wp-block-paragraph">So I wouldn&#8217;t have had to preserve those, but because I chose to use R12 and R13, I</p>



<p class="wp-block-paragraph">have to preserve those for the caller, even if the caller is not going to use them.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do, I&#8217;m going to do push R12 and I&#8217;m going to do push R13.</p>



<p class="wp-block-paragraph">And then at the bottom, let me do like a prologue here.</p>



<p class="wp-block-paragraph">is kind of helpful because it kind of helps you remember like, oh, did I forget the prologue?</p>



<p class="wp-block-paragraph">Did I forget the epilogue? They got a match. And then I guess the fact that you can see the words</p>



<p class="wp-block-paragraph">prologue and epilogue also helps remind you that everything is supposed to be in reverse order</p>



<p class="wp-block-paragraph">in the epilogue. So again, notice how R13 is on the inside and R12 is on the outside</p>



<p class="wp-block-paragraph">of the push pop pair. So now my program is good. And we&#8217;ve, you know, successfully translated</p>



<p class="wp-block-paragraph">We&#8217;ve, you know, successfully translated a nice function call there.</p>



<p class="wp-block-paragraph">And we&#8217;ve done a bunch of nonsense in the middle that I&#8217;m not writing down at this point.</p>



<p class="wp-block-paragraph">We are respecting the ABI.</p>



<p class="wp-block-paragraph">So if somebody else uses our function in their module, then they can be pretty confident</p>



<p class="wp-block-paragraph">that we&#8217;re not going to ruin their registers and so forth.</p>



<p class="wp-block-paragraph">We talked about the return value.</p>



<p class="wp-block-paragraph">We talked about that these general purpose registers are not for floats.</p>



<p class="wp-block-paragraph">We talked about pushes and pops.</p>



<p class="wp-block-paragraph">and pops. We&#8217;ve talked about function arguments,</p>



<p class="wp-block-paragraph">which right now we can only do integer arguments and return values.</p>



<p class="wp-block-paragraph">We&#8217;ve talked about, uh, well, he talked about the stack a little bit.</p>



<p class="wp-block-paragraph">So let me, let me just point out that, um,</p>



<p class="wp-block-paragraph">suppose you have a function. I just want to mention this briefly.</p>



<p class="wp-block-paragraph">Suppose you have a function F let&#8217;s say it&#8217;s, it&#8217;s void.</p>



<p class="wp-block-paragraph">So it doesn&#8217;t return anything and it has an integer a equals five.</p>



<p class="wp-block-paragraph">I think the reason that I wanted to do this is to show you how dangerous it is to mess</p>



<p class="wp-block-paragraph">with the wrong register like the stack register.</p>



<p class="wp-block-paragraph">So suppose we have two variables and then we&#8217;ll do something with a and b.</p>



<p class="wp-block-paragraph">So now suppose we have two threads.</p>



<p class="wp-block-paragraph">We have an execution thread one and an execution thread two.</p>



<p class="wp-block-paragraph">If you&#8217;re not familiar with threads, that just basically means your computer is literally</p>



<p class="wp-block-paragraph">executing your function or sorry, it&#8217;s executing twice at the exact same time.</p>



<p class="wp-block-paragraph">Maybe not the exact same time if you only have a single core on your CPU, but you could</p>



<p class="wp-block-paragraph">imagine that that&#8217;s possible, especially if you have many cores, that&#8217;s an operating systems</p>



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



<p class="wp-block-paragraph">But basically pretend that we have an execution thread.</p>



<p class="wp-block-paragraph">So it&#8217;s like going through your program, executing one instruction at a time.</p>



<p class="wp-block-paragraph">Then you launch another execution thread that also executes your program one instruction</p>



<p class="wp-block-paragraph">at a time.</p>



<p class="wp-block-paragraph">at a time so at some point it&#8217;s possible that um thread one and thread two could be executing f</p>



<p class="wp-block-paragraph">at the exact same time right so i&#8217;m not going to talk too much about this because this is not an</p>



<p class="wp-block-paragraph">os video but basically this could create a race condition where thread one and thread two kind</p>



<p class="wp-block-paragraph">of step on each other&#8217;s toes and they both try to modify the value of a uh at the wrong time and</p>



<p class="wp-block-paragraph">because the other thread modified it.</p>



<p class="wp-block-paragraph">So this is why local variables in a function actually live on the stack.</p>



<p class="wp-block-paragraph">A and B are not global variables.</p>



<p class="wp-block-paragraph">They&#8217;re actually temporary local variables that are sitting on the stack.</p>



<p class="wp-block-paragraph">And the way the stack works, I&#8217;m not going to talk about it too much in this video,</p>



<p class="wp-block-paragraph">is that, well, every function kind of has its own area of the stack.</p>



<p class="wp-block-paragraph">So when the function comes in, on the stack sits the return address</p>



<p class="wp-block-paragraph">return address so that the function knows where to jump back to when it returns and any local</p>



<p class="wp-block-paragraph">variable that the function makes. So if you think about it, when thread one calls F, we actually</p>



<p class="wp-block-paragraph">end up with a different version of A and a different version of B. We could imagine them</p>



<p class="wp-block-paragraph">as being, you know, A sub one and B sub one. But when thread two comes in and, you know,</p>



<p class="wp-block-paragraph">creates those local variables, we can imagine that we get two different versions of A and B also.</p>



<p class="wp-block-paragraph">of a and b also so this is how the computer prevents multiple threads from stepping on each</p>



<p class="wp-block-paragraph">other&#8217;s toes if you&#8217;re using local variables local variables are just local to the function</p>



<p class="wp-block-paragraph">this also helps if a function calls itself a bunch of times maybe one call or another might want to</p>



<p class="wp-block-paragraph">see a different version of a or b and it would be just incredibly difficult to keep track of that</p>



<p class="wp-block-paragraph">if you were using globals but when you use locals well they&#8217;re just basically different variables</p>



<p class="wp-block-paragraph">trying to lead up to is the stack is pretty important if you corrupt the</p>



<p class="wp-block-paragraph">stack then you probably are going to crash the entire program you might make</p>



<p class="wp-block-paragraph">it so that a function doesn&#8217;t know where to return from like when you do the</p>



<p class="wp-block-paragraph">return statement it might jump to some other part of the code that doesn&#8217;t even</p>



<p class="wp-block-paragraph">make sense and then everything crashes or you might mess up some data in a local</p>



<p class="wp-block-paragraph">variable like if you mess up if you mess up the stack you might be messing up the</p>



<p class="wp-block-paragraph">that the caller is depending on and so then the caller continues to execute and</p>



<p class="wp-block-paragraph">it sees the wrong value and it just crashes or you might even mess up your</p>



<p class="wp-block-paragraph">own local variables so don&#8217;t miss with the stack the reason I&#8217;m saying that now</p>



<p class="wp-block-paragraph">is because this very this register right here the RBP sorry that&#8217;s not it the</p>



<p class="wp-block-paragraph">RSP register that&#8217;s the stack pointer eventually you can learn how to</p>



<p class="wp-block-paragraph">manipulate the stack pointer in order to create your own local variables that&#8217;s</p>



<p class="wp-block-paragraph">variables that&#8217;s okay once you know what you&#8217;re doing but unless you know exactly what you&#8217;re</p>



<p class="wp-block-paragraph">doing you probably shouldn&#8217;t touch the stack pointer especially you shouldn&#8217;t mess with it</p>



<p class="wp-block-paragraph">right before you call a function or right before you return from your own function be very careful</p>



<p class="wp-block-paragraph">with that the rbp uh register is similarly dangerous uh it won&#8217;t like automatically destroy</p>



<p class="wp-block-paragraph">your program but it is uh it&#8217;s usually i mean it&#8217;s quite often used as sort of like a bookmark for</p>



<p class="wp-block-paragraph">mark for where the stack pointer was pointing in other modules and other</p>



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



<p class="wp-block-paragraph">So if you fail to preserve the base pointer,</p>



<p class="wp-block-paragraph">the RBP and then you returned from your function,</p>



<p class="wp-block-paragraph">then you might have actually messed up the stack pointer for the caller or some</p>



<p class="wp-block-paragraph">other caller somewhere in like the ancestry of your call graph.</p>



<p class="wp-block-paragraph">So be very careful. I mean,</p>



<p class="wp-block-paragraph">I guess all callee preserved variables or registers you should be very careful,</p>



<p class="wp-block-paragraph">careful about, but these are like kind of the two worst ones to forget about.</p>



<p class="wp-block-paragraph">first ones to forget about.</p>



<p class="wp-block-paragraph">There&#8217;s also another register that is not listed on this page called the</p>



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



<p class="wp-block-paragraph">It basically is a register that holds</p>



<p class="wp-block-paragraph">the memory location of the next instruction to be executed.</p>



<p class="wp-block-paragraph">So if you modify that, then you just told your program to go execute</p>



<p class="wp-block-paragraph">in some random crazy place and probably everything is going to crash.</p>



<p class="wp-block-paragraph">R10 and R11.</p>



<p class="wp-block-paragraph">I can&#8217;t remember already if I explain this, but I&#8217;ll just say it again.</p>



<p class="wp-block-paragraph">But I&#8217;ll just say it again, R10 and R11 are just temporary registers.</p>



<p class="wp-block-paragraph">You don&#8217;t have to preserve them.</p>



<p class="wp-block-paragraph">But the callee, if you call another function, they don&#8217;t have to preserve the registers either,</p>



<p class="wp-block-paragraph">which means it can be really fast to use R10 and R12 if you&#8217;re kind of like already running out of registers.</p>



<p class="wp-block-paragraph">If you don&#8217;t have to make a function call anywhere, so you know no one is going to ruin your R10 and R11,</p>



<p class="wp-block-paragraph">then you can also use R10 and R11 to sort of like, you know,</p>



<p class="wp-block-paragraph">reduce your hits to system RAM to speed up your program.</p>



<p class="wp-block-paragraph">So R10 and R11, they should probably be used last.</p>



<p class="wp-block-paragraph">R12, 13, 14, 15, I usually use those first.</p>



<p class="wp-block-paragraph">Once I start running out of those, Kali saved.</p>



<p class="wp-block-paragraph">Then I&#8217;ll start eating into the arguments.</p>



<p class="wp-block-paragraph">I&#8217;ll say, you know, I&#8217;m going to start using RBX</p>



<p class="wp-block-paragraph">and then I&#8217;m going to use RDI and RSI and whatever.</p>



<p class="wp-block-paragraph">It&#8217;s okay if you use, you know, the argument registers.</p>



<p class="wp-block-paragraph">as long as you&#8217;ve already done something with your incoming arguments or you don&#8217;t have any.</p>



<p class="wp-block-paragraph">So just keep that in mind.</p>



<p class="wp-block-paragraph">You&#8217;re allowed to use them.</p>



<p class="wp-block-paragraph">I mean, this is all just a standard.</p>



<p class="wp-block-paragraph">As long as you obey what the rule is, then you&#8217;ll be all right.</p>



<p class="wp-block-paragraph">The rule is you got to callee save those.</p>



<p class="wp-block-paragraph">The rule is that one&#8217;s just temporary.</p>



<p class="wp-block-paragraph">The rule is this one&#8217;s an argument and so forth.</p>



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



<p class="wp-block-paragraph">locals and how we use these registers. Let me try to just show you one more</p>



<p class="wp-block-paragraph">thing real fast. So I said before that the size of the REX register is 64 bits</p>



<p class="wp-block-paragraph">because it takes up the whole available register bits in the CPU but there are</p>



<p class="wp-block-paragraph">other versions of this register that we could use. What if you only wanted to use</p>



<p class="wp-block-paragraph">32 bits in your register? What if you wanted to pack like two different 32</p>



<p class="wp-block-paragraph">32-bit numbers in the same 64-bit register.</p>



<p class="wp-block-paragraph">You could do that.</p>



<p class="wp-block-paragraph">There are also older modules and instructions</p>



<p class="wp-block-paragraph">that will operate only on 32-bits.</p>



<p class="wp-block-paragraph">So how do you reference only 32-bits of a register?</p>



<p class="wp-block-paragraph">Well, if I search for, oh shoot, what was it?</p>



<p class="wp-block-paragraph">Is it EDI?</p>



<p class="wp-block-paragraph">No, no.</p>



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



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



<p class="wp-block-paragraph">This is like a good lesson in actually preparing before you record a video.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do architecture overview, CPU registers.</p>



<p class="wp-block-paragraph">Oh, it was at the beginning of this section.</p>



<p class="wp-block-paragraph">I always skip down to 2.4, 12 point something, and I should have gone to 2.3.1.1.</p>



<p class="wp-block-paragraph">So obviously I&#8217;m not going to remember all of this, but basically notice at the top,</p>



<p class="wp-block-paragraph">we have the REX register, all 64 bits of it.</p>



<p class="wp-block-paragraph">But if you wanted to access the lowest six, sorry, the lowest 32 bits, then in your code,</p>



<p class="wp-block-paragraph">instead of putting REX, you put EAX.</p>



<p class="wp-block-paragraph">So for example, you know, move REX, whoops, REX, let&#8217;s say the number five, but over</p>



<p class="wp-block-paragraph">here we&#8217;re going to move EAX, the number five.</p>



<p class="wp-block-paragraph">five uh here it&#8217;s going to set the whole 64 bits to just like a bunch of zeros and then there&#8217;s</p>



<p class="wp-block-paragraph">going to be a few bits that help represent five the lowest bits so it&#8217;s going to erase like the</p>



<p class="wp-block-paragraph">whole thing all 64 bits but if you use the eax instruction then only the lowest 32 bits will</p>



<p class="wp-block-paragraph">actually be modified which means whatever data you had in the higher 60 the higher 32 bits of the</p>



<p class="wp-block-paragraph">of the register itself they&#8217;re just going to stay there it&#8217;s going to be considered</p>



<p class="wp-block-paragraph">point so sometimes if you&#8217;re going to work with something that&#8217;s going to be using only 32 bits</p>



<p class="wp-block-paragraph">of your register you better make sure that the other bits are uh are cleared out if you intend</p>



<p class="wp-block-paragraph">to use rax right after that and and vice versa so keep in mind that we have other forms of the same</p>



<p class="wp-block-paragraph">register a designation but just to let the cpu know that we only want to touch 32 bits worth we</p>



<p class="wp-block-paragraph">And this, this kind of table covers all of the registers,</p>



<p class="wp-block-paragraph">REX, RBX, RCX, and so forth.</p>



<p class="wp-block-paragraph">I think it doesn&#8217;t cover the instruction pointer</p>



<p class="wp-block-paragraph">and something else in there, but you know, most of them.</p>



<p class="wp-block-paragraph">However, you know, for my purposes,</p>



<p class="wp-block-paragraph">I usually just use 64 bits</p>



<p class="wp-block-paragraph">because I&#8217;m not like super advanced</p>



<p class="wp-block-paragraph">unless I have to divide or do something.</p>



<p class="wp-block-paragraph">Okay, so that&#8217;s the last thing I wanted to mention</p>



<p class="wp-block-paragraph">about the general purpose registers.</p>



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



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



<p class="wp-block-paragraph">So the not sure if I said this before already, but basically the.</p>



<p class="wp-block-paragraph">So just to emphasize the, uh, the RBP register is the base pointer register.</p>



<p class="wp-block-paragraph">You&#8217;re probably going to mess up, uh, the caller because the caller often uses RBP as</p>



<p class="wp-block-paragraph">a bookmark for the stack pointer.</p>



<p class="wp-block-paragraph">mark for the stack pointer you definitely don&#8217;t want to mess with a stack pointer unless you</p>



<p class="wp-block-paragraph">actually know what you&#8217;re doing in order to create local variables um there&#8217;s another variable</p>



<p class="wp-block-paragraph">another register in here called rip which is the instruction pointer register so if you</p>



<p class="wp-block-paragraph">if you mess that one up then the program is going to start executing in some crazy place that doesn&#8217;t</p>



<p class="wp-block-paragraph">even make sense okay so now that we&#8217;ve talked about general purpose registers by the way i</p>



<p class="wp-block-paragraph">called special purpose registers, not general purpose.</p>



<p class="wp-block-paragraph">General purpose is like RAX, RBX, all these ones that you can just randomly use as long</p>



<p class="wp-block-paragraph">as you respect the ABI.</p>



<p class="wp-block-paragraph">But the base pointer and the stack pointer, those are kind of special.</p>



<p class="wp-block-paragraph">And the RIP, the instruction pointer, that&#8217;s a special register.</p>



<p class="wp-block-paragraph">You shouldn&#8217;t mess with those unless you really know what you&#8217;re doing or you&#8217;ll get yourself</p>



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



<p class="wp-block-paragraph">But now that we&#8217;ve talked about those, let&#8217;s talk about the floating point registers just</p>



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



<p class="wp-block-paragraph">about using floats until a lot later but basically</p>



<p class="wp-block-paragraph">i at least want to mention that we do have registers that will work with floating point</p>



<p class="wp-block-paragraph">operations let me see there we go okay so it&#8217;s 18.2 floating point registers um essentially</p>



<p class="wp-block-paragraph">we use xmm0 through xmm13 for floating point numbers there&#8217;s not going to be any</p>



<p class="wp-block-paragraph">not going to be any crazy letters like RDI, RSI that you have to memorize. You can just use zero</p>



<p class="wp-block-paragraph">through 15 and it&#8217;s totally fine. When you want to return a floating point number from a function,</p>



<p class="wp-block-paragraph">you will load up XMM0 as the return value. You won&#8217;t even touch RAX. The arguments are the same.</p>



<p class="wp-block-paragraph">They&#8217;re just kind of ordered. It&#8217;s like the first argument is going to be XMM0. The second argument</p>



<p class="wp-block-paragraph">is going to be XMM1 and so forth all the way up to XMM15. If you need more than 15 arguments,</p>



<p class="wp-block-paragraph">some sorcery with the stack or with some kind of system like the global variable or something</p>



<p class="wp-block-paragraph">and also for floating point registers we use different instructions so what I really want</p>



<p class="wp-block-paragraph">you to know because this is mostly about the regular registers is that if you have floating</p>



<p class="wp-block-paragraph">point numbers and you want to multiply them or do some other operation on them the normal</p>



<p class="wp-block-paragraph">instructions for regular registers won&#8217;t actually work you&#8217;ll have to look up a different set of</p>



<p class="wp-block-paragraph">and i&#8217;m going to do this in another video some other time uh if you want to move uh data with</p>



<p class="wp-block-paragraph">a floating point register you can&#8217;t just use the regular move instruction you got to use either</p>



<p class="wp-block-paragraph">move ss or move sd um and and both operands have to be floats or actually i think the one on the</p>



<p class="wp-block-paragraph">right can be memory but um move ss basically means a single piece of data single precision</p>



<p class="wp-block-paragraph">So you can move like multiple pieces of data at a time.</p>



<p class="wp-block-paragraph">I&#8217;m not going to really cover that.</p>



<p class="wp-block-paragraph">Um, but you can move single precision floating points, which means there are 32 bit floating</p>



<p class="wp-block-paragraph">points or you can move double precision floating points, which means they&#8217;re a full 64 bit</p>



<p class="wp-block-paragraph">floating point number.</p>



<p class="wp-block-paragraph">There&#8217;s also bigger registers, but I&#8217;m not going to talk about that.</p>



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



<p class="wp-block-paragraph">Well, yeah.</p>



<p class="wp-block-paragraph">Later processors 256.</p>



<p class="wp-block-paragraph">I think mine has that, but I&#8217;m not going to talk about that.</p>



<p class="wp-block-paragraph">I think mine are like YMM or something.</p>



<p class="wp-block-paragraph">So yeah, with that, those are the basics of registers and some stuff to keep in mind.</p>



<p class="wp-block-paragraph">In future videos, I&#8217;m going to talk more in depth about making functions and calling functions</p>



<p class="wp-block-paragraph">and all that stuff, respecting the ABI.</p>



<p class="wp-block-paragraph">But for now, here is your primer.</p>



<p class="wp-block-paragraph">I hope it was useful on CPU registers.</p>



<p class="wp-block-paragraph">Thank you for watching this video.</p>



<p class="wp-block-paragraph">I&#8217;ll see you in the future.</p>



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



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



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



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



<p class="wp-block-paragraph">longer videos, better videos, 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. You know sometimes I&#8217;m sleeping in the</p>



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



<p class="wp-block-paragraph">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</p>



<p class="wp-block-paragraph">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. 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 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</p>



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



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



<p class="wp-block-paragraph">If you have a suggestion for</p>



<p class="wp-block-paragraph">Clarifications or errata or just future videos that you want to see</p>



<p class="wp-block-paragraph">please leave a comment or if you just want to say, Hey, what&#8217;s up, what&#8217;s going on? You know,</p>



<p class="wp-block-paragraph">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</p>



<p class="wp-block-paragraph">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/mastering-x86-64-assembly-a-beginners-guide-to-cpu-registers/">Mastering x86-64 Assembly: A Beginner&#8217;s Guide to CPU Registers</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-assembly-a-beginners-guide-to-cpu-registers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Master Data Sizes in x86-64 CPUs: Bytes, Words, D Words, and Quad Words Explained</title>
		<link>https://www.NeuralLantern.com/master-data-sizes-in-x86-64-cpus-bytes-words-d-words-and-quad-words-explained/</link>
					<comments>https://www.NeuralLantern.com/master-data-sizes-in-x86-64-cpus-bytes-words-d-words-and-quad-words-explained/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 09 Jun 2025 00:07:07 +0000</pubDate>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[64-bit computing]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[bytes]]></category>
		<category><![CDATA[coding tutorial]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[CPU basics]]></category>
		<category><![CDATA[CPU registers]]></category>
		<category><![CDATA[D word]]></category>
		<category><![CDATA[data sizes]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[programming for beginners]]></category>
		<category><![CDATA[quad word]]></category>
		<category><![CDATA[tech education]]></category>
		<category><![CDATA[words]]></category>
		<category><![CDATA[x86-64]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=148</guid>

					<description><![CDATA[<p>Learn data sizes in x86-64 CPUs: bytes, words, D words, quad words. Clear guide for coders on assembly &#038; registers. Subscribe for more!</p>
<p>The post <a href="https://www.NeuralLantern.com/master-data-sizes-in-x86-64-cpus-bytes-words-d-words-and-quad-words-explained/">Master Data Sizes in x86-64 CPUs: Bytes, Words, D Words, and Quad Words Explained</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 Data Sizes in x86-64 CPUs: Bytes, Words, D Words, and Quad Words Explained" width="1380" height="776" src="https://www.youtube.com/embed/v3fMANsXIwU?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 a clear, fun breakdown of data sizes in x86-64 CPUs! This video covers bytes (8 bits), words (16 bits), D words (32 bits), and quad words (64 bits), plus how they fit into assembly and CPU registers. Perfect for programmers, students, or anyone curious about low-level computing. I’ll walk you through the basics with relatable examples, no jargon overload. Subscribe for more coding &amp; tech videos, and scan the QR code to visit my site for extra tutorials! Leave a comment with your thoughts!</p>



<p class="wp-block-paragraph">Introduction 00:00:00<br>Data Sizes Overview 00:00:01<br>Byte Definition 00:00:44<br>Bits to Bytes Conversion 00:01:27<br>Word Size Explanation 00:02:20<br>Double Word (D Word) 00:04:03<br>Quad Word Definition 00:04:44<br>CPU Registers and Memory 00:05:21<br>Conclusion and Outro 00:06:07<br>Call to Action 00:06:32</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">I&#8217;d like to talk to you about data sizes within the context of x86,</p>



<p class="wp-block-paragraph">64 CPUs,</p>



<p class="wp-block-paragraph">just like 64 bit CPUs for assembly and just whatever.</p>



<p class="wp-block-paragraph">So what&#8217;s going on inside of the machine and how do we describe the sizes of</p>



<p class="wp-block-paragraph">various clumps of data? So for starters, you know,</p>



<p class="wp-block-paragraph">of your computer, there&#8217;s just a bunch of ones and zeros represented in all the wires,</p>



<p class="wp-block-paragraph">in all the circuitry.</p>



<p class="wp-block-paragraph">The human beings with feelings decide, oh, you know, I&#8217;m going to take a group of this</p>



<p class="wp-block-paragraph">many bits, this many ones and zeros, and I&#8217;m going to say that it is something else.</p>



<p class="wp-block-paragraph">I&#8217;m going to call it an abstract unit.</p>



<p class="wp-block-paragraph">So the first thing that we do is we have something called a byte.</p>



<p class="wp-block-paragraph">That just means eight bits.</p>



<p class="wp-block-paragraph">So we can say one byte equals eight bits.</p>



<p class="wp-block-paragraph">equals eight bits. I don&#8217;t know why I hit the shift key there. That&#8217;s probably going to mess</p>



<p class="wp-block-paragraph">up my annotator. Nope. Okay. So what is eight bits? Just as a little reminder, you know, we&#8217;ll</p>



<p class="wp-block-paragraph">just say one, two, three, four, five, six, seven, eight, or it could be, you know, something more</p>



<p class="wp-block-paragraph">random looking one, two, three, four, five, six, seven, eight, you know, so like inside of the</p>



<p class="wp-block-paragraph">machine, we have eight ones and zeros for eight bits. And when we group them together, we&#8217;ll just</p>



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



<p class="wp-block-paragraph">So that means by the way, if somebody says,</p>



<p class="wp-block-paragraph">oh, my internet connection is this many megabits a second.</p>



<p class="wp-block-paragraph">If you wanna know how many bytes a second,</p>



<p class="wp-block-paragraph">try dividing by eight.</p>



<p class="wp-block-paragraph">Or if somebody says I have this many megabytes a second</p>



<p class="wp-block-paragraph">of transfer speed, try dividing that by eight.</p>



<p class="wp-block-paragraph">You know, so multiply or divide by eight</p>



<p class="wp-block-paragraph">to get the right number.</p>



<p class="wp-block-paragraph">Okay, so if we had two bytes together,</p>



<p class="wp-block-paragraph">that&#8217;s just gonna be 16 bits.</p>



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



<p class="wp-block-paragraph">I&#8217;m just going to copy paste these bits and you don&#8217;t have to do this.</p>



<p class="wp-block-paragraph">But for me personally, I like to put a little space in between bite groupings just to help</p>



<p class="wp-block-paragraph">my human brain kind of see, oh, I&#8217;m looking at two bites right here when I&#8217;m referring</p>



<p class="wp-block-paragraph">to different data sizes.</p>



<p class="wp-block-paragraph">So then eventually we kind of need an idea of, you know, let&#8217;s come up with a different</p>



<p class="wp-block-paragraph">word that means more than one bite.</p>



<p class="wp-block-paragraph">ironically based on what I just said the the term we&#8217;re going to use is word a word</p>



<p class="wp-block-paragraph">the size of a word just kind of depends on the system that you&#8217;re on on my system</p>



<p class="wp-block-paragraph">the size of a word is two bytes so I&#8217;m going to say one word equals two bytes</p>



<p class="wp-block-paragraph">and you can imagine that&#8217;s 16 bits maybe I should start doing the double equal sign here just to be</p>



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



<p class="wp-block-paragraph">It also equals 16 bits.</p>



<p class="wp-block-paragraph">And you can imagine it&#8217;s basically the same thing</p>



<p class="wp-block-paragraph">that we just wrote up above.</p>



<p class="wp-block-paragraph">So I&#8217;m gonna erase this actually, 16.</p>



<p class="wp-block-paragraph">Oh, I put an E there, embarrassing.</p>



<p class="wp-block-paragraph">Okay, so one word is two bytes.</p>



<p class="wp-block-paragraph">Again, the size of a word depends on your system.</p>



<p class="wp-block-paragraph">If you&#8217;re not sure, you should probably look it up.</p>



<p class="wp-block-paragraph">What&#8217;s the word size on my system?</p>



<p class="wp-block-paragraph">But again, the typical system here in 2024</p>



<p class="wp-block-paragraph">6664 machines running Ubuntu, um, or just like Linux is going to be like, you know,</p>



<p class="wp-block-paragraph">two bytes. Okay. So now, uh, we have like words that we can do. So if I had, uh,</p>



<p class="wp-block-paragraph">let&#8217;s say I had two words, uh, two words is going to be double of what we just wrote. Right. So</p>



<p class="wp-block-paragraph">that means we&#8217;re going to have this many, whoops, I blew it. We&#8217;re going to have this many bits.</p>



<p class="wp-block-paragraph">So two words, two words is going to be equal to, um, 32 bits.</p>



<p class="wp-block-paragraph">And it&#8217;s also going to be equal to four bytes because, you know, we multiplied everything</p>



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



<p class="wp-block-paragraph">So this is like four bytes.</p>



<p class="wp-block-paragraph">So now we need another term to represent this two words, which is four bytes, which</p>



<p class="wp-block-paragraph">is 32 bits.</p>



<p class="wp-block-paragraph">We can also call that a D word.</p>



<p class="wp-block-paragraph">Actually, I&#8217;ll say D word like that.</p>



<p class="wp-block-paragraph">And what does D word mean?</p>



<p class="wp-block-paragraph">is twice the size of the word length on your system.</p>



<p class="wp-block-paragraph">So like if we decided our word length is two bytes,</p>



<p class="wp-block-paragraph">then a D word is four bytes and that&#8217;s it.</p>



<p class="wp-block-paragraph">Four bytes is 32 bits.</p>



<p class="wp-block-paragraph">You might notice this is similar to like old school CPUs</p>



<p class="wp-block-paragraph">that ran in 32 bits and maxed out their RAM</p>



<p class="wp-block-paragraph">at about four gigabytes.</p>



<p class="wp-block-paragraph">So let&#8217;s come up with one more term here.</p>



<p class="wp-block-paragraph">Let&#8217;s come up with the term quad word.</p>



<p class="wp-block-paragraph">Actually, we&#8217;re not coming up with the term.</p>



<p class="wp-block-paragraph">We&#8217;re just using it.</p>



<p class="wp-block-paragraph">What is a quad word?</p>



<p class="wp-block-paragraph">A quad word is two D words.</p>



<p class="wp-block-paragraph">So we&#8217;re just kind of doubling it again.</p>



<p class="wp-block-paragraph">I forgot that extra equal sign there.</p>



<p class="wp-block-paragraph">Quad word is two D words, which means it&#8217;s also four words.</p>



<p class="wp-block-paragraph">And it also means it&#8217;s eight bytes.</p>



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



<p class="wp-block-paragraph">And then I guess if I want to represent this to you,</p>



<p class="wp-block-paragraph">I can just kind of like copy paste that twice.</p>



<p class="wp-block-paragraph">And this is the size of the memory address space that we have on modern CPUs.</p>



<p class="wp-block-paragraph">This is also the size of your general purpose registers in modern 64-bit CPUs.</p>



<p class="wp-block-paragraph">So if somebody says, hey, I got a quad word.</p>



<p class="wp-block-paragraph">I want you to store it somewhere.</p>



<p class="wp-block-paragraph">You can instantly think to yourself, well, obviously I could put it in memory.</p>



<p class="wp-block-paragraph">I could put it in memory.</p>



<p class="wp-block-paragraph">You could also just put it directly into a CPU register.</p>



<p class="wp-block-paragraph">And that&#8217;s it.</p>



<p class="wp-block-paragraph">This also means that you could put two D words into one CPU register if you were very clever.</p>



<p class="wp-block-paragraph">You could also do the same thing.</p>



<p class="wp-block-paragraph">You could put like four words inside of one CPU register.</p>



<p class="wp-block-paragraph">Because the CPU registers, they&#8217;re always the same size no matter how you use them.</p>



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



<p class="wp-block-paragraph">So I hope this has been, you know, kind of helpful.</p>



<p class="wp-block-paragraph">These are the basics of different data type sizes that we have in CPUs and</p>



<p class="wp-block-paragraph">assembly language. 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. 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">Follow this channel or these videos or whatever it is you do on the current social media website 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 and grow this community</p>



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



<p class="wp-block-paragraph">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 because I know somebody subscribed or followed</p>



<p class="wp-block-paragraph">and I get filled with joy. That&#8217;s exactly what happens every single time. So you could do it as</p>



<p class="wp-block-paragraph">a nice favor to me, or you could, you could troll me if you want to just wake me up in the middle</p>



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



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



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



<p class="wp-block-paragraph">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">and tutorials and things that I offer and all that good stuff and if you have a suggestion for</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 I also</p>



<p class="wp-block-paragraph">wake up for those in the middle of the night I get I wake up in a cold sweat and 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</p>



<p class="wp-block-paragraph">thank you so much for watching this video and enjoy the cool music as as i fade into the</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/master-data-sizes-in-x86-64-cpus-bytes-words-d-words-and-quad-words-explained/">Master Data Sizes in x86-64 CPUs: Bytes, Words, D Words, and Quad Words Explained</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/master-data-sizes-in-x86-64-cpus-bytes-words-d-words-and-quad-words-explained/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
