<?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>x64 assembly Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/x64-assembly/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/x64-assembly/</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.0</generator>

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>x64 assembly Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/x64-assembly/</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>
	</channel>
</rss>
