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

					<description><![CDATA[<p>Learn how to write callable functions in YASM x86-64 assembly on Ubuntu. Covers passing integers, pointers, and floating-point values, using the correct registers (RDI, RSI, XMM0, etc.), returning values in RAX/XMM0, extern \"C\" for C++ interop, and building a hybrid C++/Assembly program.</p>
<p>The post <a href="https://www.NeuralLantern.com/yasm-x86-64-assembly-functions-tutorial-integers-floats-pointers-abi-c-interoperability/">YASM x86-64 Assembly Functions Tutorial &#8211; Integers, Floats, Pointers, ABI, C++ Interoperability</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="YASM x86-64 Assembly Functions Tutorial - Integers, Floats, Pointers, ABI, C++ Interoperability" width="1380" height="776" src="https://www.youtube.com/embed/fXf3Gpf-fyM?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">Quick but complete guide to writing proper functions in YASM x86-64 assembly on Linux. See how to pass integers, pointers &amp; floats, return values, follow the ABI, disable C++ name mangling with extern &#8220;C&#8221;, and call back and forth between C++ and assembly in a real working example.</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">In this video I&#8217;m going to teach you how to write functions in YASM x86-64 assembly inside</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">so much for watching this video and um enjoy the cool music as as i fade into the darkness</p>



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



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

					<description><![CDATA[<p>Learn how local variables are allocated on the stack in x86-64 assembly using YASM. See exactly how to reserve space, address variables with offsets, understand downward stack growth, and fix mysterious printf segfaults caused by 16-byte stack misalignment. Includes two alignment solutions and a complete working example with a local integer array.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-local-variables-stack-frames-alignment-explained/">x86-64 Assembly: Local Variables, Stack Frames &amp; Alignment Explained</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="x86-64 Assembly: Local Variables, Stack Frames &amp; Alignment Explained" width="1380" height="776" src="https://www.youtube.com/embed/woCCcOlopNo?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">Quick but deep look at local variables on the stack in x86-64 assembly (YASM). We cover allocation with sub rsp, accessing via offsets, why the stack must be 16-byte aligned when calling libc functions like printf, and two practical ways to fix alignment crashes. Includes live segfault debugging and a full working example with a local array.</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">If you tried to use global variables for this, it would be really, really, really hard to get the code to work.</p>



<p class="wp-block-paragraph">get the code to work and it would be really, really hard to debug.</p>



<p class="wp-block-paragraph">So that&#8217;s kind of why we have local variables.</p>



<p class="wp-block-paragraph">The stack allows us to do this.</p>



<p class="wp-block-paragraph">So what is the stack itself?</p>



<p class="wp-block-paragraph">Again, this is not a full tutorial on the stack,</p>



<p class="wp-block-paragraph">but I just want you to kind of see what&#8217;s happening.</p>



<p class="wp-block-paragraph">Imagine a data structure that kind of grows upward.</p>



<p class="wp-block-paragraph">I&#8217;ll say that here&#8217;s the floor at the bottom.</p>



<p class="wp-block-paragraph">And when you put an item onto the stack,</p>



<p class="wp-block-paragraph">the items kind of stack on top of each other.</p>



<p class="wp-block-paragraph">So imagine that this is the call to F.</p>



<p class="wp-block-paragraph">is the call to f and embedded within this little stack frame area we&#8217;ll call it a stack frame but</p>



<p class="wp-block-paragraph">actually the stack continues to grow as you create and destroy local variables within a function call</p>



<p class="wp-block-paragraph">we&#8217;ll just imagine that there&#8217;s i don&#8217;t know a mini stack sitting inside of the stack that</p>



<p class="wp-block-paragraph">contains all the local variables i&#8217;ll just put i&#8217;m not going to put a because a is an argument so</p>



<p class="wp-block-paragraph">it&#8217;s sitting in a register but we could do dude i got to learn how to how to draw with this tablet</p>



<p class="wp-block-paragraph">We got to do B and C and the array.</p>



<p class="wp-block-paragraph">I&#8217;ll put AR for array.</p>



<p class="wp-block-paragraph">Sitting in, you know, their own little spots on the stack within the major portion of the stack that is designated for that first F call.</p>



<p class="wp-block-paragraph">And then, you know, maybe F calls G and some other stuff happens.</p>



<p class="wp-block-paragraph">I&#8217;ll just pretend that a bunch of other stuff happened.</p>



<p class="wp-block-paragraph">And then eventually F is called again.</p>



<p class="wp-block-paragraph">But there&#8217;s a different, this is a different instance of the call to F.</p>



<p class="wp-block-paragraph">it&#8217;s got its own little area that is separate from the the previous call to f and again we&#8217;ll</p>



<p class="wp-block-paragraph">also have variables that we can create locally that are supposed to be separate from the original</p>



<p class="wp-block-paragraph">variables oh god that&#8217;s awful and ugly i need to maybe decrease the size of my eraser or something</p>



<p class="wp-block-paragraph">but imagine this is uh you know these are two separate copies so that&#8217;s like what i said before</p>



<p class="wp-block-paragraph">when we have like a b1 and a b2 basically they&#8217;re not going to be called b1 and b2 but they&#8217;re just</p>



<p class="wp-block-paragraph">They&#8217;re not going to be called B1 and B2, but they&#8217;re just two separate instances.</p>



<p class="wp-block-paragraph">And I just want you to know.</p>



<p class="wp-block-paragraph">So then when we start returning from functions, like when this F eventually returns, and by</p>



<p class="wp-block-paragraph">the way, I know that what I&#8217;m drawing on the right doesn&#8217;t match the code because this</p>



<p class="wp-block-paragraph">is not a function that calls itself, but just suppose that your code is a lot more complicated</p>



<p class="wp-block-paragraph">than what I drew up.</p>



<p class="wp-block-paragraph">When it eventually returns, all that happens is those items on the stack just sort of get</p>



<p class="wp-block-paragraph">not necessarily deallocated, but ignored.</p>



<p class="wp-block-paragraph">We&#8217;ll just say that they&#8217;re gone.</p>



<p class="wp-block-paragraph">They&#8217;re still sitting as junk data in system RAM somewhere.</p>



<p class="wp-block-paragraph">And in assembly talk, we know that we have a stack pointer called RSP.</p>



<p class="wp-block-paragraph">We have a register called RSP that just sort of points to the location in the</p>



<p class="wp-block-paragraph">stack that is considered the top of the stack, like the most recent piece of data</p>



<p class="wp-block-paragraph">that we have available.</p>



<p class="wp-block-paragraph">So all the other data is actually still kind of above, but we&#8217;re not pointing to it</p>



<p class="wp-block-paragraph">anymore, so we consider that it doesn&#8217;t exist.</p>



<p class="wp-block-paragraph">So then when G eventually returns, you know, we just change the stack pointer,</p>



<p class="wp-block-paragraph">Rsp, to point to that other piece of data.</p>



<p class="wp-block-paragraph">The G data frame and the other F data frame are still sitting above somewhere in memory,</p>



<p class="wp-block-paragraph">but we just ignore them, right?</p>



<p class="wp-block-paragraph">So that&#8217;s how the stack works.</p>



<p class="wp-block-paragraph">And that&#8217;s how we have local function call copies of all of our local variables.</p>



<p class="wp-block-paragraph">Something to note.</p>



<p class="wp-block-paragraph">Something to note, this is not a stack video, but you know, just something to note that</p>



<p class="wp-block-paragraph">even though I draw the stack visually as growing vertically up, when you actually manipulate</p>



<p class="wp-block-paragraph">the stack in assembly or just like in any language, the stack grows downward in terms</p>



<p class="wp-block-paragraph">of memory locations.</p>



<p class="wp-block-paragraph">So you can imagine, I&#8217;m trying so hard not to make this like a huge stack video.</p>



<p class="wp-block-paragraph">Imagine this is a memory location 80, we&#8217;ll say.</p>



<p class="wp-block-paragraph">You would think that memory location 81 would be the next item of the stack, or I guess</p>



<p class="wp-block-paragraph">the stack or i guess if you if you&#8217;re considering the fact that the items on the stack are quad words</p>



<p class="wp-block-paragraph">we would say it goes up to 88 but that&#8217;s not true it goes down to 72 so the memory location goes down</p>



<p class="wp-block-paragraph">even though we imagine the stack growing uh upward vertically just so you know that&#8217;s the kind of</p>



<p class="wp-block-paragraph">thing we&#8217;re going to do so what i&#8217;m going to do is just show you an assembly program where we can</p>



<p class="wp-block-paragraph">create local variables and i&#8217;m just going to show you how to create an array because this array is</p>



<p class="wp-block-paragraph">just like a bunch of integers and you can imagine it would be really easy to create only one integer</p>



<p class="wp-block-paragraph">by just imagining an example where the array is a size of one so keep that in mind and i&#8217;m not going</p>



<p class="wp-block-paragraph">to show you malloc or anything like that we&#8217;re just going to look at the local variables okay</p>



<p class="wp-block-paragraph">so for starters i have a make file here that&#8217;s just going to compile a hybrid program if you don&#8217;t</p>



<p class="wp-block-paragraph">know make files or you don&#8217;t know hybrid programs that&#8217;s okay just see my other videos i&#8217;ve explained</p>



<p class="wp-block-paragraph">The first source code file here is just driver.cpp.</p>



<p class="wp-block-paragraph">Again, this is a hybrid program,</p>



<p class="wp-block-paragraph">so I&#8217;m going to mix my C++ modules with my assembly modules,</p>



<p class="wp-block-paragraph">which is pretty cool.</p>



<p class="wp-block-paragraph">The whole point of the driver is just to contain the entry point, you know, main.</p>



<p class="wp-block-paragraph">And I&#8217;m just going to print a hello message.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to call the real function that I&#8217;m interested in,</p>



<p class="wp-block-paragraph">which I&#8217;ve named local underscore varrs.</p>



<p class="wp-block-paragraph">And that&#8217;s going to be all the assembly stuff that we talked about.</p>



<p class="wp-block-paragraph">block so that C++ can call an assembly module that&#8217;s explained in other videos. And then</p>



<p class="wp-block-paragraph">here&#8217;s the real heart of what we got to do. Let&#8217;s write up an assembly module</p>



<p class="wp-block-paragraph">that can do local variables. Okay. So again, if you don&#8217;t know assembly, that&#8217;s okay,</p>



<p class="wp-block-paragraph">but you need to watch my other videos before you can understand this one. So I&#8217;m going to just</p>



<p class="wp-block-paragraph">copy paste some starter code here. This is Yasm assembly in x86-64. So I&#8217;ve got a data section up</p>



<p class="wp-block-paragraph">So I&#8217;ve got a data section up top and I&#8217;m just going to define some messages.</p>



<p class="wp-block-paragraph">So, you know, I&#8217;ve got like an intro message that just says, hello, I&#8217;m so and so.</p>



<p class="wp-block-paragraph">And that&#8217;s not my name, but I like those kinds of names.</p>



<p class="wp-block-paragraph">And then over here, I&#8217;m going to do some printf formatted strings.</p>



<p class="wp-block-paragraph">That&#8217;s why I&#8217;m using a hybrid program for this video.</p>



<p class="wp-block-paragraph">I don&#8217;t want to import my own personal library.</p>



<p class="wp-block-paragraph">I want you to be able to do this at home with just the GCC libraries.</p>



<p class="wp-block-paragraph">link a hybrid library you know linking against gcc instead of linking against ld again if you don&#8217;t</p>



<p class="wp-block-paragraph">know that stuff check my other videos then we&#8217;re allowed to call c functions in this case we&#8217;re</p>



<p class="wp-block-paragraph">going to call printf and we&#8217;re just going to give it the string percent lu meaning i would like you</p>



<p class="wp-block-paragraph">to print just you know an unsigned long integer so i&#8217;m going to give it a value at some point on the</p>



<p class="wp-block-paragraph">stack representing a local variable and then i want it to print as just like a long like a string</p>



<p class="wp-block-paragraph">along like a string that a human can read then after that this is the carriage return line feed</p>



<p class="wp-block-paragraph">the crlf printf won&#8217;t flush its output unless that is sitting at the very end of the string so</p>



<p class="wp-block-paragraph">i&#8217;m just going to use printf to also print my new lines and then i&#8217;m going to null terminate the</p>



<p class="wp-block-paragraph">string so that printf doesn&#8217;t freak out and try to print a bunch of stuff after the crlf and uh</p>



<p class="wp-block-paragraph">oh i this was from another video let me get rid of that we don&#8217;t really need crlf in this video</p>



<p class="wp-block-paragraph">CRLF in this video because we&#8217;re just putting it directly inside of the printf string we&#8217;re not</p>



<p class="wp-block-paragraph">making our own function to do that so then I&#8217;m going to make some defines I&#8217;m going to define</p>



<p class="wp-block-paragraph">that we&#8217;re going to have 50 integers so I&#8217;m calling this define 50 I&#8217;m calling it num integers</p>



<p class="wp-block-paragraph">and I&#8217;m saying that it has a value of 50 so I want to make an array that has 50 integers I don&#8217;t</p>



<p class="wp-block-paragraph">know maybe if you want to imagine 100 you know like the example that I just showed I&#8217;m going to</p>



<p class="wp-block-paragraph">going to define what is the integer size so i&#8217;m going to use quad words which are 64 bit integers</p>



<p class="wp-block-paragraph">so i&#8217;m just going to say that there are eight bytes per integer that will help me multiply later</p>



<p class="wp-block-paragraph">and then i&#8217;m going to decide to fill up the array on the stack with just some numbers just to prove</p>



<p class="wp-block-paragraph">that i can just to prove that we can like uh you know manipulate and and fetch the values</p>



<p class="wp-block-paragraph">of all this stuff going on in the stack and i&#8217;m going to say that the starting value is seven so</p>



<p class="wp-block-paragraph">with this we should expect to see like an array of numbers that starts with seven and it just</p>



<p class="wp-block-paragraph">kind of increases somehow then i&#8217;m going to do some system call codes we talked about that in</p>



<p class="wp-block-paragraph">a different video and then some file descriptors i don&#8217;t think we actually need anything but standard</p>



<p class="wp-block-paragraph">output but i put it in there anyway then the next thing we&#8217;re going to add is the text section</p>



<p class="wp-block-paragraph">let me just do copy paste on my solution here so here&#8217;s the text section section text</p>



<p class="wp-block-paragraph">section text in Yasm and I&#8217;m going to let my module know that I want to be able to call printf</p>



<p class="wp-block-paragraph">which is a function sitting in the GCC libraries when I link against a GCC I have the ability to</p>



<p class="wp-block-paragraph">do that that way I don&#8217;t have to come up with like a complicated printing method or use one</p>



<p class="wp-block-paragraph">of my own shared libraries or something so we can just ask printf to do everything so now here&#8217;s the</p>



<p class="wp-block-paragraph">entry point for the module it&#8217;s just a function called local VAERS I mark it as global so it&#8217;s</p>



<p class="wp-block-paragraph">it&#8217;s accessible to outside modules ie or eg driver dot cpp and then so here&#8217;s the label saying the</p>



<p class="wp-block-paragraph">function starts and here&#8217;s the return statement saying that we&#8217;re done with the function i&#8217;m not</p>



<p class="wp-block-paragraph">going to manipulate any registers inside of the function so i don&#8217;t really need to do any push pop</p>



<p class="wp-block-paragraph">to preserve them first thing i&#8217;m going to do is call a welcome let me comment this part out by</p>



<p class="wp-block-paragraph">the way i&#8217;m going to call a welcome a function and the whole job of the welcome function is just to</p>



<p class="wp-block-paragraph">is just to, you know, print a welcome message to the user.</p>



<p class="wp-block-paragraph">So nothing that I haven&#8217;t talked about before in other videos.</p>



<p class="wp-block-paragraph">So it&#8217;s just we&#8217;re using a system call to print a string.</p>



<p class="wp-block-paragraph">Okay, so with that in mind, let me open this up here and see if this is going to work.</p>



<p class="wp-block-paragraph">I just want to basically print the welcome message at this point.</p>



<p class="wp-block-paragraph">Clear and make run.</p>



<p class="wp-block-paragraph">And again, if you don&#8217;t know make files or anything like that, see my other videos.</p>



<p class="wp-block-paragraph">So this is the driver, I think, that prints.</p>



<p class="wp-block-paragraph">think that Prince maybe I should change the driver&#8217;s message to make it more</p>



<p class="wp-block-paragraph">clear hello about this is the driver and my name is whatever I&#8217;m gonna do it again</p>



<p class="wp-block-paragraph">and now it says hello this is the driver okay so that&#8217;s the CPP module and then</p>



<p class="wp-block-paragraph">here is the assembly module and then finally the driver gets control back and</p>



<p class="wp-block-paragraph">then the program exits so nothing really happened so now let&#8217;s upgrade the</p>



<p class="wp-block-paragraph">So now let&#8217;s upgrade the assembly module a little bit.</p>



<p class="wp-block-paragraph">Next thing I want to add is the actual demo function, which is going to be absolutely huge.</p>



<p class="wp-block-paragraph">So first I&#8217;m going to start off with, how about just the signature here?</p>



<p class="wp-block-paragraph">Let&#8217;s go right after the welcome module.</p>



<p class="wp-block-paragraph">And I&#8217;m just going to copy paste the signature, put a return at the end of it.</p>



<p class="wp-block-paragraph">So we&#8217;ll consider this a function that can be called.</p>



<p class="wp-block-paragraph">instruction on line 47 of the entry point and then now we have a demo</p>



<p class="wp-block-paragraph">function that is being called but it does nothing for starters you know I in</p>



<p class="wp-block-paragraph">my comments I like to put the signature of the function and I like to remind</p>



<p class="wp-block-paragraph">myself of how I&#8217;m using my registers hopefully I&#8217;m not using the same</p>



<p class="wp-block-paragraph">register in two different ways but you know sometimes it happens if it does and</p>



<p class="wp-block-paragraph">I&#8217;m able to break my function up into multiple parts I&#8217;ll probably do it</p>



<p class="wp-block-paragraph">you know with modular thinking modular programming but in this case I&#8217;m just</p>



<p class="wp-block-paragraph">using these registers it&#8217;s fine to use them they are all designated per the ABI</p>



<p class="wp-block-paragraph">as callee saved so that means the callee which is the demo function is</p>



<p class="wp-block-paragraph">responsible for preserving them so if you don&#8217;t remember that or if you don&#8217;t</p>



<p class="wp-block-paragraph">know about that see my other videos this is not an ABI video so I&#8217;m just going to</p>



<p class="wp-block-paragraph">push all of them to make sure that they&#8217;re preserved I&#8217;m gonna call this</p>



<p class="wp-block-paragraph">call this the prolog then at the end of my function i&#8217;m going to call this the epilog where</p>



<p class="wp-block-paragraph">i just restore them in reverse order because that&#8217;s the way the stack returns data to you</p>



<p class="wp-block-paragraph">the stack returns data to you in reverse order so i have to pop in the reverse register order to</p>



<p class="wp-block-paragraph">un-reverse the reversal if that makes sense pop r13 pop r12 okay so i think 15 14 13 okay</p>



<p class="wp-block-paragraph">so i got that next thing we&#8217;ll do is um let&#8217;s remember where the stack pointer started because</p>



<p class="wp-block-paragraph">started because we have our register here that we&#8217;re going to mess with let me just type rsp</p>



<p class="wp-block-paragraph">real fast so this is the stack pointer register rsp this helps all programs know where where</p>



<p class="wp-block-paragraph">they&#8217;re looking at in the stack all of your functions have to be really really careful about</p>



<p class="wp-block-paragraph">messing with the stack pointer if you do it wrong you will crash the entire program because not only</p>



<p class="wp-block-paragraph">will your local function not really know where its local variables end and begin it probably also</p>



<p class="wp-block-paragraph">It&#8217;s return address when you try to return from the function because that is also sitting on the stack</p>



<p class="wp-block-paragraph">And even if you were lucky enough to be able to jump back correctly to whoever called you</p>



<p class="wp-block-paragraph">If you messed up the stack pointer then you&#8217;ve also messed it up for any caller of you and any of their callers</p>



<p class="wp-block-paragraph">So the whole program is ruined. So we&#8217;ll start off by trying to remember where the stack pointer was</p>



<p class="wp-block-paragraph">We&#8217;ll move</p>



<p class="wp-block-paragraph">The stack pointer into the base pointer</p>



<p class="wp-block-paragraph">didn&#8217;t do here that I that I want to do we should since we&#8217;re messing with the</p>



<p class="wp-block-paragraph">base pointer and other programs sorry other functions or modules might also</p>



<p class="wp-block-paragraph">rely on the base pointer and it&#8217;s considered callie saved we probably also</p>



<p class="wp-block-paragraph">want to preserve that too so I&#8217;m going to do push RBP to basically say I would</p>



<p class="wp-block-paragraph">like to restore I would like to preserve the base pointer so I don&#8217;t mess it up</p>



<p class="wp-block-paragraph">for my callers so that means I have to restore it with the pop so RBP the base</p>



<p class="wp-block-paragraph">The base pointer isn&#8217;t necessarily a pointer to the stack, but it&#8217;s often used as kind of like a bookmark.</p>



<p class="wp-block-paragraph">So we have RBP at the front and the back there.</p>



<p class="wp-block-paragraph">Let&#8217;s see.</p>



<p class="wp-block-paragraph">Next thing I want to add is, so now that we&#8217;ve restored it, we&#8217;re allowed to just overwrite it because, you know, we&#8217;re kind of like keeping its value at the top.</p>



<p class="wp-block-paragraph">Then we&#8217;re restoring its value at the bottom.</p>



<p class="wp-block-paragraph">And that means we can actually mess it up in the middle if we want to, and it&#8217;ll be fine.</p>



<p class="wp-block-paragraph">let me show you real fast what happens what happens is nothing the program is</p>



<p class="wp-block-paragraph">still okay because we we restored it so now we&#8217;re using the base pointer to</p>



<p class="wp-block-paragraph">remember where the stack was now we&#8217;ve got to sort of calculate how much room</p>



<p class="wp-block-paragraph">we want to make on the stack let me show you what I&#8217;m talking about here</p>



<p class="wp-block-paragraph">remember all of our local variables are going to be on the stack and before we</p>



<p class="wp-block-paragraph">drew this thing where it was like well we&#8217;ve got like a stack sitting here and</p>



<p class="wp-block-paragraph">let&#8217;s just pretend that there&#8217;s some kind of data sitting on the stack data</p>



<p class="wp-block-paragraph">data data right if the stack pointer dude green green there we go oh I erased my</p>



<p class="wp-block-paragraph">green RSP if the stack pointer is currently pointing to this frame then</p>



<p class="wp-block-paragraph">in order for us to make room on the stack to hold our array well if the</p>



<p class="wp-block-paragraph">whole array is going to be sitting on the stack that just basically means</p>



<p class="wp-block-paragraph">have five integers suppose five suppose that we want to make five integers on the stack that just</p>



<p class="wp-block-paragraph">means we need to do five extra slots let me draw it in red here well let&#8217;s see can we get a green</p>



<p class="wp-block-paragraph">no how about a yellow my green is just having a hard time we can do it in red even though this is</p>



<p class="wp-block-paragraph">not a bad thing to do so i&#8217;ll just draw like five extra frames on top of on top of the stack here</p>



<p class="wp-block-paragraph">imagine these are all 64-bit integers and so they take eight bytes on the stack even though in our</p>



<p class="wp-block-paragraph">previous example we were using just regular ints which are 32 we&#8217;ll just say we&#8217;re going to make</p>



<p class="wp-block-paragraph">five 64-bit integers because that&#8217;s easier so they&#8217;re quad words so every frame is actually</p>



<p class="wp-block-paragraph">eight bytes and not just four bytes and it&#8217;s definitely not one byte so we make five of those</p>



<p class="wp-block-paragraph">how do we make five slots it&#8217;s pretty easy we we literally just move the stack pointer let&#8217;s see</p>



<p class="wp-block-paragraph">we just move the stack pointer to or the you know the rsp register to just say let&#8217;s let&#8217;s point you</p>



<p class="wp-block-paragraph">know further out and how do we get that number we&#8217;re just going to multiply the size of one</p>



<p class="wp-block-paragraph">integer so you know the size of one integer here we know it&#8217;s going to be eight bytes we just</p>



<p class="wp-block-paragraph">multiply that by the number of integers that we want you know that&#8217;s going to be 40 so we&#8217;ll just</p>



<p class="wp-block-paragraph">increase uh sorry decrease the stack pointers value by 40 because remember again when the</p>



<p class="wp-block-paragraph">vertically, it&#8217;s actually growing downward in memory. So we&#8217;re going to decrease by 40 there,</p>



<p class="wp-block-paragraph">at least in this drawing example. And that gives us a bunch of junk data, you know, because there</p>



<p class="wp-block-paragraph">is always going to be some kind of a value sitting at every memory location in your computer.</p>



<p class="wp-block-paragraph">It&#8217;s impossible that there is literally nothing at some memory location,</p>



<p class="wp-block-paragraph">unless you&#8217;re trying to go beyond your RAM stick. But then the system will still acknowledge that,</p>



<p class="wp-block-paragraph">you know, you&#8217;ve done that and it won&#8217;t just give you back nothing if you try to read</p>



<p class="wp-block-paragraph">nothing if you try to read.</p>



<p class="wp-block-paragraph">So there&#8217;s going to be junk data sitting on there and then we&#8217;ll loop</p>



<p class="wp-block-paragraph">through all those slots on the stack and we&#8217;ll just modify the data one by one</p>



<p class="wp-block-paragraph">so that we can control what it is instead of just printing whatever junk data we end</p>



<p class="wp-block-paragraph">up with.</p>



<p class="wp-block-paragraph">So really, we&#8217;re just moving the stack pointer, just making room and then just</p>



<p class="wp-block-paragraph">remembering where our array is.</p>



<p class="wp-block-paragraph">We could put, you know, another frame on top if we wanted to make just like one</p>



<p class="wp-block-paragraph">integer as a local variable.</p>



<p class="wp-block-paragraph">You just got to remember where it is.</p>



<p class="wp-block-paragraph">You know, what is it?</p>



<p class="wp-block-paragraph">What is its offset?</p>



<p class="wp-block-paragraph">is it what is its offset okay so i&#8217;m going to erase this because we&#8217;re going to do a lot more than</p>



<p class="wp-block-paragraph">than five uh integers on the stack but i just want you to understand what we&#8217;re doing before we do it</p>



<p class="wp-block-paragraph">okay so the next thing i&#8217;m going to grab is a move instruction and i&#8217;m going to put it right here so</p>



<p class="wp-block-paragraph">to move the stack pointer rsp so the first thing i&#8217;m going to do is i&#8217;m going to use a temporary</p>



<p class="wp-block-paragraph">register we don&#8217;t need to preserve this in the push and pop because it&#8217;s marked as temp so we&#8217;re</p>



<p class="wp-block-paragraph">not responsible for preserving it and so i&#8217;m going to say r10 is just going to be the number of</p>



<p class="wp-block-paragraph">integers that we want to create if you recall at the top of our program here num integers is just</p>



<p class="wp-block-paragraph">50 okay so then the next thing that i&#8217;m going to grab is well i&#8217;m not going to grab it i&#8217;m going</p>



<p class="wp-block-paragraph">it I&#8217;m going to straight multiply by integer size so again if you look at integer size that&#8217;s going</p>



<p class="wp-block-paragraph">to be eight because we&#8217;re using quad words for our integers so we&#8217;re really just going to take</p>



<p class="wp-block-paragraph">50 times 8 whatever number that is is that 400 tell me in the comments if that&#8217;s a right or wrong</p>



<p class="wp-block-paragraph">and so uh you may or may not know if you don&#8217;t see more videos see more textbooks you may or may not</p>



<p class="wp-block-paragraph">instruction just multiplies two numbers.</p>



<p class="wp-block-paragraph">If you use the three operand format,</p>



<p class="wp-block-paragraph">then the last two operands get multiplied</p>



<p class="wp-block-paragraph">and the results stored in the first operand.</p>



<p class="wp-block-paragraph">But if we use the two operand format like I&#8217;ve done here,</p>



<p class="wp-block-paragraph">then both of those operands get multiplied</p>



<p class="wp-block-paragraph">and then the result gets stored in the first operand.</p>



<p class="wp-block-paragraph">So basically at this point,</p>



<p class="wp-block-paragraph">R10 should hold the number of memory locations</p>



<p class="wp-block-paragraph">that we should move the stack pointer</p>



<p class="wp-block-paragraph">in order to make room for all those integers</p>



<p class="wp-block-paragraph">like I showed you a second ago.</p>



<p class="wp-block-paragraph">showed you a second ago. So then we&#8217;re going to move the stack pointer.</p>



<p class="wp-block-paragraph">And maybe I&#8217;ll leave a little comment here. Remember, the stack grows downward</p>



<p class="wp-block-paragraph">in memory. And so all I&#8217;m doing is subtracting the stack pointer. Remember the stack pointer</p>



<p class="wp-block-paragraph">register, it just holds a number, which is a memory location. So if you subtract some numbers</p>



<p class="wp-block-paragraph">from it, you&#8217;re really having it go downward in memory. And that&#8217;s what we want to do</p>



<p class="wp-block-paragraph">memory and that&#8217;s what we want to do to you know grow the stack for a local variable so I&#8217;m going</p>



<p class="wp-block-paragraph">to say well I should also say that we&#8217;re using the two operand version so just like I&#8217;m all if we</p>



<p class="wp-block-paragraph">had the three operand version then the last two operands would have one subtracted from the other</p>



<p class="wp-block-paragraph">and the result will be stored in the first one but since I&#8217;m using the two operand version</p>



<p class="wp-block-paragraph">basically it&#8217;s taking RSP minus R10 and then storing it in RSP so this this instruction just</p>



<p class="wp-block-paragraph">this instruction just says let&#8217;s move the stack pointer downward in memory</p>



<p class="wp-block-paragraph">enough times that we have room for all of our integers okay no problem next thing that we&#8217;re</p>



<p class="wp-block-paragraph">going to do is um we&#8217;re going to move r12 we&#8217;re going to move into r12 the current value of rsp</p>



<p class="wp-block-paragraph">word in memory right less okay so the first integer you know it&#8217;s up to you</p>



<p class="wp-block-paragraph">how you style this because once we do the subtraction then RSP is actually</p>



<p class="wp-block-paragraph">going to be pointing towards an integer you could consider that to be the first</p>



<p class="wp-block-paragraph">integer or the last integer because all we have is an array of integers so you</p>



<p class="wp-block-paragraph">you know rsp wherever it&#8217;s sitting when we&#8217;re finished we could say that&#8217;s pointing to the</p>



<p class="wp-block-paragraph">first integer or we could say it&#8217;s pointing to the last integer but um if we decide to say that it is</p>



<p class="wp-block-paragraph">pointing to the let&#8217;s see yeah if we&#8217;re deciding that it points to the first integer let me just do</p>



<p class="wp-block-paragraph">a little comment here first integer it just makes it a little bit easier for me to write our loop</p>



<p class="wp-block-paragraph">You could start by pointing to the one that RSP is pointing to,</p>



<p class="wp-block-paragraph">or you could start by pointing to the other one that was like the first one that you added onto the stack.</p>



<p class="wp-block-paragraph">You could call either one of those the first integer, as long as you remember where you started.</p>



<p class="wp-block-paragraph">So you can increase or decrease the memory location to get to the next integer.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to do it in this style.</p>



<p class="wp-block-paragraph">But keep in mind, as long as the only thing that you modify and read is within that range, it&#8217;s okay.</p>



<p class="wp-block-paragraph">So let&#8217;s remember where is RSP.</p>



<p class="wp-block-paragraph">So that&#8217;s like the top of the stack.</p>



<p class="wp-block-paragraph">We&#8217;re going to say R12 holds the stack pointer so that we can use R12 as sort of a running</p>



<p class="wp-block-paragraph">pointer.</p>



<p class="wp-block-paragraph">I think that&#8217;s the way I&#8217;m going to use it.</p>



<p class="wp-block-paragraph">Let me just double check my solution.</p>



<p class="wp-block-paragraph">Yeah.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">a calculation where we remember where the last integer is so we know where the first integer</p>



<p class="wp-block-paragraph">we&#8217;re just going to call this the first integer i&#8217;ll put it in quotes just to remind you that</p>



<p class="wp-block-paragraph">this is just the style i happen to be using so here we&#8217;re going to say that the first integer</p>



<p class="wp-block-paragraph">is wherever rsp is pointing and then in r13 we&#8217;re going to remember where the other side of the</p>



<p class="wp-block-paragraph">array is in this case we&#8217;re calling it the last integer and pretty much it&#8217;s just r12 which is</p>



<p class="wp-block-paragraph">And then we add to it a memory location.</p>



<p class="wp-block-paragraph">So remember we said before that the stack grows downward in memory.</p>



<p class="wp-block-paragraph">So if we consider the top of the stack to be the first integer,</p>



<p class="wp-block-paragraph">then that means previous items, maybe I should draw this,</p>



<p class="wp-block-paragraph">are going to be increased memory locations.</p>



<p class="wp-block-paragraph">So that&#8217;s kind of like the backwards of what you imagine a stack is doing,</p>



<p class="wp-block-paragraph">but it&#8217;s kind of the way that I like it sometimes.</p>



<p class="wp-block-paragraph">So imagine just in a very simple stack,</p>



<p class="wp-block-paragraph">let&#8217;s pretend that the stack has one byte values,</p>



<p class="wp-block-paragraph">one byte of values, which it doesn&#8217;t, but let&#8217;s pretend if you, if you do it,</p>



<p class="wp-block-paragraph">I got to learn how to draw. I can&#8217;t get that five right. Okay. I&#8217;m defeated. So let&#8217;s pretend</p>



<p class="wp-block-paragraph">that that address on the thing sitting at the bottom of the stack is five. So then that means</p>



<p class="wp-block-paragraph">the next address would be four, right? But if we decided, let&#8217;s say that the, well, let&#8217;s do like</p>



<p class="wp-block-paragraph">like a few more just to make it a little bit more interesting.</p>



<p class="wp-block-paragraph">Let&#8217;s do a total of five.</p>



<p class="wp-block-paragraph">So we&#8217;ll say four, three, two, one.</p>



<p class="wp-block-paragraph">Maybe I should just add some numbers in front of those values.</p>



<p class="wp-block-paragraph">So it doesn&#8217;t feel like we&#8217;re hitting zero.</p>



<p class="wp-block-paragraph">But so just pretend we have a memory location of 15.</p>



<p class="wp-block-paragraph">That&#8217;s not going to be the case in real life.</p>



<p class="wp-block-paragraph">Pretend that we have one byte integers on the stack, which is not going to be the case.</p>



<p class="wp-block-paragraph">We&#8217;re going to have quad words.</p>



<p class="wp-block-paragraph">But you can see the memory locations go downward, right?</p>



<p class="wp-block-paragraph">to the stack pointer is pointing to the topmost location we have to remember that anything up here</p>



<p class="wp-block-paragraph">might exist in system ram but it&#8217;s not considered valid data because we didn&#8217;t</p>



<p class="wp-block-paragraph">you know make it part of the stack by by growing the stack pointer so that means uh these</p>



<p class="wp-block-paragraph">these items well maybe i&#8217;ll do a check instead of an x because x looks bad i&#8217;ll do a check</p>



<p class="wp-block-paragraph">check and a check and a check and a check these items are okay to use so if I have</p>



<p class="wp-block-paragraph">RSP or in this case we just remembered where RSP was by storing r12 if we have</p>



<p class="wp-block-paragraph">that memory location 11 and we want to get an additional integer like somewhere</p>



<p class="wp-block-paragraph">else like the next integer well that would be you know this one down here we</p>



<p class="wp-block-paragraph">wouldn&#8217;t go in the other direction we wouldn&#8217;t go you know up we would just go</p>



<p class="wp-block-paragraph">know up we would just go down in the stack but down visually is actually growing upwards in memory</p>



<p class="wp-block-paragraph">because remember when we grow upwards visually we&#8217;re growing downward in memory so and you can</p>



<p class="wp-block-paragraph">see here too if we&#8217;re if we&#8217;re increasing 11 to 12 that means we&#8217;re adding memory locations to get</p>



<p class="wp-block-paragraph">to the next uh integer that we have in the stack so that&#8217;s why here my green is just like frustrating</p>



<p class="wp-block-paragraph">fix this yeah so that&#8217;s why here we&#8217;re adding a little formula instead of subtracting because</p>



<p class="wp-block-paragraph">the rsp started there we&#8217;re saying that the top of the stack is the first integer</p>



<p class="wp-block-paragraph">just so we can add in a more convenient nice way so what are we adding to it we&#8217;re just adding to it</p>



<p class="wp-block-paragraph">the number of integers minus one and then so that&#8217;s that&#8217;s the number of slots that</p>



<p class="wp-block-paragraph">size. So if you imagine, you know, if we had 10 integers, then, you know, 10 minus one is nine</p>



<p class="wp-block-paragraph">slots. So if you imagine that zero is the first integer, let&#8217;s say the memory location again,</p>



<p class="wp-block-paragraph">then that means one, two, three, four, five, six, seven, eight, nine, 10 minus one is nine. So if we</p>



<p class="wp-block-paragraph">added the number of integers minus one from the start, which we&#8217;ll consider zero here, that means</p>



<p class="wp-block-paragraph">directly so that&#8217;s just like a little math because sometimes when you when you</p>



<p class="wp-block-paragraph">think of adding two numbers together or taking the difference or including the</p>



<p class="wp-block-paragraph">first number or not including the first number it&#8217;s a little confusing right so</p>



<p class="wp-block-paragraph">keep in mind for this particular calculation we are adding to it the</p>



<p class="wp-block-paragraph">number of integers minus one so that will be sitting on the last integer</p>



<p class="wp-block-paragraph">rather than going past it so start with the first integers memory location and</p>



<p class="wp-block-paragraph">the number of integers minus one or size minus one times the integer size,</p>



<p class="wp-block-paragraph">because remember, every integer is going to be eight bytes.</p>



<p class="wp-block-paragraph">And that will give us the memory location of the last integer in R13.</p>



<p class="wp-block-paragraph">And then I&#8217;ve said this in other videos,</p>



<p class="wp-block-paragraph">but basically you&#8217;re only allowed to make these sorts of calculations in Yasm</p>



<p class="wp-block-paragraph">when you have the calculation inside of brackets,</p>



<p class="wp-block-paragraph">but brackets will automatically dereference the value on the inside.</p>



<p class="wp-block-paragraph">It&#8217;ll consider it as a pointer that needs to be dereferenced,</p>



<p class="wp-block-paragraph">but we don&#8217;t want to dereference anything.</p>



<p class="wp-block-paragraph">anything we don&#8217;t want to go to a memory location and take a value the memory location is the value</p>



<p class="wp-block-paragraph">so these dereferencing brackets which are required for the formula are kind of bad so that&#8217;s why we</p>



<p class="wp-block-paragraph">use the lea instruction instead of the move instruction if i put move there it would</p>



<p class="wp-block-paragraph">definitely dereference the memory location and give me a value in r13 so r13 wouldn&#8217;t actually</p>



<p class="wp-block-paragraph">be a pointer it would just be the value of the junk data of the last integer keep that in mind</p>



<p class="wp-block-paragraph">keep that in mind okay so uh where&#8217;s RSP so we got that so now let&#8217;s do a loop</p>



<p class="wp-block-paragraph">I&#8217;m gonna start off with a label called demo loop init so for me personally when</p>



<p class="wp-block-paragraph">I&#8217;m looping you know I like to make my labels inside of my loops start with a</p>



<p class="wp-block-paragraph">prefix that matches the function so it&#8217;s like demo and then everything else is</p>



<p class="wp-block-paragraph">gonna be demo underscore something so uh init loop init I&#8217;m gonna make a loop</p>



<p class="wp-block-paragraph">to make a loop where i initialize the values of the array so i&#8217;m calling this loop the init loop</p>



<p class="wp-block-paragraph">and then the last part is just like this is the initialization part of the loop this is where we</p>



<p class="wp-block-paragraph">sort of like set up the initial values to loop so we have r12 and 13 that point to the first</p>



<p class="wp-block-paragraph">and last integers now we&#8217;re going to set up r14 and 15 where r14 is the running pointer i think</p>



<p class="wp-block-paragraph">before i might have accidentally said that r13 is the running pointer it just points to the last</p>



<p class="wp-block-paragraph">it just points to the last integer but if you look back up at my comments r14 is the running pointer</p>



<p class="wp-block-paragraph">to the current integer what is a running pointer it&#8217;s just a pointer that runs it&#8217;s just a pointer</p>



<p class="wp-block-paragraph">that just keeps increasing so we can look at different data values so i&#8217;m going to start it</p>



<p class="wp-block-paragraph">by looking at the first integer so now r14 is pointing to the first integer and then r15 is</p>



<p class="wp-block-paragraph">going to be the value that i want to put into that position in the array so like the first integer</p>



<p class="wp-block-paragraph">integer I want to put some kind of a starting value into it you can put the</p>



<p class="wp-block-paragraph">number zero or whatever you want I just wanted to have a start value so that it</p>



<p class="wp-block-paragraph">sort of looks more like I&#8217;m putting data and less like I have a loop counter so</p>



<p class="wp-block-paragraph">remember the integer start value up here is just seven so I&#8217;m just going to start</p>



<p class="wp-block-paragraph">at the number seven and now I&#8217;m done initializing my loop then the next thing</p>



<p class="wp-block-paragraph">I&#8217;m going to add is the top of my loop so you can imagine this as the top of a</p>



<p class="wp-block-paragraph">top of a while loop where you start comparing some sort of an expression maybe i&#8217;ll say uh</p>



<p class="wp-block-paragraph">you know expr to say that we&#8217;re comparing some sort of an expression</p>



<p class="wp-block-paragraph">and if that expression evaluates to true the loop continues if it evaluates to false</p>



<p class="wp-block-paragraph">then the loop does not continue let&#8217;s see so i&#8217;m going to compare r14 with r13 inside of the</p>



<p class="wp-block-paragraph">and 13 remember R14 is the running pointer and R13 is the last integer so basically I&#8217;m trying to</p>



<p class="wp-block-paragraph">figure out am I looking at or like let&#8217;s compare the running pointer with the pointer of the last</p>



<p class="wp-block-paragraph">integer then I&#8217;m going to say if the running pointer has a greater memory location than than</p>



<p class="wp-block-paragraph">the last integer that means I&#8217;ve gone beyond the last integer and again the way I arranged the first</p>



<p class="wp-block-paragraph">last integers just makes it easier for me to think of them as having increasing</p>



<p class="wp-block-paragraph">memory locations so I&#8217;m going to jump if the running pointer has already</p>



<p class="wp-block-paragraph">surpassed the last integer by saying let&#8217;s jump if it&#8217;s greater than so you</p>



<p class="wp-block-paragraph">can imagine maybe in the expression here I should probably say while not</p>



<p class="wp-block-paragraph">r14 is greater than r13 not a great expression but it&#8217;ll do so that&#8217;s what</p>



<p class="wp-block-paragraph">up there at the top let me just put that into my notes too so that my notes match</p>



<p class="wp-block-paragraph">the video okay so we are comparing and then we&#8217;re jumping to the end of the</p>



<p class="wp-block-paragraph">loop if we end up you know going beyond the last integer so that labels not</p>



<p class="wp-block-paragraph">created yet I&#8217;ll create that in a moment but pretty much that&#8217;s a label that&#8217;s</p>



<p class="wp-block-paragraph">just going to be below the loop just to say like we&#8217;re finished with the loop</p>



<p class="wp-block-paragraph">the loop body I like to put comments here to help myself remember oh this is</p>



<p class="wp-block-paragraph">the part of the while loop that I&#8217;m currently inside of just makes things a</p>



<p class="wp-block-paragraph">little bit easier to understand you know you put a block comment up top of every</p>



<p class="wp-block-paragraph">label or every you know chunk of instructions just to let you know the</p>



<p class="wp-block-paragraph">general idea and then you know sometimes you put comments also on the right side</p>



<p class="wp-block-paragraph">to help you remember what each instruction is actually doing so then</p>



<p class="wp-block-paragraph">what am I going to do here remember R14 is the running pointer if I D ref R14</p>



<p class="wp-block-paragraph">ref r14 that means i want to move an actual value into that memory location rather than changing the</p>



<p class="wp-block-paragraph">memory location that r14 points to so r15 is going to be the uh the integer that we want to</p>



<p class="wp-block-paragraph">write into the array so all i&#8217;m doing here is i&#8217;m saying let&#8217;s take that value seven which is what</p>



<p class="wp-block-paragraph">it starts as and just move it in to you know the ram stick at that memory location so i&#8217;m setting</p>



<p class="wp-block-paragraph">seven right now then I&#8217;m going to increase our 15 so that means every time</p>



<p class="wp-block-paragraph">we iterate the loop we should see that the value increases so the first integer</p>



<p class="wp-block-paragraph">should be seven the second one should be eight next one should be nine and so</p>



<p class="wp-block-paragraph">forth so just a simple loop where I&#8217;m just writing data into my array so now</p>



<p class="wp-block-paragraph">that we&#8217;ve ended the loop body let&#8217;s write the bottom of the loop which is</p>



<p class="wp-block-paragraph">just going to increase the running pointer and jump back up to the top and</p>



<p class="wp-block-paragraph">And this is not necessarily the only style for translating while loops.</p>



<p class="wp-block-paragraph">I&#8217;m just doing it.</p>



<p class="wp-block-paragraph">And, you know, I&#8217;m going to make another video in the future where we talk about, you know, for loops and while loops and all that stuff.</p>



<p class="wp-block-paragraph">But this video is just really about local variables on the stack.</p>



<p class="wp-block-paragraph">So I&#8217;m not going to go over all the different ways you can do it.</p>



<p class="wp-block-paragraph">Anyway, so R14 is the running pointer.</p>



<p class="wp-block-paragraph">So I&#8217;m going to just make the running pointer jump to the next integer.</p>



<p class="wp-block-paragraph">And we can do that by increasing its memory location by the size of one integer.</p>



<p class="wp-block-paragraph">Again, this is another benefit of the first and last pointers that I chose at the beginning.</p>



<p class="wp-block-paragraph">I can just increase to go to the next integer.</p>



<p class="wp-block-paragraph">So we&#8217;re going to increase by 8 bytes to just go to the next integer.</p>



<p class="wp-block-paragraph">If you increase by 1 byte, you&#8217;ll probably have a huge corrupted mess because you&#8217;re</p>



<p class="wp-block-paragraph">messing with 8 byte integers but you&#8217;re only increasing by 1 byte.</p>



<p class="wp-block-paragraph">And then after we increase, we&#8217;re just going to jump to the top of the loop.</p>



<p class="wp-block-paragraph">So notice how I have a jump statement here.</p>



<p class="wp-block-paragraph">It&#8217;s going to go just to loop top.</p>



<p class="wp-block-paragraph">loop top. So now this part is here is basically just going to execute over and over and over again</p>



<p class="wp-block-paragraph">until we finally scan through all of the integers in our array. So that&#8217;s the bottom of the loop.</p>



<p class="wp-block-paragraph">And then I&#8217;ll make the label for the loop being done. It&#8217;s not really going to do anything</p>



<p class="wp-block-paragraph">except just be done. And do you, you know, I don&#8217;t know, depending on your style, maybe you can let</p>



<p class="wp-block-paragraph">drop through if the loop&#8217;s done rather than always jumping to the top but i&#8217;m just going to say when</p>



<p class="wp-block-paragraph">we&#8217;re done we jump to the loop done label and therefore there&#8217;s no more looping of that</p>



<p class="wp-block-paragraph">initialization loop okay so we got that done let me just run the program real fast to make sure</p>



<p class="wp-block-paragraph">that i haven&#8217;t screwed it up we actually should not see anything right now oh what did i do</p>



<p class="wp-block-paragraph">must have done something naughty maybe if i finish</p>



<p class="wp-block-paragraph">uh this program then everything will be okay oh my gosh what did i even do</p>



<p class="wp-block-paragraph">well i&#8217;ve got a working solution in the other window so hopefully when i paste all the extra</p>



<p class="wp-block-paragraph">steps everything will be fine you never know subtract the stack pointer oh did i forget to</p>



<p class="wp-block-paragraph">oh did i forget to restore something at the very end print body move the base pointer into the</p>



<p class="wp-block-paragraph">RSP stack pointer oh yeah okay that&#8217;s definitely what you got that&#8217;s why I</p>



<p class="wp-block-paragraph">crashed okay so um remember I said you got to be very careful about the stack</p>



<p class="wp-block-paragraph">this is a great lesson so I did preserve the base pointer but I didn&#8217;t actually</p>



<p class="wp-block-paragraph">preserve the stack pointer notice how right here I subtracted from the stack</p>



<p class="wp-block-paragraph">pointer but I did not restore the stack pointer anywhere so that means I</p>



<p class="wp-block-paragraph">corrupted the stack for anyone that called me and also for my return address</p>



<p class="wp-block-paragraph">address. So I&#8217;m kind of trying to copy paste my instructions from top to bottom, but I think I&#8217;m</p>



<p class="wp-block-paragraph">just going to copy paste something else to make sure that we can actually run this. So I&#8217;m going</p>



<p class="wp-block-paragraph">to copy paste into the epilog a restoration of the stack pointer. And that&#8217;s why we saved the</p>



<p class="wp-block-paragraph">stack pointer in the base pointer, just to remember where it was when we originally started our</p>



<p class="wp-block-paragraph">function. So now on line 128, it should be restored and the program should work. Let me just double</p>



<p class="wp-block-paragraph">yeah it worked okay nothing happened that we can see but it did write values</p>



<p class="wp-block-paragraph">into the array now let&#8217;s do another loop where we just print the array so let&#8217;s</p>



<p class="wp-block-paragraph">see loop in it done looping it bottom and looping it done okay so now there&#8217;s</p>



<p class="wp-block-paragraph">like gonna be another loop here we&#8217;re gonna call this the print loop and so</p>



<p class="wp-block-paragraph">the print loop is gonna be kind of the same pattern we&#8217;re just gonna loop</p>



<p class="wp-block-paragraph">going to loop through all the integers in the array but instead of modifying them we&#8217;re just</p>



<p class="wp-block-paragraph">going to print them so now you know the first thing we&#8217;ll do is we&#8217;ll set r12 we&#8217;ll store that</p>



<p class="wp-block-paragraph">inside of r14 in order to start the loop at the first integer what was r14 again that was the</p>



<p class="wp-block-paragraph">running pointer remember let&#8217;s see where is that yeah it was the running pointer so now we&#8217;re</p>



<p class="wp-block-paragraph">resetting the r14 running pointer to the very beginning of the array and we know where the</p>



<p class="wp-block-paragraph">thing we&#8217;ll do is we will set up the top of the loop and the body so I&#8217;m just</p>



<p class="wp-block-paragraph">going to copy paste again this stuff right here</p>



<p class="wp-block-paragraph">right there okay so the top of the loop we&#8217;re asking ourselves you know we&#8217;re</p>



<p class="wp-block-paragraph">gonna compare I&#8217;m not gonna put all the extra while stuff that I put in the</p>



<p class="wp-block-paragraph">previous loop because hopefully by now you understand loops a little bit better</p>



<p class="wp-block-paragraph">and if we&#8217;re beyond the last integer because the running point of r14 is beyond the memory</p>



<p class="wp-block-paragraph">location of r13 that means we&#8217;re totally done so we should jump if it&#8217;s greater than</p>



<p class="wp-block-paragraph">now i feel bad let&#8217;s uh let&#8217;s put a comment in here on the top let&#8217;s go uh</p>



<p class="wp-block-paragraph">basically that you know if uh keep going as long as r14 is not greater than r13 so if it is</p>



<p class="wp-block-paragraph">in the body and in the body all i&#8217;m going to do is use r14 the running pointer to print uh you know</p>



<p class="wp-block-paragraph">whatever value is sitting in that particular integer so how do we do that i&#8217;m just going to</p>



<p class="wp-block-paragraph">use the printf statement or sorry the printf function which is provided by the c libraries</p>



<p class="wp-block-paragraph">that&#8217;s why we&#8217;re doing you know a modular or a hybrid program with multiple modules and c linking</p>



<p class="wp-block-paragraph">very quickly there is a function called printf which I can call it it takes multiple arguments</p>



<p class="wp-block-paragraph">but the first two arguments that I can give it are the string that represents the formatting</p>



<p class="wp-block-paragraph">that I want to print like I could do like a regular string message I could do tokens</p>



<p class="wp-block-paragraph">to format some data inside of them and then the second argument is going to be the piece of data</p>



<p class="wp-block-paragraph">that I actually want to use let me see if I can just type that up for you real fast</p>



<p class="wp-block-paragraph">So, you know, the printf instruction, or sorry, this is not an instruction.</p>



<p class="wp-block-paragraph">This is a function in C.</p>



<p class="wp-block-paragraph">We would typically, you know, give it some kind of string.</p>



<p class="wp-block-paragraph">The string should be null terminated, and it should have a new line at the very end of it</p>



<p class="wp-block-paragraph">to make sure that printf actually flushes.</p>



<p class="wp-block-paragraph">It won&#8217;t flush if you don&#8217;t have a new line, so the program will look really weird.</p>



<p class="wp-block-paragraph">But I guess it&#8217;s more performant if you have a way to delay the flushing,</p>



<p class="wp-block-paragraph">flushing and you know that you can flush it later at the very end but for now i&#8217;m just going to</p>



<p class="wp-block-paragraph">flush every time and then every argument after that is some sort of you know data that we can</p>



<p class="wp-block-paragraph">print so imagine we have a long and we&#8217;ll call it a and we&#8217;ll say that it has like some giant value</p>



<p class="wp-block-paragraph">so that means we would give that long as the next argument the rsi argument and then for the string</p>



<p class="wp-block-paragraph">what I&#8217;m using right here %lu so you can imagine instead of this string it is this</p>



<p class="wp-block-paragraph">string right here whoops too many too many quotes it&#8217;s just this string right</p>



<p class="wp-block-paragraph">here and then instead of a 10 13 that&#8217;s the same thing as just doing an or if</p>



<p class="wp-block-paragraph">you want to be you know more of a windows windows person slash r slash n</p>



<p class="wp-block-paragraph">it&#8217;s all good and the zero is not needed because the string if you put a string</p>



<p class="wp-block-paragraph">literal it&#8217;s automatically going to be null terminated which means there&#8217;s just</p>



<p class="wp-block-paragraph">null terminated which means there&#8217;s just a zero at the end of the string in memory so this is</p>



<p class="wp-block-paragraph">basically what I&#8217;m doing I&#8217;m making a an integer in the case of the assembly program it&#8217;s going to</p>



<p class="wp-block-paragraph">grab an integer from that position in the array that we&#8217;re looking at and it&#8217;s going to give it</p>



<p class="wp-block-paragraph">as an argument and then the first argument is going to say let&#8217;s just print this as a unsigned</p>



<p class="wp-block-paragraph">long so that&#8217;s why I have that string here let me search for it and go down a little bit again so</p>



<p class="wp-block-paragraph">I&#8217;m saying first argument is this the format that I want to be printed second argument is the actual</p>



<p class="wp-block-paragraph">is the actual value and then I&#8217;m gonna make a call to printf why do I have this</p>



<p class="wp-block-paragraph">weird push and pop pair sitting around printf so this is not a video about</p>



<p class="wp-block-paragraph">stack alignment in GCC but basically the GCC libraries expect that your stack is</p>



<p class="wp-block-paragraph">aligned to I think 16 bytes but since we use 8 byte integers every single time we</p>



<p class="wp-block-paragraph">address to the stack which is eight bytes and then every time we do one single push or pop</p>



<p class="wp-block-paragraph">we&#8217;re modifying the alignment of the stack by eight bytes so if you think about it when we&#8217;re</p>



<p class="wp-block-paragraph">programming in assembly for the most part the stack is going in and out of alignment because</p>



<p class="wp-block-paragraph">every time we modify it by eight bytes it it might line up with a 16 byte alignment or it might not</p>



<p class="wp-block-paragraph">it&#8217;s just kind of like oscillating right so when I first wrote this solution I wasn&#8217;t doing the</p>



<p class="wp-block-paragraph">what happened oh actually maybe i guess i don&#8217;t need to jump to the top right now i don&#8217;t need</p>



<p class="wp-block-paragraph">to finish the loop let&#8217;s see if this prints just one number well let me let me let me see if this</p>



<p class="wp-block-paragraph">prints one number certainly and if it&#8217;s an okay assembly program just to print one number is it</p>



<p class="wp-block-paragraph">going to work okay we got to do the done symbol okay so i&#8217;ll show you in a minute why we need</p>



<p class="wp-block-paragraph">that for stack alignment but i guess i&#8217;ll just finish the loop so demo print loop done so we</p>



<p class="wp-block-paragraph">just did print loop top and that means we need the bottom and the done so i&#8217;m just going to copy paste</p>



<p class="wp-block-paragraph">into program here i&#8217;ll just say nada because we&#8217;re not really doing anything and then at the bottom</p>



<p class="wp-block-paragraph">you know that&#8217;s the epilogue that&#8217;s separate from the the other label so basically now let me finish</p>



<p class="wp-block-paragraph">know we already know the loops but I&#8217;ll just I&#8217;ll just say it at the bottom of</p>



<p class="wp-block-paragraph">the loop we do the same thing that we did with the initialization loop we just</p>



<p class="wp-block-paragraph">increase the running pointer you know we move it along to the next integer and</p>



<p class="wp-block-paragraph">then we jump to the top of the loop that&#8217;s it and then the done label we</p>



<p class="wp-block-paragraph">don&#8217;t really do anything we&#8217;re just letting execution drop through down to</p>



<p class="wp-block-paragraph">that point so that the loop doesn&#8217;t continue so now we should be able to run</p>



<p class="wp-block-paragraph">the program don&#8217;t get excited oh actually you know what get excited I</p>



<p class="wp-block-paragraph">was gonna say don&#8217;t get excited because it was gonna totally work now but now</p>



<p class="wp-block-paragraph">because it was going to totally work now,</p>



<p class="wp-block-paragraph">but now I think we can just say that it&#8217;s going to crash.</p>



<p class="wp-block-paragraph">So if I run it, notice how there&#8217;s a segpult.</p>



<p class="wp-block-paragraph">So the GCC libraries, many functions expect your stack</p>



<p class="wp-block-paragraph">to be aligned to 16 bytes.</p>



<p class="wp-block-paragraph">So if you see mysterious crashes</p>



<p class="wp-block-paragraph">and you are absolutely sure that you&#8217;re not ruining</p>



<p class="wp-block-paragraph">the stack pointer or ruining something else,</p>



<p class="wp-block-paragraph">you&#8217;re doing everything correctly,</p>



<p class="wp-block-paragraph">but the program still crashes, it might be stack alignment.</p>



<p class="wp-block-paragraph">So one way to get around stack alignment</p>



<p class="wp-block-paragraph">is just to move the stack pointer.</p>



<p class="wp-block-paragraph">move the stack pointer like at the top here we could have said oh we&#8217;ve got one two three four</p>



<p class="wp-block-paragraph">five we&#8217;ve got five pushes and then here we&#8217;re moving the stack by I don&#8217;t know how many other</p>



<p class="wp-block-paragraph">addresses the stack might be out of alignment somehow so we could add an extra push up here</p>



<p class="wp-block-paragraph">and then add a corresponding pop down at the bottom like we could easily do this let me just</p>



<p class="wp-block-paragraph">show you real fast we could push r15 twice for no reason I acknowledge and then at the bottom</p>



<p class="wp-block-paragraph">and then at the bottom we pop R15 twice,</p>



<p class="wp-block-paragraph">that would change the alignment because that&#8217;s one more 8 byte push.</p>



<p class="wp-block-paragraph">But in my case, and actually that would be a little bit smarter</p>



<p class="wp-block-paragraph">because if you have our loop where it&#8217;s constantly calling on printf,</p>



<p class="wp-block-paragraph">this is a lot of hits to memory, right?</p>



<p class="wp-block-paragraph">This is like 100 hits to memory because every single time we do a push-pop pair</p>



<p class="wp-block-paragraph">around a call to printf, we&#8217;re like touching memory.</p>



<p class="wp-block-paragraph">Whereas if I did it at the beginning and the end,</p>



<p class="wp-block-paragraph">Maybe I should just do it this way.</p>



<p class="wp-block-paragraph">I want to do it both ways so you understand, but it&#8217;s more efficient, I think, if we do</p>



<p class="wp-block-paragraph">it this way.</p>



<p class="wp-block-paragraph">Anyway, so we&#8217;ll do pop twice at the bottom and then push twice at the top.</p>



<p class="wp-block-paragraph">And so then we don&#8217;t really need to surround it with a push-pop pair.</p>



<p class="wp-block-paragraph">I think I haven&#8217;t tested this.</p>



<p class="wp-block-paragraph">We&#8217;ll hope now that the stack is in alignment at all times in our functions so that it doesn&#8217;t</p>



<p class="wp-block-paragraph">crash.</p>



<p class="wp-block-paragraph">Yeah, so now see how the program works.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do it the other way now, which is the less efficient way, because here</p>



<p class="wp-block-paragraph">now which is the less efficient way because here we just have one extra push</p>



<p class="wp-block-paragraph">pop pair but if we do it the other way it&#8217;ll still work but we&#8217;ll be hitting</p>



<p class="wp-block-paragraph">memory much more often so I&#8217;m just gonna do it this way just to show you you can</p>



<p class="wp-block-paragraph">surround any call because sometimes in your programs you might have the stack</p>



<p class="wp-block-paragraph">like you know modified throughout the function many different times so it</p>



<p class="wp-block-paragraph">wouldn&#8217;t make too much sense for you to add an extra push pop pair in the prologue</p>



<p class="wp-block-paragraph">and epilogue because that might not solve it for all of your calls to all of</p>



<p class="wp-block-paragraph">to all of your seed library functions.</p>



<p class="wp-block-paragraph">So in that case,</p>



<p class="wp-block-paragraph">where you can&#8217;t really predict the stack well enough,</p>



<p class="wp-block-paragraph">you can just surround your call with a push-pop pair.</p>



<p class="wp-block-paragraph">It hits memory more, but it&#8217;ll work.</p>



<p class="wp-block-paragraph">So this is basically, you can imagine,</p>



<p class="wp-block-paragraph">by the time we get to line 139,</p>



<p class="wp-block-paragraph">the stack is out of alignment.</p>



<p class="wp-block-paragraph">So I just do a push that puts it into alignment.</p>



<p class="wp-block-paragraph">And then after the call comes back,</p>



<p class="wp-block-paragraph">I just pop it so it&#8217;s back out of alignment again,</p>



<p class="wp-block-paragraph">but I don&#8217;t have extra erroneous data sitting on the stack.</p>



<p class="wp-block-paragraph">Because if I only had push and not pop,</p>



<p class="wp-block-paragraph">then it&#8217;s going to push it more and more out of alignment.</p>



<p class="wp-block-paragraph">It&#8217;s going to push it in and out and in and out,</p>



<p class="wp-block-paragraph">but it&#8217;s going to add a bunch of junk data to the stack</p>



<p class="wp-block-paragraph">that I&#8217;ll never recover from.</p>



<p class="wp-block-paragraph">Or actually, I guess I will recover at the very end</p>



<p class="wp-block-paragraph">when I restore the stack pointer, but it&#8217;s pointless.</p>



<p class="wp-block-paragraph">It&#8217;s going to consume too much memory.</p>



<p class="wp-block-paragraph">What if I was writing like a billion</p>



<p class="wp-block-paragraph">or like a million items on the stack, right?</p>



<p class="wp-block-paragraph">a billion iterations of the loop would probably be a bad idea to start adding onto the stack</p>



<p class="wp-block-paragraph">we&#8217;ll probably end up stack overflowing probably with far less than a billion items so anyway i&#8217;m</p>



<p class="wp-block-paragraph">going to surround the call with the push pop pair and then at the bottom we just do the same thing</p>



<p class="wp-block-paragraph">you know increase the integer and then go to the top and so now we you know you already can see it</p>



<p class="wp-block-paragraph">it starts off printing well it starts off initializing the array but then the thing that we</p>



<p class="wp-block-paragraph">can see starts off printing the first integer and then the next iteration of the loop prints the</p>



<p class="wp-block-paragraph">second integer and the third and the fourth and so forth until we get all the way down just to</p>



<p class="wp-block-paragraph">prove to you maybe we can increase it by two instead of one each time I&#8217;ll just modify that</p>



<p class="wp-block-paragraph">Oh no, that&#8217;s the print loop. Let&#8217;s do the init loop.</p>



<p class="wp-block-paragraph">Init loop bottom.</p>



<p class="wp-block-paragraph">Oh, right there, when we&#8217;re in the body.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to increase R15 twice.</p>



<p class="wp-block-paragraph">Just to show you, you know, we can kind of control what&#8217;s going inside of the local array.</p>



<p class="wp-block-paragraph">So see how it goes from 7 to 9 to 11 instead of 7, 8, 9.</p>



<p class="wp-block-paragraph">So I&#8217;m going to take that out and then just show you that we can control</p>



<p class="wp-block-paragraph">how many integers we have in our local array with just the number of integers.</p>



<p class="wp-block-paragraph">just the number of integers so I&#8217;m going to change the 50 to a 5 and run it again and you can see</p>



<p class="wp-block-paragraph">what the dang did I do number of integers is 5 and then it was 50 did I put the stack out of</p>



<p class="wp-block-paragraph">alignment no I I have a push put pair there number 50 is not hard-coded anywhere anywhere</p>



<p class="wp-block-paragraph">there. Oh, I have a bug that I can debug. That&#8217;s nice. I guess the bug debugging is not for this</p>



<p class="wp-block-paragraph">video. I wonder if some other value would work for that. There&#8217;s a number of things that could</p>



<p class="wp-block-paragraph">be the problem. It could be like stack alignment somehow. It could also be, let&#8217;s see, am I making</p>



<p class="wp-block-paragraph">any calls here inside of my function demo? I&#8217;m saying sub and LEA and then start and then jump</p>



<p class="wp-block-paragraph">and start and then jump and increase and jump and then RDI.</p>



<p class="wp-block-paragraph">Not really doing anything else.</p>



<p class="wp-block-paragraph">So I don&#8217;t think it&#8217;s stack alignment.</p>



<p class="wp-block-paragraph">I must have miscalculated somewhere somehow for changing that.</p>



<p class="wp-block-paragraph">All right.</p>



<p class="wp-block-paragraph">Well, if I figured it out, then I will release another video.</p>



<p class="wp-block-paragraph">But basically, this is the idea.</p>



<p class="wp-block-paragraph">100 works, 50 works.</p>



<p class="wp-block-paragraph">What about 99?</p>



<p class="wp-block-paragraph">Would that work?</p>



<p class="wp-block-paragraph">Save faults on 99.</p>



<p class="wp-block-paragraph">faults on 99 and then 98. 98 works so like every two seems to work but that is</p>



<p class="wp-block-paragraph">two quad words or 16 oh right so like a hundred integers if we assume that</p>



<p class="wp-block-paragraph">that&#8217;s in alignment then 99 would be eight bytes less that we&#8217;re moving the</p>



<p class="wp-block-paragraph">98, notice how it is okay.</p>



<p class="wp-block-paragraph">And then 97, it&#8217;s going to segfault again.</p>



<p class="wp-block-paragraph">Watch.</p>



<p class="wp-block-paragraph">This is a great stack alignment video, I guess.</p>



<p class="wp-block-paragraph">96, it won&#8217;t segfault.</p>



<p class="wp-block-paragraph">So it&#8217;s going in and out of alignment.</p>



<p class="wp-block-paragraph">I think I figured it out.</p>



<p class="wp-block-paragraph">So if we use the number 100, it&#8217;s in alignment.</p>



<p class="wp-block-paragraph">Sorry, it&#8217;s out of alignment.</p>



<p class="wp-block-paragraph">If we use the number 100, the program works because the number 100 throws it out of alignment.</p>



<p class="wp-block-paragraph">number 100 throws it out of alignment but then we have this push pop pair here around the call to</p>



<p class="wp-block-paragraph">printf which puts it back in alignment so if I change the number of integers I&#8217;m actually</p>



<p class="wp-block-paragraph">changing the number of memory locations that I modify the stack pointer so I have to do it by</p>



<p class="wp-block-paragraph">twos if I wanted to do 99 here then that means the stack is in alignment by the time I call</p>



<p class="wp-block-paragraph">by the time I&#8217;m getting ready to call printf which means the push pop pair around it throws</p>



<p class="wp-block-paragraph">push pop pair around it throws the stack out of alignment so just watch here I&#8217;ll</p>



<p class="wp-block-paragraph">prove it okay so it&#8217;s sake false I comment that out now it won&#8217;t throw it</p>



<p class="wp-block-paragraph">out of alignment okay should have known that before I recorded the video it&#8217;s</p>



<p class="wp-block-paragraph">fun to guess sometimes though you know I get a little nervous oh no my program</p>



<p class="wp-block-paragraph">broke on camera can I debug it live well I guess I can but kind of slowly</p>



<p class="wp-block-paragraph">anyway so we I think we&#8217;ve gone over every single part that I wanted to show you</p>



<p class="wp-block-paragraph">we know how to create a local array on the stack and and therefore you also know how to create like</p>



<p class="wp-block-paragraph">any other data type on the stack if you want you want to create a long a 64-bit integer</p>



<p class="wp-block-paragraph">just move it by eight bytes instead of moving it by eight times however many integers we were doing</p>



<p class="wp-block-paragraph">in this video you want to I don&#8217;t know put a character on the stack you can do that if you</p>



<p class="wp-block-paragraph">Just, you know, move it by one memory location instead of eight memory locations.</p>



<p class="wp-block-paragraph">So, you know, one byte instead of eight bytes.</p>



<p class="wp-block-paragraph">You want to store a short, you know, a two byte integer?</p>



<p class="wp-block-paragraph">Well, just move it by two bytes instead of eight bytes, right?</p>



<p class="wp-block-paragraph">So you can do this as many times as you want.</p>



<p class="wp-block-paragraph">You want to have several local variables?</p>



<p class="wp-block-paragraph">Just move it one time for every local variable.</p>



<p class="wp-block-paragraph">Same thing for accessing.</p>



<p class="wp-block-paragraph">You just have to remember where everything is.</p>



<p class="wp-block-paragraph">offset of the of the first variable local variable and then the offset of the second</p>



<p class="wp-block-paragraph">local variable you can store those in globals or store those in registers if you can you just</p>



<p class="wp-block-paragraph">got to remember somehow where everything starts but it&#8217;s all sitting on the stack if it&#8217;s a local</p>



<p class="wp-block-paragraph">variable okay I guess that&#8217;s everything that I really have to say I hope you enjoyed this video</p>



<p class="wp-block-paragraph">I hope you learned a little bit of stuff and had a little bit of fun I&#8217;ll see you in the next video</p>



<p class="wp-block-paragraph">have a good one</p>



<p class="wp-block-paragraph">you</p>



<p class="wp-block-paragraph">Hey everybody, thanks for watching this video again from the bottom of my heart. I really appreciate it</p>



<p class="wp-block-paragraph">I do hope you did learn something and have some fun</p>



<p class="wp-block-paragraph">If you could do me a please a small little favor</p>



<p class="wp-block-paragraph">Could you please subscribe and follow this channel or these videos or whatever it is?</p>



<p class="wp-block-paragraph">You do on the current social media website that you&#8217;re looking at right now</p>



<p class="wp-block-paragraph">It would really mean the world to me and it&#8217;ll help make more videos and grow this community</p>



<p class="wp-block-paragraph">more videos, longer videos, better videos, or just I&#8217;ll be able to keep making videos in general.</p>



<p class="wp-block-paragraph">So please do me a kindness and subscribe. You know, sometimes I&#8217;m sleeping in the middle of</p>



<p class="wp-block-paragraph">the night and I just wake up because I know somebody subscribed or followed. It just wakes</p>



<p class="wp-block-paragraph">me up and I get filled with joy. That&#8217;s exactly what happens every single time. So you could do</p>



<p class="wp-block-paragraph">it as a nice favor to me or you could troll me if you want to just wake me up in the middle of</p>



<p class="wp-block-paragraph">the night, just subscribe and then I&#8217;ll just wake up. I promise that&#8217;s what will happen.</p>



<p class="wp-block-paragraph">Also, if you look at the middle of the screen right now, you should see a QR code which you can scan in order to go to the website</p>



<p class="wp-block-paragraph">which I think is also named somewhere at the bottom of this video and</p>



<p class="wp-block-paragraph">It&#8217;ll take you to my main website where you can just kind of like see all the videos</p>



<p class="wp-block-paragraph">I published and the services and tutorials and things that I offer and all that good stuff and</p>



<p class="wp-block-paragraph">If you have a suggestion for</p>



<p class="wp-block-paragraph">Please leave a comment or if you just want to say hey, what&#8217;s up? What&#8217;s going on?</p>



<p class="wp-block-paragraph">You know just send me a comment whatever I also wake up for those in the middle of the night</p>



<p class="wp-block-paragraph">I get I wake up in a cold sweat. I&#8217;m like</p>



<p class="wp-block-paragraph">It would really it really mean the world to me. I would really appreciate it. So again, thank you so much for watching this video and</p>



<p class="wp-block-paragraph">Enjoy the cool music as I fade into the darkness, which is coming for us all</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-local-variables-stack-frames-alignment-explained/">x86-64 Assembly: Local Variables, Stack Frames &amp; Alignment Explained</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/x86-64-assembly-local-variables-stack-frames-alignment-explained/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>x86-64 Assembly Pointers &#038; Dereferencing Explained &#8211; Hybrid C++/YASM Example</title>
		<link>https://www.NeuralLantern.com/x86-64-assembly-pointers-dereferencing-explained-hybrid-c-yasm-example/</link>
					<comments>https://www.NeuralLantern.com/x86-64-assembly-pointers-dereferencing-explained-hybrid-c-yasm-example/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 22 Feb 2026 03:21:32 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly C++ interop]]></category>
		<category><![CDATA[assembly data section]]></category>
		<category><![CDATA[assembly memory addresses]]></category>
		<category><![CDATA[assembly pointers]]></category>
		<category><![CDATA[assembly tutorial]]></category>
		<category><![CDATA[dereferencing assembly]]></category>
		<category><![CDATA[extern C assembly]]></category>
		<category><![CDATA[hybrid C++ assembly]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[passing pointers assembly]]></category>
		<category><![CDATA[stack alignment assembly]]></category>
		<category><![CDATA[systems programming]]></category>
		<category><![CDATA[x86 assembly tutorial]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[x86-64 calling convention]]></category>
		<category><![CDATA[x86-64 pointers]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=305</guid>

					<description><![CDATA[<p>This video explains pointers and dereferencing in x86-64 YASM assembly and demonstrates passing pointers between assembly and C++ in a hybrid program. We show how to read a C string from assembly, modify a long via pointer dereference so the change is visible in C++, and send assembly-owned data (string, long, double) back to C++ using pointers.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-pointers-dereferencing-explained-hybrid-c-yasm-example/">x86-64 Assembly Pointers &amp; Dereferencing Explained &#8211; Hybrid C++/YASM Example</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="x86-64 Assembly Pointers &amp; Dereferencing Explained - Hybrid C++/YASM Example" width="1380" height="776" src="https://www.youtube.com/embed/jzj0iqC-XJI?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">Learn pointers &amp; dereferencing in x86-64 YASM assembly and how to pass them correctly between assembly and C++ in a hybrid program. We build a small working example that sends strings, longs, and doubles both directions using pointers, modifies values across module boundaries, and explains why pointer-to-double still uses general-purpose registers. Includes a quick demo of stack misalignment crash + fix.</p>



<p class="wp-block-paragraph">Great for assembly beginners moving to real programs, systems programming students, or anyone curious how low-level code talks to C/C++.</p>



<p class="wp-block-paragraph">00:00 Introduction to Pointers and Dereferencing in x86-64 Assembly<br>00:28 Pointers explained in C++<br>01:02 Changing values via pointers in C++<br>01:43 Pointers in assembly basics<br>02:09 Defining variables and pointers in YASM data section<br>03:23 Pointers are always integers even to doubles<br>04:20 Function arguments are pointers treated as 64-bit integers<br>05:00 Driver C++ code overview<br>05:58 Marking extern &#8220;C&#8221; functions<br>06:40 Local stack variables and passing pointers<br>07:51 Stack lifetime warning<br>08:34 Assembly data section strings and numbers<br>09:39 Print null-terminated string helper functions<br>10:38 External symbols and hey_driver_print_this<br>11:29 Point function prologue and stack alignment<br>13:04 Extra push for 16-byte alignment<br>14:20 Printing welcome message from assembly<br>16:00 Driver sees initial long value<br>16:58 Printing received string from C++<br>18:20 Using received char pointer without dereference<br>20:21 Modifying incoming long via dereference<br>21:46 Driver sees modified long value 101<br>22:43 Calling back to C++ to print assembly-owned data<br>23:48 Passing pointers to assembly string long and double<br>25:08 Driver prints assembly-owned values and addresses<br>26:14 Summary of pointer passing between modules<br>26:36 Stack alignment crash demonstration<br>27:39 Adding extra push/pop fixes segfault<br>28:00 Closing remarks and call to subscribe</p>



<p class="wp-block-paragraph">=-=-=-=-=-=-=-=-=</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>



<li>Twitter / X: https://x.com/NeuralLantern</li>



<li>Rumble: https://rumble.com/c/c-3696939</li>



<li>BitChute: https://www.bitchute.com/channel/pg1Pvv5dN4Gt</li>



<li>Daily Motion: https://www.dailymotion.com/neurallantern</li>



<li>Minds: https://www.minds.com/neurallantern/</li>



<li>Odysee: https://odysee.com/@NeuralLantern:5</li>
</ul>



<p class="wp-block-paragraph">Please show your support!</p>



<ul class="wp-block-list">
<li>Buy me a coffee: https://ko-fi.com/neurallantern</li>



<li>Subscribe + Sharing on Social Media</li>



<li>Leave a comment or suggestion</li>



<li>Subscribe to Blog: https://www.NeuralLantern.com</li>



<li>Watching the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p class="wp-block-paragraph">Hey there! In this video we&#8217;re going to talk about pointers and dereferencing in a YASM x8664</p>



<p class="wp-block-paragraph">assembly program, also as a hybrid program so that assembly and C++ can talk to each other</p>



<p class="wp-block-paragraph">and send each other pointers and send each other data and things like that.</p>



<p class="wp-block-paragraph">for what pointers are.</p>



<p class="wp-block-paragraph">I&#8217;m going to write in C++ for a second.</p>



<p class="wp-block-paragraph">Suppose you have a pointer for an integer.</p>



<p class="wp-block-paragraph">We&#8217;ll call it P.</p>



<p class="wp-block-paragraph">Suppose you have an integer by itself.</p>



<p class="wp-block-paragraph">We&#8217;ll call it A.</p>



<p class="wp-block-paragraph">Let&#8217;s say that the value of A is 5.</p>



<p class="wp-block-paragraph">And if you wanted to say that P points to A,</p>



<p class="wp-block-paragraph">you could say P equals the address of A.</p>



<p class="wp-block-paragraph">I&#8217;ll put C++ at the top here.</p>



<p class="wp-block-paragraph">And so now if I set A to 6</p>



<p class="wp-block-paragraph">then I print P a dereference of P this is not like a full pointers tutorial</p>



<p class="wp-block-paragraph">but basically by changing a I&#8217;m changing what P thinks it sees as a value</p>



<p class="wp-block-paragraph">assuming ID reference it I could also let me do a print 6 here I could also</p>



<p class="wp-block-paragraph">just change the value through P I could say dereference P and I could say equals</p>



<p class="wp-block-paragraph">would actually print a seven right so you know you can have regular variables global variables</p>



<p class="wp-block-paragraph">whatever kind of you know memory stuff on the stack and to get a pointer to it you really just</p>



<p class="wp-block-paragraph">need to get its memory location in c++ it&#8217;s kind of easy syntactically you can see what&#8217;s happening</p>



<p class="wp-block-paragraph">in assembly you really just need the memory location stored somewhere you could store that</p>



<p class="wp-block-paragraph">variable that just simply stored the memory location of some other variable.</p>



<p class="wp-block-paragraph">You could have a 64-bit register store the value of a variable.</p>



<p class="wp-block-paragraph">Let&#8217;s say we have like a, I don&#8217;t know, my whatever, my number let&#8217;s say inside of assembly.</p>



<p class="wp-block-paragraph">I&#8217;ll do ASM here and we say it&#8217;s a quad word and it starts off as this number or whatever.</p>



<p class="wp-block-paragraph">So if you haven&#8217;t seen my previous videos, go see them for the basics of assembly and</p>



<p class="wp-block-paragraph">of assembly and linking and make files and all that stuff but you know if you</p>



<p class="wp-block-paragraph">have an assembly program and you have a data section and you define a global</p>



<p class="wp-block-paragraph">variable like this what you&#8217;re basically saying is I want to take this giant</p>



<p class="wp-block-paragraph">number and I want to write it into eight bytes that&#8217;s the DQ it says data quad</p>



<p class="wp-block-paragraph">word I want to write that giant number across eight bytes and then I want to</p>



<p class="wp-block-paragraph">get a pointer to it stored in the my number symbol so my number is not</p>



<p class="wp-block-paragraph">actually the value it&#8217;s a pointer to the value so you know later if you want to</p>



<p class="wp-block-paragraph">you know later if you want to move you know something into a register if you did this</p>



<p class="wp-block-paragraph">that would move the pointer into rax but if you did this</p>



<p class="wp-block-paragraph">with deref symbols after it or around it then you would move</p>



<p class="wp-block-paragraph">maybe i&#8217;ll put that into rex you&#8217;d move that actual number that we specified into rex</p>



<p class="wp-block-paragraph">into Rx. It&#8217;s important to understand also that pointers are integers even when we&#8217;re pointing to</p>



<p class="wp-block-paragraph">doubles. So for example sometimes people make this mistake they&#8217;ll say you know my double</p>



<p class="wp-block-paragraph">and they&#8217;ll say it&#8217;s a quad word meaning this is going to be a 64-bit double precision floating</p>



<p class="wp-block-paragraph">point number and they&#8217;ll do like 44.55 or whatever. So that is a double and it is in memory</p>



<p class="wp-block-paragraph">you know what is the symbol of my double remember it&#8217;s supposed to be just a</p>



<p class="wp-block-paragraph">pointer right it can&#8217;t be an actual double because a memory location is not</p>



<p class="wp-block-paragraph">a double a memory location is an integer so that means if you wanted to move a</p>



<p class="wp-block-paragraph">pointer into a register you would only be able to move the pointer into a</p>



<p class="wp-block-paragraph">regular general purpose register not a floating point register and you should</p>



<p class="wp-block-paragraph">use the regular movement instructions for just regular general purpose</p>



<p class="wp-block-paragraph">So keep that in mind if you see a signature like this like let&#8217;s say function F and we have</p>



<p class="wp-block-paragraph">You know, let&#8217;s say long a and long B and actually let&#8217;s do pointers</p>



<p class="wp-block-paragraph">Let&#8217;s say long pointer a and long pointer</p>



<p class="wp-block-paragraph">B and double pointer C all three of those arguments are actually 64 bit integers</p>



<p class="wp-block-paragraph">Because they&#8217;re all pointers even if one of the pointers points to adult a double</p>



<p class="wp-block-paragraph">double why did I say dull pointers aren&#8217;t dull they&#8217;re exciting okay so I&#8217;m gonna open up some</p>



<p class="wp-block-paragraph">code here real fast so usually I don&#8217;t explain my uh my driver I&#8217;m gonna explain it to you this time</p>



<p class="wp-block-paragraph">because it&#8217;s kind of doing a little bit more than my other videos um again if you don&#8217;t have uh the</p>



<p class="wp-block-paragraph">knowledge of how to make a make file see my other videos because that&#8217;s explained there for now I&#8217;m</p>



<p class="wp-block-paragraph">what we really need to do is write a driver and an assembly module for a</p>



<p class="wp-block-paragraph">hybrid program again hybrid programs covered in other videos so the driver is</p>



<p class="wp-block-paragraph">pretty easy I&#8217;m just going to copy paste it honestly here and then just kind of</p>



<p class="wp-block-paragraph">explain it to you the driver is pretty easy we&#8217;re going to do I O stream so we</p>



<p class="wp-block-paragraph">can print stuff we&#8217;re going to mark an external function called point as extern</p>



<p class="wp-block-paragraph">C so that just disables name mangling which means the C++ module will be able</p>



<p class="wp-block-paragraph">will be able to call on this function called point and it won&#8217;t expect that</p>



<p class="wp-block-paragraph">the point function has its name mangled like C++ does the reason being is that</p>



<p class="wp-block-paragraph">point is actually going to be in a side it&#8217;s going to be inside assembly where</p>



<p class="wp-block-paragraph">its name will not be mangled this disables the ability to overload but</p>



<p class="wp-block-paragraph">that&#8217;s okay we don&#8217;t care it&#8217;s going to take two pointers a pointer to a character</p>



<p class="wp-block-paragraph">and a pointer to a long since both of those are pointers they&#8217;re both</p>



<p class="wp-block-paragraph">64-bit integers even the character pointer and then we have a function that is internal to this</p>



<p class="wp-block-paragraph">module called hey driver print this remember we&#8217;re inside of the driver program right now</p>



<p class="wp-block-paragraph">so if you look at the bottom it&#8217;s just a function that takes in some pointers</p>



<p class="wp-block-paragraph">and then prints some stuff so it&#8217;s going to print like it&#8217;s going to print what the string is</p>



<p class="wp-block-paragraph">it&#8217;s going to print what the long is my dog&#8217;s growling at me i&#8217;m going to ignore him because</p>



<p class="wp-block-paragraph">i literally just let him pee and poop at this point now he&#8217;s harassing me for treats</p>



<p class="wp-block-paragraph">now he&#8217;s harassing me for treats he always does this okay so uh the string the long the double</p>



<p class="wp-block-paragraph">this function expects to receive three pointers to different data types it&#8217;s just going to print</p>



<p class="wp-block-paragraph">all of them and the point get it the point of this function is we&#8217;re going to go inside of</p>



<p class="wp-block-paragraph">the assembly module and then have the assembly module call on this function so that we can we</p>



<p class="wp-block-paragraph">can prove that we can have stuff sent from assembly to c plus plus or c using pointers</p>



<p class="wp-block-paragraph">using pointers we can have data sent over so anyway that&#8217;s why both of these</p>



<p class="wp-block-paragraph">are in here the point needs to be marked as no name mangling because point is</p>



<p class="wp-block-paragraph">inside of assembly which will not name mangle and then hey driver print this</p>



<p class="wp-block-paragraph">that needs to have name mangling disabled also so that the assembly</p>



<p class="wp-block-paragraph">module can call on this other than that we&#8217;re just basically inside of a main</p>



<p class="wp-block-paragraph">saying hey this is the c string we&#8217;re making a c string inside of the main function notice how</p>



<p class="wp-block-paragraph">this is a local variable so that c string is going to show up on the stack it&#8217;s going to show up in</p>



<p class="wp-block-paragraph">the area that is owned by main for main stack area same thing for my long that&#8217;s a local variable on</p>



<p class="wp-block-paragraph">the stack um and but then we can actually send pointers to those pieces of data to another</p>



<p class="wp-block-paragraph">function in another module you don&#8217;t have to only transport globals or stuff on the heap</p>



<p class="wp-block-paragraph">or stuff on the heap, you can transport pointers to local variables. Just make sure that by the</p>



<p class="wp-block-paragraph">time this function finishes, then nowhere else is actually using that data because,</p>



<p class="wp-block-paragraph">well, being on the stack, once main function or once any function finishes, then its portion of</p>



<p class="wp-block-paragraph">the stack will be cleaned up and removed and it&#8217;ll be junk data. You&#8217;ll probably get a seg fault.</p>



<p class="wp-block-paragraph">But for now, we&#8217;re not going to use anything on the stack. We&#8217;re not going to use these local</p>



<p class="wp-block-paragraph">just going to use them quickly on this call to point and then we&#8217;re going to return to the</p>



<p class="wp-block-paragraph">operating system and finish the program. So that&#8217;s the driver. Now the hard part. Let&#8217;s do this in</p>



<p class="wp-block-paragraph">assembly. So for starters, I&#8217;m going to make a data section and just explain it to you very,</p>



<p class="wp-block-paragraph">very quickly. Again, if you don&#8217;t understand the basics of YASM x86-64 assembly, did I mention</p>



<p class="wp-block-paragraph">that that&#8217;s what this language is at the beginning of the video? I guess I should put that in the</p>



<p class="wp-block-paragraph">put that in the description or record an announcement that I can tack on at the beginning</p>



<p class="wp-block-paragraph">or something. Anyway, so if you don&#8217;t understand how to do this, see my other videos, but basically</p>



<p class="wp-block-paragraph">we&#8217;re going to make a data section. We&#8217;re going to define some strings. Here&#8217;s like an announcement.</p>



<p class="wp-block-paragraph">Oh, we&#8217;re inside of, you know, the module now, the assembly module. And now we&#8217;re going to print</p>



<p class="wp-block-paragraph">the received string. And then we&#8217;re going to make a string that is owned by assembly, which we can</p>



<p class="wp-block-paragraph">into C++ when we call the function inside of the driver.</p>



<p class="wp-block-paragraph">So this string is owned by the assembly module.</p>



<p class="wp-block-paragraph">Notice how these are null terminated strings.</p>



<p class="wp-block-paragraph">I just have like a comma zero there,</p>



<p class="wp-block-paragraph">which means I have some extra functions</p>



<p class="wp-block-paragraph">I&#8217;m gonna paste in that we&#8217;re not really gonna talk about</p>



<p class="wp-block-paragraph">because they&#8217;ve been discussed in other videos</p>



<p class="wp-block-paragraph">just so that we can print null terminated strings.</p>



<p class="wp-block-paragraph">Then I&#8217;ve got a new line here,</p>



<p class="wp-block-paragraph">you know, carriage return line feed.</p>



<p class="wp-block-paragraph">And then I&#8217;ve just got some numbers</p>



<p class="wp-block-paragraph">that are owned by the assembly module.</p>



<p class="wp-block-paragraph">Then I&#8217;ve got a system write call,</p>



<p class="wp-block-paragraph">call code one for the system call writes and file descriptor standard output so I</p>



<p class="wp-block-paragraph">can print just to the terminal again if you don&#8217;t understand this see my other</p>



<p class="wp-block-paragraph">videos so now let&#8217;s start the actual text section so this is where our</p>



<p class="wp-block-paragraph">instructions start so we got the text section here and we&#8217;re going to use some</p>



<p class="wp-block-paragraph">external symbols don&#8217;t worry about these I&#8217;m just using my own little library to</p>



<p class="wp-block-paragraph">and input integers if you have access to this library use it if you don&#8217;t if you&#8217;re watching</p>



<p class="wp-block-paragraph">at home and you don&#8217;t have this library then that&#8217;s fine you can use you know printf or</p>



<p class="wp-block-paragraph">scanf or something like that to get and print floats from and to the user</p>



<p class="wp-block-paragraph">but yeah I&#8217;m just using that and then I&#8217;m marking an external function here called hey driver print</p>



<p class="wp-block-paragraph">this if you recall the driver module has a function called hey driver print this so</p>



<p class="wp-block-paragraph">just allows my assembly code to call on that external function. Okay now next</p>



<p class="wp-block-paragraph">piece of code. This is going to be… actually I&#8217;m going to paste the print</p>



<p class="wp-block-paragraph">null terminated string function and related code because it&#8217;s just like a</p>



<p class="wp-block-paragraph">big giant mess and we&#8217;re mostly going to ignore it. So just to show you what I&#8217;m</p>



<p class="wp-block-paragraph">doing here I have a function called print null terminated string so that I</p>



<p class="wp-block-paragraph">can print these strings up here and then I have it rely on a function called</p>



<p class="wp-block-paragraph">string length that I have implemented up here and all it does is just</p>



<p class="wp-block-paragraph">implemented up here and all it does just calculates the length of the string and</p>



<p class="wp-block-paragraph">then a crlf function so I can just call that so that&#8217;s all explained in other</p>



<p class="wp-block-paragraph">videos don&#8217;t worry about it for now we&#8217;re going to start the actual entry</p>



<p class="wp-block-paragraph">point remember the driver was just gonna call point right so now we just have to</p>



<p class="wp-block-paragraph">implement point in the assembly module so that&#8217;s gonna be like down here our</p>



<p class="wp-block-paragraph">our entry point so the signature for this function is going to be character</p>



<p class="wp-block-paragraph">pointer and then a long pointer and it doesn&#8217;t return anything and remember</p>



<p class="wp-block-paragraph">that if we look back at the driver that should match the signature right it&#8217;s a</p>



<p class="wp-block-paragraph">character pointer and a long pointer and of course this is just a comment that</p>



<p class="wp-block-paragraph">reminds me of what to do in assembly you don&#8217;t really have a signature you just</p>



<p class="wp-block-paragraph">sort of use registers but I&#8217;m reminding myself that RDI is going to be a</p>



<p class="wp-block-paragraph">character pointer and RSI is going to be a long pointer.</p>



<p class="wp-block-paragraph">Here&#8217;s a note to myself that I&#8217;m going to use R12 and R13, which means</p>



<p class="wp-block-paragraph">the first thing that I should do, well actually before I even do that, I should</p>



<p class="wp-block-paragraph">return from this function because it is a function. I marked it as global</p>



<p class="wp-block-paragraph">so that the other module could call it, the driver module could call it. Again,</p>



<p class="wp-block-paragraph">see my other videos for hybrid programs.</p>



<p class="wp-block-paragraph">But so now the, you know, if the driver calls this function, then now we&#8217;re inside of</p>



<p class="wp-block-paragraph">and there&#8217;s a return statement so it&#8217;s a valid function I should preserve the</p>



<p class="wp-block-paragraph">registers that I&#8217;m going to use that are marked as Kali saved for the ABI so I&#8217;m</p>



<p class="wp-block-paragraph">going to go prologue and then an epilogue and I&#8217;m going to say push r12 and push</p>



<p class="wp-block-paragraph">r13 and then I&#8217;m going to pop r13 pop r12 they should be in reverse order if</p>



<p class="wp-block-paragraph">you&#8217;ve seen my other videos you&#8217;ll know this and the the thing about this</p>



<p class="wp-block-paragraph">the thing about this particular program is we&#8217;re going to run into stack alignment issues</p>



<p class="wp-block-paragraph">so uh if you don&#8217;t know about stack alignment and how it can crash your program without you</p>



<p class="wp-block-paragraph">realizing what&#8217;s wrong see my other videos but for now we&#8217;ll assume you know that and uh i i</p>



<p class="wp-block-paragraph">already know from running this program in advance that it&#8217;s going to be out of alignment by eight</p>



<p class="wp-block-paragraph">bytes so i&#8217;m just going to push an extra register onto the stack and that&#8217;s going to put it back</p>



<p class="wp-block-paragraph">I know it looks weird, but this is going to work.</p>



<p class="wp-block-paragraph">Let me get rid of this here.</p>



<p class="wp-block-paragraph">Okay, so.</p>



<p class="wp-block-paragraph">And then maybe if I can remember at the end of the video,</p>



<p class="wp-block-paragraph">I can just remove that extra push-pop pair,</p>



<p class="wp-block-paragraph">and you&#8217;ll see the program starts crashing.</p>



<p class="wp-block-paragraph">But at home, you can do it just to double check.</p>



<p class="wp-block-paragraph">So the first thing I really want to do is,</p>



<p class="wp-block-paragraph">after I push and pop,</p>



<p class="wp-block-paragraph">is save our incoming arguments.</p>



<p class="wp-block-paragraph">Remember, the first integer argument</p>



<p class="wp-block-paragraph">and the second integer argument,</p>



<p class="wp-block-paragraph">argument they come in as RDI and RSI in assembly per the ABI if both of these</p>



<p class="wp-block-paragraph">things are pointers it doesn&#8217;t matter what the data type is it could be</p>



<p class="wp-block-paragraph">pointing to anything including a double and these would still be considered</p>



<p class="wp-block-paragraph">integer arguments because well RDI and RSI are just going to be loaded up with</p>



<p class="wp-block-paragraph">memory locations which which are integers so I&#8217;m going to save our</p>



<p class="wp-block-paragraph">arguments to R12 and R13 now justifying our push and pop pair then I&#8217;m going to</p>



<p class="wp-block-paragraph">little welcome message so print a little welcome message again you don&#8217;t need to know about this</p>



<p class="wp-block-paragraph">function but it&#8217;s explained in other videos that I&#8217;ve already published we&#8217;re going to print our</p>



<p class="wp-block-paragraph">hello beginning message I&#8217;m getting nervous he needs to take a second poop sometimes it&#8217;s poopoo</p>



<p class="wp-block-paragraph">number two time for him and he&#8217;s not really just lying about a treat but he did go pee and poop</p>



<p class="wp-block-paragraph">But he did go pee and poop already.</p>



<p class="wp-block-paragraph">Okay, he just left and walked away.</p>



<p class="wp-block-paragraph">Okay, if he comes back, I&#8217;m letting him out this time.</p>



<p class="wp-block-paragraph">I&#8217;ll pause the video if he does it again.</p>



<p class="wp-block-paragraph">Okay, I&#8217;m pausing the video.</p>



<p class="wp-block-paragraph">No pee lied.</p>



<p class="wp-block-paragraph">He went outside, lifted up his little leg, and a couple of drops of pee came out.</p>



<p class="wp-block-paragraph">Now he&#8217;s staring at me like he deserves a treat.</p>



<p class="wp-block-paragraph">Sorry, buddy.</p>



<p class="wp-block-paragraph">I wish I could eat constantly all day long, too.</p>



<p class="wp-block-paragraph">But life isn&#8217;t always fair.</p>



<p class="wp-block-paragraph">isn&#8217;t always fair anyway let&#8217;s see I might even lined up on the camera</p>



<p class="wp-block-paragraph">anymore I don&#8217;t even know so we&#8217;re looking at this code here is going to</p>



<p class="wp-block-paragraph">print a welcome message let&#8217;s see if that actually works so I&#8217;m gonna do make</p>



<p class="wp-block-paragraph">run again make files are whoops what did I do loader dot asm what did I do what</p>



<p class="wp-block-paragraph">did I do I somehow copy pasted the wrong make file</p>



<p class="wp-block-paragraph">What&#8217;s the name of my source code file?</p>



<p class="wp-block-paragraph">It&#8217;s point.</p>



<p class="wp-block-paragraph">I guess I&#8217;ll just change it, and then it&#8217;ll probably work.</p>



<p class="wp-block-paragraph">It&#8217;s still in assembly module.</p>



<p class="wp-block-paragraph">Hopefully that didn&#8217;t mess it up too bad by copy-pasting the wrong source code.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">What is going on here?</p>



<p class="wp-block-paragraph">Floater.</p>



<p class="wp-block-paragraph">Oh, I need to change that.</p>



<p class="wp-block-paragraph">Hang on.</p>



<p class="wp-block-paragraph">Let me fix this.</p>



<p class="wp-block-paragraph">I don&#8217;t know if I&#8217;m going to edit this out.</p>



<p class="wp-block-paragraph">out. It&#8217;s fun to watch me struggle sometimes. There we go.</p>



<p class="wp-block-paragraph">Point.</p>



<p class="wp-block-paragraph">Alright, let&#8217;s give it another try.</p>



<p class="wp-block-paragraph">Oh no, star dot so no such file a directory. Dang it.</p>



<p class="wp-block-paragraph">Okay, now this seems to work. I may or may not have edited</p>



<p class="wp-block-paragraph">that out. I copy pasted the wrong source code into my make</p>



<p class="wp-block-paragraph">file. So I had to manually adjust it. Then I forgot to</p>



<p class="wp-block-paragraph">copy paste my library file into the build directory. So I had</p>



<p class="wp-block-paragraph">The driver sees my long as whatever.</p>



<p class="wp-block-paragraph">What&#8217;s going on?</p>



<p class="wp-block-paragraph">Print an alternate string begin.</p>



<p class="wp-block-paragraph">Oh, the driver is printing a bunch of stuff.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">I started to think, why does it look like the program has a lot of stuff going on?</p>



<p class="wp-block-paragraph">Oh, that&#8217;s the driver.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So the driver says it sees its long as 100.</p>



<p class="wp-block-paragraph">And then now we&#8217;re inside of the point module.</p>



<p class="wp-block-paragraph">So that&#8217;s the only thing we&#8217;ve done in assembly so far.</p>



<p class="wp-block-paragraph">so far then the driver has regained control maybe I should add a couple of</p>



<p class="wp-block-paragraph">new lines in there so I don&#8217;t get confused again we will do a C out and L</p>



<p class="wp-block-paragraph">and we&#8217;ll do two of those run the program again and then I won&#8217;t get</p>



<p class="wp-block-paragraph">confused about the messages okay so now we&#8217;re inside of the point module and</p>



<p class="wp-block-paragraph">nothing is happening so points let me get rid of the make file here and</p>



<p class="wp-block-paragraph">and we&#8217;re just printing a welcome message nothing else so now let&#8217;s print</p>



<p class="wp-block-paragraph">the received string so what am I talking about so we&#8217;re gonna print a prefix</p>



<p class="wp-block-paragraph">basically saying hey we received the following string right so if you look at</p>



<p class="wp-block-paragraph">the symbol message received string it&#8217;s just gonna say we&#8217;re now printing the</p>



<p class="wp-block-paragraph">received string and then it&#8217;ll print it so what are we actually printing we&#8217;re</p>



<p class="wp-block-paragraph">What is R12? R12 is a character pointer to the print me string. And so basically this</p>



<p class="wp-block-paragraph">function print null terminated string, it takes a character pointer. So we&#8217;re giving it a character</p>



<p class="wp-block-paragraph">pointer that we received. When point was called by the driver, notice how it gave a pointer to</p>



<p class="wp-block-paragraph">the C string. You know, all arrays are basically pointers. They&#8217;re just different syntactically</p>



<p class="wp-block-paragraph">just different syntactically sometimes so if i declare an array of some length and i give the</p>



<p class="wp-block-paragraph">symbol somewhere that symbol is really a character pointer so um by calling point with my c string</p>



<p class="wp-block-paragraph">i&#8217;m calling point inside of the assembly module with this character pointer so that means even</p>



<p class="wp-block-paragraph">though this c string is owned by the driver by the c plus plus module the assembly module has access</p>



<p class="wp-block-paragraph">So that means we should be able to print it right now already.</p>



<p class="wp-block-paragraph">So just the rest of it is just like giving a pointer.</p>



<p class="wp-block-paragraph">And notice how I&#8217;m not dereferencing R12.</p>



<p class="wp-block-paragraph">If I did dereferencing around R12, then we would be looking to that address and seeing what&#8217;s there,</p>



<p class="wp-block-paragraph">which wouldn&#8217;t work for printing a null terminated string.</p>



<p class="wp-block-paragraph">So let&#8217;s just run it again.</p>



<p class="wp-block-paragraph">I don&#8217;t know if you can hear him.</p>



<p class="wp-block-paragraph">This dude is growling at me still because he wants another treat.</p>



<p class="wp-block-paragraph">He just got denied.</p>



<p class="wp-block-paragraph">He&#8217;s trying to do it again.</p>



<p class="wp-block-paragraph">do it again. I let him outside people. He&#8217;s been outside like three times already and he just went</p>



<p class="wp-block-paragraph">out like two minutes ago. Okay. I love him so much. It hurts my heart and he knows eventually he&#8217;s</p>



<p class="wp-block-paragraph">going to break me because it hurts my heart or I&#8217;m like too distracted. It&#8217;s like, you know,</p>



<p class="wp-block-paragraph">pulling the crank on a slot machine in Vegas. You know, eventually something comes out.</p>



<p class="wp-block-paragraph">That&#8217;s what he does to me. I&#8217;ve accidentally trained him. So now printing the received</p>



<p class="wp-block-paragraph">Now printing the received string and notice how it prints.</p>



<p class="wp-block-paragraph">Hello, this is a C string owned by me.</p>



<p class="wp-block-paragraph">So our assembly module is able to print a C string that was created locally by a C++ module.</p>



<p class="wp-block-paragraph">So we&#8217;re handing around pointers.</p>



<p class="wp-block-paragraph">Nice.</p>



<p class="wp-block-paragraph">Can you hear me?</p>



<p class="wp-block-paragraph">He&#8217;s getting louder.</p>



<p class="wp-block-paragraph">So now let&#8217;s modify the incoming long.</p>



<p class="wp-block-paragraph">Can you shush your freaking pants, please?</p>



<p class="wp-block-paragraph">Shush your pants.</p>



<p class="wp-block-paragraph">shush your pants you know the sad thing also is he&#8217;s so old that he&#8217;s deaf now</p>



<p class="wp-block-paragraph">so he used to know what shush your pants meant it meant I&#8217;m not listening to you</p>



<p class="wp-block-paragraph">and you might as well stop because I&#8217;m not gonna do anything based on your</p>



<p class="wp-block-paragraph">harassment but now he can&#8217;t hear me say shush your pants so he just harasses me</p>



<p class="wp-block-paragraph">all day and all night okay um so I&#8217;m gonna copy paste a little bit more code</p>



<p class="wp-block-paragraph">Modify the incoming long.</p>



<p class="wp-block-paragraph">So remember again that the point function, it received a pointer to a long.</p>



<p class="wp-block-paragraph">We&#8217;re calling the long change me on the inside of this, but it&#8217;s coming in as R13.</p>



<p class="wp-block-paragraph">And if you notice what I&#8217;m doing here is I&#8217;m just saying let&#8217;s increase the long.</p>



<p class="wp-block-paragraph">So I&#8217;m going to dereference R13 because R13 is a pointer.</p>



<p class="wp-block-paragraph">So I&#8217;m saying let&#8217;s go to the memory and change the long that is inside of memory.</p>



<p class="wp-block-paragraph">And we have to specify that it is a keyword.</p>



<p class="wp-block-paragraph">it as a keyword so that we you know we don&#8217;t confuse the system the system might think are</p>



<p class="wp-block-paragraph">you modifying a keyword or like a double word or like a word like how big is your data all we know</p>



<p class="wp-block-paragraph">is it&#8217;s an integer because it&#8217;s the increase instruction so I&#8217;m saying we got a keyword you</p>



<p class="wp-block-paragraph">know a 64-bit integer sitting at that memory location I want you to dereference it and increase</p>



<p class="wp-block-paragraph">it and going back to the driver we&#8217;re providing a pointer to our long so the long starts off is 100</p>



<p class="wp-block-paragraph">and we&#8217;re just giving a pointer to it the next thing that we can do is we can</p>



<p class="wp-block-paragraph">ask the driver to print our own stuff actually you know what let&#8217;s run the program right now</p>



<p class="wp-block-paragraph">just to show that the driver can see the change in the long so i&#8217;m going to run it again notice how</p>



<p class="wp-block-paragraph">first when the driver says hello it sees its own long as 100 then we&#8217;re inside the assembly module</p>



<p class="wp-block-paragraph">long and then we return to the caller which is the driver notice how at the</p>



<p class="wp-block-paragraph">very end of the program the driver sees its long as being 101 so we were able to</p>



<p class="wp-block-paragraph">modify data that was owned by a different module just by passing pointers</p>



<p class="wp-block-paragraph">and de-referencing them okay cool so now the next thing that we should do is let&#8217;s</p>



<p class="wp-block-paragraph">ask the driver to print our own stuff that we own because remember if you go</p>



<p class="wp-block-paragraph">to the very top you know we own some stuff we own some we own a long we own</p>



<p class="wp-block-paragraph">float, right? So we want to be able to do something with that. So I&#8217;m going to copy paste this,</p>



<p class="wp-block-paragraph">ask the driver to print our own stuff. So I&#8217;m going to move three items inside of arguments</p>



<p class="wp-block-paragraph">for a function call. And then I&#8217;m going to make a function call calling the function,</p>



<p class="wp-block-paragraph">Hey driver, print this again, Hey driver, print this is actually owned by the C++ module.</p>



<p class="wp-block-paragraph">a pointer to a long and a pointer to a double remember even pointers to doubles are actually</p>



<p class="wp-block-paragraph">integers so they use the general purpose register so that&#8217;s the three arguments right there rdi rsi</p>



<p class="wp-block-paragraph">and rdx m and then we&#8217;re giving the first pointer is going to be the c string so message string</p>



<p class="wp-block-paragraph">inside asm so you can see that&#8217;s this right here and then the next pointer is the long</p>



<p class="wp-block-paragraph">inside ASM and the third is the float where did I just go I&#8217;m getting confused my dog is harassing</p>



<p class="wp-block-paragraph">me right now so bad notice how I&#8217;m not dereferencing so like if when we were increasing the incoming</p>



<p class="wp-block-paragraph">long before R13 was a pointer so we dereferenced while we increased so that we would increase the</p>



<p class="wp-block-paragraph">actual value and not the pointer and not the pointer&#8217;s memory location but here we&#8217;re not</p>



<p class="wp-block-paragraph">C++ module the actual pointers to our data. We don&#8217;t want to give it the data itself. We want</p>



<p class="wp-block-paragraph">to give pointers to the data so we&#8217;re not derefing with the brackets. So then we call it and when we</p>



<p class="wp-block-paragraph">get back in here it should just be able to print everything. So I&#8217;m going to run it one more time.</p>



<p class="wp-block-paragraph">We&#8217;re going to make it and run it and so now let&#8217;s see. So here we&#8217;re inside of our assembly module</p>



<p class="wp-block-paragraph">And then here the assembly module has just called on hey driver print this.</p>



<p class="wp-block-paragraph">Remember the C++ module doesn&#8217;t actually call this function.</p>



<p class="wp-block-paragraph">The assembly module calls it.</p>



<p class="wp-block-paragraph">So we&#8217;re like going back and forth.</p>



<p class="wp-block-paragraph">We&#8217;re kind of crisscrossing.</p>



<p class="wp-block-paragraph">So now the drivers print this function says we got the following string.</p>



<p class="wp-block-paragraph">Notice how that&#8217;s the string that is owned by assembly.</p>



<p class="wp-block-paragraph">So we define that inside of our data section in the assembly module.</p>



<p class="wp-block-paragraph">And then it prints the long.</p>



<p class="wp-block-paragraph">It prints it as hex.</p>



<p class="wp-block-paragraph">And it just sort of prints the value.</p>



<p class="wp-block-paragraph">it just sort of prints the value then it prints it as hex again and then prints at the value</p>



<p class="wp-block-paragraph">i think actually not hex i think this prints the memory location let&#8217;s double check real fast</p>



<p class="wp-block-paragraph">yeah so remember um in c plus plus i know this is not like a c plus plus video but um</p>



<p class="wp-block-paragraph">if the long is a pointer then if we just print it without dereferencing it we should see a memory</p>



<p class="wp-block-paragraph">location so it&#8217;s telling us uh that the long&#8217;s memory location is this and the doubles memory</p>



<p class="wp-block-paragraph">location is that and if you stare at those two numbers long enough and you understand hex which</p>



<p class="wp-block-paragraph">And do you understand hex, which you can see my other videos for?</p>



<p class="wp-block-paragraph">You&#8217;ll see that those memory locations are right next to each other because that&#8217;s the way we define them inside of assembly.</p>



<p class="wp-block-paragraph">So we now have the ability to have data that is owned by assembly and give it to C++ or C using pointers.</p>



<p class="wp-block-paragraph">No problem at all.</p>



<p class="wp-block-paragraph">And then the printing driver thing exits and then the actual driver regains control.</p>



<p class="wp-block-paragraph">And it just says that it sees it&#8217;s long as 101.</p>



<p class="wp-block-paragraph">it sees it&#8217;s long as 101 so uh yeah that&#8217;s that&#8217;s pretty much all i wanted to show you for this</p>



<p class="wp-block-paragraph">now you hopefully are an expert at passing data back and forth between various modules using</p>



<p class="wp-block-paragraph">pointers we&#8217;re not using references because references are like a little bit a little bit</p>



<p class="wp-block-paragraph">less compatible pointers are just really easy they totally work in assembly no problem</p>



<p class="wp-block-paragraph">one more thing i just wanted to show you real fast before we go even though there&#8217;s another</p>



<p class="wp-block-paragraph">video you should check out for stack alignment I just want you to see what</p>



<p class="wp-block-paragraph">happens if I remove this extra push-pop pair so now my stack is about eight</p>



<p class="wp-block-paragraph">bytes off of its previous alignment because you know we&#8217;re not pushing an</p>



<p class="wp-block-paragraph">extra eight byte value and somewhere inside of the let&#8217;s see print null</p>



<p class="wp-block-paragraph">terminated string and then the hey driver print this oh and then we go into</p>



<p class="wp-block-paragraph">like a bunch of C stuff the program should probably crash because anytime</p>



<p class="wp-block-paragraph">you use a GCC function or a GCC library or something like that the stack has to</p>



<p class="wp-block-paragraph">be aligned to 16 bytes so if it&#8217;s off by 8 then it&#8217;ll crash and how did I know</p>



<p class="wp-block-paragraph">that I needed this well I just ran it first and it crashed and then I added</p>



<p class="wp-block-paragraph">the extra push pop pair and it didn&#8217;t crash and I realized it was definitely</p>



<p class="wp-block-paragraph">one more time we should get a seg fault yeah we get a seg fault stack alignment oh no with no</p>



<p class="wp-block-paragraph">description of what&#8217;s going on if you were in gcc you could i mean sorry if you were in gdb you</p>



<p class="wp-block-paragraph">could probably figure that out eventually but why not just give it a try add another push pop pair</p>



<p class="wp-block-paragraph">run the program again with no other modifications now it totally works</p>



<p class="wp-block-paragraph">okay well uh i think that&#8217;s uh that&#8217;s all i have for this video thank you so much for watching i</p>



<p class="wp-block-paragraph">I hope you learned a little bit of stuff and you had a little bit of fun.</p>



<p class="wp-block-paragraph">I will see you in the next video.</p>



<p class="wp-block-paragraph">Hey everybody.</p>



<p class="wp-block-paragraph">Thanks for watching this video again from the bottom of my heart.</p>



<p class="wp-block-paragraph">I really appreciate it.</p>



<p class="wp-block-paragraph">I do hope you did learn something and have some fun.</p>



<p class="wp-block-paragraph">If you could do me a please, a small little favor,</p>



<p class="wp-block-paragraph">could you please subscribe and follow this channel or these videos</p>



<p class="wp-block-paragraph">or whatever it is you do on the current social media website</p>



<p class="wp-block-paragraph">that you&#8217;re looking at right now.</p>



<p class="wp-block-paragraph">It would really mean the world to me</p>



<p class="wp-block-paragraph">would really mean the world to me and it&#8217;ll help make more videos and grow</p>



<p class="wp-block-paragraph">this community so we&#8217;ll be able to do more videos longer videos better videos</p>



<p class="wp-block-paragraph">or just I&#8217;ll be able to keep making videos in general so please do do me a</p>



<p class="wp-block-paragraph">kindness and and subscribe you know sometimes I&#8217;m sleeping in the middle of</p>



<p class="wp-block-paragraph">the night and I just wake up because I know somebody subscribed or followed it</p>



<p class="wp-block-paragraph">just wakes me up and I get filled with joy that&#8217;s exactly what happens every</p>



<p class="wp-block-paragraph">single time so you could do it as a nice favor to me or you could you control me</p>



<p class="wp-block-paragraph">up in the middle of the night just subscribe and then I&#8217;ll just wake up I promise that&#8217;s what will</p>



<p class="wp-block-paragraph">happen also if you look at the middle of the screen right now you should see a QR code which</p>



<p class="wp-block-paragraph">you can scan in order to go to the website which I think is also named somewhere at the bottom of</p>



<p class="wp-block-paragraph">this video and it&#8217;ll take you to my main website where you can just kind of like see all the videos</p>



<p class="wp-block-paragraph">I published and the services and tutorials and things that I offer and all that good stuff and</p>



<p class="wp-block-paragraph">for</p>



<p class="wp-block-paragraph">Clarifications or errata or just future videos that you want to see please leave a comment or if you just want to say hey</p>



<p class="wp-block-paragraph">What&#8217;s up? What&#8217;s going on? You know, just send me a comment, whatever</p>



<p class="wp-block-paragraph">I also wake up for those in the middle of the night. I get I wake up in a cold sweat. I&#8217;m like this</p>



<p class="wp-block-paragraph">It would really it really mean the world to me. I would really appreciate it. So again, thank you so much for watching this video and</p>



<p class="wp-block-paragraph">darkness, which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-pointers-dereferencing-explained-hybrid-c-yasm-example/">x86-64 Assembly Pointers &amp; Dereferencing Explained &#8211; Hybrid C++/YASM Example</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/x86-64-assembly-pointers-dereferencing-explained-hybrid-c-yasm-example/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>x86-64 Assembly: Floating Point Registers Basics with YASM (MOVSD, MULSD, CVTSI2SD)</title>
		<link>https://www.NeuralLantern.com/x86-64-assembly-floating-point-registers-basics-with-yasm-movsd-mulsd-cvtsi2sd/</link>
					<comments>https://www.NeuralLantern.com/x86-64-assembly-floating-point-registers-basics-with-yasm-movsd-mulsd-cvtsi2sd/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 15 Feb 2026 03:15:33 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly language tutorial]]></category>
		<category><![CDATA[convert integer to float assembly]]></category>
		<category><![CDATA[cvtsi2sd]]></category>
		<category><![CDATA[double precision assembly]]></category>
		<category><![CDATA[ed jorgensen x86 book]]></category>
		<category><![CDATA[floating point registers]]></category>
		<category><![CDATA[movsd]]></category>
		<category><![CDATA[mulsd]]></category>
		<category><![CDATA[nasm yasm floating point]]></category>
		<category><![CDATA[stack alignment assembly]]></category>
		<category><![CDATA[system v abi xmm]]></category>
		<category><![CDATA[x86 assembly floating point]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[x86-64 linux assembly]]></category>
		<category><![CDATA[xmm registers]]></category>
		<category><![CDATA[xmm0]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=302</guid>

					<description><![CDATA[<p>Learn the basics of floating-point registers (XMM0–XMM15) in x86-64 assembly with YASM. Covers MOVSD, MULSD, CVTSI2SD, ABI rules, why floats return in XMM0 instead of RAX, saving/restoring around calls, and common stack alignment crashes when using printf.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-floating-point-registers-basics-with-yasm-movsd-mulsd-cvtsi2sd/">x86-64 Assembly: Floating Point Registers Basics with YASM (MOVSD, MULSD, CVTSI2SD)</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="x86-64 Assembly: Floating Point Registers Basics with YASM (MOVSD, MULSD, CVTSI2SD)" width="1380" height="776" src="https://www.youtube.com/embed/0Wcu_7LDw_w?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 thorough introduction to floating-point registers in x86-64 assembly using YASM.</p>



<p class="wp-block-paragraph">Learn why XMM0 is special, how to use MOVSD / MULSD / CVTSI2SD, why you must save floats around function calls, and how easy (or sneaky) stack alignment bugs can crash your program.</p>



<p class="wp-block-paragraph">Live coding + real examples converting integers to doubles and multiplying them.</p>



<p class="wp-block-paragraph">Great next step after basic integer assembly tutorials.</p>



<p class="wp-block-paragraph">00:00 Introduction to Floating Point Registers<br>00:28 Why Floating Point Uses Special Registers<br>01:35 Floating Point Return Value in XMM0<br>02:17 XMM Registers Overview XMM0 to XMM15<br>02:48 ABI Rules No Callee-Saved XMM Registers<br>03:16 128-bit XMM Registers Purpose and Size<br>04:00 Ed Jorgensen x86-64 Textbook Reference<br>05:03 Locating XMM Documentation in Textbook<br>05:20 Earthquake &#8211; I am going to die<br>06:24 Chapter 18 Floating Point Instructions<br>07:34 MOVSS vs MOVSD Single vs Double Precision<br>09:11 Understanding SS and SD Instruction Suffixes<br>10:58 MOVSD Example Register to Register<br>11:03 Conversion Instructions CVT Family<br>13:02 Floating Point Arithmetic ADDSD MULSD SUBSD<br>25:48 Program Demo User Input Section<br>26:01 Converting Integer to Double CVTSI2SD<br>26:29 Multiplying by Constant Float MULSD<br>28:56 Saving Result Printing Modified Float<br>31:38 Multiplying User Integer by User Float<br>33:54 Final Result Display Program Summary<br>35:19 Stack Alignment Crash Demonstration<br>36:24 Conclusion Key Takeaways<br>36:52 Outro Subscribe and Thanks</p>



<p class="wp-block-paragraph">=-=-=-=-=-=-=-=-=</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>



<li>Twitter / X: https://x.com/NeuralLantern</li>



<li>Rumble: https://rumble.com/c/c-3696939</li>



<li>BitChute: https://www.bitchute.com/channel/pg1Pvv5dN4Gt</li>



<li>Daily Motion: https://www.dailymotion.com/neurallantern</li>



<li>Minds: https://www.minds.com/neurallantern/</li>



<li>Odysee: https://odysee.com/@NeuralLantern:5</li>
</ul>



<p class="wp-block-paragraph">Please show your support!</p>



<ul class="wp-block-list">
<li>Buy me a coffee: https://ko-fi.com/neurallantern</li>



<li>Subscribe + Sharing on Social Media</li>



<li>Leave a comment or suggestion</li>



<li>Subscribe to Blog: https://www.NeuralLantern.com</li>



<li>Watching the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p class="wp-block-paragraph">Hey there! In this video we&#8217;re going to talk about pointers and dereferencing in a YASM x8664</p>



<p class="wp-block-paragraph">assembly program, also as a hybrid program so that assembly and C++ can talk to each other</p>



<p class="wp-block-paragraph">and send each other pointers and send each other data and things like that.</p>



<p class="wp-block-paragraph">for what pointers are.</p>



<p class="wp-block-paragraph">I&#8217;m going to write in C++ for a second.</p>



<p class="wp-block-paragraph">Suppose you have a pointer for an integer.</p>



<p class="wp-block-paragraph">We&#8217;ll call it P.</p>



<p class="wp-block-paragraph">Suppose you have an integer by itself.</p>



<p class="wp-block-paragraph">We&#8217;ll call it A.</p>



<p class="wp-block-paragraph">Let&#8217;s say that the value of A is 5.</p>



<p class="wp-block-paragraph">And if you wanted to say that P points to A,</p>



<p class="wp-block-paragraph">you could say P equals the address of A.</p>



<p class="wp-block-paragraph">I&#8217;ll put C++ at the top here.</p>



<p class="wp-block-paragraph">And so now if I set A to 6</p>



<p class="wp-block-paragraph">then I print P a dereference of P this is not like a full pointers tutorial</p>



<p class="wp-block-paragraph">but basically by changing a I&#8217;m changing what P thinks it sees as a value</p>



<p class="wp-block-paragraph">assuming ID reference it I could also let me do a print 6 here I could also</p>



<p class="wp-block-paragraph">just change the value through P I could say dereference P and I could say equals</p>



<p class="wp-block-paragraph">would actually print a seven right so you know you can have regular variables global variables</p>



<p class="wp-block-paragraph">whatever kind of you know memory stuff on the stack and to get a pointer to it you really just</p>



<p class="wp-block-paragraph">need to get its memory location in c++ it&#8217;s kind of easy syntactically you can see what&#8217;s happening</p>



<p class="wp-block-paragraph">in assembly you really just need the memory location stored somewhere you could store that</p>



<p class="wp-block-paragraph">variable that just simply stored the memory location of some other variable.</p>



<p class="wp-block-paragraph">You could have a 64-bit register store the value of a variable.</p>



<p class="wp-block-paragraph">Let&#8217;s say we have like a, I don&#8217;t know, my whatever, my number let&#8217;s say inside of assembly.</p>



<p class="wp-block-paragraph">I&#8217;ll do ASM here and we say it&#8217;s a quad word and it starts off as this number or whatever.</p>



<p class="wp-block-paragraph">So if you haven&#8217;t seen my previous videos, go see them for the basics of assembly and</p>



<p class="wp-block-paragraph">of assembly and linking and make files and all that stuff but you know if you</p>



<p class="wp-block-paragraph">have an assembly program and you have a data section and you define a global</p>



<p class="wp-block-paragraph">variable like this what you&#8217;re basically saying is I want to take this giant</p>



<p class="wp-block-paragraph">number and I want to write it into eight bytes that&#8217;s the DQ it says data quad</p>



<p class="wp-block-paragraph">word I want to write that giant number across eight bytes and then I want to</p>



<p class="wp-block-paragraph">get a pointer to it stored in the my number symbol so my number is not</p>



<p class="wp-block-paragraph">actually the value it&#8217;s a pointer to the value so you know later if you want to</p>



<p class="wp-block-paragraph">you know later if you want to move you know something into a register if you did this</p>



<p class="wp-block-paragraph">that would move the pointer into rax but if you did this</p>



<p class="wp-block-paragraph">with deref symbols after it or around it then you would move</p>



<p class="wp-block-paragraph">maybe i&#8217;ll put that into rex you&#8217;d move that actual number that we specified into rex</p>



<p class="wp-block-paragraph">into Rx. It&#8217;s important to understand also that pointers are integers even when we&#8217;re pointing to</p>



<p class="wp-block-paragraph">doubles. So for example sometimes people make this mistake they&#8217;ll say you know my double</p>



<p class="wp-block-paragraph">and they&#8217;ll say it&#8217;s a quad word meaning this is going to be a 64-bit double precision floating</p>



<p class="wp-block-paragraph">point number and they&#8217;ll do like 44.55 or whatever. So that is a double and it is in memory</p>



<p class="wp-block-paragraph">you know what is the symbol of my double remember it&#8217;s supposed to be just a</p>



<p class="wp-block-paragraph">pointer right it can&#8217;t be an actual double because a memory location is not</p>



<p class="wp-block-paragraph">a double a memory location is an integer so that means if you wanted to move a</p>



<p class="wp-block-paragraph">pointer into a register you would only be able to move the pointer into a</p>



<p class="wp-block-paragraph">regular general purpose register not a floating point register and you should</p>



<p class="wp-block-paragraph">use the regular movement instructions for just regular general purpose</p>



<p class="wp-block-paragraph">So keep that in mind if you see a signature like this like let&#8217;s say function F and we have</p>



<p class="wp-block-paragraph">You know, let&#8217;s say long a and long B and actually let&#8217;s do pointers</p>



<p class="wp-block-paragraph">Let&#8217;s say long pointer a and long pointer</p>



<p class="wp-block-paragraph">B and double pointer C all three of those arguments are actually 64 bit integers</p>



<p class="wp-block-paragraph">Because they&#8217;re all pointers even if one of the pointers points to adult a double</p>



<p class="wp-block-paragraph">double why did I say dull pointers aren&#8217;t dull they&#8217;re exciting okay so I&#8217;m gonna open up some</p>



<p class="wp-block-paragraph">code here real fast so usually I don&#8217;t explain my uh my driver I&#8217;m gonna explain it to you this time</p>



<p class="wp-block-paragraph">because it&#8217;s kind of doing a little bit more than my other videos um again if you don&#8217;t have uh the</p>



<p class="wp-block-paragraph">knowledge of how to make a make file see my other videos because that&#8217;s explained there for now I&#8217;m</p>



<p class="wp-block-paragraph">what we really need to do is write a driver and an assembly module for a</p>



<p class="wp-block-paragraph">hybrid program again hybrid programs covered in other videos so the driver is</p>



<p class="wp-block-paragraph">pretty easy I&#8217;m just going to copy paste it honestly here and then just kind of</p>



<p class="wp-block-paragraph">explain it to you the driver is pretty easy we&#8217;re going to do I O stream so we</p>



<p class="wp-block-paragraph">can print stuff we&#8217;re going to mark an external function called point as extern</p>



<p class="wp-block-paragraph">C so that just disables name mangling which means the C++ module will be able</p>



<p class="wp-block-paragraph">will be able to call on this function called point and it won&#8217;t expect that</p>



<p class="wp-block-paragraph">the point function has its name mangled like C++ does the reason being is that</p>



<p class="wp-block-paragraph">point is actually going to be in a side it&#8217;s going to be inside assembly where</p>



<p class="wp-block-paragraph">its name will not be mangled this disables the ability to overload but</p>



<p class="wp-block-paragraph">that&#8217;s okay we don&#8217;t care it&#8217;s going to take two pointers a pointer to a character</p>



<p class="wp-block-paragraph">and a pointer to a long since both of those are pointers they&#8217;re both</p>



<p class="wp-block-paragraph">64-bit integers even the character pointer and then we have a function that is internal to this</p>



<p class="wp-block-paragraph">module called hey driver print this remember we&#8217;re inside of the driver program right now</p>



<p class="wp-block-paragraph">so if you look at the bottom it&#8217;s just a function that takes in some pointers</p>



<p class="wp-block-paragraph">and then prints some stuff so it&#8217;s going to print like it&#8217;s going to print what the string is</p>



<p class="wp-block-paragraph">it&#8217;s going to print what the long is my dog&#8217;s growling at me i&#8217;m going to ignore him because</p>



<p class="wp-block-paragraph">i literally just let him pee and poop at this point now he&#8217;s harassing me for treats</p>



<p class="wp-block-paragraph">now he&#8217;s harassing me for treats he always does this okay so uh the string the long the double</p>



<p class="wp-block-paragraph">this function expects to receive three pointers to different data types it&#8217;s just going to print</p>



<p class="wp-block-paragraph">all of them and the point get it the point of this function is we&#8217;re going to go inside of</p>



<p class="wp-block-paragraph">the assembly module and then have the assembly module call on this function so that we can we</p>



<p class="wp-block-paragraph">can prove that we can have stuff sent from assembly to c plus plus or c using pointers</p>



<p class="wp-block-paragraph">using pointers we can have data sent over so anyway that&#8217;s why both of these</p>



<p class="wp-block-paragraph">are in here the point needs to be marked as no name mangling because point is</p>



<p class="wp-block-paragraph">inside of assembly which will not name mangle and then hey driver print this</p>



<p class="wp-block-paragraph">that needs to have name mangling disabled also so that the assembly</p>



<p class="wp-block-paragraph">module can call on this other than that we&#8217;re just basically inside of a main</p>



<p class="wp-block-paragraph">saying hey this is the c string we&#8217;re making a c string inside of the main function notice how</p>



<p class="wp-block-paragraph">this is a local variable so that c string is going to show up on the stack it&#8217;s going to show up in</p>



<p class="wp-block-paragraph">the area that is owned by main for main stack area same thing for my long that&#8217;s a local variable on</p>



<p class="wp-block-paragraph">the stack um and but then we can actually send pointers to those pieces of data to another</p>



<p class="wp-block-paragraph">function in another module you don&#8217;t have to only transport globals or stuff on the heap</p>



<p class="wp-block-paragraph">or stuff on the heap, you can transport pointers to local variables. Just make sure that by the</p>



<p class="wp-block-paragraph">time this function finishes, then nowhere else is actually using that data because,</p>



<p class="wp-block-paragraph">well, being on the stack, once main function or once any function finishes, then its portion of</p>



<p class="wp-block-paragraph">the stack will be cleaned up and removed and it&#8217;ll be junk data. You&#8217;ll probably get a seg fault.</p>



<p class="wp-block-paragraph">But for now, we&#8217;re not going to use anything on the stack. We&#8217;re not going to use these local</p>



<p class="wp-block-paragraph">just going to use them quickly on this call to point and then we&#8217;re going to return to the</p>



<p class="wp-block-paragraph">operating system and finish the program. So that&#8217;s the driver. Now the hard part. Let&#8217;s do this in</p>



<p class="wp-block-paragraph">assembly. So for starters, I&#8217;m going to make a data section and just explain it to you very,</p>



<p class="wp-block-paragraph">very quickly. Again, if you don&#8217;t understand the basics of YASM x86-64 assembly, did I mention</p>



<p class="wp-block-paragraph">that that&#8217;s what this language is at the beginning of the video? I guess I should put that in the</p>



<p class="wp-block-paragraph">put that in the description or record an announcement that I can tack on at the beginning</p>



<p class="wp-block-paragraph">or something. Anyway, so if you don&#8217;t understand how to do this, see my other videos, but basically</p>



<p class="wp-block-paragraph">we&#8217;re going to make a data section. We&#8217;re going to define some strings. Here&#8217;s like an announcement.</p>



<p class="wp-block-paragraph">Oh, we&#8217;re inside of, you know, the module now, the assembly module. And now we&#8217;re going to print</p>



<p class="wp-block-paragraph">the received string. And then we&#8217;re going to make a string that is owned by assembly, which we can</p>



<p class="wp-block-paragraph">into C++ when we call the function inside of the driver.</p>



<p class="wp-block-paragraph">So this string is owned by the assembly module.</p>



<p class="wp-block-paragraph">Notice how these are null terminated strings.</p>



<p class="wp-block-paragraph">I just have like a comma zero there,</p>



<p class="wp-block-paragraph">which means I have some extra functions</p>



<p class="wp-block-paragraph">I&#8217;m gonna paste in that we&#8217;re not really gonna talk about</p>



<p class="wp-block-paragraph">because they&#8217;ve been discussed in other videos</p>



<p class="wp-block-paragraph">just so that we can print null terminated strings.</p>



<p class="wp-block-paragraph">Then I&#8217;ve got a new line here,</p>



<p class="wp-block-paragraph">you know, carriage return line feed.</p>



<p class="wp-block-paragraph">And then I&#8217;ve just got some numbers</p>



<p class="wp-block-paragraph">that are owned by the assembly module.</p>



<p class="wp-block-paragraph">Then I&#8217;ve got a system write call,</p>



<p class="wp-block-paragraph">call code one for the system call writes and file descriptor standard output so I</p>



<p class="wp-block-paragraph">can print just to the terminal again if you don&#8217;t understand this see my other</p>



<p class="wp-block-paragraph">videos so now let&#8217;s start the actual text section so this is where our</p>



<p class="wp-block-paragraph">instructions start so we got the text section here and we&#8217;re going to use some</p>



<p class="wp-block-paragraph">external symbols don&#8217;t worry about these I&#8217;m just using my own little library to</p>



<p class="wp-block-paragraph">and input integers if you have access to this library use it if you don&#8217;t if you&#8217;re watching</p>



<p class="wp-block-paragraph">at home and you don&#8217;t have this library then that&#8217;s fine you can use you know printf or</p>



<p class="wp-block-paragraph">scanf or something like that to get and print floats from and to the user</p>



<p class="wp-block-paragraph">but yeah I&#8217;m just using that and then I&#8217;m marking an external function here called hey driver print</p>



<p class="wp-block-paragraph">this if you recall the driver module has a function called hey driver print this so</p>



<p class="wp-block-paragraph">just allows my assembly code to call on that external function. Okay now next</p>



<p class="wp-block-paragraph">piece of code. This is going to be… actually I&#8217;m going to paste the print</p>



<p class="wp-block-paragraph">null terminated string function and related code because it&#8217;s just like a</p>



<p class="wp-block-paragraph">big giant mess and we&#8217;re mostly going to ignore it. So just to show you what I&#8217;m</p>



<p class="wp-block-paragraph">doing here I have a function called print null terminated string so that I</p>



<p class="wp-block-paragraph">can print these strings up here and then I have it rely on a function called</p>



<p class="wp-block-paragraph">string length that I have implemented up here and all it does is just</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-floating-point-registers-basics-with-yasm-movsd-mulsd-cvtsi2sd/">x86-64 Assembly: Floating Point Registers Basics with YASM (MOVSD, MULSD, CVTSI2SD)</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-floating-point-registers-basics-with-yasm-movsd-mulsd-cvtsi2sd/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Implement strlen for Null-Terminated Strings in x86-64 Assembly (YASM)</title>
		<link>https://www.NeuralLantern.com/implement-strlen-for-null-terminated-strings-in-x86-64-assembly-yasm/</link>
					<comments>https://www.NeuralLantern.com/implement-strlen-for-null-terminated-strings-in-x86-64-assembly-yasm/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 08 Feb 2026 01:59:27 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[abi register preservation]]></category>
		<category><![CDATA[assembly language tutorial]]></category>
		<category><![CDATA[assembly length function]]></category>
		<category><![CDATA[assembly string handling]]></category>
		<category><![CDATA[assembly while loop]]></category>
		<category><![CDATA[c strings assembly]]></category>
		<category><![CDATA[callee saved registers]]></category>
		<category><![CDATA[learn assembly 2025]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[NASM vs YASM]]></category>
		<category><![CDATA[null terminated string]]></category>
		<category><![CDATA[reverse engineering]]></category>
		<category><![CDATA[strlen assembly]]></category>
		<category><![CDATA[sys_write tutorial]]></category>
		<category><![CDATA[systems programming]]></category>
		<category><![CDATA[write syscall assembly]]></category>
		<category><![CDATA[x86 assembly linux]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[x86-64 linux assembly]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=298</guid>

					<description><![CDATA[<p>Learn to implement strlen from scratch in x86-64 assembly using YASM. We walk through building a null-terminated string length function with a while loop, proper register preservation, and ABI compliance, then use the length to print the string efficiently via sys_write.</p>
<p>The post <a href="https://www.NeuralLantern.com/implement-strlen-for-null-terminated-strings-in-x86-64-assembly-yasm/">Implement strlen for Null-Terminated Strings in x86-64 Assembly (YASM)</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Implement strlen for Null-Terminated Strings in x86-64 Assembly (YASM)" width="1380" height="776" src="https://www.youtube.com/embed/KQaIEBP6Qp4?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">Learn how to write your own strlen function in x86-64 assembly (YASM) that finds the length of a null-terminated string using a simple while loop.</p>



<p class="wp-block-paragraph">We preserve the proper registers, follow the ABI, compute the length safely, and then use that length to print the full string efficiently with a single sys_write call.</p>



<p class="wp-block-paragraph">Great for anyone studying low-level programming, operating systems, or wanting to understand C strings at the assembly level.</p>



<p class="wp-block-paragraph">00:00:00 Introduction to implementing string length in assembly<br>00:00:25 What are null-terminated strings and why they exist<br>00:01:59 Pre-computing length vs using null terminators<br>00:02:53 How the null byte (0) actually works in memory<br>00:04:14 Naive approach: printing one character at a time<br>00:05:20 Goal: efficient printing using computed length<br>00:06:00 Program structure overview – two main functions<br>00:06:32 Data section: defining null-terminated strings<br>00:08:19 Additional strings for output (prefix, CRLF)<br>00:09:15 Text section start and global looper function<br>00:10:44 Preserving callee-saved registers (ABI prologue)<br>00:11:28 Calling print_null_terminated_string<br>00:12:43 Simple crlf printing helper function<br>00:13:10 print_null_terminated_string function signature<br>00:14:31 Prologue for print_null_terminated_string<br>00:15:44 Saving arguments and calling strlen<br>00:17:12 Using sys_write with computed length<br>00:18:19 string_length (strlen) function begins<br>00:19:20 Prologue and fake return value testing<br>00:20:44 Planning the while loop in C-like pseudocode<br>00:21:33 While loop initialization (pointer and counter)<br>00:24:23 Loop top: check for null terminator<br>00:26:23 Loop body: increment pointer and counter<br>00:27:37 Done label and return length in RAX<br>00:28:29 First successful run – full string printed<br>00:29:30 Adding direct strlen call and length printing<br>00:31:02 Final run showing both string and its length (54)<br>00:31:53 Summary – benefits of computed length printing<br>00:32:59 Improving loop structure (better jump pattern)<br>00:34:07 Final improved loop verification<br>00:35:03 Closing thoughts and thanks<br>00:35:27 Outro, call to subscribe, website mention</p>



<p class="wp-block-paragraph">=-=-=-=-=-=-=-=-=</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>



<li>Twitter / X: https://x.com/NeuralLantern</li>



<li>Rumble: https://rumble.com/c/c-3696939</li>



<li>BitChute: https://www.bitchute.com/channel/pg1Pvv5dN4Gt</li>



<li>Daily Motion: https://www.dailymotion.com/neurallantern</li>



<li>Minds: https://www.minds.com/neurallantern/</li>



<li>Odysee: https://odysee.com/@NeuralLantern:5</li>
</ul>



<p class="wp-block-paragraph">Please show your support!</p>



<ul class="wp-block-list">
<li>Buy me a coffee: https://ko-fi.com/neurallantern</li>



<li>Subscribe + Sharing on Social Media</li>



<li>Leave a comment or suggestion</li>



<li>Subscribe to Blog: https://www.NeuralLantern.com</li>



<li>Watching the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p class="wp-block-paragraph">Hey there, in this video, I&#8217;m going to show you how to implement the function string length.</p>



<p class="wp-block-paragraph">So you can find the length of a null terminated string in a Yasm x86-64 assembly program.</p>



<p class="wp-block-paragraph">Although if you&#8217;re using a different assembly language or different architecture, this video</p>



<p class="wp-block-paragraph">will probably still be useful to you because the concepts are going to be the same.</p>



<p class="wp-block-paragraph">So what am I talking about?</p>



<p class="wp-block-paragraph">I&#8217;m not going to be around here with this.</p>



<p class="wp-block-paragraph">So in a previous video, I discussed null terminated strings.</p>



<p class="wp-block-paragraph">I should also point out that a lot of basic knowledge is going to be skipped in this video</p>



<p class="wp-block-paragraph">because I&#8217;ve explained it in other videos.</p>



<p class="wp-block-paragraph">For example, if you don&#8217;t know how to compile, link, assemble, write a basic assembly program,</p>



<p class="wp-block-paragraph">write a make file and so forth, then you should see my other videos first.</p>



<p class="wp-block-paragraph">I&#8217;ve also already published a video about null terminated strings, but I&#8217;ll just do</p>



<p class="wp-block-paragraph">it again here since that&#8217;s in the title of the video.</p>



<p class="wp-block-paragraph">of the video so imagine you have a string and it&#8217;s hello right so under the hood uh the string is</p>



<p class="wp-block-paragraph">probably a collection of characters on some level so we&#8217;ll just say this is h e uh l</p>



<p class="wp-block-paragraph">l o right um when you&#8217;re printing</p>



<p class="wp-block-paragraph">it&#8217;s unlikely that your entire memory stick is just done like it just you&#8217;re at the very end of your memory by the time that O hits</p>



<p class="wp-block-paragraph">So that means you need some way of understanding when the string ends because if the memory is not over at that point</p>



<p class="wp-block-paragraph">There could probably be some junk data at the end of it</p>



<p class="wp-block-paragraph">You know like a bunch of other random letters or you can even imagine these as just you know</p>



<p class="wp-block-paragraph">One byte that&#8217;s not one byte one byte numbers that just go on and on and on forever for the entirety of your RAM stick</p>



<p class="wp-block-paragraph">of your RAM stick and you have to know how do we actually stop at the O. One thing that you can do</p>



<p class="wp-block-paragraph">is just pre-compute the length of the string so we do that in assembly a lot before we know how</p>



<p class="wp-block-paragraph">to scan for null terminators. We&#8217;ll say all right well that string is just five long so I&#8217;ll tell</p>



<p class="wp-block-paragraph">the system I want you to print five characters starting at that memory location wherever the H is</p>



<p class="wp-block-paragraph">and then the system knows okay I&#8217;ll just you know print the H-E-L-L-O and just stop after that.</p>



<p class="wp-block-paragraph">null terminated strings are a lot more convenient because you don&#8217;t have to pre-compute the strings.</p>



<p class="wp-block-paragraph">I mean, maybe your user entered a string.</p>



<p class="wp-block-paragraph">Maybe you have a lot of strings or they change quite often.</p>



<p class="wp-block-paragraph">Maybe you have like a multinational program that has tons of translations,</p>



<p class="wp-block-paragraph">or I think multilingual is probably the better word for that.</p>



<p class="wp-block-paragraph">But it can be a pain in the butt to constantly compute the length of strings in advance.</p>



<p class="wp-block-paragraph">So with a null terminated string, you basically just say,</p>



<p class="wp-block-paragraph">that I want to print and I&#8217;m just going to stick actually the number zero at the end of the string.</p>



<p class="wp-block-paragraph">I&#8217;ll leave the junk data there just to let you know that there is some stuff happening in memory.</p>



<p class="wp-block-paragraph">Notice how this zero, it is not actually the character that looks like a zero to a human.</p>



<p class="wp-block-paragraph">That&#8217;s actually a totally different code than just zero. So you can imagine just an actual zero here.</p>



<p class="wp-block-paragraph">You know, each of these characters that a human would look at has a number underneath it.</p>



<p class="wp-block-paragraph">You know, this H is not really an H.</p>



<p class="wp-block-paragraph">It&#8217;s just some number between 0 and 255 if we&#8217;re talking about ASCII.</p>



<p class="wp-block-paragraph">The E is a different number and so forth.</p>



<p class="wp-block-paragraph">So if we just put the literal number 0 in our data,</p>



<p class="wp-block-paragraph">or if you want to quote this inside of a single quote,</p>



<p class="wp-block-paragraph">you can do, I think, slash 0 just to let the compiler know</p>



<p class="wp-block-paragraph">that you intend to have the number 0 there</p>



<p class="wp-block-paragraph">instead of something that looks like the number 0, you know, the character.</p>



<p class="wp-block-paragraph">you know the character but anyways the point is we just have to stick a zero at the end</p>



<p class="wp-block-paragraph">of the string we call it a null terminator because zero is also you know an alias for null</p>



<p class="wp-block-paragraph">whenever you have a null pointer or you assign null to a memory location or a pointer or something</p>



<p class="wp-block-paragraph">you know it&#8217;s zero basically under the hood so a zero will terminate it&#8217;ll be like a token to let</p>



<p class="wp-block-paragraph">us know that the string is finished and so since zero is also considered null we&#8217;ll say it&#8217;s a</p>



<p class="wp-block-paragraph">we&#8217;ll say it&#8217;s a null terminator.</p>



<p class="wp-block-paragraph">It&#8217;s a basic idea for null terminators.</p>



<p class="wp-block-paragraph">Now the question is, how do we actually know when to stop?</p>



<p class="wp-block-paragraph">Well, the first thing that you could do if you&#8217;re trying to write a program that is highly inefficient,</p>



<p class="wp-block-paragraph">which I&#8217;ve definitely done before, is you could just print one character at a time.</p>



<p class="wp-block-paragraph">You use a for loop.</p>



<p class="wp-block-paragraph">You start at the very beginning of your string, you know, a pointer,</p>



<p class="wp-block-paragraph">whatever the user gave you as like this is the first character.</p>



<p class="wp-block-paragraph">We&#8217;ll just print that letter, and then we&#8217;ll go on to the next letter.</p>



<p class="wp-block-paragraph">the next letter and before we print it actually before we print the first letter even before we</p>



<p class="wp-block-paragraph">print this letter we&#8217;ll uh we&#8217;ll say is this like a regular character or is this a null terminator</p>



<p class="wp-block-paragraph">is this a zero if it&#8217;s not a zero we print that character if it is a zero we terminate the loop</p>



<p class="wp-block-paragraph">and then we go through every character one by one just you know checking and printing checking and</p>



<p class="wp-block-paragraph">printing checking and printing unfortunately that&#8217;s kind of inefficient because every time</p>



<p class="wp-block-paragraph">you call a print you know you&#8217;re you&#8217;re calling on a function you&#8217;re asking the system to do some</p>



<p class="wp-block-paragraph">for you and it would be a lot better if we could just flush the whole string at</p>



<p class="wp-block-paragraph">the same time but but know how long the string was that would increase our</p>



<p class="wp-block-paragraph">efficiency so the program that we&#8217;re going to write together is basically</p>



<p class="wp-block-paragraph">going to use our knowledge of a while loop which I&#8217;ve explained in other</p>



<p class="wp-block-paragraph">videos already so see those other videos if you don&#8217;t know how to do while loops</p>



<p class="wp-block-paragraph">in Yasm we&#8217;re going to use our knowledge of a while loop to sort of scan the</p>



<p class="wp-block-paragraph">string real fast just you know kind of scan it and figure out how far into the</p>



<p class="wp-block-paragraph">far into the string until we see a null terminator and use that to determine what is the length of</p>



<p class="wp-block-paragraph">the string. At that point, we can use a system call in YASM, in assembly, to just say, I want you to</p>



<p class="wp-block-paragraph">print this sequence of characters and here&#8217;s the length and then let the system worry about</p>



<p class="wp-block-paragraph">efficiency. So with that said, let&#8217;s look at some code. Okay, it&#8217;s just going to be a simple while</p>



<p class="wp-block-paragraph">loop. What we&#8217;re going to need to do is break this up into two parts. The first part is going to be</p>



<p class="wp-block-paragraph">the first part is going to be a function called string length which you&#8217;ve probably already seen</p>



<p class="wp-block-paragraph">in c if you program in c or c plus plus the second function is going to be called print null terminated</p>



<p class="wp-block-paragraph">string which will just ask string length what the length of the string is first and then actually</p>



<p class="wp-block-paragraph">print it with the system call so let me uh i guess let me start off with my data section here</p>



<p class="wp-block-paragraph">to print I&#8217;m gonna copy paste that for my solution again this is not a not an</p>



<p class="wp-block-paragraph">assembly basics video so if you don&#8217;t understand what I&#8217;m doing you should</p>



<p class="wp-block-paragraph">watch my other videos first I&#8217;m assuming you know how to make a data section by</p>



<p class="wp-block-paragraph">now we&#8217;ll put some C strings I&#8217;m just gonna make one null terminated string</p>



<p class="wp-block-paragraph">actually I guess I&#8217;m making two but the focus of this program is just the first</p>



<p class="wp-block-paragraph">one I&#8217;m calling it null terminated string and in assembly it&#8217;s pretty easy</p>



<p class="wp-block-paragraph">you just make it a you know a character array just like a sequence of bytes with</p>



<p class="wp-block-paragraph">a sequence of bytes with this DB meaning data bytes.</p>



<p class="wp-block-paragraph">And I can just put a quoted string like this.</p>



<p class="wp-block-paragraph">No problem.</p>



<p class="wp-block-paragraph">As many characters as I want.</p>



<p class="wp-block-paragraph">I can start injecting specific ASCII values if I wanted to</p>



<p class="wp-block-paragraph">or byte values if I wanted to just by putting a comma</p>



<p class="wp-block-paragraph">and then a number.</p>



<p class="wp-block-paragraph">So I could do something like this.</p>



<p class="wp-block-paragraph">I could do like, you know, 47, you know, 49, you know, 50, whatever.</p>



<p class="wp-block-paragraph">If I knew the ASCII codes for the characters,</p>



<p class="wp-block-paragraph">fortunately, I don&#8217;t need to.</p>



<p class="wp-block-paragraph">normally into the double quoted area but then i need to be able to put a null terminator at the</p>



<p class="wp-block-paragraph">end of my string because it&#8217;s not going to happen automatically so then i am going to do comma zero</p>



<p class="wp-block-paragraph">and you&#8217;ll end up with something like this like if i guess if we look at the previous example real</p>



<p class="wp-block-paragraph">fast i&#8217;ll call this a hello string just so that you see some similarity from what we just looked</p>



<p class="wp-block-paragraph">a notepad thing would just be typing the word hello and then putting comma zero.</p>



<p class="wp-block-paragraph">So it is now a null terminated string and it looks just like this inside of system memory.</p>



<p class="wp-block-paragraph">Well, not just like that.</p>



<p class="wp-block-paragraph">There would be numbers where the letters are, but you know, that&#8217;s basically what we have created.</p>



<p class="wp-block-paragraph">And then of course there&#8217;s junk data afterwards, but we don&#8217;t really care about that.</p>



<p class="wp-block-paragraph">You know, we&#8217;re just going to ignore it with the null terminator.</p>



<p class="wp-block-paragraph">So I&#8217;m going to erase that since we&#8217;re not just going to print the word hello.</p>



<p class="wp-block-paragraph">We have a null terminated string here and then after we print the null terminated string</p>



<p class="wp-block-paragraph">I&#8217;m just going to print out what was the length of the string.</p>



<p class="wp-block-paragraph">So this is a prefix string where it&#8217;s just, you know, it&#8217;s a prettier program.</p>



<p class="wp-block-paragraph">The program is going to say the null terminated string&#8217;s length was something.</p>



<p class="wp-block-paragraph">And then we&#8217;re going to use the null terminated string printer to print that also.</p>



<p class="wp-block-paragraph">Convenient, right?</p>



<p class="wp-block-paragraph">And then I&#8217;m going to actually print the number.</p>



<p class="wp-block-paragraph">Then we have this down here, crlf, which is just printing a new line in the terminal.</p>



<p class="wp-block-paragraph">That&#8217;s character code 13 and then 10 and then a null terminator so that we can use the null terminated string printer again.</p>



<p class="wp-block-paragraph">And then we&#8217;re going to use system call code 1 to print a standard output right here.</p>



<p class="wp-block-paragraph">If you don&#8217;t understand that, then see my other videos.</p>



<p class="wp-block-paragraph">But let&#8217;s move on to the text section where all our instructions will go.</p>



<p class="wp-block-paragraph">Okay, so now the instructions begin in our text section right here.</p>



<p class="wp-block-paragraph">section.text and I&#8217;m using an external symbol this video is not about this</p>



<p class="wp-block-paragraph">library here but basically I have a library that will help me print integers</p>



<p class="wp-block-paragraph">you don&#8217;t need to worry about that you could imagine well I guess in your</p>



<p class="wp-block-paragraph">example when you&#8217;re practicing if you don&#8217;t have this library you could just</p>



<p class="wp-block-paragraph">not print the length of the string and just use it only and it all should still</p>



<p class="wp-block-paragraph">work or you could hard code the thing that you&#8217;re printing if you really</p>



<p class="wp-block-paragraph">wanted to. Okay, so I&#8217;m just going to continue on here. Now let&#8217;s do our entry point. So again,</p>



<p class="wp-block-paragraph">this is not a video about hybrid programs. Just assume that there is another module in my program.</p>



<p class="wp-block-paragraph">It&#8217;s a C++ module. It&#8217;s got the main function, you know, for the entry point for a hybrid program,</p>



<p class="wp-block-paragraph">and it&#8217;ll just call on my looper function. So that&#8217;s why I&#8217;m marking a looper as global.</p>



<p class="wp-block-paragraph">So my other module can call it. And well, it is a function that needs to return. So I&#8217;m going to</p>



<p class="wp-block-paragraph">to return so i&#8217;m going to put ret at the end of it and you can see here i left myself a note saying</p>



<p class="wp-block-paragraph">i&#8217;m going to use r12 to remember the length of the string so that i can print it back to the user</p>



<p class="wp-block-paragraph">so that means i have to preserve r12 for the caller because the abi or the application binary</p>



<p class="wp-block-paragraph">interface says that r12 is a callie saved register and if you don&#8217;t respect the abi</p>



<p class="wp-block-paragraph">the abi is not going to respect you your program is going to end up crashing eventually</p>



<p class="wp-block-paragraph">So I&#8217;m just going to do a push pop pair to preserve R12.</p>



<p class="wp-block-paragraph">Oops, prologue and call that epilogue.</p>



<p class="wp-block-paragraph">Okay. So we got a push pop pair. We got a return statement.</p>



<p class="wp-block-paragraph">This program should probably do nothing so far. So let&#8217;s run it and see,</p>



<p class="wp-block-paragraph">just make sure that it at least compiles.</p>



<p class="wp-block-paragraph">So I&#8217;m going to say clear and make run running the program.</p>



<p class="wp-block-paragraph">Hello from the driver. You don&#8217;t know that the driver has that.</p>



<p class="wp-block-paragraph">that the driver has that. This is not a driver video. And then the driver regains control because</p>



<p class="wp-block-paragraph">nothing happened inside of the assembly module. We just basically looper got called and then we</p>



<p class="wp-block-paragraph">preserved R12 and then restored it and then we did nothing. Okay, so now let&#8217;s make a call to</p>



<p class="wp-block-paragraph">print null terminated string. We have to make another function for this, but right now this is</p>



<p class="wp-block-paragraph">just the call. So the name of the function that we&#8217;re going to write is called print null terminated</p>



<p class="wp-block-paragraph">it it will call on the string length function to figure out how long the string is then it will use</p>



<p class="wp-block-paragraph">a simple system call to print the whole string giving the length to the system call it also takes</p>



<p class="wp-block-paragraph">two arguments the first argument is a pointer to the null terminated string so that&#8217;s just that</p>



<p class="wp-block-paragraph">symbol we defined up above remember when you define variables up in the data section then</p>



<p class="wp-block-paragraph">these symbols tend to be pointers so that symbol is a pointer to the h basically or just the memory</p>



<p class="wp-block-paragraph">that h is sitting in ram then the second argument that it wants is uh is where we&#8217;re going to print</p>



<p class="wp-block-paragraph">it so we&#8217;re just going to print it to standard output um which is just file descriptor number one</p>



<p class="wp-block-paragraph">so again if you don&#8217;t understand arguments or you know file descriptors or function calls</p>



<p class="wp-block-paragraph">see my other videos because i&#8217;ve explained those already anyway so we&#8217;re going to call</p>



<p class="wp-block-paragraph">print null terminated string then we&#8217;re going to call on crlf which will just print a new line</p>



<p class="wp-block-paragraph">So now maybe we should implement, well, let&#8217;s copy paste crlf so that I can implement the</p>



<p class="wp-block-paragraph">other function a little bit more slowly.</p>



<p class="wp-block-paragraph">What does crlf do?</p>



<p class="wp-block-paragraph">It literally just asks the print null terminated string function to just print a crlf for us.</p>



<p class="wp-block-paragraph">So it&#8217;s very, very simple.</p>



<p class="wp-block-paragraph">Here&#8217;s the signature.</p>



<p class="wp-block-paragraph">Nothing much to it.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">Now, a little bit more complicated is the print null terminated string function.</p>



<p class="wp-block-paragraph">So in our looper, we&#8217;re going to print the null terminated string.</p>



<p class="wp-block-paragraph">We have to have a function that actually does that.</p>



<p class="wp-block-paragraph">So that&#8217;s going to be this one right here.</p>



<p class="wp-block-paragraph">Here&#8217;s the signature that I&#8217;ve chosen for my print null terminated string function.</p>



<p class="wp-block-paragraph">Basically, I want to receive a character pointer to the first character in the string that we&#8217;re going to print.</p>



<p class="wp-block-paragraph">And then a file handle designating where we&#8217;re going to print it.</p>



<p class="wp-block-paragraph">The reason I want to receive the file handle is so I could print a standard output or standard error.</p>



<p class="wp-block-paragraph">or standard error, or I could print to a file,</p>



<p class="wp-block-paragraph">like whatever I want to do.</p>



<p class="wp-block-paragraph">You don&#8217;t have to have that in there, but it&#8217;s nice.</p>



<p class="wp-block-paragraph">Anyway, so we have this function set up.</p>



<p class="wp-block-paragraph">Notice how my notes that I left for myself</p>



<p class="wp-block-paragraph">is that I&#8217;m gonna use R12</p>



<p class="wp-block-paragraph">to remember the incoming C string pointer argument,</p>



<p class="wp-block-paragraph">and I&#8217;m gonna use R13 to remember the file handle.</p>



<p class="wp-block-paragraph">Remember, it&#8217;s probably not a good idea</p>



<p class="wp-block-paragraph">to just let the incoming arguments</p>



<p class="wp-block-paragraph">stay in their original registers,</p>



<p class="wp-block-paragraph">original registers because those registers tend to get overwritten as you do system calls or</p>



<p class="wp-block-paragraph">calls to any other function. So I&#8217;m just going to grab them real fast into R12 and R13. And then R14</p>



<p class="wp-block-paragraph">is the string&#8217;s length, which I&#8217;m going to compute with a call to the function called string length.</p>



<p class="wp-block-paragraph">So just three things to remember. And that&#8217;s it. So that means I&#8217;m going to have to preserve those</p>



<p class="wp-block-paragraph">Okay, so we&#8217;re going to do a prologue to preserve those registers.</p>



<p class="wp-block-paragraph">And then at the very end, we&#8217;re going to do an epilogue where we restore those registers.</p>



<p class="wp-block-paragraph">Oh, I think I already overwrote my return statement from the previous function.</p>



<p class="wp-block-paragraph">I think I did that in the last video and I was a little confused as to what was wrong.</p>



<p class="wp-block-paragraph">So make sure you don&#8217;t accidentally overwrite or push down your return instructions.</p>



<p class="wp-block-paragraph">Let me just double check here.</p>



<p class="wp-block-paragraph">Looper&#8217;s got return.</p>



<p class="wp-block-paragraph">Print and alternated string has got a return.</p>



<p class="wp-block-paragraph">string has got a return.</p>



<p class="wp-block-paragraph">CRLF has a return.</p>



<p class="wp-block-paragraph">What the heck did I do?</p>



<p class="wp-block-paragraph">Oh, I think I copy pasted in a bizarre place.</p>



<p class="wp-block-paragraph">That&#8217;s probably what happened because the epilog for for print null terminated</p>



<p class="wp-block-paragraph">string is like down in CRLF already.</p>



<p class="wp-block-paragraph">That&#8217;s not good.</p>



<p class="wp-block-paragraph">Okay, that would have been a crashing program.</p>



<p class="wp-block-paragraph">Although sometimes if you omit the return statements, execution will just fall</p>



<p class="wp-block-paragraph">through down to the next label and maybe your program will survive accidentally.</p>



<p class="wp-block-paragraph">accidentally but for now it&#8217;s just crlf is supposed to be very simple it doesn&#8217;t preserve</p>



<p class="wp-block-paragraph">any registers so we&#8217;ve got a prologue and an epilogue here notice how the push and pops are</p>



<p class="wp-block-paragraph">in reverse order you want to know more about that see my other videos but now that we are preserving</p>



<p class="wp-block-paragraph">the appropriate registers we can actually grab our incoming arguments so first thing i&#8217;m going to do</p>



<p class="wp-block-paragraph">is i&#8217;m going to say r12 is going to be the first argument that i received and then r13 is going to</p>



<p class="wp-block-paragraph">okay no problem then let&#8217;s rely on the string length function to compute the actual length of</p>



<p class="wp-block-paragraph">the string i didn&#8217;t feel like having print null terminated string compute the length of the</p>



<p class="wp-block-paragraph">string it&#8217;s a good idea especially in assembly or any language when you have multiple distinct</p>



<p class="wp-block-paragraph">jobs happening within the same function you probably want to break that function up into</p>



<p class="wp-block-paragraph">multiple functions just to reduce you know strain on your brain right cognitive load</p>



<p class="wp-block-paragraph">So I&#8217;m going to use this function strlen string length to compute the length of the string.</p>



<p class="wp-block-paragraph">It&#8217;s only going to take one argument and it&#8217;s going to take the pointer to the null terminated</p>



<p class="wp-block-paragraph">string which is now in R12. It&#8217;s going to take that as its first argument so that&#8217;s why I&#8217;m loading</p>



<p class="wp-block-paragraph">that up into RDI. When string length returns it&#8217;s going to give me the length of the string in the</p>



<p class="wp-block-paragraph">RAX register which is the usual return register for integer or pointer return types. So I&#8217;m just</p>



<p class="wp-block-paragraph">So I&#8217;m just going to save that in R14.</p>



<p class="wp-block-paragraph">And that&#8217;s the usage of all those registers R12, 13, and 14.</p>



<p class="wp-block-paragraph">We still have to implement string length.</p>



<p class="wp-block-paragraph">Don&#8217;t worry.</p>



<p class="wp-block-paragraph">Although if you were linking a hybrid program, you could probably just call</p>



<p class="wp-block-paragraph">STRLEN in the C libraries and be fine.</p>



<p class="wp-block-paragraph">But this is an assembly video.</p>



<p class="wp-block-paragraph">We want to do everything in assembly if we can, or at least more of it.</p>



<p class="wp-block-paragraph">So then finally, when we know what the strings length is, we can just use a</p>



<p class="wp-block-paragraph">system call to actually print the string we&#8217;re going to say load up call code one to say you</p>



<p class="wp-block-paragraph">know mr. system I want you to print a string and then r13 is going to be the file handle so we&#8217;re</p>



<p class="wp-block-paragraph">going to basically say wherever the caller of print null terminated string said to print which</p>



<p class="wp-block-paragraph">is probably going to be standard output we&#8217;ll just tell the system we want to print to the same place</p>



<p class="wp-block-paragraph">and then r12 is a pointer to the c string so we just give that to the system call as well</p>



<p class="wp-block-paragraph">system call wants to know how long the string is that&#8217;s r14 now now that we have used strlen</p>



<p class="wp-block-paragraph">to determine the length of the string so not really that complicated of a function we just</p>



<p class="wp-block-paragraph">kind of like grab some arguments preserve those registers and we ask another function to compute</p>



<p class="wp-block-paragraph">the length of the string and then we actually just print it once we have the length this is still not</p>



<p class="wp-block-paragraph">getting to the point where we&#8217;re going to use our while loop knowledge to compute the length so i</p>



<p class="wp-block-paragraph">That&#8217;s probably all I need right now.</p>



<p class="wp-block-paragraph">And I think we&#8217;re ready to use or to start the string length function.</p>



<p class="wp-block-paragraph">Okay, so now let&#8217;s make another function called string length.</p>



<p class="wp-block-paragraph">Hopefully I&#8217;ll paste in the right spot this time.</p>



<p class="wp-block-paragraph">You&#8217;re cringing at home.</p>



<p class="wp-block-paragraph">That just tells me that you care.</p>



<p class="wp-block-paragraph">So the string length function, at least the version that I&#8217;m making right now,</p>



<p class="wp-block-paragraph">just is going to take one argument.</p>



<p class="wp-block-paragraph">It&#8217;s going to be a character pointer to the string that you want to compute.</p>



<p class="wp-block-paragraph">It will expect that the string has a null terminator at the end.</p>



<p class="wp-block-paragraph">the end if you accidentally didn&#8217;t put a null terminator at the end of the string then this</p>



<p class="wp-block-paragraph">function definitely won&#8217;t work it&#8217;ll probably give you some huge number because it&#8217;ll go through ram</p>



<p class="wp-block-paragraph">until it accidentally finds a zero um and then it&#8217;s going to return to you as its return value</p>



<p class="wp-block-paragraph">and uh assigned a 64-bit integer actually this should be unsigned but i&#8217;m just putting long for</p>



<p class="wp-block-paragraph">now um to indicate the length of the string okay inside the notes we&#8217;re going to use r12 and r13</p>



<p class="wp-block-paragraph">So that means I should probably preserve those registers first before I do anything else.</p>



<p class="wp-block-paragraph">So in the prolog, we&#8217;re going to push R12 and R13 so that we don&#8217;t break this program</p>



<p class="wp-block-paragraph">for others.</p>



<p class="wp-block-paragraph">And then we&#8217;re going to do an epilog.</p>



<p class="wp-block-paragraph">Whoops.</p>



<p class="wp-block-paragraph">Then we&#8217;re going to do an epilog to restore the registers.</p>



<p class="wp-block-paragraph">And this is a function.</p>



<p class="wp-block-paragraph">So it&#8217;s got to return to the caller.</p>



<p class="wp-block-paragraph">If I didn&#8217;t put a return statement here, then execution is going to just go all the way</p>



<p class="wp-block-paragraph">down to CRLF.</p>



<p class="wp-block-paragraph">And this will be an infinite loop.</p>



<p class="wp-block-paragraph">and this will be an infinite loop because crlf will end up calling null terminated string,</p>



<p class="wp-block-paragraph">which we&#8217;ll then call string length, which will then fall through to crlf,</p>



<p class="wp-block-paragraph">so the whole program won&#8217;t even work if we don&#8217;t have return.</p>



<p class="wp-block-paragraph">And, you know, you don&#8217;t want to omit return statements anyways,</p>



<p class="wp-block-paragraph">because that&#8217;s always a bad idea.</p>



<p class="wp-block-paragraph">So now string length will just not do anything right now.</p>



<p class="wp-block-paragraph">Maybe we could return a fake value for a second before we start implementing the loop.</p>



<p class="wp-block-paragraph">the number five into RAX so that string length will always trick the caller into thinking that</p>



<p class="wp-block-paragraph">the length of the string is five let&#8217;s see if that actually works we should get a portion</p>



<p class="wp-block-paragraph">of the null terminated string unless I screwed something up</p>



<p class="wp-block-paragraph">hello from the main driver notice how it just says hello here that&#8217;s kind of confusing let&#8217;s</p>



<p class="wp-block-paragraph">let&#8217;s hard code the five to like a nine we should see more of that null terminated string</p>



<p class="wp-block-paragraph">I sound when I wake up sometimes hello okay so let&#8217;s finish the str len function so again you</p>



<p class="wp-block-paragraph">should know how while loops work if you don&#8217;t see my other videos but we&#8217;re going to use a while</p>



<p class="wp-block-paragraph">loop to count the length of the string so we&#8217;re going to start with a little portion up here</p>



<p class="wp-block-paragraph">think the string is and a running pointer so rdi is already supposed to come in as a pointer to the</p>



<p class="wp-block-paragraph">string that we&#8217;re measuring so i&#8217;m going to save um the pointer into r12 so that we can have a</p>



<p class="wp-block-paragraph">pointer that points to a character we&#8217;re going to use this as a running pointer so it&#8217;s going to like</p>



<p class="wp-block-paragraph">sweep through the whole entire string until it hits a null terminator and then r13 is going to</p>



<p class="wp-block-paragraph">keep track of uh how big we think the string is so when we first start we&#8217;re just looking at the</p>



<p class="wp-block-paragraph">first start we&#8217;re just looking at the first letter and then we think the string has zero length.</p>



<p class="wp-block-paragraph">So that&#8217;s the initialization part which will not be repeated as we continue looping. Now we&#8217;re</p>



<p class="wp-block-paragraph">going to implement the top of the loop. I don&#8217;t know should I should I write this out as c code</p>



<p class="wp-block-paragraph">for you? I don&#8217;t know if I should maybe let me do it. I didn&#8217;t prepare this so if it&#8217;s slow sorry</p>



<p class="wp-block-paragraph">Maybe this is like a long strln, something like that.</p>



<p class="wp-block-paragraph">And then we&#8217;ll do if my code is wrong or doesn&#8217;t compile, I&#8217;m so sorry.</p>



<p class="wp-block-paragraph">I did not, I did not prepare this.</p>



<p class="wp-block-paragraph">We&#8217;ll say character pointer s and then we&#8217;ll say, uh, maybe we can actually just leave</p>



<p class="wp-block-paragraph">s alone because it&#8217;s coming in as an argument and in C plus plus you can just continue to</p>



<p class="wp-block-paragraph">use that symbol.</p>



<p class="wp-block-paragraph">It&#8217;s not going to get destroyed.</p>



<p class="wp-block-paragraph">So imagine we&#8217;ve saved it already into R 12 and then we just keep using it.</p>



<p class="wp-block-paragraph">using it so we&#8217;ll say while a let&#8217;s say a dereferencing of s is not equal to zero meaning</p>



<p class="wp-block-paragraph">if we look at the value that the pointer is currently pointing to if we assume it&#8217;s just</p>



<p class="wp-block-paragraph">pointing to one byte is we&#8217;ll keep going as long as that value is not a zero so that means</p>



<p class="wp-block-paragraph">if the user called this function and gave us a pointer that was already looking at a zero</p>



<p class="wp-block-paragraph">we would just return whoops we would just return that the length was zero so</p>



<p class="wp-block-paragraph">that means I should probably keep track of the length here size type actually</p>



<p class="wp-block-paragraph">long just to just to match the return signature long we&#8217;ll put size equals zero</p>



<p class="wp-block-paragraph">and then at the very end we&#8217;ll just return the size and so again if the user</p>



<p class="wp-block-paragraph">gave us a pointer that pointed to a zero already nothing would happen inside the</p>



<p class="wp-block-paragraph">while loop we&#8217;d break through it right away and we would just return the number</p>



<p class="wp-block-paragraph">the number zero that makes sense so then as long as it is not pointing at a zero</p>



<p class="wp-block-paragraph">we&#8217;ll just increase what we think the size is and then we will increase the</p>



<p class="wp-block-paragraph">pointer we can use s plus plus in C++ that&#8217;s just pointer arithmetic that&#8217;s</p>



<p class="wp-block-paragraph">just going to tell the pointer to advance you know one memory location</p>



<p class="wp-block-paragraph">further or whatever the data type is but in this case the data type is a</p>



<p class="wp-block-paragraph">character so it really is going to be one memory location one byte so we&#8217;re</p>



<p class="wp-block-paragraph">going to sweep through the string until we see a zero and then we stop and every time we see a</p>



<p class="wp-block-paragraph">character that&#8217;s not a zero we increase our our measured length of the string by one and then</p>



<p class="wp-block-paragraph">advance the pointer. So I haven&#8217;t tested this I don&#8217;t know if there&#8217;s an error in it but I hope</p>



<p class="wp-block-paragraph">you get the basic idea of what we&#8217;re going to do. So that means up here you know this is the</p>



<p class="wp-block-paragraph">initialization part that we were just talking about so we just set the running pointer to look</p>



<p class="wp-block-paragraph">okay so then after we do that we are going to make the top of the while loop</p>



<p class="wp-block-paragraph">so at the top of the while loop where we evaluate you know like right here this</p>



<p class="wp-block-paragraph">is the top of the while loop it has to have its own label just like we explained</p>



<p class="wp-block-paragraph">in the other videos and it is basically where we decide if we&#8217;re going to keep</p>



<p class="wp-block-paragraph">looping or not are we going to jump into the body the loop or are we going to do</p>



<p class="wp-block-paragraph">a long jump after the body to say that we&#8217;re done so the top of the loop is a</p>



<p class="wp-block-paragraph">label. We compare the value that R12 is currently pointing at. We say that we only want to look at</p>



<p class="wp-block-paragraph">one byte. We dereference R12 because remember R12 is supposed to be a pointer. You put the</p>



<p class="wp-block-paragraph">brackets around it, it&#8217;s going to go to the memory location and then check what the value is that</p>



<p class="wp-block-paragraph">the pointer is pointing to. That&#8217;s what dereferencing is, right? So we&#8217;re just going to</p>



<p class="wp-block-paragraph">compare the byte that we&#8217;re looking at with a zero and we&#8217;ll say if it is equal to a zero,</p>



<p class="wp-block-paragraph">jump to the done this is actually kind of a poor design pattern on my part usually we should jump</p>



<p class="wp-block-paragraph">if it&#8217;s not equal into the body meaning we&#8217;ll always take a short jump into the body and then</p>



<p class="wp-block-paragraph">execution will fall through on the next line to a long jump which has the ability to jump further</p>



<p class="wp-block-paragraph">out of the body i&#8217;ve said in other videos that the conditional branch instructions they can only jump</p>



<p class="wp-block-paragraph">about 128 bytes so if your if your loop body is too big then they won&#8217;t work but it&#8217;ll work for</p>



<p class="wp-block-paragraph">But it&#8217;ll work for this example.</p>



<p class="wp-block-paragraph">I don&#8217;t know, maybe if I have the gumption, I will fix up the loop for you if you want</p>



<p class="wp-block-paragraph">me to after I copy paste my existing solution.</p>



<p class="wp-block-paragraph">So for now we&#8217;re going to say, all right, I&#8217;m not going to do it.</p>



<p class="wp-block-paragraph">I&#8217;m not going to do that.</p>



<p class="wp-block-paragraph">Maybe in another video, if somebody requested, I might post another video in like five years.</p>



<p class="wp-block-paragraph">Anyway, so we&#8217;re going to jump if it is a null terminator to the done label.</p>



<p class="wp-block-paragraph">Otherwise we will fall through to the loop&#8217;s body where we&#8217;re just literally going to increase the pointer and also increase our idea of how big the string is.</p>



<p class="wp-block-paragraph">So remember R12 is the pointer.</p>



<p class="wp-block-paragraph">Integer arithmetic doesn&#8217;t, sorry, pointer arithmetic doesn&#8217;t really work here, but it accidentally works here because we&#8217;re looking at a byte array.</p>



<p class="wp-block-paragraph">So if we just increase by one memory location, it will literally just increase by one memory location and we&#8217;ll be fine.</p>



<p class="wp-block-paragraph">Just keep in mind that if you were sweeping through an array of, you know, quad words or some larger data type,</p>



<p class="wp-block-paragraph">then just a simple ink wouldn&#8217;t actually work.</p>



<p class="wp-block-paragraph">You&#8217;d have to increase by the appropriate number of bytes.</p>



<p class="wp-block-paragraph">But hey, the number of bytes in one item is just one byte, so it&#8217;s easy.</p>



<p class="wp-block-paragraph">So we&#8217;re making the pointer go forward by one on line 134 and then in line 135.</p>



<p class="wp-block-paragraph">line 135 we&#8217;re increasing our idea of how big the string is and then we will unconditionally jump</p>



<p class="wp-block-paragraph">to the top of our loop and so if you just kind of look at this what did i do i pasted that twice</p>



<p class="wp-block-paragraph">oh god okay sorry guess i lost track of what i was doing so then we will unconditionally jump</p>



<p class="wp-block-paragraph">to the top of the loop so basically you can imagine this loop is gonna it&#8217;s just gonna</p>



<p class="wp-block-paragraph">continue forever just moving the pointer and increasing the counter and moving the pointer</p>



<p class="wp-block-paragraph">finally when it sees a zero a null terminator then it actually breaks to</p>



<p class="wp-block-paragraph">the done label and the done label is just doesn&#8217;t really do much it&#8217;s just a</p>



<p class="wp-block-paragraph">label to get us out of the loop so the top of the loop says if we are done then</p>



<p class="wp-block-paragraph">just jump to the done area notice how that skips over the the top jump and then</p>



<p class="wp-block-paragraph">of course under that is going to be the epilog and then we can we can take the</p>



<p class="wp-block-paragraph">we can take the return value and set that up now because at this point R13 should contain</p>



<p class="wp-block-paragraph">the actual length of the string. So if we move that into RAX respecting the ABI for return values,</p>



<p class="wp-block-paragraph">then the caller should be able to get the string length just at that point by itself.</p>



<p class="wp-block-paragraph">So let&#8217;s see, that might actually be the whole entire program already. Let me</p>



<p class="wp-block-paragraph">double check here. All right, let&#8217;s run it and see if it actually works.</p>



<p class="wp-block-paragraph">and then do a make run.</p>



<p class="wp-block-paragraph">What&#8217;s up with those asterisks?</p>



<p class="wp-block-paragraph">Did I put that in there?</p>



<p class="wp-block-paragraph">Oh, I wonder.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So the driver comes in,</p>



<p class="wp-block-paragraph">it calls on our function,</p>



<p class="wp-block-paragraph">and the whole null terminated string gets printed out.</p>



<p class="wp-block-paragraph">It says, hello, this is an example</p>



<p class="wp-block-paragraph">of our null terminated string.</p>



<p class="wp-block-paragraph">Notice how it printed the full length of the string,</p>



<p class="wp-block-paragraph">not any less,</p>



<p class="wp-block-paragraph">and it also didn&#8217;t print more than the length of the string,</p>



<p class="wp-block-paragraph">i.e. junk data,</p>



<p class="wp-block-paragraph">because it knew exactly how long the string was.</p>



<p class="wp-block-paragraph">was and this is way better than printing one character at a time in terms of efficiency we</p>



<p class="wp-block-paragraph">just pre-compute the length and then print exactly that length and then we&#8217;re done i think there is</p>



<p class="wp-block-paragraph">one more thing i wanted to do here let me see up at the top yeah okay let me go back up to the top</p>



<p class="wp-block-paragraph">of the program here so in the looper function we called on print null terminated string and we</p>



<p class="wp-block-paragraph">didn&#8217;t do anything else so what i would like to do is just make an explicit call to string length</p>



<p class="wp-block-paragraph">explicit call to string length inside of the lubr function just to get the length of the</p>



<p class="wp-block-paragraph">null terminated string so we can just print it to the caller or print it to the user</p>



<p class="wp-block-paragraph">and then I&#8217;m going to use my special library function here actually just just for your</p>



<p class="wp-block-paragraph">information notice how I&#8217;m calling string length just like the the print null terminated string</p>



<p class="wp-block-paragraph">function did and I&#8217;m just giving it as an argument a pointer to that null terminated string so then</p>



<p class="wp-block-paragraph">So now I can just print r12</p>



<p class="wp-block-paragraph">Well not yet, I&#8217;m gonna print a prefix if you look at the prefix here, it&#8217;s just</p>



<p class="wp-block-paragraph">The null terminated strings length was and then I&#8217;ll print a number after that</p>



<p class="wp-block-paragraph">You do it this way, you know your program is more pretty it&#8217;s more</p>



<p class="wp-block-paragraph">It&#8217;s more nice to the user and so forth so I&#8217;m going to do this</p>



<p class="wp-block-paragraph">we&#8217;re printing a nice prefix, a hard-coded string to the user to let them know that I&#8217;m about to</p>



<p class="wp-block-paragraph">show them the length of the string. And then I use my external function that just prints a number to</p>



<p class="wp-block-paragraph">the user. Again, this video is not about this library. You can use some other library if you</p>



<p class="wp-block-paragraph">want to print something, or you can omit that part if you don&#8217;t have one set up yet. But</p>



<p class="wp-block-paragraph">so I&#8217;m going to tell, I&#8217;m going to do first argument is R12, which was the length of the</p>



<p class="wp-block-paragraph">I&#8217;m going to call this function and say I would like you to print r12 which is the length of the string so</p>



<p class="wp-block-paragraph">After that we&#8217;ll print a new line to make things a little bit tidier and then I think this program is actually finished</p>



<p class="wp-block-paragraph">Run it again now it says here&#8217;s the null terminated string and then on the next line it just says</p>



<p class="wp-block-paragraph">The null terminated strings length was that was the prefix and then when I called my library</p>



<p class="wp-block-paragraph">the number it says 54. so i don&#8217;t know was it 54? let&#8217;s just double check to make sure that it</p>



<p class="wp-block-paragraph">actually was 54. 54 should not include the null terminator so i&#8217;m going to go 1 2 3 4 5 6 7 8 9 10</p>



<p class="wp-block-paragraph">1 2 3 4 5 6 7 9 20 1 2 3 5 6 7 9 30 1 2 3 5 6 7 9 9 30 1 2 3 5 6 7 9 9 50 51 52 53 54 was it 54? i can&#8217;t even remember anymore.</p>



<p class="wp-block-paragraph">So we have basically proved that this works.</p>



<p class="wp-block-paragraph">We have leveraged our knowledge of while loops to implement a string length function, which</p>



<p class="wp-block-paragraph">will let us have a printing function that is very smart.</p>



<p class="wp-block-paragraph">So we don&#8217;t have to hard code string lengths up at the top anymore.</p>



<p class="wp-block-paragraph">As long as we&#8217;re working with null terminated strings, everything will just work out now</p>



<p class="wp-block-paragraph">with less variables or less defines.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">Let&#8217;s see.</p>



<p class="wp-block-paragraph">I think that&#8217;s pretty much everything that I wanted to talk to you about.</p>



<p class="wp-block-paragraph">I don&#8217;t know. Could I do,</p>



<p class="wp-block-paragraph">could I do this easy, easily?</p>



<p class="wp-block-paragraph">Loop top.</p>



<p class="wp-block-paragraph">Okay. Yeah. I think I could probably do this reasonably.</p>



<p class="wp-block-paragraph">So at this point,</p>



<p class="wp-block-paragraph">you are satisfied that you understand how to implement this and you&#8217;re happy just cut the</p>



<p class="wp-block-paragraph">video the rest of this video is going to be me sort of like improvising trying to figure out if</p>



<p class="wp-block-paragraph">i can rearrange the logic in a fast enough time for a video uh just to show you that you know you</p>



<p class="wp-block-paragraph">should you should probably write your loops a little bit better than i did so here we go but</p>



<p class="wp-block-paragraph">this is this is just redundant stuff so we have our loop here and we have our initialization</p>



<p class="wp-block-paragraph">The loop top, it should compare R12 to 0 and it should break the loop if it is a 0.</p>



<p class="wp-block-paragraph">So that means I&#8217;m going to comment out this.</p>



<p class="wp-block-paragraph">And I&#8217;m going to do jump not equal to 0 to the body.</p>



<p class="wp-block-paragraph">And I just need to make a label for the body here.</p>



<p class="wp-block-paragraph">So I&#8217;m going to say str lane loop bottom.</p>



<p class="wp-block-paragraph">So there&#8217;s a label, which is the body.</p>



<p class="wp-block-paragraph">Maybe I&#8217;ll do a comment here just to remind us that this is actually the body.</p>



<p class="wp-block-paragraph">I guess I&#8217;ll do another comment right here.</p>



<p class="wp-block-paragraph">So that&#8217;s the loop&#8217;s body.</p>



<p class="wp-block-paragraph">So I&#8217;m going to say if R12 is not a null terminator, jump into the loop&#8217;s body.</p>



<p class="wp-block-paragraph">Otherwise, we fall through to the next instruction,</p>



<p class="wp-block-paragraph">and that will just be an unconditional jump to the done area.</p>



<p class="wp-block-paragraph">Okay, and then when we&#8217;re inside the loop&#8217;s body, we&#8217;ll jump back up to the top.</p>



<p class="wp-block-paragraph">I don&#8217;t know why I thought this was going to be hard.</p>



<p class="wp-block-paragraph">Let me run this to make sure I didn&#8217;t break the program.</p>



<p class="wp-block-paragraph">Yeah, it still works.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">I guess I overestimated the difficulty there.</p>



<p class="wp-block-paragraph">The point being, the body is a lot closer to the top of the loop.</p>



<p class="wp-block-paragraph">So that should be the thing that does a conditional branch.</p>



<p class="wp-block-paragraph">You should conditionally branch to the body because it&#8217;s a shorter jump and therefore</p>



<p class="wp-block-paragraph">much less likely to be out of bounds of that 128 conditional jump bite restriction.</p>



<p class="wp-block-paragraph">And then when we fall through to the next line, because we did not do that jump,</p>



<p class="wp-block-paragraph">because we did not do that jump then we&#8217;ll do an unconditional jump to the done area and you know</p>



<p class="wp-block-paragraph">our loop is small so it didn&#8217;t really matter the first time we did this but um again imagine your</p>



<p class="wp-block-paragraph">loop is huge that you definitely want an unconditional jump that goes to the done area</p>



<p class="wp-block-paragraph">at that point and that&#8217;s also what we&#8217;re doing an unconditional jump to the top here when we get to</p>



<p class="wp-block-paragraph">the end of the body so when you&#8217;re jumping large uh you know spans you want to use unconditional</p>



<p class="wp-block-paragraph">Alright, so I guess that&#8217;s it.</p>



<p class="wp-block-paragraph">I&#8217;m going to erase maybe this comment.</p>



<p class="wp-block-paragraph">Well, I&#8217;ll leave that in there just for posterity.</p>



<p class="wp-block-paragraph">And now I will officially say that I hope you had a good time watching this video.</p>



<p class="wp-block-paragraph">I hope you learned a little bit of stuff and I hope you had a little bit of fun.</p>



<p class="wp-block-paragraph">I will see you in the next video.</p>



<p class="wp-block-paragraph">I&#8217;m going to go play some video games.</p>



<p class="wp-block-paragraph">Maybe I&#8217;m going to eat some soup first.</p>



<p class="wp-block-paragraph">Hey everybody.</p>



<p class="wp-block-paragraph">Hey everybody, thanks for watching this video again from the bottom of my heart. I really</p>



<p class="wp-block-paragraph">appreciate it. I do hope you did learn something and have some fun. If you could do me a please,</p>



<p class="wp-block-paragraph">a small little favor, could you please subscribe and follow this channel or these videos or</p>



<p class="wp-block-paragraph">whatever it is you do on the current social media website that you&#8217;re looking at right now.</p>



<p class="wp-block-paragraph">It would really mean the world to me and it&#8217;ll help make more videos and grow this community.</p>



<p class="wp-block-paragraph">So we&#8217;ll be able to do more videos, longer videos, better videos, or just I&#8217;ll be able to keep making</p>



<p class="wp-block-paragraph">to keep making videos in general. So please do me a kindness and subscribe. You know, sometimes</p>



<p class="wp-block-paragraph">I&#8217;m sleeping in the middle of the night and I just wake up because I know somebody subscribed</p>



<p class="wp-block-paragraph">or followed. It just wakes me up and I get filled with joy. That&#8217;s exactly what happens every single</p>



<p class="wp-block-paragraph">time. So you could do it as a nice favor to me or you could troll me if you want to just wake me up</p>



<p class="wp-block-paragraph">in the middle of the night, just subscribe and then I&#8217;ll just wake up. I promise that&#8217;s what</p>



<p class="wp-block-paragraph">will happen. Also, if you look at the middle of the screen right now, you should see a QR code,</p>



<p class="wp-block-paragraph">you should see a QR code which you can scan in order to go to the website which I think is also</p>



<p class="wp-block-paragraph">named somewhere at the bottom of this video and it&#8217;ll take you to my main website where you can</p>



<p class="wp-block-paragraph">just kind of like see all the videos I published and the services and tutorials and things that I</p>



<p class="wp-block-paragraph">offer and all that good stuff and if you have a suggestion for clarifications or errata or just</p>



<p class="wp-block-paragraph">future videos that you want to see please leave a comment or if you just want to say hey what&#8217;s up</p>



<p class="wp-block-paragraph">what&#8217;s going on? You know, just send me a comment, whatever. I also wake up for those in the middle</p>



<p class="wp-block-paragraph">of the night. I get, I wake up in a cold sweat and I&#8217;m like, it would really, it would really mean</p>



<p class="wp-block-paragraph">the world to me. I would really appreciate it. So again, thank you so much for watching this video</p>



<p class="wp-block-paragraph">and enjoy the cool music as, as I fade into the darkness, which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/implement-strlen-for-null-terminated-strings-in-x86-64-assembly-yasm/">Implement strlen for Null-Terminated Strings in x86-64 Assembly (YASM)</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/implement-strlen-for-null-terminated-strings-in-x86-64-assembly-yasm/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>x86-64 Assembly For Loops Explained &#8211; YASM Tutorial</title>
		<link>https://www.NeuralLantern.com/x86-64-assembly-for-loops-explained-yasm-tutorial/</link>
					<comments>https://www.NeuralLantern.com/x86-64-assembly-for-loops-explained-yasm-tutorial/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 01 Feb 2026 02:49:29 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[asm tutorial 2025]]></category>
		<category><![CDATA[assembly for loop]]></category>
		<category><![CDATA[assembly language tutorial]]></category>
		<category><![CDATA[assembly while loop]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[conditional jump assembly]]></category>
		<category><![CDATA[implementing for loop in assembly]]></category>
		<category><![CDATA[inc instruction assembly]]></category>
		<category><![CDATA[learning assembly language]]></category>
		<category><![CDATA[loop in assembly]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[NASM vs YASM]]></category>
		<category><![CDATA[reverse engineering basics]]></category>
		<category><![CDATA[systems programming]]></category>
		<category><![CDATA[x86 assembly loops]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[x86-64 linux assembly]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<category><![CDATA[yasm x86-64]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=295</guid>

					<description><![CDATA[<p>This tutorial explains how to implement a C-style for loop in x86-64 assembly using YASM by converting it to while-loop logic with initialization, conditional jump (jl), increment (inc), and unconditional jump back to the loop top. Demonstrates register usage (r12/r13), loop structure, and printing numbers inside the body.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-for-loops-explained-yasm-tutorial/">x86-64 Assembly For Loops Explained &#8211; YASM Tutorial</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="x86-64 Assembly For Loops Explained - YASM Tutorial" width="1380" height="776" src="https://www.youtube.com/embed/pftzv5lIPso?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 practical guide showing how to implement a classic for loop (i=0; i&lt;n; i++) in x86-64 assembly with YASM. We break the for loop into init/check/update parts, convert it to while-style logic, then build it with compares, conditional jumps, inc, and unconditional jumps back to the top. Includes real working code, user input, and printed output.</p>



<p class="wp-block-paragraph">Great for students learning assembly after C/C++, OSdev hobbyists, or reverse engineering beginners.</p>



<p class="wp-block-paragraph">Introduction to For Loops in Assembly 00:00:00<br>For Loop Structure in High-Level Languages 00:00:28<br>Breaking Down For Loop Parts: Init, Check, Update 00:01:04<br>Converting For Loop to While Loop 00:01:52<br>Why While Loop Style Helps in Assembly 00:03:16<br>Program Overview and Setup 00:04:12<br>Data Section &#8211; Strings and Messages 00:04:40<br>External Functions and Hybrid Program 00:06:11<br>Entry Point &#8211; Looper Function 00:07:12<br>For Test Function and Register Usage 00:07:42<br>Preserving Callee-Saved Registers 00:08:05<br>Printing Welcome Message 00:09:00<br>Prompting User for Number 00:09:48<br>Getting User Input 00:10:25<br>For Loop Structure in Assembly &#8211; Comments 00:11:12<br>Initialization &#8211; Setting Counter to Zero 00:12:50<br>Loop Top Label and Condition Check 00:14:28<br>Conditional Jump Setup (jl) 00:15:07<br>Handling Jump Distances 00:15:34<br>Loop Body &#8211; Printing Current Number 00:16:58<br>Update Part &#8211; Increment Counter 00:18:40<br>Unconditional Jump Back to Loop Top 00:19:14<br>Loop Done Label and Exit 00:19:55<br>Printing Goodbye Message 00:20:23<br>Testing the Program 00:22:08<br>Final Results and Demo 00:22:24<br>Outro and Call to Action 00:23:25<br>Thanks and Subscribe Request 00:23:45<br>Website and QR Code Mention 00:24:38<br>Closing Thanks 00:25:16</p>



<p class="wp-block-paragraph">=-=-=-=-=-=-=-=-=</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>



<li>Twitter / X: https://x.com/NeuralLantern</li>



<li>Rumble: https://rumble.com/c/c-3696939</li>



<li>BitChute: https://www.bitchute.com/channel/pg1Pvv5dN4Gt</li>



<li>Daily Motion: https://www.dailymotion.com/neurallantern</li>



<li>Minds: https://www.minds.com/neurallantern/</li>



<li>Odysee: https://odysee.com/@NeuralLantern:5</li>
</ul>



<p class="wp-block-paragraph">Please show your support!</p>



<ul class="wp-block-list">
<li>Buy me a coffee: https://ko-fi.com/neurallantern</li>



<li>Subscribe + Sharing on Social Media</li>



<li>Leave a comment or suggestion</li>



<li>Subscribe to Blog: https://www.NeuralLantern.com</li>



<li>Watching the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p class="wp-block-paragraph">Hey there! In this video I&#8217;m going to teach you how to implement for loops in YASM x86-64 assembly.</p>



<p class="wp-block-paragraph">Although if you&#8217;re using a different type of machine or a different assembler,</p>



<p class="wp-block-paragraph">this video should still be useful to you because it&#8217;s mostly about the concepts involved in implementing a for loop.</p>



<p class="wp-block-paragraph">Okay, so for starters here, what am I even talking about? Let&#8217;s do…</p>



<p class="wp-block-paragraph">are you even talking about dude imagine in a higher level language we have a for loop here</p>



<p class="wp-block-paragraph">and so we have like four size type i equals zero i is less than 99 i plus plus right probably most</p>



<p class="wp-block-paragraph">programmers looking up assembly uh on the internet probably are interested in uh or probably already</p>



<p class="wp-block-paragraph">know how to implement for loops in c plus plus so let&#8217;s just pretend that you do and so inside of</p>



<p class="wp-block-paragraph">your for loop we&#8217;ll do stuff and what happens here is uh well you know this for loop is going to run</p>



<p class="wp-block-paragraph">Well, you know, this for loop is going to run 99 times.</p>



<p class="wp-block-paragraph">It&#8217;s going to start at zero.</p>



<p class="wp-block-paragraph">And, you know, so we have like an initialization part right here.</p>



<p class="wp-block-paragraph">Maybe I should copy paste this and kind of break down the parts.</p>



<p class="wp-block-paragraph">We have an init part here for the for loop.</p>



<p class="wp-block-paragraph">We&#8217;ll say init part.</p>



<p class="wp-block-paragraph">And then here we have a check part.</p>



<p class="wp-block-paragraph">We&#8217;ll continue to loop as long as this expression is true.</p>



<p class="wp-block-paragraph">You can make a big complicated expression if you want to,</p>



<p class="wp-block-paragraph">but we&#8217;ll just keep it simple for this video.</p>



<p class="wp-block-paragraph">So I&#8217;ll call this the check part.</p>



<p class="wp-block-paragraph">And then we have sort of an update or a maintenance part.</p>



<p class="wp-block-paragraph">an update or a maintenance part. So I&#8217;m going to call this the update part where we increment our</p>



<p class="wp-block-paragraph">counter variable, move pointers, even call functions, do whatever it is you think you need</p>



<p class="wp-block-paragraph">to do to update the loop so that it runs it, you know, so that the loops don&#8217;t actually have to be</p>



<p class="wp-block-paragraph">integers. But in this case, I think it&#8217;s better, it&#8217;s easier to understand what the loop is doing</p>



<p class="wp-block-paragraph">if we just kind of break down these parts. Anyway, so what is really a while loop? Sorry,</p>



<p class="wp-block-paragraph">but a while loop that breaks and has break has like a nit logic and breaking logic and checking logic just in specific places.</p>



<p class="wp-block-paragraph">Imagine this. What if I said before the loop starts, I&#8217;ll let&#8217;s let&#8217;s say we&#8217;re going to do a while loop before the loop starts.</p>



<p class="wp-block-paragraph">I&#8217;m going to initialize a variable size type I equals zero, right?</p>



<p class="wp-block-paragraph">That&#8217;s the initialization part that&#8217;ll only happen one time.</p>



<p class="wp-block-paragraph">And then when you have a while loop and it checks to see if it should keep going, that&#8217;s</p>



<p class="wp-block-paragraph">the check part of the for loop.</p>



<p class="wp-block-paragraph">So I&#8217;ll just say while i is less than 99.</p>



<p class="wp-block-paragraph">And then for the update part, we just have to stick that inside of the while loop somewhere.</p>



<p class="wp-block-paragraph">Depending on your logic, you might want to put it at the top or the middle or the bottom.</p>



<p class="wp-block-paragraph">I&#8217;m going to put it at the bottom because I&#8217;m going to expect that it happens right</p>



<p class="wp-block-paragraph">before we do the next check.</p>



<p class="wp-block-paragraph">So I&#8217;m going to maybe do a comment here and call it the update part and I&#8217;m just going</p>



<p class="wp-block-paragraph">So maybe if I update this code to print something, it&#8217;ll seem a little bit more clear.</p>



<p class="wp-block-paragraph">Value of I is, and then I&#8217;ll just say I end L, and then I&#8217;ll copy paste that to here.</p>



<p class="wp-block-paragraph">And then I can also put this inside of the while loop.</p>



<p class="wp-block-paragraph">And now we have transformed our for loop into a while loop.</p>



<p class="wp-block-paragraph">transform your for loop into a while loop it&#8217;s pretty easy to implement in assembly if you</p>



<p class="wp-block-paragraph">already know how to implement while loops so i should again say that there are lots of parts</p>



<p class="wp-block-paragraph">i don&#8217;t know if i said that during this video but there are lots of things in this video that i&#8217;m</p>



<p class="wp-block-paragraph">not going to that i&#8217;m not going to explain specifically because i&#8217;ve already explained</p>



<p class="wp-block-paragraph">them in other videos so for example implementing a while loop is in another video so if you don&#8217;t</p>



<p class="wp-block-paragraph">already know how to implement while loops if you don&#8217;t know how to use conditional branching or</p>



<p class="wp-block-paragraph">If you don&#8217;t know the basics of assembly, hybrid programs, make files, all the basic stuff that I&#8217;m going to skim over in this video,</p>



<p class="wp-block-paragraph">then you should probably check out my other videos first.</p>



<p class="wp-block-paragraph">But for now, I&#8217;m just going to assume that you know how to implement a while loop.</p>



<p class="wp-block-paragraph">And really the lesson is, hey, just take your for loop and convert it into a while loop.</p>



<p class="wp-block-paragraph">And then you can implement the while loop pretty easily.</p>



<p class="wp-block-paragraph">That&#8217;s the secret as far as I&#8217;m concerned.</p>



<p class="wp-block-paragraph">So imagine this now. Let&#8217;s do some assembly code.</p>



<p class="wp-block-paragraph">I have a source code file here called looper.asm for assembly.</p>



<p class="wp-block-paragraph">And you can imagine that I have a hybrid program running under the hood.</p>



<p class="wp-block-paragraph">I&#8217;m not going to show you all the code involved.</p>



<p class="wp-block-paragraph">I&#8217;m not going to show you the make file or the C++ driver that calls on this module.</p>



<p class="wp-block-paragraph">We&#8217;re just going to write the assembly here.</p>



<p class="wp-block-paragraph">So, you know, if you&#8217;re writing pure assembly at home to practice, that&#8217;s fine.</p>



<p class="wp-block-paragraph">Just keep in mind, there&#8217;s like a few things under the hood that I&#8217;m not showing in this specific video.</p>



<p class="wp-block-paragraph">So first off, I&#8217;m going to copy paste my data section.</p>



<p class="wp-block-paragraph">paste my data section of my assembly program. The first thing to note is we have a bunch of</p>



<p class="wp-block-paragraph">C strings here. We&#8217;re basically going to be telling the user, hey, we&#8217;re going to begin</p>



<p class="wp-block-paragraph">the four tests. Maybe I should have capitalized for, I&#8217;ll leave it. And then we&#8217;re going to prompt</p>



<p class="wp-block-paragraph">the user for a number. We&#8217;re going to say, please enter a number. The loop will print from zero to</p>



<p class="wp-block-paragraph">the number minus one, which is like the typical four loop that you usually write in the most</p>



<p class="wp-block-paragraph">and then we&#8217;re going to have a little prefix we&#8217;re going to say you know like an arrow and then we&#8217;re</p>



<p class="wp-block-paragraph">going to print the number back to the user that we&#8217;re looping through so if the user enters a</p>



<p class="wp-block-paragraph">10 it&#8217;s going to print the arrow 0 and then arrow 1 arrow 2 all the way up to arrow 9</p>



<p class="wp-block-paragraph">and then when we&#8217;re done we&#8217;re going to print an ending message so this is nothing new if you know</p>



<p class="wp-block-paragraph">assembly already just c strings that will print with system calls see my other videos if you don&#8217;t</p>



<p class="wp-block-paragraph">And then a CRLF string, just basically doing a new line, a carriage return new line feed</p>



<p class="wp-block-paragraph">on the system.</p>



<p class="wp-block-paragraph">And then we&#8217;re going to output using system call code one.</p>



<p class="wp-block-paragraph">And we&#8217;re going to use file descriptor one so we can print to standard output.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So now the real fun begins.</p>



<p class="wp-block-paragraph">Let&#8217;s start our text section, which is where the instructions of our assembly program go.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do text section, section text right there.</p>



<p class="wp-block-paragraph">And I&#8217;m going to use two external functions to just help me input and output numbers to the user.</p>



<p class="wp-block-paragraph">If you wanted to, you could use a system call that just inputted a character.</p>



<p class="wp-block-paragraph">And then you could just kind of like loop printing various characters.</p>



<p class="wp-block-paragraph">And like, let&#8217;s say if the user typed A, maybe you could imagine doing a loop that increases the character that they typed all the way until it hits Z.</p>



<p class="wp-block-paragraph">Or if they hit, you know, F, it&#8217;ll just print F all the way to Z.</p>



<p class="wp-block-paragraph">to z you could do that without using an external library for printing integers this video is not</p>



<p class="wp-block-paragraph">about this library right here so i&#8217;m not really going to go over it but uh you know you could</p>



<p class="wp-block-paragraph">hard code the start and end points when you&#8217;re practicing um you could you could use a different</p>



<p class="wp-block-paragraph">library or a different function call to get the inputs you could use a system call just to input</p>



<p class="wp-block-paragraph">one character um or you could you know use a hybrid program to to utilize printf and scanf</p>



<p class="wp-block-paragraph">f. Either way I&#8217;m just going to be using these two functions just to like get input and output.</p>



<p class="wp-block-paragraph">It&#8217;s not really part of the idea of looping. So now let&#8217;s start our entry point.</p>



<p class="wp-block-paragraph">Our function is called looper. Since this is a hybrid program you can imagine there&#8217;s a C++</p>



<p class="wp-block-paragraph">module elsewhere calling on the looper function and so that&#8217;s why I mark it as global so it can</p>



<p class="wp-block-paragraph">be called upon. And then I have another function called for test. I don&#8217;t really know why I chose</p>



<p class="wp-block-paragraph">it this way but i wanted to make another function that was called upon by our entry point here</p>



<p class="wp-block-paragraph">so the looper function really doesn&#8217;t do anything except recall the for test function</p>



<p class="wp-block-paragraph">so now let&#8217;s start the for test function actually maybe this is where the fun begins</p>



<p class="wp-block-paragraph">so i&#8217;m gonna put it down here and so you can see the signature it doesn&#8217;t take any arguments it</p>



<p class="wp-block-paragraph">doesn&#8217;t return anything it just does stuff and then i have a note to myself this is how we&#8217;re</p>



<p class="wp-block-paragraph">going to use the registers we&#8217;re going to use r12 for the user&#8217;s number and then we&#8217;re going to use</p>



<p class="wp-block-paragraph">And then we&#8217;re going to use R13 to keep track of where we&#8217;re going.</p>



<p class="wp-block-paragraph">So let&#8217;s see.</p>



<p class="wp-block-paragraph">I&#8217;m going to start by saying let&#8217;s preserve R12 and R13</p>



<p class="wp-block-paragraph">because you have to respect the application binary interface, the ABI,</p>



<p class="wp-block-paragraph">and that designates R12 and R13 as Kali saved registers.</p>



<p class="wp-block-paragraph">If I don&#8217;t preserve those and my program is even a little bit complicated,</p>



<p class="wp-block-paragraph">I&#8217;m probably going to be debugging forever</p>



<p class="wp-block-paragraph">debugging forever or I&#8217;m going to just crash my program for no reason. So I&#8217;m going to just</p>



<p class="wp-block-paragraph">do a push pop pair. Notice how the pops are in reverse order. This is not a push pop video,</p>



<p class="wp-block-paragraph">but just so you know, I guess while I&#8217;m here, I&#8217;m going to copy paste my crlf function,</p>



<p class="wp-block-paragraph">which really does nothing. It just prints out the crlf string with a system call. That&#8217;s all</p>



<p class="wp-block-paragraph">Okay, so do I even need CRLF in this program?</p>



<p class="wp-block-paragraph">I think I just modified this.</p>



<p class="wp-block-paragraph">Maybe I don&#8217;t even need it anymore.</p>



<p class="wp-block-paragraph">No, I guess I do.</p>



<p class="wp-block-paragraph">The first thing we&#8217;ll do is we&#8217;ll print an introduction message to the user.</p>



<p class="wp-block-paragraph">So inside of the for test, we&#8217;ll just use a system call to print out, you know, a welcome</p>



<p class="wp-block-paragraph">message to the user.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to call CRLF, which will just give us a new line.</p>



<p class="wp-block-paragraph">And yeah, you can hard code, you know, the 13, 10 at the end of these strings, but I</p>



<p class="wp-block-paragraph">don&#8217;t really like doing that.</p>



<p class="wp-block-paragraph">doing that. Okay, so we should have a working program at this point. Let me see if it actually</p>



<p class="wp-block-paragraph">does work. Clear and make run. Okay, so the driver prints a little welcome message. You don&#8217;t see the</p>



<p class="wp-block-paragraph">driver code, but that&#8217;s what it&#8217;s doing. And then the for test prints the welcome message that we</p>



<p class="wp-block-paragraph">just added. And then the driver lets us know it&#8217;s retained control. And then it&#8217;s responsible for</p>



<p class="wp-block-paragraph">returning to the operating system for us. Okay, so then the next thing we&#8217;re going to do is we&#8217;re</p>



<p class="wp-block-paragraph">we&#8217;re going to ask the user for a number so that we know how many times to loop.</p>



<p class="wp-block-paragraph">And again, you could hard code this number if you don&#8217;t want to do IO right now.</p>



<p class="wp-block-paragraph">You could even print a character a certain number of times</p>



<p class="wp-block-paragraph">if you don&#8217;t want to even deal with printing an integer.</p>



<p class="wp-block-paragraph">But I&#8217;m going to ask the user for a number.</p>



<p class="wp-block-paragraph">That&#8217;s going to be the prompt string.</p>



<p class="wp-block-paragraph">And if we run it again, now you should see it asks the user for a number.</p>



<p class="wp-block-paragraph">So it&#8217;s going to say, please enter a number.</p>



<p class="wp-block-paragraph">The loop will print from zero to N minus one.</p>



<p class="wp-block-paragraph">print from 0 to n minus 1. It doesn&#8217;t actually ask for the number though. It doesn&#8217;t actually</p>



<p class="wp-block-paragraph">I guess take the number so that&#8217;s going to be my external library that this video is not about</p>



<p class="wp-block-paragraph">where I just call a function called input assigned 64 integer and I&#8217;m going to receive that back in</p>



<p class="wp-block-paragraph">rax and I&#8217;m just going to save rax into r12. So r12 is now going to be the number that the user</p>



<p class="wp-block-paragraph">inputted and that&#8217;s why up here I have it designated as the user&#8217;s number. So really not a big deal but</p>



<p class="wp-block-paragraph">a big deal but uh you know if we run it again it&#8217;ll ask for a number and then it won&#8217;t do</p>



<p class="wp-block-paragraph">anything else it&#8217;ll just kind of quit okay so now we can implement our for loop this is going to be</p>



<p class="wp-block-paragraph">a little tricky so we&#8217;re going to start off with the initialization part remember we had several</p>



<p class="wp-block-paragraph">parts here if i just kind of drag this off to the side maybe pin it up to the top so we can see it</p>



<p class="wp-block-paragraph">aren&#8217;t we? If I move it a little bit to the side. So remember that for loop, it&#8217;s got an init part</p>



<p class="wp-block-paragraph">and a check part and an update part. And I&#8217;ve kind of added that as a comment just to remind myself</p>



<p class="wp-block-paragraph">of the way my mind is supposed to be wrapped around this concept. And so I made another comment</p>



<p class="wp-block-paragraph">here with four and then empty parentheses just to denote that some comments that come below</p>



<p class="wp-block-paragraph">I guess the top of the for loop</p>



<p class="wp-block-paragraph">So now I&#8217;m going to make a label here and what I like to do with my labels is I like to</p>



<p class="wp-block-paragraph">prefix them with the name of the function that I&#8217;m currently in and then an underscore and that kind of helps me keep track of</p>



<p class="wp-block-paragraph">My symbols a little bit more easily, especially if I have a large module the symbols are less likely to overlap if I prefix them with the</p>



<p class="wp-block-paragraph">The function names</p>



<p class="wp-block-paragraph">to be Fortest underscore something and since I&#8217;m only doing one thing inside of the Fortest function</p>



<p class="wp-block-paragraph">I&#8217;m just going to have a suffix only for the most part but you can imagine if you had more</p>



<p class="wp-block-paragraph">parts inside of your function and it started getting a little cluttered you might want to have</p>



<p class="wp-block-paragraph">you know another label you know that just sort of another I guess like part to your label that names</p>



<p class="wp-block-paragraph">the part of your function you&#8217;re in although this is assembly it gets really hard really fast so</p>



<p class="wp-block-paragraph">really hard really fast so if your function is even a little bit complicated you should probably</p>



<p class="wp-block-paragraph">consider breaking it up into multiple functions if you can. We can&#8217;t really do that at this point</p>



<p class="wp-block-paragraph">because it&#8217;s just a for loop but keep that in mind. Okay so we&#8217;re going to initialize. Remember</p>



<p class="wp-block-paragraph">the first thing we had to do to initialize was you know setting i to zero or you know whatever</p>



<p class="wp-block-paragraph">it is that we&#8217;re going to set up in the init part so I&#8217;m just going to do that here. I&#8217;m going to say</p>



<p class="wp-block-paragraph">That&#8217;s the first part, the update part where we set size type i equals zero.</p>



<p class="wp-block-paragraph">In fact, maybe I could do another copy paste of this.</p>



<p class="wp-block-paragraph">Where instead of using a size type, we&#8217;ll just say that the register r13 equals zero.</p>



<p class="wp-block-paragraph">I know that&#8217;s not going to make sense in a higher level language right now,</p>



<p class="wp-block-paragraph">but just so that the for loop looks a little bit more like assembly.</p>



<p class="wp-block-paragraph">We&#8217;ll keep going as long as r13 is less than r12.</p>



<p class="wp-block-paragraph">and then we increase R13.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to put ink R13</p>



<p class="wp-block-paragraph">so it looks more assembly-like,</p>



<p class="wp-block-paragraph">even though this completely and totally</p>



<p class="wp-block-paragraph">would not compile in C++.</p>



<p class="wp-block-paragraph">I hope that this helps your understanding a little bit.</p>



<p class="wp-block-paragraph">So yeah, we can do everything</p>



<p class="wp-block-paragraph">except for just the increase part at the very top.</p>



<p class="wp-block-paragraph">I&#8217;m going to choose to increase it at the bottom.</p>



<p class="wp-block-paragraph">If you wanted to, I guess you could start off R13</p>



<p class="wp-block-paragraph">as a negative number and then increase it at the top.</p>



<p class="wp-block-paragraph">But I personally don&#8217;t feel that&#8217;s like very clean.</p>



<p class="wp-block-paragraph">feel that&#8217;s like very clean it also forces you to use signed integers maybe you wanted to use an</p>



<p class="wp-block-paragraph">unsigned integer so you could get like a gigantic um maximum number that you looped up to i don&#8217;t</p>



<p class="wp-block-paragraph">know so we&#8217;ll just initialize here and we&#8217;ll say r13 is equal to zero the init part should not be</p>



<p class="wp-block-paragraph">part of the actual like looping like every time you loop up to the top of the for loop you should</p>



<p class="wp-block-paragraph">not repeat that part again it should only happen once so the next label that i have is called loop</p>



<p class="wp-block-paragraph">have is called loop top and that&#8217;s just going to be the top of the loop that I</p>



<p class="wp-block-paragraph">continue to go back up to every time I want to see if we&#8217;re supposed to</p>



<p class="wp-block-paragraph">continue looping and then go into the loops body so maybe I should say that&#8217;s</p>



<p class="wp-block-paragraph">why this is named to underscore loop top and I don&#8217;t know you don&#8217;t have to do</p>



<p class="wp-block-paragraph">camel casing and your labels you know you could just do loop in it or whatever</p>



<p class="wp-block-paragraph">but I&#8217;m just choosing to do it this way so at the top of our loop we&#8217;ll be</p>



<p class="wp-block-paragraph">Remember that was the check part, right?</p>



<p class="wp-block-paragraph">So we&#8217;re going to check to see that R13 is still less than R12.</p>



<p class="wp-block-paragraph">If it is, we&#8217;ll continue with the for loop.</p>



<p class="wp-block-paragraph">If it&#8217;s not, then we jump out of the for loop.</p>



<p class="wp-block-paragraph">So that means probably the true case where R13 is indeed less than R12,</p>



<p class="wp-block-paragraph">that&#8217;s going to be a short jump just into the loop&#8217;s body.</p>



<p class="wp-block-paragraph">And the false case where R13 is not less than R12,</p>



<p class="wp-block-paragraph">that should probably end up being a much longer jump.</p>



<p class="wp-block-paragraph">on how big your your loop is maybe that jump is too long too far away for a conditional branching</p>



<p class="wp-block-paragraph">instruction to reach if you&#8217;ve watched my previous videos you should know already that</p>



<p class="wp-block-paragraph">the conditional branching instructions like jl like jump less than they can only reach about 128</p>



<p class="wp-block-paragraph">bytes away if you try to go further than that the assembler will actually stop and block you from</p>



<p class="wp-block-paragraph">finishing your compilation it&#8217;ll say i think it&#8217;s like jump out of range or something like that</p>



<p class="wp-block-paragraph">So you want to try to keep the short jump points with your conditional branches.</p>



<p class="wp-block-paragraph">And then in the false case, where the conditional branch doesn&#8217;t actually do anything,</p>



<p class="wp-block-paragraph">then it falls through to the next instruction where you will have an unconditional jump.</p>



<p class="wp-block-paragraph">And remember, the unconditional jumps, they don&#8217;t have a limitation of 128 bytes.</p>



<p class="wp-block-paragraph">They can jump like all over the place.</p>



<p class="wp-block-paragraph">Like they can jump anywhere, basically.</p>



<p class="wp-block-paragraph">that means if you think about it, we come in to the loop top right here.</p>



<p class="wp-block-paragraph">We immediately do a compare instruction and a conditional branch.</p>



<p class="wp-block-paragraph">So if R13 is less than R12, meaning we should continue to for loop,</p>



<p class="wp-block-paragraph">then we&#8217;ll just do a short jump into the loop&#8217;s body and actually execute its body.</p>



<p class="wp-block-paragraph">And if not, we will end up falling through to line 92,</p>



<p class="wp-block-paragraph">where there&#8217;s just an unconditional jump instruction that just says,</p>



<p class="wp-block-paragraph">all right, let&#8217;s jump all the way down to being done.</p>



<p class="wp-block-paragraph">Let&#8217;s jump all the way down to being done, which could be very far away for all we know.</p>



<p class="wp-block-paragraph">Okay, so we&#8217;ve done that.</p>



<p class="wp-block-paragraph">Now let&#8217;s implement the loops body because the first thing that we did is we wanted to</p>



<p class="wp-block-paragraph">jump into the loop body to actually execute our instructions, which in this case, we&#8217;ll</p>



<p class="wp-block-paragraph">just be, you know, printing a number every time we loop and then increasing that number.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do a little like new line there.</p>



<p class="wp-block-paragraph">I&#8217;m going to paste the loop body.</p>



<p class="wp-block-paragraph">little comments to help remind myself that this is actually the loop&#8217;s body. So for test loop body</p>



<p class="wp-block-paragraph">that takes care of the branching instruction, hitting on that and actually going into the body.</p>



<p class="wp-block-paragraph">And then later we&#8217;re going to have to implement a label for the loop being done. But for now,</p>



<p class="wp-block-paragraph">we&#8217;ll just say the body. What does it do? It just prints a little message to the user.</p>



<p class="wp-block-paragraph">If you look at the message for current number string, if I go up real fast,</p>



<p class="wp-block-paragraph">message for current number, it&#8217;s just an arrow. So the user&#8217;s going to see an arrow and then</p>



<p class="wp-block-paragraph">So the user is going to see an arrow and then their current number, I guess of the loop&#8217;s</p>



<p class="wp-block-paragraph">current number.</p>



<p class="wp-block-paragraph">And then every time it loops, it&#8217;s just going to continue printing that arrow with a number</p>



<p class="wp-block-paragraph">on it.</p>



<p class="wp-block-paragraph">So we&#8217;re doing that.</p>



<p class="wp-block-paragraph">And then we&#8217;re going to say R13, which is the current counter variable, which started</p>



<p class="wp-block-paragraph">at zero is going to get loaded into RDI, which if you watched my other videos, it&#8217;s just</p>



<p class="wp-block-paragraph">the first integer argument for a function call.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to call this other function here, which is not part of the video to just</p>



<p class="wp-block-paragraph">which is not part of the video to just say hey please print this number for me so the first</p>



<p class="wp-block-paragraph">time this iterates it&#8217;s going to print zero because it&#8217;s going to print r13 and then it&#8217;s</p>



<p class="wp-block-paragraph">going to print a new line so that the cursor goes to the next you know line of the terminal</p>



<p class="wp-block-paragraph">so that&#8217;s all the body does it just kind of like prints the current number with an arrow and does</p>



<p class="wp-block-paragraph">a new line and then at the very bottom of the loop body we just sort of maintain the for loop this is</p>



<p class="wp-block-paragraph">for loop this is going to be the update part so like let&#8217;s see maybe scooch this up a little bit</p>



<p class="wp-block-paragraph">the update part where we have let&#8217;s see on the very right side of the for loop which is like i</p>



<p class="wp-block-paragraph">plus plus or in more assembly speak increasing the counter variable so i&#8217;m just going to increase r13</p>



<p class="wp-block-paragraph">and then that way the loop can progress you know we&#8217;re always looking at r13</p>



<p class="wp-block-paragraph">to decide if we need to stop or not you know we&#8217;re comparing r13 to r12</p>



<p class="wp-block-paragraph">So we&#8217;re just increasing at the very bottom of the loop and then we unconditionally jump back up to the top.</p>



<p class="wp-block-paragraph">Probably a better idea to unconditionally jump back up to the top because maybe the jump to the top is very very far and if it&#8217;s greater than 128 bytes it won&#8217;t work.</p>



<p class="wp-block-paragraph">So the regular jump instruction doesn&#8217;t have that limitation. So now we&#8217;re jumping up to the loop top.</p>



<p class="wp-block-paragraph">So you can imagine now that you know we&#8217;re not going to the initialization part. That would be bad.</p>



<p class="wp-block-paragraph">We&#8217;re just jumping up to the top here where we immediately ask you know are we done?</p>



<p class="wp-block-paragraph">If we&#8217;re not done, we jump into the loop body and print another number.</p>



<p class="wp-block-paragraph">And then at the very bottom of the body, we say, all right, increase the counter and then jump back up to the top.</p>



<p class="wp-block-paragraph">So this is a simple for loop, but you can see what it&#8217;s doing, right?</p>



<p class="wp-block-paragraph">It&#8217;s just going to be printing a number over and over again as the number increases.</p>



<p class="wp-block-paragraph">And then eventually it&#8217;ll stop when it hits the correct number.</p>



<p class="wp-block-paragraph">The last thing we need is the loop done label.</p>



<p class="wp-block-paragraph">If you look back up at the top here on line 92, if R13 was not less than R12,</p>



<p class="wp-block-paragraph">less than r12 then execution would fall through to line 92 and there is our unconditional jump</p>



<p class="wp-block-paragraph">instruction basically saying if r13 is not less than r12 then we&#8217;ll jump to the loop done label</p>



<p class="wp-block-paragraph">which means we&#8217;re just totally finished with this loop so we have to make that real fast</p>



<p class="wp-block-paragraph">and all it&#8217;s going to do is just basically say goodbye it&#8217;s just going to print a message to</p>



<p class="wp-block-paragraph">to the restoration functions.</p>



<p class="wp-block-paragraph">Oh, did I ruin my return somewhere?</p>



<p class="wp-block-paragraph">Uh-oh, what did I do wrong?</p>



<p class="wp-block-paragraph">I lost my return statement.</p>



<p class="wp-block-paragraph">Did I accidentally delete that somehow?</p>



<p class="wp-block-paragraph">That&#8217;s a bad program.</p>



<p class="wp-block-paragraph">Or did I not even…</p>



<p class="wp-block-paragraph">Hmm, I wonder if it like…</p>



<p class="wp-block-paragraph">I wonder if I didn&#8217;t have my return statement</p>



<p class="wp-block-paragraph">and it fell through into the CRLF function</p>



<p class="wp-block-paragraph">and then the CRLF function returned to the caller</p>



<p class="wp-block-paragraph">to the caller on behalf of the for test function I don&#8217;t even know only all only</p>



<p class="wp-block-paragraph">the spirits know at this point I don&#8217;t know leave a comment or something if</p>



<p class="wp-block-paragraph">you know what happened but every function has to have its own return for</p>



<p class="wp-block-paragraph">sure so you know the loop done label is usually where you want to jump to when</p>



<p class="wp-block-paragraph">you know the for loop is finished or the while loop is finished when it comes to</p>



<p class="wp-block-paragraph">saying goodbye it probably would be a little bit more clear of me to add an</p>



<p class="wp-block-paragraph">you know for test you know say goodbye just so that I that I remember that this</p>



<p class="wp-block-paragraph">is this is the place where we&#8217;re done and this other place this is just</p>



<p class="wp-block-paragraph">something else that&#8217;s happening maybe I&#8217;m doing more instructions or more</p>



<p class="wp-block-paragraph">operations or calling a function or whatever so obviously if I did it this</p>



<p class="wp-block-paragraph">way then the loop done label would just end up falling through to the goodbye</p>



<p class="wp-block-paragraph">label and it would be fine but it&#8217;s just more visually clear I&#8217;m gonna take it</p>



<p class="wp-block-paragraph">Anyway, so at this point we might have a program that actually works.</p>



<p class="wp-block-paragraph">Let&#8217;s see if it does.</p>



<p class="wp-block-paragraph">What else do I need to add?</p>



<p class="wp-block-paragraph">No, I think we&#8217;re done with that.</p>



<p class="wp-block-paragraph">Okay, let&#8217;s try it.</p>



<p class="wp-block-paragraph">So we&#8217;ll do a make run and we&#8217;ll enter the number five and let&#8217;s see if it works or it</p>



<p class="wp-block-paragraph">crashes.</p>



<p class="wp-block-paragraph">It worked on the first try.</p>



<p class="wp-block-paragraph">So I had a solution though.</p>



<p class="wp-block-paragraph">Not fair, but I could have typoed.</p>



<p class="wp-block-paragraph">Anyway, so it says we entered a five, the loop will print from zero to N minus one.</p>



<p class="wp-block-paragraph">print from zero to n minus one so we should see from zero to four and so then every iteration of</p>



<p class="wp-block-paragraph">the loop it&#8217;s just that little message we&#8217;re just printing a zero printing one printing a two you</p>



<p class="wp-block-paragraph">know the number increases because we did did that little inc instruction the increase instruction</p>



<p class="wp-block-paragraph">we unconditionally jump to the top of the loop where we decide if we&#8217;re supposed to uh finish</p>



<p class="wp-block-paragraph">you know be done with the loop by jumping to the done label eventually after we uh let&#8217;s see</p>



<p class="wp-block-paragraph">it prints this four here it&#8217;ll do the increase instruction at the bottom of the loop&#8217;s body</p>



<p class="wp-block-paragraph">and then it&#8217;ll jump to the loop&#8217;s top then the loop&#8217;s top will see that it&#8217;s a five because we</p>



<p class="wp-block-paragraph">just increased the four it will see that five is definitely not less than five so that&#8217;s a false</p>



<p class="wp-block-paragraph">which means execution will you know fall through where the heck is that it&#8217;ll fall through to the</p>



<p class="wp-block-paragraph">line 92 jump instruction which is just the loop being done so that&#8217;s here where it says goodbye</p>



<p class="wp-block-paragraph">says goodbye and then we have successfully implemented a basic for loop</p>



<p class="wp-block-paragraph">all right so thank you so much for watching this video I hope you learned</p>



<p class="wp-block-paragraph">a little bit and had a little bit of fun I will see you in the next video happy</p>



<p class="wp-block-paragraph">coding and happy studying hey everybody thanks for watching this video again</p>



<p class="wp-block-paragraph">from the bottom of my heart I really appreciate it I do hope you did learn</p>



<p class="wp-block-paragraph">hope you did learn something and have some fun. If you could do me a please, a small little favor,</p>



<p class="wp-block-paragraph">could you please subscribe and follow this channel or these videos or whatever it is you do on the</p>



<p class="wp-block-paragraph">current social media website that you&#8217;re looking at right now. It would really mean the world to</p>



<p class="wp-block-paragraph">me and it&#8217;ll help make more videos and grow this community. So we&#8217;ll be able to do more videos,</p>



<p class="wp-block-paragraph">longer videos, better videos, or just I&#8217;ll be able to keep making videos in general. So please</p>



<p class="wp-block-paragraph">and subscribe. You know sometimes I&#8217;m sleeping in the middle of the night and I just wake up because</p>



<p class="wp-block-paragraph">I know somebody subscribed or followed. It just wakes me up and I get filled with joy. That&#8217;s</p>



<p class="wp-block-paragraph">exactly what happens every single time. So you could do it as a nice favor to me or you could</p>



<p class="wp-block-paragraph">you could troll me if you want to just wake me up in the middle of the night just subscribe</p>



<p class="wp-block-paragraph">and then I&#8217;ll just wake up. I promise that&#8217;s what will happen. Also if you look at the middle of the</p>



<p class="wp-block-paragraph">screen right now you should see a QR code which you can scan in order to go to the website which</p>



<p class="wp-block-paragraph">at the bottom of this video and it&#8217;ll take you to my main website where you can just kind of like</p>



<p class="wp-block-paragraph">see all the videos I published and the services and tutorials and things that I offer and all</p>



<p class="wp-block-paragraph">that good stuff and if you have a suggestion for clarifications or errata or just future videos</p>



<p class="wp-block-paragraph">that you want to see please leave a comment or if you just want to say hey what&#8217;s up what&#8217;s going on</p>



<p class="wp-block-paragraph">you know just send me a comment whatever I also wake up for those in the middle of the night I get</p>



<p class="wp-block-paragraph">I wake up in a cold sweat and I&#8217;m like,</p>



<p class="wp-block-paragraph">it would really mean the world to me.</p>



<p class="wp-block-paragraph">I would really appreciate it.</p>



<p class="wp-block-paragraph">So again, thank you so much for watching this video</p>



<p class="wp-block-paragraph">and enjoy the cool music as I fade into the darkness,</p>



<p class="wp-block-paragraph">which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-for-loops-explained-yasm-tutorial/">x86-64 Assembly For Loops Explained &#8211; YASM Tutorial</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-for-loops-explained-yasm-tutorial/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>x86-64 Assembly While Loops Explained Step by Step in YASM &#8211; From Concept to Working Program</title>
		<link>https://www.NeuralLantern.com/x86-64-assembly-while-loops-explained-step-by-step-in-yasm-from-concept-to-working-program/</link>
					<comments>https://www.NeuralLantern.com/x86-64-assembly-while-loops-explained-step-by-step-in-yasm-from-concept-to-working-program/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 25 Jan 2026 01:02:57 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly control flow]]></category>
		<category><![CDATA[assembly language loops]]></category>
		<category><![CDATA[assembly language tutorial]]></category>
		<category><![CDATA[assembly while loop example]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[conditional jump assembly]]></category>
		<category><![CDATA[learn assembly language]]></category>
		<category><![CDATA[low level loops]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[NASM vs YASM]]></category>
		<category><![CDATA[systems programming]]></category>
		<category><![CDATA[unconditional jump]]></category>
		<category><![CDATA[while loop assembly]]></category>
		<category><![CDATA[x86 assembly loops]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[x86-64 linux assembly]]></category>
		<category><![CDATA[x86-64 programming]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=291</guid>

					<description><![CDATA[<p>This video teaches how to implement a classic while loop structure in x86-64 assembly language using YASM. We cover the conceptual mapping from C-style while(condition) { body } to assembly labels (while_top, while_body, while_done), conditional jumps (jne, je, etc.), and the unconditional jump back to the top of the loop. A complete runnable example repeatedly reads integers from the user and echoes them until 99 is entered.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-while-loops-explained-step-by-step-in-yasm-from-concept-to-working-program/">x86-64 Assembly While Loops Explained Step by Step in YASM &#8211; From Concept to Working Program</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="x86-64 Assembly While Loops Explained Step by Step in YASM - From Concept to Working Program" width="1380" height="776" src="https://www.youtube.com/embed/JnkyFMw09Us?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">In this detailed tutorial I walk you through exactly how to implement while loops in x86-64 assembly language using YASM. We start with the basic concept of a while loop as it exists in higher-level languages like C/C++, then break it down into labels, conditional jumps, and unconditional jumps so you can see exactly how the control flow works at the assembly level.</p>



<p class="wp-block-paragraph">I explain why we usually put the condition check at the top, how to handle the jump distances (especially the ~128-byte limit of conditional branches), why unconditional jumps are preferred for the loop-back, and how to structure init / top / body / done sections clearly.</p>



<p class="wp-block-paragraph">We then build a complete, runnable hybrid program (assembly + tiny C++ driver) that:</p>



<ul class="wp-block-list">
<li>prints an intro message</li>



<li>repeatedly asks the user to enter numbers</li>



<li>echoes each number back</li>



<li>continues until the user enters 99</li>



<li>prints a goodbye message when finished</li>
</ul>



<p class="wp-block-paragraph">Lots of practical tips about label naming conventions, register preservation (R12 in this case), and debugging flow are included along the way.</p>



<p class="wp-block-paragraph">Assumed knowledge: basic x86-64 assembly, how to use a makefile, simple system calls, and calling external functions. If you&#8217;re new to those topics, check my earlier videos first.</p>



<p class="wp-block-paragraph">Hope this helps someone finally &#8220;get&#8221; while loops in assembly!<br>Thanks for watching &#8211; subscribe if these kinds of low-level explanations are useful to you.</p>



<p class="wp-block-paragraph">Introduction to While Loops in x86-64 YASM 00:00:00<br>While Loop Concept in High-Level Languages 00:00:56<br>Breaking Down While Loop Structure 00:02:00<br>Labeling Key Sections Top Body Done 00:02:40<br>Conditional and Unconditional Jumps Explained 00:03:26<br>Why Prefer Shorter Conditional Jumps 00:04:40<br>Diagram of While Loop Flow 00:06:52<br>Alternative While True with Internal Break 00:08:08<br>Do-While vs Regular While Difference 00:09:32<br>Program Setup and Data Section Overview 00:09:53<br>Hybrid Program Structure and External Functions 00:10:56<br>Main Function and While Test Call 00:12:56<br>While Test Function Prologue 00:13:21<br>CRLF Helper Function 00:14:00<br>Intro Message and Loop Initialization 00:14:50<br>While Top Comparison with 99 00:16:57<br>Entering the Loop Body 00:19:08<br>User Input and Echo Output 00:19:38<br>Jump Back to While Top 00:20:29<br>Exit to While Done Section 00:21:39<br>Goodbye Message and Function Epilogue 00:22:14<br>Live Demo Running the Program 00:22:30<br>Summary and Closing Remarks 00:23:12<br>Call to Subscribe and Website Mention 00:23:46</p>



<p class="wp-block-paragraph">=-=-=-=-=-=-=-=-=</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>



<li>Twitter / X: https://x.com/NeuralLantern</li>



<li>Rumble: https://rumble.com/c/c-3696939</li>



<li>BitChute: https://www.bitchute.com/channel/pg1Pvv5dN4Gt</li>



<li>Daily Motion: https://www.dailymotion.com/neurallantern</li>



<li>Minds: https://www.minds.com/neurallantern/</li>



<li>Odysee: https://odysee.com/@NeuralLantern:5</li>
</ul>



<p class="wp-block-paragraph">Please show your support!</p>



<ul class="wp-block-list">
<li>Buy me a coffee: https://ko-fi.com/neurallantern</li>



<li>Subscribe + Sharing on Social Media</li>



<li>Leave a comment or suggestion</li>



<li>Subscribe to Blog: https://www.NeuralLantern.com</li>



<li>Watching the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p class="wp-block-paragraph">All right, hello there. In this video, I&#8217;m going to teach you how to implement while loops in YASM</p>



<p class="wp-block-paragraph">x86-64 assembly. Although you probably don&#8217;t need to be using YASM assembly to understand</p>



<p class="wp-block-paragraph">this video or to benefit from it. So what am I talking about? First off, okay, so we&#8217;re going</p>



<p class="wp-block-paragraph">to do while loops. There&#8217;s a lot of assumed knowledge in this video. If you don&#8217;t know how</p>



<p class="wp-block-paragraph">assembly program, if you don&#8217;t know how to make a make file, if you don&#8217;t know a lot</p>



<p class="wp-block-paragraph">of the basics that I&#8217;m just skimming over in this video, you&#8217;ll need to see my other</p>



<p class="wp-block-paragraph">videos because I explain all of those things at length.</p>



<p class="wp-block-paragraph">For this video, I&#8217;m just going to be talking about while loops only.</p>



<p class="wp-block-paragraph">So for starters, let&#8217;s see here and open up a little notepad here and just type a while</p>



<p class="wp-block-paragraph">loop and try to explain a little bit about it conceptually.</p>



<p class="wp-block-paragraph">And then after that, we&#8217;re going to, we&#8217;re going to write a program in Yasm to show that</p>



<p class="wp-block-paragraph">we can get this to work.</p>



<p class="wp-block-paragraph">So what is a while loop in a higher level language?</p>



<p class="wp-block-paragraph">We&#8217;ll just suppose C or C++.</p>



<p class="wp-block-paragraph">You can imagine whatever language you want, but typically in a while loop,</p>



<p class="wp-block-paragraph">we&#8217;re not talking about a do while loop, although that would be really easy</p>



<p class="wp-block-paragraph">to implement after watching this video.</p>



<p class="wp-block-paragraph">But in a regular while loop, you have the keyword while usually,</p>



<p class="wp-block-paragraph">and then you have some sort of an expression like does a equal B or whatever.</p>



<p class="wp-block-paragraph">So maybe I&#8217;ll just say does a equal B?</p>



<p class="wp-block-paragraph">Expression evaluates to true then the body of the while loop will execute and then when it&#8217;s done executing</p>



<p class="wp-block-paragraph">Execution will go back up to look at the expression again and make sure that it&#8217;s still true</p>



<p class="wp-block-paragraph">So the loop will run forever until that expression evaluates to false</p>



<p class="wp-block-paragraph">Maybe that&#8217;s what you want. Maybe that&#8217;s not what you want, but that&#8217;s what it&#8217;ll do</p>



<p class="wp-block-paragraph">And that means the body runs an endless number of times</p>



<p class="wp-block-paragraph">Also, if the expression evaluates to false on the very first run</p>



<p class="wp-block-paragraph">then the body of the while loop will just never run at all.</p>



<p class="wp-block-paragraph">So that&#8217;s, you know, a basic idea of a while loop in a higher level language.</p>



<p class="wp-block-paragraph">Hopefully you kind of already know that.</p>



<p class="wp-block-paragraph">So let&#8217;s look at the parts right here.</p>



<p class="wp-block-paragraph">If we imagine this as assembly,</p>



<p class="wp-block-paragraph">then probably the first thing we should do is label the part</p>



<p class="wp-block-paragraph">where we look at the expression and decide whether we&#8217;re going to continue or not, right?</p>



<p class="wp-block-paragraph">So I&#8217;m going to put a little label here</p>



<p class="wp-block-paragraph">and maybe we&#8217;ll call it a while top or something.</p>



<p class="wp-block-paragraph">You know, whatever you want to do.</p>



<p class="wp-block-paragraph">just to label the part of the code</p>



<p class="wp-block-paragraph">So that we know when we write assembly</p>



<p class="wp-block-paragraph">We&#8217;ll be able to put the appropriate labels on the different parts of the while loop and we&#8217;ll be able to imagine</p>



<p class="wp-block-paragraph">The while loop that we&#8217;re implementing in assembly</p>



<p class="wp-block-paragraph">So I&#8217;m gonna say this is the top or you can call it the eval part whatever you want</p>



<p class="wp-block-paragraph">Then we&#8217;ll make another label down here. We&#8217;ll call it</p>



<p class="wp-block-paragraph">You know while body something like that and so basically if we want to jump up to the top of the while loop to evaluate the</p>



<p class="wp-block-paragraph">expression, we just have to basically jump to that label called while top.</p>



<p class="wp-block-paragraph">And if we want to jump to the body to actually execute the code,</p>



<p class="wp-block-paragraph">then we just have to jump to that other label instead. We should also have a label at the</p>



<p class="wp-block-paragraph">very bottom after the whole while loop is over and we&#8217;ll call it, you know, while finished</p>



<p class="wp-block-paragraph">or while, whoops, finished or while done or while over something like that. So I&#8217;m just going to</p>



<p class="wp-block-paragraph">Now you can imagine that when we&#8217;re at the top of the while loop and we see that the</p>



<p class="wp-block-paragraph">expression is false, then we can just jump down to the done label, thereby exiting the</p>



<p class="wp-block-paragraph">entire while loop.</p>



<p class="wp-block-paragraph">Then of course we probably want to have some jump statements in here.</p>



<p class="wp-block-paragraph">So for example, the expression part here, it&#8217;s probably going to jump either to the</p>



<p class="wp-block-paragraph">body or either to the done area based on whether or not the expression evaluated to true or</p>



<p class="wp-block-paragraph">false.</p>



<p class="wp-block-paragraph">label while top you know which contains a conditional branch and then like an unconditional</p>



<p class="wp-block-paragraph">jump so if a condition is true maybe we can jump into the body if the condition is false we&#8217;ll fall</p>



<p class="wp-block-paragraph">through to the next statement and do an unconditional jump to the done label if you don&#8217;t</p>



<p class="wp-block-paragraph">know conditional branching this is another one of the topics that i&#8217;ve covered in previous videos</p>



<p class="wp-block-paragraph">as we implement the while loop.</p>



<p class="wp-block-paragraph">So just keep in mind, there are other videos that I have</p>



<p class="wp-block-paragraph">that you should watch first</p>



<p class="wp-block-paragraph">if you don&#8217;t understand conditional branching or jumps.</p>



<p class="wp-block-paragraph">The reason that I&#8217;m going to kind of design</p>



<p class="wp-block-paragraph">the jumps like this where I&#8217;m going to say,</p>



<p class="wp-block-paragraph">let&#8217;s do a conditional branch.</p>



<p class="wp-block-paragraph">And basically if the loop evaluates to true,</p>



<p class="wp-block-paragraph">we&#8217;ll jump into the body of the loop.</p>



<p class="wp-block-paragraph">And if it doesn&#8217;t evaluate to true,</p>



<p class="wp-block-paragraph">then the conditional branch will allow execution</p>



<p class="wp-block-paragraph">to fall through down to the next statement,</p>



<p class="wp-block-paragraph">which will simply contain a jump to the done portion.</p>



<p class="wp-block-paragraph">to the done portion so basically as soon as the while loop is ready to break it gets uh implemented</p>



<p class="wp-block-paragraph">by an unconditional jump which was fallen through to by the conditional branch that would only jump</p>



<p class="wp-block-paragraph">into the body if uh expression was true i&#8217;m doing it this way because it&#8217;s usually a shorter jump</p>



<p class="wp-block-paragraph">uh to jump to the true area you know the the body of the while loop and remember conditional</p>



<p class="wp-block-paragraph">branching instructions have a maximum you know jump reach of about 128 bytes so if you try to</p>



<p class="wp-block-paragraph">128 bytes. So if you try to jump too far, like for example, if you wanted to, you could reverse</p>



<p class="wp-block-paragraph">the logic to where you could say, if an expression is true, let&#8217;s jump to the done portion and say,</p>



<p class="wp-block-paragraph">we&#8217;re going to break the loop. If the expression was false, we&#8217;ll jump into the body. You could</p>



<p class="wp-block-paragraph">do that. But then if the body of your while loop was too long, maybe it was so long that the last</p>



<p class="wp-block-paragraph">instruction was greater than 128 bytes away from the top. Then by the time you wanted to</p>



<p class="wp-block-paragraph">let&#8217;s see yeah if you have if you have too long of while loops you can&#8217;t jump more than 128</p>



<p class="wp-block-paragraph">instructions away so that&#8217;s a limitation of contentional branching so I just I want to put</p>



<p class="wp-block-paragraph">the biggest jump on an unconditional jump instruction because it doesn&#8217;t have a limitation</p>



<p class="wp-block-paragraph">like that I was having a hard time imagining what I was about to say just now for the</p>



<p class="wp-block-paragraph">the reversal I guess I&#8217;m just going to ignore the reversal scenario and we&#8217;ll just do it the</p>



<p class="wp-block-paragraph">and we&#8217;ll just do it the regular way.</p>



<p class="wp-block-paragraph">Anyway, so we want to have a conditional jump that either jumps into the body or to the done area.</p>



<p class="wp-block-paragraph">And then at the very bottom, we want to have an unconditional jump to the top,</p>



<p class="wp-block-paragraph">which basically means every time we get to the bottom of the while loop body,</p>



<p class="wp-block-paragraph">we&#8217;re just going to jump up to the while top.</p>



<p class="wp-block-paragraph">Actually, I&#8217;ll just say to while top.</p>



<p class="wp-block-paragraph">Remember again, the regular jump instructions,</p>



<p class="wp-block-paragraph">maybe I&#8217;ll take out the U here.</p>



<p class="wp-block-paragraph">The regular jump instructions don&#8217;t have a limitation</p>



<p class="wp-block-paragraph">on how far they can jump.</p>



<p class="wp-block-paragraph">So that should be fine.</p>



<p class="wp-block-paragraph">And that&#8217;s the basic idea of implementing a while loop.</p>



<p class="wp-block-paragraph">Maybe I should draw just a quick diagram</p>



<p class="wp-block-paragraph">before we start looking at the code,</p>



<p class="wp-block-paragraph">just to make sure everybody of different learning styles</p>



<p class="wp-block-paragraph">understands what I&#8217;m talking about.</p>



<p class="wp-block-paragraph">So we&#8217;ll, let&#8217;s see, eval.</p>



<p class="wp-block-paragraph">I&#8217;m still adjusting my pen and you know what?</p>



<p class="wp-block-paragraph">You know what? It looked a lot better before I hit record.</p>



<p class="wp-block-paragraph">Eval the expression.</p>



<p class="wp-block-paragraph">So I&#8217;m going to first evaluate the expression.</p>



<p class="wp-block-paragraph">If it evaluates to true, we&#8217;ll put a green arrow here and I&#8217;ll put like a T for true.</p>



<p class="wp-block-paragraph">Then this will be the body of the while loop.</p>



<p class="wp-block-paragraph">I&#8217;ll say like the body.</p>



<p class="wp-block-paragraph">If it evaluates to false, let&#8217;s see.</p>



<p class="wp-block-paragraph">I&#8217;ll just put that in red and I&#8217;ll put an F here then this is just going to be the done area</p>



<p class="wp-block-paragraph">right so you can imagine we evaluate an expression using the compare instruction in assembly and</p>



<p class="wp-block-paragraph">based on the results of comparing something whatever it is that your condition is then we</p>



<p class="wp-block-paragraph">you know we we either branch to the done area or we branch to the to the body area</p>



<p class="wp-block-paragraph">want to have a more complicated while loop and you don&#8217;t want to put a huge</p>



<p class="wp-block-paragraph">amount of expressions or if you don&#8217;t want to logically concatenate a bunch of</p>



<p class="wp-block-paragraph">different components into the expression you know that&#8217;s fair I usually write my</p>



<p class="wp-block-paragraph">while loops in real life as just while true and then I break when certain</p>



<p class="wp-block-paragraph">conditions are met you can do that too we&#8217;re not going to talk about that in</p>



<p class="wp-block-paragraph">this video but you could just have a while true so you always jump to the top</p>



<p class="wp-block-paragraph">and there&#8217;s no branching that goes to while done but then throughout the loop</p>



<p class="wp-block-paragraph">loop you can just sort of check to see if certain conditions are met and if they are then you&#8217;ll do</p>



<p class="wp-block-paragraph">a branch to the done area and if they&#8217;re not then by the time you make it down to the bottom of the</p>



<p class="wp-block-paragraph">loop then it just automatically jumps to the top although you got to make sure with the the reach</p>



<p class="wp-block-paragraph">of conditional branching you probably want to test to see if your condition is true or false or</p>



<p class="wp-block-paragraph">whatever if it means we&#8217;re going to continue the loop then you probably just want to do a conditional</p>



<p class="wp-block-paragraph">then you probably just want to do a conditional branch that jumps down a little bit</p>



<p class="wp-block-paragraph">so that it can continue the body of the loop.</p>



<p class="wp-block-paragraph">And then the part that it skipped over,</p>



<p class="wp-block-paragraph">just a little part should be an unconditional jump that jumps out of the loop.</p>



<p class="wp-block-paragraph">That way, it doesn&#8217;t matter how big your while loop body is,</p>



<p class="wp-block-paragraph">you can always jump out of the loop.</p>



<p class="wp-block-paragraph">But we&#8217;re not going to talk about that in this video.</p>



<p class="wp-block-paragraph">Anyway, so we evaluate the expression.</p>



<p class="wp-block-paragraph">If it&#8217;s true, we go to the body.</p>



<p class="wp-block-paragraph">If it&#8217;s false, we go to done.</p>



<p class="wp-block-paragraph">After the body is done executing,</p>



<p class="wp-block-paragraph">then we just jump back up to the top where we evaluate the expression.</p>



<p class="wp-block-paragraph">where we evaluate the expression.</p>



<p class="wp-block-paragraph">I&#8217;ll put while here just to make it more clear that we&#8217;re talking about the basic</p>



<p class="wp-block-paragraph">idea of a while loop.</p>



<p class="wp-block-paragraph">Not too hard, you know.</p>



<p class="wp-block-paragraph">And then if you wanted to implement a do while loop, just make sure that you always</p>



<p class="wp-block-paragraph">evaluate the body at least once.</p>



<p class="wp-block-paragraph">That&#8217;s really the only difference.</p>



<p class="wp-block-paragraph">But that&#8217;s up to you.</p>



<p class="wp-block-paragraph">This is just a regular while loop video.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So we&#8217;ve kind of talked about it a little bit.</p>



<p class="wp-block-paragraph">We&#8217;ve sort of, you know, drawn it out in a diagram and put some regular code in.</p>



<p class="wp-block-paragraph">and put some regular code in let&#8217;s set up an actual assembly program that will do this so for starters</p>



<p class="wp-block-paragraph">again i&#8217;m not going to show you my make file because i&#8217;ve already made other videos where i</p>



<p class="wp-block-paragraph">explained how to make a make file from scratch same thing goes for hybrid programs i&#8217;m going</p>



<p class="wp-block-paragraph">to have a driver which is a c plus plus module that just sort of calls on this assembly module</p>



<p class="wp-block-paragraph">if you want to know how to make hybrid programs or you know drivers or you know whatever see my</p>



<p class="wp-block-paragraph">to be talking about looping only. Okay, so let me get my solution up here. My source code is called</p>



<p class="wp-block-paragraph">looper. And I&#8217;m just going to copy paste the data section for my program real fast, just so you can</p>



<p class="wp-block-paragraph">see it. Okay, so here&#8217;s the data section. Again, this is not a basics for assembly video. If you</p>



<p class="wp-block-paragraph">don&#8217;t know how to do a data section in Yasm, see my other videos. But for now, I&#8217;m just going to say</p>



<p class="wp-block-paragraph">like the actual array of bytes and then a length and all I&#8217;m doing is printing out messages you</p>



<p class="wp-block-paragraph">know begin the wild test your numbers will be printed back to you until you decide to quit so</p>



<p class="wp-block-paragraph">that&#8217;s going to be printed to the user right away then every time the program wants a number from</p>



<p class="wp-block-paragraph">the user it&#8217;ll just ask you know please enter a number or 99 to quit and then it&#8217;ll prefix</p>



<p class="wp-block-paragraph">the echo back it&#8217;ll say you entered and then it will actually print the number and then when you</p>



<p class="wp-block-paragraph">we&#8217;re done and i&#8217;m going to use an external uh function that i have available to just kind of</p>



<p class="wp-block-paragraph">like input and output numbers this video is not about uh input and outputting and external libraries</p>



<p class="wp-block-paragraph">if you&#8217;re interested you could probably just link a hybrid program and use printf and scan</p>



<p class="wp-block-paragraph">f to very easily do input and output but that&#8217;s not what this video is about see my other videos</p>



<p class="wp-block-paragraph">then i have crlf which is just a carriage return in line feed uh you know select the cursor goes</p>



<p class="wp-block-paragraph">You know select the cursor goes to the next line and then I have like a system call code to just print and</p>



<p class="wp-block-paragraph">Then a file descriptor just to print a standard output again. That&#8217;s explained in other videos</p>



<p class="wp-block-paragraph">So now I&#8217;m ready to start copy pasting the main portion of my program</p>



<p class="wp-block-paragraph">So first off I&#8217;m going to start the text section, which is where the instructions go in the ASM</p>



<p class="wp-block-paragraph">So there it is and then I&#8217;m going to copy paste</p>



<p class="wp-block-paragraph">of external symbols. So I just have a little library that I&#8217;m using that makes it easier for</p>



<p class="wp-block-paragraph">me to input and output integers. So you can do this any way you want, or you can hard code a</p>



<p class="wp-block-paragraph">number in the globals area. If you don&#8217;t want to deal with input and output while you&#8217;re learning</p>



<p class="wp-block-paragraph">how to do loops, that&#8217;s totally fine. But then the entry point is going to be a function called</p>



<p class="wp-block-paragraph">looper. So this is my function. And again, this is a hybrid program. So the main function or the</p>



<p class="wp-block-paragraph">be present in my assembly module the driver is going to be a c plus plus module that just calls</p>



<p class="wp-block-paragraph">on a function named looper so that means this function is going to get called from another</p>



<p class="wp-block-paragraph">module and that&#8217;s why i have to mark it as global and then it is a function so i&#8217;m going to call</p>



<p class="wp-block-paragraph">return at the end of it to say we&#8217;re done and then within this function i&#8217;m just going to call two</p>



<p class="wp-block-paragraph">other functions that i&#8217;m about to create one is called while test which is going to actually do</p>



<p class="wp-block-paragraph">the while loop and the other is called crlf which just print which just prints a new line for me i</p>



<p class="wp-block-paragraph">which just prints a new line for me.</p>



<p class="wp-block-paragraph">I don&#8217;t know why I do it that way, but I want to.</p>



<p class="wp-block-paragraph">Sorry, not sorry.</p>



<p class="wp-block-paragraph">So this is the real meat of the function here.</p>



<p class="wp-block-paragraph">Let&#8217;s start, well, the real meat of the code or the video.</p>



<p class="wp-block-paragraph">Let&#8217;s start a function called while test.</p>



<p class="wp-block-paragraph">You can see it&#8217;s got a void signature with no arguments,</p>



<p class="wp-block-paragraph">so it doesn&#8217;t really, you know, take anything or return anything.</p>



<p class="wp-block-paragraph">I&#8217;m going to use register R12 to hold the user&#8217;s input</p>



<p class="wp-block-paragraph">so that I can, you know, print it out and stuff.</p>



<p class="wp-block-paragraph">print it out and stuff so that means I have to do a preservation of R12 because it&#8217;s a</p>



<p class="wp-block-paragraph">callee saved for the ABI which you should respect so I&#8217;m going to push it at the beginning</p>



<p class="wp-block-paragraph">and then I&#8217;m going to pop it at the end and then this is a function so I have to return</p>



<p class="wp-block-paragraph">at the end of the function let me just double check that there&#8217;s nothing else weird at the</p>



<p class="wp-block-paragraph">bottom of that nope okay so we&#8217;ve got the prologue and epilogue the wild test function</p>



<p class="wp-block-paragraph">maybe I should copy paste my crlf real fast my crlf function it&#8217;s just sad but</p>



<p class="wp-block-paragraph">it&#8217;s also kind of cute right it does nothing except just print out a new line</p>



<p class="wp-block-paragraph">that&#8217;s all it does this video is not about that so at this point I should</p>



<p class="wp-block-paragraph">have a program that probably works let&#8217;s see if it does I&#8217;m gonna do clear and</p>



<p class="wp-block-paragraph">make run again if you want to know how to use make files or compile or link or</p>



<p class="wp-block-paragraph">anything like that see my other videos so I&#8217;m gonna run it and it just says</p>



<p class="wp-block-paragraph">which is some code that we&#8217;re not looking at.</p>



<p class="wp-block-paragraph">And then it says it&#8217;s regain control.</p>



<p class="wp-block-paragraph">And this line in the middle, which is just an empty new line,</p>



<p class="wp-block-paragraph">that&#8217;s definitely from the assembly program.</p>



<p class="wp-block-paragraph">Because at the top here we have CRLF.</p>



<p class="wp-block-paragraph">If I call it multiple times CRLF,</p>



<p class="wp-block-paragraph">then you&#8217;ll see there are multiple blank lines.</p>



<p class="wp-block-paragraph">All right, I&#8217;m gonna take that out.</p>



<p class="wp-block-paragraph">Now let&#8217;s continue with while test.</p>



<p class="wp-block-paragraph">So what should we do here?</p>



<p class="wp-block-paragraph">The first thing that we should do is print an intro message</p>



<p class="wp-block-paragraph">message just to let the user know that we&#8217;re about to you know begin our while</p>



<p class="wp-block-paragraph">tests and if we run the program one more time we should see that message now</p>



<p class="wp-block-paragraph">begin the while test your numbers will be printed back to you system calls and</p>



<p class="wp-block-paragraph">simple printing is covered in other videos so now uh you know for me I kind</p>



<p class="wp-block-paragraph">of like to init all of my loops even if it&#8217;s a while loop and not just a for</p>



<p class="wp-block-paragraph">loop or anything so I always have an extra label that I like to call init</p>



<p class="wp-block-paragraph">or before or something like that.</p>



<p class="wp-block-paragraph">So I have a label now called while test underscore init,</p>



<p class="wp-block-paragraph">and it&#8217;s just where I&#8217;m gonna initialize</p>



<p class="wp-block-paragraph">whatever it is that I think I need to initialize</p>



<p class="wp-block-paragraph">so that the loop will actually work.</p>



<p class="wp-block-paragraph">If you look here, all I&#8217;m really doing is setting R12 to zero</p>



<p class="wp-block-paragraph">because what I&#8217;m gonna do is stop the loop.</p>



<p class="wp-block-paragraph">I&#8217;m gonna break the loop whenever the user enters a 99.</p>



<p class="wp-block-paragraph">So I don&#8217;t know what&#8217;s inside of R12</p>



<p class="wp-block-paragraph">when we first start this function.</p>



<p class="wp-block-paragraph">And I just wanna make sure that it&#8217;s not 99,</p>



<p class="wp-block-paragraph">the stopping number at the very start so I&#8217;m just going to set it to zero. So</p>



<p class="wp-block-paragraph">another note about my labels you don&#8217;t have to do it this way but I love to</p>



<p class="wp-block-paragraph">write labels where the first part of the label is always the function that the</p>



<p class="wp-block-paragraph">label is inside of so notice how the function is named while test and so my</p>



<p class="wp-block-paragraph">label is always while test underscore something and I&#8217;m putting init here</p>



<p class="wp-block-paragraph">just to say we&#8217;re initializing the loop but if you had a function that had a</p>



<p class="wp-block-paragraph">had a lot of stuff going on in it for starters you should probably be breaking up that function</p>



<p class="wp-block-paragraph">into multiple functions but but assuming you didn&#8217;t uh you should probably do another underscore</p>



<p class="wp-block-paragraph">and then like another component and then another underscore based on what giant chunk of your</p>



<p class="wp-block-paragraph">function you&#8217;re inside of so if there was like an if part a while part an input part an output part</p>



<p class="wp-block-paragraph">you probably want to you know stick that into your labels your labels will get huge but for me</p>



<p class="wp-block-paragraph">infused in assembly and this makes it easier.</p>



<p class="wp-block-paragraph">So we&#8217;re going to initialize so that we can run our while loop and then the next thing</p>



<p class="wp-block-paragraph">we&#8217;re going to do is implement the top of the while loop.</p>



<p class="wp-block-paragraph">So remember if we looked at this code up here, maybe if I drag this over to the side and</p>



<p class="wp-block-paragraph">pin it to the top for a little while, you can see that the top of the while loop is</p>



<p class="wp-block-paragraph">where we kind of evaluate the expression to see if we need to keep going or not.</p>



<p class="wp-block-paragraph">jump into the ending area, the done area. So the top, whoops, the top is always for that.</p>



<p class="wp-block-paragraph">We&#8217;ll say first, I&#8217;m going to ask, are we done? So, you know, how do we know if we&#8217;re done?</p>



<p class="wp-block-paragraph">In this particular while loop, we want to compare the user&#8217;s input, which is R12 to the number 99.</p>



<p class="wp-block-paragraph">And if it&#8217;s equal, then we will quit, which means also if it&#8217;s not equal, we will jump into the</p>



<p class="wp-block-paragraph">So by the way, you&#8217;re probably wondering how did R12 get the user&#8217;s input?</p>



<p class="wp-block-paragraph">Well, we&#8217;re going to do that as the next step.</p>



<p class="wp-block-paragraph">And of course, your design pattern may vary a little bit.</p>



<p class="wp-block-paragraph">No, that&#8217;s okay.</p>



<p class="wp-block-paragraph">I just like to implement it this way.</p>



<p class="wp-block-paragraph">So first thing we&#8217;re going to do is compare R12 with 99.</p>



<p class="wp-block-paragraph">And that&#8217;s why I&#8217;ve written this comment here, just like the blank while comparison part.</p>



<p class="wp-block-paragraph">And I guess I could have put this R12 not equal to 99 up at the top, but then it kind</p>



<p class="wp-block-paragraph">of feels like I&#8217;m leaving these other two instructions.</p>



<p class="wp-block-paragraph">So I moved it down one.</p>



<p class="wp-block-paragraph">these other two instructions so I moved it down one so basically as long as you know we compare</p>



<p class="wp-block-paragraph">R12 and 99 compare and conditional branching is covered in other videos but we compare those two</p>



<p class="wp-block-paragraph">values and then we say if R12 is not equal to 99 then jump to the body and we expect that the body</p>



<p class="wp-block-paragraph">will be like a short jump which will be within the range of a conditional branch</p>



<p class="wp-block-paragraph">So anyway, if the not equal branch didn&#8217;t happen, that means R12 is equal to 99.</p>



<p class="wp-block-paragraph">At that point, execution falls through to line 84, and we&#8217;ll just unconditionally jump out of the while loop.</p>



<p class="wp-block-paragraph">So basically we&#8217;re saying, if it&#8217;s true, we continue looping.</p>



<p class="wp-block-paragraph">If it&#8217;s not true, we just jump outside of the loop.</p>



<p class="wp-block-paragraph">We&#8217;re just totally done with the while loop.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So then we need a body, because obviously we&#8217;re going to jump into the body here.</p>



<p class="wp-block-paragraph">that means I&#8217;m going to just copy paste another little set of code here.</p>



<p class="wp-block-paragraph">Right after that unconditional jump.</p>



<p class="wp-block-paragraph">So now we got the body.</p>



<p class="wp-block-paragraph">Notice how I put a little comment here that has a brace just to indicate to you,</p>



<p class="wp-block-paragraph">hey, this is the beginning of the actual while loop body,</p>



<p class="wp-block-paragraph">just to make it a little bit more clear.</p>



<p class="wp-block-paragraph">And what are we going to do inside of the body?</p>



<p class="wp-block-paragraph">We&#8217;re just going to ask the user for some input.</p>



<p class="wp-block-paragraph">So I&#8217;m printing a simple message here,</p>



<p class="wp-block-paragraph">and then I&#8217;m calling on my helper function</p>



<p class="wp-block-paragraph">to just actually input a number from the user.</p>



<p class="wp-block-paragraph">I&#8217;m going to store that number into R12.</p>



<p class="wp-block-paragraph">So that&#8217;s how R12 gets the numbers, gets the user&#8217;s input.</p>



<p class="wp-block-paragraph">And the way I&#8217;ve written this, if the user enters a 99,</p>



<p class="wp-block-paragraph">it&#8217;ll echo it back to the user and then it&#8217;ll break the loop afterwards.</p>



<p class="wp-block-paragraph">So, you know, if you wanted to rearrange things like I talked about before,</p>



<p class="wp-block-paragraph">where you input before you check to see if you&#8217;re going to keep going, you could do that.</p>



<p class="wp-block-paragraph">But it would be a little harder to echo the user&#8217;s input back to them before you break.</p>



<p class="wp-block-paragraph">I don&#8217;t know. It&#8217;s up to you.</p>



<p class="wp-block-paragraph">I don&#8217;t know it&#8217;s up to you anyway so we grab input from the user and then we print another</p>



<p class="wp-block-paragraph">message basically saying here&#8217;s the thing that you inputted no problem and then again we use</p>



<p class="wp-block-paragraph">one of my helper functions to actually spit the number back out at them so this is not</p>



<p class="wp-block-paragraph">a very complicated body it&#8217;s just asking for a number and then printing the number back to them</p>



<p class="wp-block-paragraph">and then after that I&#8217;m going to do another label and I&#8217;m going to call it the body bottom</p>



<p class="wp-block-paragraph">we don&#8217;t really have to do this label but for clarity I think it&#8217;s probably a good idea</p>



<p class="wp-block-paragraph">I think it&#8217;s probably a good idea.</p>



<p class="wp-block-paragraph">So the very bottom of the while loop&#8217;s body is usually where you don&#8217;t do any more instructions</p>



<p class="wp-block-paragraph">that are part of the work of the while loop&#8217;s body,</p>



<p class="wp-block-paragraph">but just sort of the place where you jump back up to the top</p>



<p class="wp-block-paragraph">so you can evaluate and decide to continue or not again.</p>



<p class="wp-block-paragraph">So notice how I&#8217;m using an unconditional jump here.</p>



<p class="wp-block-paragraph">That&#8217;s a good idea because again, if you have like a huge while loop body,</p>



<p class="wp-block-paragraph">you might end up surpassing the threshold of 128 bytes</p>



<p class="wp-block-paragraph">of 128 bytes and then you&#8217;ll get a assembler error that says</p>



<p class="wp-block-paragraph">I can never remember this. It&#8217;s like a</p>



<p class="wp-block-paragraph">jump out of range error or something like that. Basically, if you do a conditional branch to jump up the top</p>



<p class="wp-block-paragraph">because some people like to check to see</p>



<p class="wp-block-paragraph">if they should continue the loop, they like to check for that at the bottom. I&#8217;ve done that before.</p>



<p class="wp-block-paragraph">And then if true, then we&#8217;ll jump to the top of the loop. But if the loop is too big, that won&#8217;t work. So</p>



<p class="wp-block-paragraph">I just like to take a long jump to the top of the loop and then decide if I&#8217;m going to keep going at the very top.</p>



<p class="wp-block-paragraph">if I&#8217;m going to keep going at the very top. And then there&#8217;s a comment saying, hey, that&#8217;s the</p>



<p class="wp-block-paragraph">end of the body. Okay, no problem. Now let&#8217;s do the done label, which is basically where we jump</p>



<p class="wp-block-paragraph">if the loop is actually finished. So remember, if this expression right here evaluates to false,</p>



<p class="wp-block-paragraph">then execution is going to fall through to line 84, where we jump to this while test done label.</p>



<p class="wp-block-paragraph">And so I&#8217;m just going to put the while test done label right here. So we&#8217;re done. So the done is</p>



<p class="wp-block-paragraph">So we&#8217;re done. So the done is not part of the loop. It comes after the loop. That&#8217;s this right</p>



<p class="wp-block-paragraph">here on line nine of the little notepad. And we can just kind of do whatever we want. At that</p>



<p class="wp-block-paragraph">point, we can return to a caller, we can just do other stuff, we can, you know, do a different</p>



<p class="wp-block-paragraph">loop or, you know, whatever, we&#8217;re just done with the original loop. For me, I&#8217;m just going to say</p>



<p class="wp-block-paragraph">goodbye with this little print to just sort of like, you know, print an exit message. And then</p>



<p class="wp-block-paragraph">we&#8217;ll do the epilogue where we restore R12. And then we just return to the caller,</p>



<p class="wp-block-paragraph">the driver worry about exiting the program okay so if we&#8217;ve done this correctly we should now have</p>



<p class="wp-block-paragraph">a working program let&#8217;s see let&#8217;s see okay let&#8217;s enter a number let&#8217;s do 22 and it says you enter</p>



<p class="wp-block-paragraph">22 and let&#8217;s do 55 and we just we can enter any numbers we want and as long as we&#8217;re not entering</p>



<p class="wp-block-paragraph">99 the program will just continue forever so this is a while loop if i want to quit i do 99</p>



<p class="wp-block-paragraph">breaks at the top it breaks by jumping down to the done area where we print our goodbye message</p>



<p class="wp-block-paragraph">which just is end while test and then if we run this again if i do 99 from the start then it just</p>



<p class="wp-block-paragraph">immediately breaks you know it prints out what you entered but then it immediately breaks</p>



<p class="wp-block-paragraph">and that&#8217;s it that&#8217;s uh the basics for how to write a while loop you just use basically</p>



<p class="wp-block-paragraph">layered on top of an abstract concept of what you think a while loop is,</p>



<p class="wp-block-paragraph">or what I guess the world thinks a while loop is.</p>



<p class="wp-block-paragraph">Okay, thank you so much for watching this video.</p>



<p class="wp-block-paragraph">I hope you learned a little bit of stuff and had a little bit of fun.</p>



<p class="wp-block-paragraph">I&#8217;ll see you in the next video.</p>



<p class="wp-block-paragraph">Hey everybody.</p>



<p class="wp-block-paragraph">Thanks for watching this video again from the bottom of my heart.</p>



<p class="wp-block-paragraph">I really appreciate it.</p>



<p class="wp-block-paragraph">I do hope you did learn something and have some fun.</p>



<p class="wp-block-paragraph">if you could do me a please a small little favor could you please subscribe and follow this channel</p>



<p class="wp-block-paragraph">or these videos or whatever it is you do on the current social media website that you&#8217;re looking</p>



<p class="wp-block-paragraph">at right now it would really mean the world to me and it&#8217;ll help make more videos and grow this</p>



<p class="wp-block-paragraph">community so we&#8217;ll be able to do more videos longer videos better videos or just i&#8217;ll be able</p>



<p class="wp-block-paragraph">to keep making videos in general so please do do me a kindness and uh and subscribe you know</p>



<p class="wp-block-paragraph">I&#8217;m sleeping in the middle of the night and I just wake up because I know somebody subscribed or followed.</p>



<p class="wp-block-paragraph">It just wakes me up and I get filled with joy.</p>



<p class="wp-block-paragraph">That&#8217;s exactly what happens every single time.</p>



<p class="wp-block-paragraph">So you could do it as a nice favor to me or you could troll me if you want to just wake me up in the middle of the night.</p>



<p class="wp-block-paragraph">Just subscribe and then I&#8217;ll just wake up.</p>



<p class="wp-block-paragraph">I promise that&#8217;s what will happen.</p>



<p class="wp-block-paragraph">Also, if you look at the middle of the screen right now, you should see a QR code which you can scan in order to go to the website,</p>



<p class="wp-block-paragraph">which I think is also named somewhere at the bottom of this video.</p>



<p class="wp-block-paragraph">and it&#8217;ll take you to my main website where you can just kind of like see all the videos</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 uh uh clarifications or errata or just future videos that you want to</p>



<p class="wp-block-paragraph">see please leave a comment or if you just want to say hey what&#8217;s up what&#8217;s going on you know</p>



<p class="wp-block-paragraph">just send me a comment whatever i also wake up for those in the middle of the night i get</p>



<p class="wp-block-paragraph">it would really mean the world to me. I would really appreciate it.</p>



<p class="wp-block-paragraph">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</p>



<p class="wp-block-paragraph">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-while-loops-explained-step-by-step-in-yasm-from-concept-to-working-program/">x86-64 Assembly While Loops Explained Step by Step in YASM &#8211; From Concept to Working Program</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-while-loops-explained-step-by-step-in-yasm-from-concept-to-working-program/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Learn Conditional Branching in x86-64 Assembly (YASM/NASM) &#8211; CMP, JE, JNE, JL, JG &#038; More</title>
		<link>https://www.NeuralLantern.com/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/</link>
					<comments>https://www.NeuralLantern.com/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 18 Jan 2026 00:49:38 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly language tutorial]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[cmp instruction]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[conditional branching]]></category>
		<category><![CDATA[conditional jumps]]></category>
		<category><![CDATA[Ed Jorgensen]]></category>
		<category><![CDATA[JE JNE]]></category>
		<category><![CDATA[JL JG]]></category>
		<category><![CDATA[jump out of range]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[NASM tutorial]]></category>
		<category><![CDATA[operating systems programming]]></category>
		<category><![CDATA[reverse engineering]]></category>
		<category><![CDATA[signed unsigned jumps]]></category>
		<category><![CDATA[x86 assembly]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=287</guid>

					<description><![CDATA[<p>Learn how conditional branching really works in x86-64 assembly using CMP and conditional jumps like JE, JNE, JL, JG. See signed vs unsigned comparison instructions and real code examples that show how if-statements are implemented at the machine level.</p>
<p>The post <a href="https://www.NeuralLantern.com/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/">Learn Conditional Branching in x86-64 Assembly (YASM/NASM) &#8211; CMP, JE, JNE, JL, JG &amp; More</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Learn Conditional Branching in x86-64 Assembly (YASM/NASM) - CMP, JE, JNE, JL, JG &amp; More" width="1380" height="776" src="https://www.youtube.com/embed/aB_BOp0KCh8?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">In this practical x86-64 assembly language tutorial we dive deep into conditional branching using YASM (also works with NASM) on Ubuntu/Linux.</p>



<p class="wp-block-paragraph">We cover:</p>



<ul class="wp-block-list">
<li>The CMP instruction and how it sets the RFLAGS register</li>



<li>All major conditional jump instructions (JE, JNE, JL, JLE, JG, JGE, JB, JBE…)</li>



<li>Important difference between signed (JL/JG) vs unsigned (JB/JA) jumps</li>



<li>Real code examples showing how to implement if-style decisions</li>



<li>How to handle the &#8220;jump out of range&#8221; assembler error</li>



<li>Visual flow diagrams explaining control flow</li>
</ul>



<p class="wp-block-paragraph">Perfect for students learning low-level programming, reverse engineering, operating systems, or anyone who wants to understand how if-statements, comparisons, and decisions are actually implemented at the machine level.</p>



<p class="wp-block-paragraph">Highly recommended companion resource: Professor Ed Jorgensen&#8217;s free x86-64 Assembly Language Programming book (linked in comments).</p>



<p class="wp-block-paragraph">Enjoy learning assembly!</p>



<p class="wp-block-paragraph">Intro to Conditional Branching in Assembly 00:00:00<br>Recommended Book: x86-64 Guide by Ed Jorgensen 00:00:23<br>Unconditional vs Conditional Jumps 00:01:39<br>How Conditional Branching Works 00:02:06<br>The CMP Instruction Explained 00:03:21<br>Overview of Conditional Jump Instructions 00:04:50<br>Signed vs Unsigned Jump Instructions 00:06:10<br>Jump Equal &amp; Jump Not Equal (JE/JNE) 00:07:00<br>Main Conditional Jumps Summary 00:08:14<br>Starting the Code Example 00:09:09<br>Program Structure &amp; Printing Setup 00:10:35<br>Function Prologue &amp; Getting User Input 00:13:21<br>First Branch: Is Number Greater Than 0? 00:15:17<br>Live Demo: Testing Greater Than Zero 00:21:53<br>Second Branch: Is Number Less Than 10? 00:22:52<br>Live Demo: Testing Range 0-10 00:26:06<br>Third Branch: Is Number == 5? 00:26:52<br>Live Demo: Testing Equal to 5 00:28:00<br>Jump Out of Range Error &amp; Workaround 00:29:00<br>Final Summary &amp; Closing 00:32:17<br>Thanks &amp; Call to Subscribe 00:32:57</p>



<p class="wp-block-paragraph">=-=-=-=-=-=-=-=-=</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>



<li>Twitter / X: https://x.com/NeuralLantern</li>



<li>Rumble: https://rumble.com/c/c-3696939</li>



<li>BitChute: https://www.bitchute.com/channel/pg1Pvv5dN4Gt</li>



<li>Daily Motion: https://www.dailymotion.com/neurallantern</li>



<li>Minds: https://www.minds.com/neurallantern/</li>



<li>Odysee: https://odysee.com/@NeuralLantern:5</li>
</ul>



<p class="wp-block-paragraph">Please show your support!</p>



<ul class="wp-block-list">
<li>Buy me a coffee: https://ko-fi.com/neurallantern</li>



<li>Subscribe + Sharing on Social Media</li>



<li>Leave a comment or suggestion</li>



<li>Subscribe to Blog: https://www.NeuralLantern.com</li>



<li>Watching the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p class="wp-block-paragraph">Hey there. In this video,</p>



<p class="wp-block-paragraph">we&#8217;re going to talk about conditional branching in an assembly program.</p>



<p class="wp-block-paragraph">We&#8217;re going to be looking at YASM x86-64 assembly or AMD 64 assembly</p>



<p class="wp-block-paragraph">within Ubuntu, but this will probably be a useful video.</p>



<p class="wp-block-paragraph">If you&#8217;re just thinking about how to branch in assembly in general.</p>



<p class="wp-block-paragraph">So for starters, I&#8217;m going to promote one of my favorite books,</p>



<p class="wp-block-paragraph">as I often do. This is a book that is free and open source. You can get a copy yourself. I did</p>



<p class="wp-block-paragraph">not write this. The author of this book is a genius. His name is Professor Ed Jorgensen, PhD.</p>



<p class="wp-block-paragraph">He made this whole book and released it under an open source license. So you can literally just go</p>



<p class="wp-block-paragraph">to his website and get a copy of this for free. You can use this to make yourself into an assembly</p>



<p class="wp-block-paragraph">expert. So I highly recommend this to everybody. Anyway, so I&#8217;m going to open up this book. I&#8217;m</p>



<p class="wp-block-paragraph">up this book i&#8217;m going to assume that maybe you&#8217;ll get a copy or you&#8217;re just going to follow along</p>



<p class="wp-block-paragraph">with what i&#8217;m doing on the screen but i&#8217;m going to open this book and i&#8217;m going to go to section 7</p>



<p class="wp-block-paragraph">which is entitled instruction set overview i&#8217;m going to double click it so open out open the</p>



<p class="wp-block-paragraph">subsections and then i&#8217;m going to click on 7.7 control control instructions and then under that</p>



<p class="wp-block-paragraph">i&#8217;m going to click on 7.7.3 conditional control instructions so uh that&#8217;s a lot of subsections</p>



<p class="wp-block-paragraph">So that&#8217;s a lot of subsections, isn&#8217;t it? I love a book with a ton of subsections. It makes things so much easier to find</p>



<p class="wp-block-paragraph">If you or someone you know is going to write a book, especially if it&#8217;s technical, please use lots and lots of subsections</p>



<p class="wp-block-paragraph">Anyway, so conditional control instructions in my last video</p>



<p class="wp-block-paragraph">We talked about the jump instruction where we can just be sitting somewhere in our program and then just jump somewhere else</p>



<p class="wp-block-paragraph">Anywhere we want based on the label so conditional branching or conditional control instructions</p>



<p class="wp-block-paragraph">conditional control instructions are kind of the same thing except they will</p>



<p class="wp-block-paragraph">decide whether or not to actually jump they might fall through to the next</p>



<p class="wp-block-paragraph">instruction or they might jump away to somewhere else so conditional branching</p>



<p class="wp-block-paragraph">or conditional jumping it basically kind of goes like this step one you compare</p>



<p class="wp-block-paragraph">two values to each other they will probably be sitting in registers and then</p>



<p class="wp-block-paragraph">conditional branching instruction or a conditional jump instruction there&#8217;s</p>



<p class="wp-block-paragraph">lots of synonyms for these but basically in the regular jump instruction that I</p>



<p class="wp-block-paragraph">just talked about in the last video you just jumped and it was only one thing</p>



<p class="wp-block-paragraph">that you had to do but in this case we first have to compare two items and then</p>



<p class="wp-block-paragraph">we&#8217;ll issue the jumping instruction because the jumping instruction will</p>



<p class="wp-block-paragraph">have to decide whether to jump or not based on the result of your comparison</p>



<p class="wp-block-paragraph">which makes sense right like if you&#8217;re going to implement something that&#8217;s like</p>



<p class="wp-block-paragraph">if you know I&#8217;ll put like a expr for expression then well the first thing that we have to decide</p>



<p class="wp-block-paragraph">before we jump either into the block of the if or if we jump into the block of the else is whether</p>



<p class="wp-block-paragraph">or not that expression is true right so that&#8217;s kind of what the compare instruction is doing</p>



<p class="wp-block-paragraph">sort of in higher level languages like c++ it can get a lot more complicated than that but you know</p>



<p class="wp-block-paragraph">Okay, so we&#8217;re going to do conditional branching instructions.</p>



<p class="wp-block-paragraph">Let me show you a few of those.</p>



<p class="wp-block-paragraph">Okay, so for starters, here&#8217;s the compare instruction right here on page 126.</p>



<p class="wp-block-paragraph">I think it&#8217;s actually 127 in the PDF, but basically, you know, 126.</p>



<p class="wp-block-paragraph">We do CMP followed by two operands.</p>



<p class="wp-block-paragraph">So if we want to compare, I don&#8217;t know, let&#8217;s say R12 with R13, that&#8217;s how you would do it.</p>



<p class="wp-block-paragraph">just a two operand instruction there&#8217;s no results that are going to get stored in one of these</p>



<p class="wp-block-paragraph">operands both of these operands are just for input the result actually gets stored in a special</p>



<p class="wp-block-paragraph">register called r flags which we hardly ever use or look at in in the most common assembly</p>



<p class="wp-block-paragraph">instructions and design patterns we just kind of like let the r flags do whatever it&#8217;s going to do</p>



<p class="wp-block-paragraph">and the compare instruction will fill that up so imagine that you know depending on whether r13 is</p>



<p class="wp-block-paragraph">is like greater than r12 or less than or they&#8217;re equal or something else is going on between the</p>



<p class="wp-block-paragraph">two of them the compare instruction just fills up all sorts of flags defining the relationship</p>



<p class="wp-block-paragraph">between those two registers or those two operands r12 and r13 and then after the r flags register</p>



<p class="wp-block-paragraph">is all set up then the conditional branching instruction will sort of rely on r flags in</p>



<p class="wp-block-paragraph">order to decide whether or not it&#8217;s going to jump or not okay so let&#8217;s see there are tons</p>



<p class="wp-block-paragraph">there are tons of conditional well not tons i guess like half a page worth but there are lots</p>



<p class="wp-block-paragraph">of conditional branching instructions we can use the first two i want to show you and after this</p>



<p class="wp-block-paragraph">by the way i&#8217;m going to write a sample program just to show you how it is in practice but so the</p>



<p class="wp-block-paragraph">first one is je which means let&#8217;s jump if the two operands were equal so again you know we compare</p>



<p class="wp-block-paragraph">here we&#8217;ll say sets up r flags and then we&#8217;ll decide like what we&#8217;re going to do are we going</p>



<p class="wp-block-paragraph">to jump if they&#8217;re equal to some label i&#8217;ll write some label here so that means uh the je instruction</p>



<p class="wp-block-paragraph">will look at the r flags register and decide did compare think that r12 and r13 were equal if it</p>



<p class="wp-block-paragraph">did then we&#8217;ll jump if not execution will fall down to the next line so i&#8217;ll put a nope instruction</p>



<p class="wp-block-paragraph">So if they were equal, then we&#8217;ll end up jumping to some label.</p>



<p class="wp-block-paragraph">And if they were not equal, we&#8217;ll end up letting execution fall through to the nope on line 10.</p>



<p class="wp-block-paragraph">And same thing for jump not equal and all these other ones.</p>



<p class="wp-block-paragraph">But there are actually two different versions of some of these instructions that kind of seem the same.</p>



<p class="wp-block-paragraph">Like for instance, this one right here, JL, that means jump if the first operand was less than the second operand.</p>



<p class="wp-block-paragraph">is a jump below that jumps also if the first operand was less than the second operand so</p>



<p class="wp-block-paragraph">what&#8217;s the difference between these two let me get my pen I love excuses to do my little pen now</p>



<p class="wp-block-paragraph">well I&#8217;ve got my pen it&#8217;s working oh I erased it okay okay it&#8217;s working anyway so notice how jump</p>



<p class="wp-block-paragraph">less than and jump below are basically doing the same thing but there&#8217;s one difference one of them</p>



<p class="wp-block-paragraph">One of them works on signed operands and one of them works on unsigned operands.</p>



<p class="wp-block-paragraph">So keep this in mind.</p>



<p class="wp-block-paragraph">If you&#8217;re comparing two signed integers, you want to use this group up here of instructions.</p>



<p class="wp-block-paragraph">But if you&#8217;re using unsigned integers or just bits, then you want to use this group of instructions down here.</p>



<p class="wp-block-paragraph">And you definitely want to know what kind of data you have that you&#8217;re comparing.</p>



<p class="wp-block-paragraph">Notice though that jump equal and jump not equal, they don&#8217;t really care.</p>



<p class="wp-block-paragraph">not equal they don&#8217;t really care if the operands are signed or unsigned that&#8217;s because if you want</p>



<p class="wp-block-paragraph">to check to see if two things are equal you just look at all of their bits and if all of their</p>



<p class="wp-block-paragraph">bits are the same then you just say they&#8217;re equal you don&#8217;t even care actually if it&#8217;s a signed or</p>



<p class="wp-block-paragraph">unsigned integer if it&#8217;s a float if it&#8217;s a whatever you just you just go are all the bits equal or are</p>



<p class="wp-block-paragraph">at least one of the bits unequal then we&#8217;ll say it&#8217;s equal or not equal that&#8217;s it so keep that in</p>



<p class="wp-block-paragraph">mind for all of your jump instructions where you&#8217;re trying to see if things are equal or not</p>



<p class="wp-block-paragraph">or not equal you&#8217;re going to definitely be using these whether you&#8217;re using signed or unsigned</p>



<p class="wp-block-paragraph">integers or floats or anything else like that but when it comes to actually you know integers that</p>



<p class="wp-block-paragraph">are signed or unsigned these are the instructions you use for signed and these are the instructions</p>



<p class="wp-block-paragraph">whoops that you use for what have i done that you&#8217;ll use for unsigned i keep confusing the on</p>



<p class="wp-block-paragraph">off button for the red button is what i&#8217;m doing oh okay whatever i guess we&#8217;re done anyway</p>



<p class="wp-block-paragraph">I guess we&#8217;re done anyway.</p>



<p class="wp-block-paragraph">So keep that in mind.</p>



<p class="wp-block-paragraph">In this video, we&#8217;re just going to be using the signed integer instructions</p>



<p class="wp-block-paragraph">because that&#8217;s the example that I have set up.</p>



<p class="wp-block-paragraph">But just imagine you could very easily translate this to unsigned integers or floats.</p>



<p class="wp-block-paragraph">Okay, so let&#8217;s see.</p>



<p class="wp-block-paragraph">We&#8217;ve got jump less than, we&#8217;ve got jump less than or equal to,</p>



<p class="wp-block-paragraph">then we&#8217;ve got jump greater than, and then we&#8217;ve got jump greater than or equal to.</p>



<p class="wp-block-paragraph">And when you combine those with jump equal to or jump not equal to,</p>



<p class="wp-block-paragraph">jump not equal to that&#8217;s pretty much all the comparison that you need and if you want to do</p>



<p class="wp-block-paragraph">something much much more complex well then you can just you know start stacking logic on top of</p>



<p class="wp-block-paragraph">logic and and branching on top of branching to do something that&#8217;s like this is what i&#8217;m talking</p>



<p class="wp-block-paragraph">like if you want to do this expression and expression or expression and then maybe like a</p>



<p class="wp-block-paragraph">expression, right? If you want to do something super complicated, like we can do in a higher</p>



<p class="wp-block-paragraph">level language, you&#8217;re just going to have to make a bunch of mini jumps or find another</p>



<p class="wp-block-paragraph">way to reduce your logic. But in this video, we&#8217;re just going to be taking a very simple</p>



<p class="wp-block-paragraph">comparison. All right. So I think that&#8217;s everything that I can show you inside of this book. Let&#8217;s</p>



<p class="wp-block-paragraph">go to the code now. So here&#8217;s a little empty code, source code, a page. I&#8217;m going to stick</p>



<p class="wp-block-paragraph">all my code inside of it. Another reminder that if you don&#8217;t know how to write assembly</p>



<p class="wp-block-paragraph">you don&#8217;t know how to write assembly at all you should see my other videos because i&#8217;m not going</p>



<p class="wp-block-paragraph">to cover the basics of assembly in this video if you want to learn how to make a make file or compile</p>



<p class="wp-block-paragraph">or assemble or like do all the basics that i&#8217;m not talking about in this video i&#8217;ve covered them all</p>



<p class="wp-block-paragraph">in my other videos up to this point so for now we&#8217;re just going to focus only on conditional</p>



<p class="wp-block-paragraph">branching conditional jumping inside of an assembly module so i&#8217;m going to start off by setting up my</p>



<p class="wp-block-paragraph">and i&#8217;m gonna say paste and again you know this is covered in other videos but long story short</p>



<p class="wp-block-paragraph">i&#8217;m just going to be printing out a bunch of strings while this program executes so</p>



<p class="wp-block-paragraph">i&#8217;m going to announce that the branching test is going to begin and then i&#8217;m going to ask the user</p>



<p class="wp-block-paragraph">for some input and then i&#8217;m going to based on whatever the user inputs i&#8217;m going to just print</p>



<p class="wp-block-paragraph">out a message that sort of you know understands what the user inputted so if the user inputted</p>



<p class="wp-block-paragraph">something greater than zero it&#8217;s going to output that if they entered something that was less than</p>



<p class="wp-block-paragraph">10 it&#8217;s going to print that if they entered something that was equal to five it&#8217;s going to</p>



<p class="wp-block-paragraph">print that and then it&#8217;s going to say we&#8217;ve ended the branching tests then i have like a crlf again</p>



<p class="wp-block-paragraph">all this stuff is explained in other videos but we&#8217;re just basically printing so i&#8217;m going to</p>



<p class="wp-block-paragraph">start the text section of my program which is where all the instructions are and i&#8217;m going to</p>



<p class="wp-block-paragraph">a function that I can call on. Again, this video does not cover a bunch of other extra stuff. I&#8217;m</p>



<p class="wp-block-paragraph">just going to show you how to branch. Just trust me when I say that this helps me easily print</p>



<p class="wp-block-paragraph">numbers and things, or I guess in this particular case, it&#8217;ll help me input a number from the user.</p>



<p class="wp-block-paragraph">But for you, if you&#8217;re trying to run this at home, you can just start hard coding numbers just to</p>



<p class="wp-block-paragraph">make sure that you can branch correctly and then eventually work your way up to a library that can</p>



<p class="wp-block-paragraph">videos in the distant future. I don&#8217;t know years from now. I don&#8217;t know.</p>



<p class="wp-block-paragraph">I might release the library or some other stuff to the casual viewer.</p>



<p class="wp-block-paragraph">So now here&#8217;s our entry point. Again,</p>



<p class="wp-block-paragraph">I mentioned that there&#8217;s going to be other parts of this program that I&#8217;m not</p>



<p class="wp-block-paragraph">showing. So this is a hybrid program where a C++ driver actually will call on</p>



<p class="wp-block-paragraph">my cool function. So just imagine from somewhere else in the program,</p>



<p class="wp-block-paragraph">you&#8217;re calling on the cool function. How do we know it&#8217;s a function?</p>



<p class="wp-block-paragraph">a label called cool and then it has a return statement at the very end.</p>



<p class="wp-block-paragraph">That&#8217;s pretty much all we need to know right now.</p>



<p class="wp-block-paragraph">Let me get rid of that load.</p>



<p class="wp-block-paragraph">I don&#8217;t know why I always have the word load in all of my things.</p>



<p class="wp-block-paragraph">I think I put it in there on an early example</p>



<p class="wp-block-paragraph">and then I just kept copy pasting to my heart&#8217;s content and never stopped.</p>



<p class="wp-block-paragraph">We mark the function cool as global so that other modules can call on it</p>



<p class="wp-block-paragraph">like the C++ module that I&#8217;m not showing you.</p>



<p class="wp-block-paragraph">But if you&#8217;re writing a pure assembly module,</p>



<p class="wp-block-paragraph">you don&#8217;t really need to mark your functions as global.</p>



<p class="wp-block-paragraph">if everything&#8217;s within the same piece of source code it&#8217;s totally fine so then I&#8217;m going to make</p>



<p class="wp-block-paragraph">a function call to a function called branch test so very quickly before I actually add branch test</p>



<p class="wp-block-paragraph">I&#8217;m just going to add another function that we&#8217;re not covering in this video called crlf and long</p>



<p class="wp-block-paragraph">story short all it does is just it prints a new line in the terminal and that&#8217;s it like it&#8217;s just</p>



<p class="wp-block-paragraph">it&#8217;s a convenience function I like to be able to call crlf and have a new line okay so let&#8217;s start</p>



<p class="wp-block-paragraph">our branch test function so here&#8217;s the signature it is just branch test with a</p>



<p class="wp-block-paragraph">void in front of it I&#8217;m gonna put a ret to say that this is a function that we</p>



<p class="wp-block-paragraph">will return from and I left myself a little note here saying that I&#8217;m gonna</p>



<p class="wp-block-paragraph">use register r12 as the number that the user inputs so that means I&#8217;m gonna have</p>



<p class="wp-block-paragraph">to preserve that because you have to respect the ABI and the ABI says that</p>



<p class="wp-block-paragraph">The ABI says that R12 is a Kali saved register.</p>



<p class="wp-block-paragraph">Again, see my other videos if you don&#8217;t know what I&#8217;m talking about.</p>



<p class="wp-block-paragraph">But I&#8217;m just going to preserve it in a section that I call the prologue, or actually the</p>



<p class="wp-block-paragraph">book calls the prologue.</p>



<p class="wp-block-paragraph">I do that now though too.</p>



<p class="wp-block-paragraph">And, you know, restore it in a section called the epilogue.</p>



<p class="wp-block-paragraph">So we have prologue and epilogue.</p>



<p class="wp-block-paragraph">We can have a push-pop pair.</p>



<p class="wp-block-paragraph">And then in between, we can have, you know, the meat of our function.</p>



<p class="wp-block-paragraph">Okay, so for the introduction, I&#8217;m just going to write a few instructions that just print</p>



<p class="wp-block-paragraph">out a message.</p>



<p class="wp-block-paragraph">print out a message. So we&#8217;re going to print out that intro message and then call</p>



<p class="wp-block-paragraph">CRLF to print a new line and then return. So maybe actually at this point we could</p>



<p class="wp-block-paragraph">probably check to see if this program works by trying to run it.</p>



<p class="wp-block-paragraph">So let me say clear and make run.</p>



<p class="wp-block-paragraph">Okay. So now the driver just prints hello. The driver is</p>



<p class="wp-block-paragraph">elsewhere and we get that welcome message that says begin</p>



<p class="wp-block-paragraph">branching test and then the driver retains control after that. So</p>



<p class="wp-block-paragraph">let&#8217;s do let&#8217;s do a goodbye message real fast so we&#8217;ll do our goodbye message</p>



<p class="wp-block-paragraph">it&#8217;s just another string that I&#8217;m printing it&#8217;s no big deal right now but</p>



<p class="wp-block-paragraph">if I run the program again you can see that begin branching test and end</p>



<p class="wp-block-paragraph">branching test okay so far we&#8217;re not really doing branching yet the next thing</p>



<p class="wp-block-paragraph">we need to do is ask for a number so this is how I&#8217;m going to do it in my</p>



<p class="wp-block-paragraph">this is how I&#8217;m going to do it in my program. In your program it might be different or you might</p>



<p class="wp-block-paragraph">have a hard-coded number or something like that. So for now just trust me when I say that I&#8217;m going</p>



<p class="wp-block-paragraph">to print a prompt basically saying could you please enter a number and then the next two lines</p>



<p class="wp-block-paragraph">are going to be just calling on one of my custom functions that will input a number from the user</p>



<p class="wp-block-paragraph">so the user can type at the terminal and we will receive their number through REX which is the</p>



<p class="wp-block-paragraph">return value register for integer return values and we&#8217;re going to store it in R12. So that&#8217;s why</p>



<p class="wp-block-paragraph">And we&#8217;re going to store it in R12.</p>



<p class="wp-block-paragraph">So that&#8217;s why I had to preserve R12,</p>



<p class="wp-block-paragraph">because I&#8217;m actually going to start,</p>



<p class="wp-block-paragraph">you know, messing with the value of R12 now.</p>



<p class="wp-block-paragraph">Okay, so we ask for a number,</p>



<p class="wp-block-paragraph">and then we sort of don&#8217;t do anything after that.</p>



<p class="wp-block-paragraph">If we run the program again,</p>



<p class="wp-block-paragraph">it&#8217;s just asking for a number,</p>



<p class="wp-block-paragraph">and I can just like type some stuff and hit enter,</p>



<p class="wp-block-paragraph">and then the program ends, nothing actually happens.</p>



<p class="wp-block-paragraph">We&#8217;re building, we&#8217;re building.</p>



<p class="wp-block-paragraph">So now let&#8217;s ask ourselves,</p>



<p class="wp-block-paragraph">is the number greater than zero?</p>



<p class="wp-block-paragraph">So let me copy paste some code here.</p>



<p class="wp-block-paragraph">some code here so right now we&#8217;ve just finished inputting a number from the user and we&#8217;ll ask</p>



<p class="wp-block-paragraph">you know is that number that they inputted greater than zero so again the first thing we do is we use</p>



<p class="wp-block-paragraph">a compare instruction cmp we give it two operands the order doesn&#8217;t necessarily matter um for</p>



<p class="wp-block-paragraph">instance uh right now i&#8217;m asking is r12 greater than zero so i can do a jump less than or equal</p>



<p class="wp-block-paragraph">to like a different branch or i can jump greater than equal to if i switch the operands or if i</p>



<p class="wp-block-paragraph">in a different order, but I&#8217;m just choosing to say, let&#8217;s compare the two and then let&#8217;s jump</p>



<p class="wp-block-paragraph">if R12 is less than or equal to, which basically means not greater than, this label right here,</p>



<p class="wp-block-paragraph">which is just branch test after greater test. So I&#8217;m going to do a print statement right after that</p>



<p class="wp-block-paragraph">and then I&#8217;m going to draw out what the code is kind of doing. So let me make a label here so</p>



<p class="wp-block-paragraph">actually will compile. Whoops. I&#8217;m in the wrong window. Okay. There we go. So what am I saying</p>



<p class="wp-block-paragraph">here? Okay. Do the comparison. And then if the condition seems to be satisfied, then we&#8217;ll jump</p>



<p class="wp-block-paragraph">to this, which means we&#8217;ll just say goodbye and then exit the program because we&#8217;ll return to the</p>



<p class="wp-block-paragraph">caller. But if that condition was not satisfied, then we&#8217;re going to end up executing this stuff</p>



<p class="wp-block-paragraph">in the middle. So think about this. This is kind of the idea. Let&#8217;s see if I can remember how to</p>



<p class="wp-block-paragraph">idea let&#8217;s see if I can remember how to draw this right now off the top of my head suppose we have</p>



<p class="wp-block-paragraph">like some sort of an input I&#8217;ll call this in put and then maybe the input is going to be you know</p>



<p class="wp-block-paragraph">whether r12 and r0 are equal or greater than or whatever so I&#8217;m just gonna say r12 and 0</p>



<p class="wp-block-paragraph">god that&#8217;s awful I really need to practice this draw pad I spend like a whole week practicing</p>



<p class="wp-block-paragraph">whole week practicing with a different draw pad and then when I come back to</p>



<p class="wp-block-paragraph">this one I&#8217;m even worse than when I started so we we come in and we kind of</p>



<p class="wp-block-paragraph">look at our 12 versus zero and then we will jump in one direction if that&#8217;s not</p>



<p class="wp-block-paragraph">you know greater than or equal to or less than or equal to and we&#8217;ll jump in</p>



<p class="wp-block-paragraph">another direction otherwise so basically here we&#8217;ll say jump less than equal to</p>



<p class="wp-block-paragraph">um less than or equal to whoops maybe i&#8217;ll do r12 uh i need to somehow adjust the pressure on this</p>



<p class="wp-block-paragraph">thing r12 less than or equal to and so this is sort of uh has to do with you know this branching</p>



<p class="wp-block-paragraph">path that we take like if r12 was less than or equal to zero then we&#8217;ll take the right branching</p>



<p class="wp-block-paragraph">to after, I&#8217;ll just put the word after,</p>



<p class="wp-block-paragraph">we&#8217;ll jump to the after greater test sub label.</p>



<p class="wp-block-paragraph">And otherwise, if R12 is, sorry,</p>



<p class="wp-block-paragraph">I said less than or equal to, yeah,</p>



<p class="wp-block-paragraph">if R12 is greater than zero,</p>



<p class="wp-block-paragraph">then we jump to this other thing,</p>



<p class="wp-block-paragraph">which is gonna be the fall through.</p>



<p class="wp-block-paragraph">So it&#8217;s gonna be line 96, but I&#8217;ll just say,</p>



<p class="wp-block-paragraph">I&#8217;ll say fall for fall through.</p>



<p class="wp-block-paragraph">basically the jump won&#8217;t happen if it&#8217;s greater than or equal to so you could imagine if you</p>



<p class="wp-block-paragraph">wanted to reverse the logic so you can use a jump greater than instruction you could but I&#8217;m choosing</p>



<p class="wp-block-paragraph">to do it this way and then both of those will eventually reach let&#8217;s see will eventually</p>



<p class="wp-block-paragraph">actually yeah the fall through instructions those will eventually reach the after instruction I&#8217;ll</p>



<p class="wp-block-paragraph">here and then the after instruction will just kind of like finish the program</p>



<p class="wp-block-paragraph">and exit and all that stuff. So if you think about the control path here,</p>



<p class="wp-block-paragraph">we have an input we&#8217;re looking at R12 with the zero with the compare instruction</p>



<p class="wp-block-paragraph">and then we use a conditional branching instruction. So maybe I should write,</p>



<p class="wp-block-paragraph">what do you want to write here? Maybe I&#8217;ll just put JLE up here because usually the compare</p>



<p class="wp-block-paragraph">usually the compare instruction and the conditional jump or branching instructions</p>



<p class="wp-block-paragraph">kind of you know come as a pair so both of these combined mean let&#8217;s look at r12 versus zero and</p>



<p class="wp-block-paragraph">if r12 is less than or equal to zero meaning if it will jump if we&#8217;re less than or equal to the</p>



<p class="wp-block-paragraph">the right operand r12 less than equal to the right operand then we go down this path on the right</p>



<p class="wp-block-paragraph">to the after label if that is false meaning r12 was greater than zero then we&#8217;ll fall through</p>



<p class="wp-block-paragraph">this stuff right here and then whether or not we actually fall through will always end up at the</p>



<p class="wp-block-paragraph">at the after area because we&#8217;re either going to jump directly to the after area or we&#8217;re going</p>



<p class="wp-block-paragraph">to fall through and then the fall through falls through to the after area anyway as well let me</p>



<p class="wp-block-paragraph">just show you that real fast oh man i&#8217;m having a hard time with this okay so let&#8217;s pretend that r12</p>



<p class="wp-block-paragraph">less than or equal to. So that means instructions are coming, you know,</p>



<p class="wp-block-paragraph">instructions are getting executed. We&#8217;re going down and down and down and down and down.</p>



<p class="wp-block-paragraph">Once we see jump less than or equal to, if that&#8217;s true, then we jump to the after label,</p>



<p class="wp-block-paragraph">which means we just kind of jump around this message. And so we&#8217;re not going to exit,</p>



<p class="wp-block-paragraph">sorry, we&#8217;re not going to execute that code. So in the case where that is true,</p>



<p class="wp-block-paragraph">where it is less than or equal to, we&#8217;re not going to execute those instructions.</p>



<p class="wp-block-paragraph">So we&#8217;re not going to say their number was greater than zero. We&#8217;re just going to simply</p>



<p class="wp-block-paragraph">We&#8217;re just going to simply say goodbye.</p>



<p class="wp-block-paragraph">However, on the other hand, if that is not true, meaning if R12 was indeed greater than</p>



<p class="wp-block-paragraph">zero, then we&#8217;re going to fall through because this jump less than or equal to instruction,</p>



<p class="wp-block-paragraph">it will only jump if the R flags, if the comparison instruction thought that R12 was less than</p>



<p class="wp-block-paragraph">or equal to zero.</p>



<p class="wp-block-paragraph">If that&#8217;s not true, if it&#8217;s greater than, then instructions, the control path is just</p>



<p class="wp-block-paragraph">the control path is just going to fall through.</p>



<p class="wp-block-paragraph">So that means we will actually execute these.</p>



<p class="wp-block-paragraph">And then when they&#8217;re done,</p>



<p class="wp-block-paragraph">execution will continue to fall through to the rest.</p>



<p class="wp-block-paragraph">So that means we&#8217;re either going to see a message saying</p>



<p class="wp-block-paragraph">their number was greater than zero or not,</p>



<p class="wp-block-paragraph">based on whether their number actually was greater than zero.</p>



<p class="wp-block-paragraph">Okay, hopefully I explained that somewhat clearly.</p>



<p class="wp-block-paragraph">Now let&#8217;s run the code and see if it works.</p>



<p class="wp-block-paragraph">Hopefully I didn&#8217;t forget anything.</p>



<p class="wp-block-paragraph">This is kind of a partial program at this point.</p>



<p class="wp-block-paragraph">program at this point I&#8217;m gonna add a bunch more stuff to it so hopefully</p>



<p class="wp-block-paragraph">this actually compiles enter an integer five notice how it says your number was</p>



<p class="wp-block-paragraph">greater than zero and if I run it again and I just type zero it does not say</p>



<p class="wp-block-paragraph">that it was greater than zero if I run it again I type like a negative five it</p>



<p class="wp-block-paragraph">also does not say that it was greater than zero you know a huge giant number</p>



<p class="wp-block-paragraph">here it&#8217;ll say your number was greater than zero you could probably type a</p>



<p class="wp-block-paragraph">number that was just absolutely huge and have it overflow I actually don&#8217;t know</p>



<p class="wp-block-paragraph">nines I need to type and I don&#8217;t even know what the behavior is going to be.</p>



<p class="wp-block-paragraph">I don&#8217;t know if it&#8217;s just going to crash the program because this also relies on my ability</p>



<p class="wp-block-paragraph">to input a number which is happening in a different library.</p>



<p class="wp-block-paragraph">Let&#8217;s see if that even works greater than zero.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">It probably overflowed and went to a very, very low negative number.</p>



<p class="wp-block-paragraph">So okay.</p>



<p class="wp-block-paragraph">I guess at least I know my library will probably work sort of.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So let&#8217;s go back to the code here and let&#8217;s upgrade it.</p>



<p class="wp-block-paragraph">how to to check for jump less than or equal to um we&#8217;re going to say goodbye right there so now the</p>



<p class="wp-block-paragraph">next thing we should do is check to see if the number was less than 10. so after the greater test</p>



<p class="wp-block-paragraph">it&#8217;s going to be here so again the same thing like we can just reuse r12 because we&#8217;re not really</p>



<p class="wp-block-paragraph">modifying it and we&#8217;re not calling uh well i guess even if we were calling a function r12 was supposed</p>



<p class="wp-block-paragraph">supposed to be Kali preserved so we&#8217;re just comparing it and then maybe</p>



<p class="wp-block-paragraph">printing a message if we want to and regardless we&#8217;re going to end up at 103</p>



<p class="wp-block-paragraph">this after greater test label so then we&#8217;ll compare again r12 with the number</p>



<p class="wp-block-paragraph">10 and we&#8217;ll say jump if it&#8217;s greater than or equal to branch test after less</p>



<p class="wp-block-paragraph">so now at this point we&#8217;re saying compare r12 with 10 if it&#8217;s greater than or equal to 10 then</p>



<p class="wp-block-paragraph">just skip over the message so if it&#8217;s greater than or equal to 10 then wait a minute wait wait</p>



<p class="wp-block-paragraph">yeah yeah if it&#8217;s greater than or equal to 10 then skip the message otherwise the message</p>



<p class="wp-block-paragraph">that is printed is going to say that their number is less than 10. so again you could imagine a</p>



<p class="wp-block-paragraph">want me to write it out for you I don&#8217;t know I think once is probably enough but let me just do</p>



<p class="wp-block-paragraph">it again we&#8217;ll do input is coming in and it&#8217;s just going to be you know comparing and then</p>



<p class="wp-block-paragraph">jumping greater than or equal to the input is going to be where we&#8217;re just looking to see if</p>



<p class="wp-block-paragraph">R12 how it compares to zero god the number one is awful my entire penmanship is awful okay one</p>



<p class="wp-block-paragraph">we&#8217;ll do r12 and 10 and so that&#8217;s just sort of what we&#8217;re comparing with the compare instruction</p>



<p class="wp-block-paragraph">and then the jump instruction and so if we go on the left i&#8217;m going to say that&#8217;s the fall through</p>



<p class="wp-block-paragraph">path again and if we go to the right it&#8217;s going to be um r12 greater than or equal to 10 is going</p>



<p class="wp-block-paragraph">and then here this is going to be after less I&#8217;ll say the after label the after less label</p>



<p class="wp-block-paragraph">so if we fall through it&#8217;s just going to print a message maybe I should do another little bubble</p>



<p class="wp-block-paragraph">here that&#8217;s just like print p for how about p for print p for print it&#8217;s going to fall through and</p>



<p class="wp-block-paragraph">then it&#8217;s going to print and then it&#8217;s going to go to the after label so you can see that</p>



<p class="wp-block-paragraph">the print instructions are only reached if r12 is not greater than equal to 10 which is the same</p>



<p class="wp-block-paragraph">to 10 which is the same thing as saying it&#8217;s going to fall through if r12 is less than</p>



<p class="wp-block-paragraph">10.</p>



<p class="wp-block-paragraph">And then after we finally get to the after label, then we&#8217;re just going to do, you know,</p>



<p class="wp-block-paragraph">the rest of the instructions.</p>



<p class="wp-block-paragraph">We&#8217;re going to exit the program.</p>



<p class="wp-block-paragraph">We&#8217;re going to say goodbye.</p>



<p class="wp-block-paragraph">We&#8217;re going to do whatever.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So say there never was less than 10.</p>



<p class="wp-block-paragraph">And then regardless, here&#8217;s like the ending label that we were conditionally jumping to.</p>



<p class="wp-block-paragraph">Let&#8217;s see if this works.</p>



<p class="wp-block-paragraph">Make run.</p>



<p class="wp-block-paragraph">So we&#8217;ll enter five.</p>



<p class="wp-block-paragraph">we&#8217;ll enter five notice how it says your number was greater than zero and your</p>



<p class="wp-block-paragraph">number was less than ten let&#8217;s do an 11 I guess it says your number was greater</p>



<p class="wp-block-paragraph">than zero but it does not say that our number was less than ten because 11 is</p>



<p class="wp-block-paragraph">not less than ten if I do a negative three here it says that our number was</p>



<p class="wp-block-paragraph">less than ten but it doesn&#8217;t say that it&#8217;s greater than zero so we have like a</p>



<p class="wp-block-paragraph">lot of decisions that we could make right like imagine if instead of just</p>



<p class="wp-block-paragraph">based on something happening, you could call a full function or skip a bunch of extra code,</p>



<p class="wp-block-paragraph">you know, whatever you want it to do.</p>



<p class="wp-block-paragraph">So I&#8217;m going to go to the next condition where we&#8217;re going to compare R12 to the number five.</p>



<p class="wp-block-paragraph">And we&#8217;re going to ask, you know, is R12 equal to five?</p>



<p class="wp-block-paragraph">So this is going to be the jump not equal branch.</p>



<p class="wp-block-paragraph">And again, you know, if you wanted to say jump, if it&#8217;s equal to the place where we</p>



<p class="wp-block-paragraph">and then maybe like right after that fall through and just do an unconditional jump you can</p>



<p class="wp-block-paragraph">I just think there are less jumps if I do it this way so uh here we&#8217;re just going to print you know</p>



<p class="wp-block-paragraph">your number was equal to five and so basically if it was not equal we should have another label here</p>



<p class="wp-block-paragraph">that allows us to skip that so all right so same thing I&#8217;m not going to draw the diagram this time</p>



<p class="wp-block-paragraph">R12 with 5 and if it&#8217;s not equal to 5 then jump to this label down here which just means</p>



<p class="wp-block-paragraph">let&#8217;s say goodbye and not actually say that their number was equal to 5.</p>



<p class="wp-block-paragraph">But if it was equal to 5 then this JNE conditional branch won&#8217;t actually jump anywhere.</p>



<p class="wp-block-paragraph">Execution will fall through to the next statements which is just printing the message that their</p>



<p class="wp-block-paragraph">number is equal to 5 and then when that&#8217;s done it&#8217;ll fall through to this other label</p>



<p class="wp-block-paragraph">and eventually fall through to the return statement.</p>



<p class="wp-block-paragraph">the whole thing already nice that was easy um let&#8217;s do another run and um let&#8217;s enter the</p>



<p class="wp-block-paragraph">integer one it says one is uh greater than zero and one is less than 10</p>



<p class="wp-block-paragraph">greater than zero and less than 10 is there any way i can get nothing to print out</p>



<p class="wp-block-paragraph">greater than zero and less than 10</p>



<p class="wp-block-paragraph">you don&#8217;t think i can do it let me try it nine no</p>



<p class="wp-block-paragraph">why did I even put 9 okay yeah it&#8217;s always gonna say 0 or 10 okay so let&#8217;s</p>



<p class="wp-block-paragraph">do a 4 we&#8217;ll get both of those greater than 0 and also less than 10 let&#8217;s do a</p>



<p class="wp-block-paragraph">5 now we should get all those messages your number was greater than 0 your</p>



<p class="wp-block-paragraph">number was less than 10 your number was equal to 5 and if I just do a 6 in there</p>



<p class="wp-block-paragraph">again it doesn&#8217;t print your number was equal to 5 and we have to enter an</p>



<p class="wp-block-paragraph">actual 5 for that so that&#8217;s the that&#8217;s the basics of</p>



<p class="wp-block-paragraph">conditional branching oh there&#8217;s one other thing that I wanted to make sure</p>



<p class="wp-block-paragraph">in my previous video where we only talked about jumps bear in mind that</p>



<p class="wp-block-paragraph">these conditional branching statements or instructions you know je j any all</p>



<p class="wp-block-paragraph">these things they have a limited range of jumps of where they can jump to so if</p>



<p class="wp-block-paragraph">you have a gigantic program and you&#8217;re trying to jump very very very far away</p>



<p class="wp-block-paragraph">based on a condition you might have an assembler error where your program won&#8217;t</p>



<p class="wp-block-paragraph">even compile won&#8217;t even assemble telling you i think the message is i wrote it</p>



<p class="wp-block-paragraph">I wrote it down somewhere jump out of range coming from the assembler that basically means that these</p>



<p class="wp-block-paragraph">instructions can only jump about 128 bytes away from whatever instruction they are at or whatever</p>



<p class="wp-block-paragraph">memory location they are at so you know after your assembler assembles your program and after</p>



<p class="wp-block-paragraph">your linker links your program and you just have like a binary you know if you were to inspect</p>



<p class="wp-block-paragraph">all the instructions inside of your executable your binary you would see that they have offsets</p>



<p class="wp-block-paragraph">offsets, right? You know, like one instruction is basically eight bytes away from the one that</p>



<p class="wp-block-paragraph">comes right after it. But some instructions are a lot further away. So if you&#8217;re trying to jump</p>



<p class="wp-block-paragraph">to an instruction that is 100 is more than 128 bytes away, it won&#8217;t work, you&#8217;ll get that error.</p>



<p class="wp-block-paragraph">So how do you solve that problem? Well, maybe I&#8217;ll just write this down real fast. I&#8217;m not going to</p>



<p class="wp-block-paragraph">make this part of the code example. But just imagine we have, you know, jump equal to some</p>



<p class="wp-block-paragraph">and maybe I&#8217;ll do a comment very very far away and here&#8217;s the label some label right</p>



<p class="wp-block-paragraph">so if your assembler says hey you can&#8217;t jump to some label because it&#8217;s too far away it&#8217;s a</p>



<p class="wp-block-paragraph">jump out of range assembler error then basically you can just make some other labels here you can</p>



<p class="wp-block-paragraph">say uh you can say short jump true and short jump false you know just make up some sort of a label</p>



<p class="wp-block-paragraph">label and basically say if it is um well false yeah okay i guess this is the way i&#8217;ll do it this</p>



<p class="wp-block-paragraph">is not super efficient but instead of jumping to the very far away label in the case of true you</p>



<p class="wp-block-paragraph">can just jump to the true label so i&#8217;m going to say jump uh to the short jump true label otherwise</p>



<p class="wp-block-paragraph">To the false label. So now you&#8217;re definitely jumping in either case and both of these labels are close by</p>



<p class="wp-block-paragraph">So then I can pretty much just have the close by true label do an unconditional</p>



<p class="wp-block-paragraph">Jump to some label and that overcomes the limitation of branching instructions</p>



<p class="wp-block-paragraph">So, you know JMP the regular jump instruction</p>



<p class="wp-block-paragraph">It has an unlimited jump that it can do it can go just to like very very very far away instructions</p>



<p class="wp-block-paragraph">And then for the false, you know, I don&#8217;t know, do whatever you want and then jump wherever you want.</p>



<p class="wp-block-paragraph">It doesn&#8217;t really matter.</p>



<p class="wp-block-paragraph">I&#8217;m just trying to show you that if you make a short jump to a label that will only have an unconditional jump,</p>



<p class="wp-block-paragraph">so it&#8217;s sort of like two jumps, you know, like a short jump and then a long jump,</p>



<p class="wp-block-paragraph">then you can overcome that limitation.</p>



<p class="wp-block-paragraph">Again, bearing in mind that some label is supposed to be very, very far away and sort of unreachable.</p>



<p class="wp-block-paragraph">Okay, so let me just double check that I talked about everything that I wanted to.</p>



<p class="wp-block-paragraph">We looked at the branching instructions, the sign in this, I drew a diagram for you.</p>



<p class="wp-block-paragraph">We solved the jump out of range error.</p>



<p class="wp-block-paragraph">Okay, so that&#8217;s, I think that&#8217;s basically it.</p>



<p class="wp-block-paragraph">Hopefully you feel like an expert now at conditional branching and how to make decisions.</p>



<p class="wp-block-paragraph">In future videos, I&#8217;ll talk about how to implement an if else block and how to implement loops</p>



<p class="wp-block-paragraph">and all that stuff.</p>



<p class="wp-block-paragraph">decisions in yasm x86-64 assembly and conceptually you can apply this to other assembly languages too</p>



<p class="wp-block-paragraph">so thanks for watching my video i hope you learned a little bit of stuff and had a little bit of fun</p>



<p class="wp-block-paragraph">i&#8217;ll see you in the next video</p>



<p class="wp-block-paragraph">hey everybody 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 uh if you could do me a please a small little</p>



<p class="wp-block-paragraph">Could you please subscribe and follow this channel or these videos or whatever it is you do on the current social media website that you&#8217;re looking at right now?</p>



<p class="wp-block-paragraph">It would really mean the world to me and it&#8217;ll help make more videos and grow this community.</p>



<p class="wp-block-paragraph">So we&#8217;ll be able to do more videos, longer videos, better videos, or just I&#8217;ll be able to keep making videos in general.</p>



<p class="wp-block-paragraph">So please do me a kindness and subscribe.</p>



<p class="wp-block-paragraph">You know, sometimes I&#8217;m sleeping in the middle of the night and I just wake up because I know somebody subscribed or followed.</p>



<p class="wp-block-paragraph">or followed it just wakes me up and I get filled with joy that&#8217;s exactly what</p>



<p class="wp-block-paragraph">happens every single time so you could do it as a nice favor to me or you could</p>



<p class="wp-block-paragraph">you control me if you want to just wake me up in the middle of the night just</p>



<p class="wp-block-paragraph">subscribe and then I&#8217;ll just wake up I promise that&#8217;s what will happen also if</p>



<p class="wp-block-paragraph">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</p>



<p class="wp-block-paragraph">all the videos i published and the services and tutorials and things that i offer and all that</p>



<p class="wp-block-paragraph">good stuff and uh if you have a suggestion for uh uh clarifications or errata or just future videos</p>



<p class="wp-block-paragraph">that you want to see please leave a comment or if you just want to say hey what&#8217;s up what&#8217;s going on</p>



<p class="wp-block-paragraph">you know just send me a comment whatever i also wake up for those in the middle of the night i</p>



<p class="wp-block-paragraph">get i wake up in a cold sweat and i&#8217;m like it would really it really mean the world to me i</p>



<p class="wp-block-paragraph">appreciate it so again thank you so much for watching this video and enjoy the cool music</p>



<p class="wp-block-paragraph">as as i fade into the darkness which is coming for us all</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/">Learn Conditional Branching in x86-64 Assembly (YASM/NASM) &#8211; CMP, JE, JNE, JL, JG &amp; More</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>x86-64 Assembly Jump Instructions Explained: Unconditional JMP with Full Example in Yasm</title>
		<link>https://www.NeuralLantern.com/x86-64-assembly-jump-instructions-explained-unconditional-jmp-with-full-example-in-yasm/</link>
					<comments>https://www.NeuralLantern.com/x86-64-assembly-jump-instructions-explained-unconditional-jmp-with-full-example-in-yasm/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 11 Jan 2026 01:54:51 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly control flow]]></category>
		<category><![CDATA[assembly jump instructions]]></category>
		<category><![CDATA[assembly language basics]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[ed jorgensen textbook]]></category>
		<category><![CDATA[jmp instruction]]></category>
		<category><![CDATA[learning assembly]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[systems programming]]></category>
		<category><![CDATA[unconditional jump]]></category>
		<category><![CDATA[x86 assembly tutorial]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[x86-64 programming]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=278</guid>

					<description><![CDATA[<p>Learn how unconditional jump instructions (JMP) work in x86-64 assembly language using Yasm. This tutorial explains labels, unlimited jump range, and demonstrates skipping code sections with practical examples. Essential knowledge before studying conditional branching in modern assembly programming.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-jump-instructions-explained-unconditional-jmp-with-full-example-in-yasm/">x86-64 Assembly Jump Instructions Explained: Unconditional JMP with Full Example in Yasm</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="x86-64 Assembly Jump Instructions Explained: Unconditional JMP with Full Example in Yasm" width="1380" height="776" src="https://www.youtube.com/embed/jkc7ydqAp7M?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">In this beginner-to-intermediate assembly language tutorial, we dive deep into unconditional jump instructions (JMP) in x86-64 assembly using Yasm syntax.</p>



<p class="wp-block-paragraph">We cover:</p>



<ul class="wp-block-list">
<li>What unconditional jumps really are (basically a &#8220;go to&#8221; for assembly)</li>



<li>How labels work and how to create them</li>



<li>Why JMP has unlimited range (unlike conditional jumps)</li>



<li>Practical demo showing how to skip code sections using jumps</li>



<li>Comparison between jumping over code vs letting it execute</li>



<li>Quick look at why this matters before learning conditional branching</li>
</ul>



<p class="wp-block-paragraph">We also reference the excellent free open-source textbook by Professor Ed Jorgensen (May 2024 version) which is highly recommended for anyone serious about learning x86-64 assembly.</p>



<p class="wp-block-paragraph">Whether you&#8217;re preparing for university courses, reverse engineering, operating systems development, or just love low-level programming, this video will give you a clear understanding of how unconditional control flow works in modern x86-64 assembly.</p>



<p class="wp-block-paragraph">Next video will cover conditional jumps (je, jne, jg, jl, etc.) and their limitations.</p>



<p class="wp-block-paragraph">Enjoy the video and happy coding at the machine level!</p>



<p class="wp-block-paragraph">Introduction to Jump Instructions 00:00:00<br>Recommended Free Assembly Textbook 00:00:23<br>What Unconditional Jumps Actually Do 00:01:27<br>Labels Explained with Examples 00:02:40<br>Unlimited Jump Range Advantage 00:04:43<br>Overview of the Demonstration Program 00:06:56<br>Building and Running the Jump Test 00:09:21<br>Live Jump Test Demonstration 00:10:53<br>Effect of Removing the Jump Instruction 00:13:50<br>Jumping in Different Directions Example 00:14:58<br>Summary and Next Video Teaser 00:17:28<br>Closing Remarks and Call to Action 00:17:44</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>
</ul>



<p class="wp-block-paragraph">Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



<li>Leaving a comment or suggestion</li>



<li>Subscribing to our Blog</li>



<li>Watching the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p class="wp-block-paragraph">Hello there.</p>



<p class="wp-block-paragraph">In this video, we&#8217;re going to be talking about jump instructions in assembly.</p>



<p class="wp-block-paragraph">This video is going to be about x86-64 Yasm assembly,</p>



<p class="wp-block-paragraph">but I think probably anyone who&#8217;s interested in jump instructions</p>



<p class="wp-block-paragraph">will benefit from this video because the concept is usually the same</p>



<p class="wp-block-paragraph">throughout any system that you might use.</p>



<p class="wp-block-paragraph">So for starters, I want to direct your attention to a textbook</p>



<p class="wp-block-paragraph">that I think is wonderful.</p>



<p class="wp-block-paragraph">This is an open source free textbook that will help you become an expert in assembly.</p>



<p class="wp-block-paragraph">It&#8217;s not mine, I didn&#8217;t write it.</p>



<p class="wp-block-paragraph">The author is Professor Ed Jorgensen, PhD.</p>



<p class="wp-block-paragraph">He releases this textbook for free.</p>



<p class="wp-block-paragraph">It&#8217;s under a copyleft license so you can literally just go to his website and download it and</p>



<p class="wp-block-paragraph">send it to your friends and everything and it&#8217;s wonderful.</p>



<p class="wp-block-paragraph">This book will take you from zero to hero when it comes to Yasm Assembly.</p>



<p class="wp-block-paragraph">It&#8217;s wonderful and amazing.</p>



<p class="wp-block-paragraph">This is the book and I just want to show you the section on jump instructions real fast</p>



<p class="wp-block-paragraph">and then I&#8217;ll talk about them a little bit and then I&#8217;ll show you a sample program that</p>



<p class="wp-block-paragraph">uses jump instructions.</p>



<p class="wp-block-paragraph">So this version of the book that I&#8217;m working with right now is May 2024 version 1.1.56.</p>



<p class="wp-block-paragraph">I&#8217;m going to go down to let&#8217;s see section 7 instruction set overview.</p>



<p class="wp-block-paragraph">Inside of there there&#8217;s another subsection called where the heck is it control instructions</p>



<p class="wp-block-paragraph">instructions 7.7 within that there&#8217;s a another subsection called 7.7.2 that&#8217;s</p>



<p class="wp-block-paragraph">why I look this other not one of the many reasons that I love this book it</p>



<p class="wp-block-paragraph">has so many subsections there&#8217;s just so many yummy subsections to organize</p>



<p class="wp-block-paragraph">everything in a way that you can find it all so fast and okay so we&#8217;re looking</p>



<p class="wp-block-paragraph">at unconditional control instructions in other words a jump instruction if</p>



<p class="wp-block-paragraph">you&#8217;re an old-school programmer especially if you use some sort of like</p>



<p class="wp-block-paragraph">if you use some sort of like a basic language or a language with go-to&#8217;s you might recognize jumps</p>



<p class="wp-block-paragraph">as just being a go-to meaning we&#8217;re not actually going to call a function and then return from it</p>



<p class="wp-block-paragraph">which is what the modern programs tend to do we&#8217;re just going to say let&#8217;s write a jump instruction</p>



<p class="wp-block-paragraph">and we will literally just change execution to to jump to some other location just kind of go there</p>



<p class="wp-block-paragraph">forever maybe we come back but if we do it&#8217;s going to be because there was a different jump instruction</p>



<p class="wp-block-paragraph">instruction that told us to jump back.</p>



<p class="wp-block-paragraph">So we&#8217;re not calling and returning.</p>



<p class="wp-block-paragraph">We&#8217;re just going somewhere and that&#8217;s it.</p>



<p class="wp-block-paragraph">Obviously it&#8217;s a little bit more convenient to be able to call functions,</p>



<p class="wp-block-paragraph">but that&#8217;s sort of like an abstraction that has to be implemented after we</p>



<p class="wp-block-paragraph">understand how to jump. So anyway, the jump instruction is pretty simple.</p>



<p class="wp-block-paragraph">You just put JMP in Yasm anyway, and then follow it with a label.</p>



<p class="wp-block-paragraph">So, you know, just as a quick little recap here, what&#8217;s a label?</p>



<p class="wp-block-paragraph">imagine we have an assembly program here and maybe here&#8217;s our text section and we put some</p>



<p class="wp-block-paragraph">instructions maybe there&#8217;s like an entry point right here I&#8217;ll say a global entry point and</p>



<p class="wp-block-paragraph">literally just taking the word entry point and putting a colon after it now makes that a label</p>



<p class="wp-block-paragraph">so if there are any instructions underneath I&#8217;m gonna put a bunch of nopes then if someone somewhere</p>



<p class="wp-block-paragraph">to say jump entry point they should be able to go right here to instruction 8 and then start</p>



<p class="wp-block-paragraph">executing downward. I guess maybe I didn&#8217;t need to put the global keyword global just means let&#8217;s</p>



<p class="wp-block-paragraph">make this label available to other modules within the same program so if you have a multi-source</p>



<p class="wp-block-paragraph">program or a hybrid program with multiple different languages then you know you should do this but if</p>



<p class="wp-block-paragraph">it&#8217;s just a pure assembly program and there&#8217;s only one source code filed you don&#8217;t need to mark a</p>



<p class="wp-block-paragraph">Just as a quick example here, entry points, I&#8217;ll just put hello as a label and I&#8217;ll say like do exit stuff.</p>



<p class="wp-block-paragraph">So imagine on line 16, you add some instructions just to kind of exit.</p>



<p class="wp-block-paragraph">If I wanted to skip all these nope instructions for some reason, I could just do this.</p>



<p class="wp-block-paragraph">I could say jump hello.</p>



<p class="wp-block-paragraph">And what would happen is execution.</p>



<p class="wp-block-paragraph">Oh, I can use my pen.</p>



<p class="wp-block-paragraph">Execution would just sort of, you know, it would come into the text section.</p>



<p class="wp-block-paragraph">you know, it&#8217;d come into the text section.</p>



<p class="wp-block-paragraph">It would go down through the label and it would execute this first jump</p>



<p class="wp-block-paragraph">instruction and then execution would jump over the nopes into the hello label.</p>



<p class="wp-block-paragraph">And then, you know, if there was other stuff here, then it would get executed.</p>



<p class="wp-block-paragraph">So by jumping over the nopes,</p>



<p class="wp-block-paragraph">I&#8217;m essentially saying that the nopes should not actually end up being</p>



<p class="wp-block-paragraph">executed. They&#8217;ll be there in the program, but they won&#8217;t actually execute.</p>



<p class="wp-block-paragraph">So that&#8217;s the basics of a jump instruction. Okay.</p>



<p class="wp-block-paragraph">So what else do I need to tell you real fast?</p>



<p class="wp-block-paragraph">What else do I need to tell you real fast?</p>



<p class="wp-block-paragraph">Oh, one thing that&#8217;s really good about jump instructions is they have unlimited jump range.</p>



<p class="wp-block-paragraph">So you can jump from a place at the very, very beginning of your assembly program and</p>



<p class="wp-block-paragraph">jump to a place that is at the very, very, very end of your assembly program.</p>



<p class="wp-block-paragraph">There&#8217;s not going to be a limitation on how far you can jump.</p>



<p class="wp-block-paragraph">I mean, in theory, there&#8217;s a limit, but practically speaking, there&#8217;s not a limit.</p>



<p class="wp-block-paragraph">Why would you care that there&#8217;s not a limit?</p>



<p class="wp-block-paragraph">not a limit well because in a future video that i&#8217;m going to release we&#8217;re going to talk about</p>



<p class="wp-block-paragraph">conditional branching which is sort of a jump that only jumps if a certain condition is true</p>



<p class="wp-block-paragraph">and those have limited ranges where they can jump so there&#8217;s going to be a bunch of different</p>



<p class="wp-block-paragraph">instructions but one of the conditional branching instructions is jne and another one is jge and</p>



<p class="wp-block-paragraph">there&#8217;s another one that&#8217;s je basically you know jump if something is equal jump if something is</p>



<p class="wp-block-paragraph">can only jump about 128 bytes away.</p>



<p class="wp-block-paragraph">So after your assembler assembles and compiles</p>



<p class="wp-block-paragraph">down to object code,</p>



<p class="wp-block-paragraph">and then after your linker links your final executable,</p>



<p class="wp-block-paragraph">wherever it is that the instructions happen to end up</p>



<p class="wp-block-paragraph">inside of your program,</p>



<p class="wp-block-paragraph">the conditional jumps,</p>



<p class="wp-block-paragraph">the conditional branching instructions,</p>



<p class="wp-block-paragraph">they can&#8217;t jump more than 128 bytes away</p>



<p class="wp-block-paragraph">to some other instruction.</p>



<p class="wp-block-paragraph">So keep that in mind.</p>



<p class="wp-block-paragraph">Even if later on you graduate</p>



<p class="wp-block-paragraph">to making decisions in your program,</p>



<p class="wp-block-paragraph">like I&#8217;m going to do in the next video,</p>



<p class="wp-block-paragraph">in your program like i&#8217;m going to do in the next video you can only jump so far and if you have to</p>



<p class="wp-block-paragraph">jump too far you actually might not be able to jump at all unless you jump a very short jump</p>



<p class="wp-block-paragraph">to a regular jump instruction and then that jump instruction jumps very very far away that&#8217;s kind</p>



<p class="wp-block-paragraph">of the workaround for it i&#8217;m not going to talk about that in this video though this is not a</p>



<p class="wp-block-paragraph">video for uh conditional branching i just wanted you to be aware of one of the benefits of regular</p>



<p class="wp-block-paragraph">Okay, so we&#8217;re looking at the book here.</p>



<p class="wp-block-paragraph">There&#8217;s not really a whole lot to the jump instruction, just jump and then a label.</p>



<p class="wp-block-paragraph">We talked about its benefit over conditional branch instructions,</p>



<p class="wp-block-paragraph">but we also talked about its, I guess, its shortcoming,</p>



<p class="wp-block-paragraph">meaning it can&#8217;t actually make a decision.</p>



<p class="wp-block-paragraph">It will always jump to a label no matter what.</p>



<p class="wp-block-paragraph">There&#8217;s no condition.</p>



<p class="wp-block-paragraph">So there&#8217;s the book there, and now I&#8217;m going to make a sample program</p>



<p class="wp-block-paragraph">and show you how to run it.</p>



<p class="wp-block-paragraph">I&#8217;m just going to run it.</p>



<p class="wp-block-paragraph">I&#8217;m just gonna run it I&#8217;m show you what it does in order to implement conditional branches so for</p>



<p class="wp-block-paragraph">starters I want you to know that there&#8217;s a make file that I&#8217;ve generated under the hood and we&#8217;re</p>



<p class="wp-block-paragraph">not going to be talking about that in this video this is also a hybrid program so there&#8217;s a C++</p>



<p class="wp-block-paragraph">entry point a driver module under the hood of this we&#8217;re not going to talk about that if you</p>



<p class="wp-block-paragraph">want to know how to make hybrid programs you want to generate make files you want to learn the basics</p>



<p class="wp-block-paragraph">videos for now we&#8217;re only going to be talking about jump instructions so I&#8217;m</p>



<p class="wp-block-paragraph">going to skip a lot of information okay so for starters I&#8217;m going to make a</p>



<p class="wp-block-paragraph">little data section here and again this is explained in other videos but for now</p>



<p class="wp-block-paragraph">we&#8217;ll just trust that we can make a data section that contains strings C strings</p>



<p class="wp-block-paragraph">and other values so pretty much I&#8217;m just going to make a string called begin jump</p>



<p class="wp-block-paragraph">test just to announce to the user that we&#8217;re we&#8217;re going to start doing this</p>



<p class="wp-block-paragraph">We&#8217;re going to start doing this and then I&#8217;m going to make a string called this message</p>



<p class="wp-block-paragraph">should not appear.</p>



<p class="wp-block-paragraph">So in the code, I&#8217;m going to try to print that message, but then I&#8217;m going to jump over</p>



<p class="wp-block-paragraph">the call to print it just to prove to you that there are instructions that would print</p>



<p class="wp-block-paragraph">that message, but we&#8217;re jumping over them with the jump instruction.</p>



<p class="wp-block-paragraph">And then there&#8217;s like an exit message.</p>



<p class="wp-block-paragraph">And then there&#8217;s a CRLF, which is just a carriage return line feed.</p>



<p class="wp-block-paragraph">Again, all of this stuff is in other videos already.</p>



<p class="wp-block-paragraph">So we&#8217;re going to use system call one to print.</p>



<p class="wp-block-paragraph">We&#8217;re going to print a file descriptor one, which is just standard output for your program.</p>



<p class="wp-block-paragraph">Then we&#8217;re going to start the text section where the actual code lives.</p>



<p class="wp-block-paragraph">So this text section is here and it&#8217;s supposed to be at line 37 already.</p>



<p class="wp-block-paragraph">I think I missed a bunch of lines.</p>



<p class="wp-block-paragraph">Oh no, I think I missed some comments.</p>



<p class="wp-block-paragraph">Anyway, so we have a text section here and an entry point and I&#8217;m calling it cool.</p>



<p class="wp-block-paragraph">calling it cool and I am marking it as global because in this particular program that I&#8217;m</p>



<p class="wp-block-paragraph">building it&#8217;s a hybrid program there&#8217;s going to be a C++ module that will call on our cool</p>



<p class="wp-block-paragraph">function so cool has to be global and then I&#8217;m just going to call on a method called jump test</p>



<p class="wp-block-paragraph">I don&#8217;t know I have the words load there I&#8217;m just going to get rid of that real fast locally and in</p>



<p class="wp-block-paragraph">my solution up above and so we&#8217;re going to call a function called jump test and then when we&#8217;re</p>



<p class="wp-block-paragraph">finished we&#8217;re going to return to the caller which is going to be the driver and that&#8217;ll</p>



<p class="wp-block-paragraph">pretty much be it.</p>



<p class="wp-block-paragraph">So if I comment this out real fast, let&#8217;s see,</p>



<p class="wp-block-paragraph">this might actually work.</p>



<p class="wp-block-paragraph">Let&#8217;s see if I can get it to run in the terminal.</p>



<p class="wp-block-paragraph">But there&#8217;s a bunch more code that we have to add, so I&#8217;m not really sure.</p>



<p class="wp-block-paragraph">So let&#8217;s do clear and make run.</p>



<p class="wp-block-paragraph">And it seems to not have a shared object directory.</p>



<p class="wp-block-paragraph">Let me pause the video while I copy paste one of my stupid libraries into the</p>



<p class="wp-block-paragraph">program. You don&#8217;t need this library.</p>



<p class="wp-block-paragraph">It just helps me print things.</p>



<p class="wp-block-paragraph">okay so now I have copy pasted my shared object which allows me to do extra printing stuffs</p>



<p class="wp-block-paragraph">just for just to make this demo easier for me but you don&#8217;t need to know it or you don&#8217;t need to have</p>



<p class="wp-block-paragraph">it to to learn jump instructions anyway so I&#8217;m going to do that again and now it actually prints</p>



<p class="wp-block-paragraph">something okay so hello from the main CPP driver and then it says the driver has regained control</p>



<p class="wp-block-paragraph">make a call to jump test here and then let&#8217;s start the actual jump test function. So I&#8217;m going to do</p>



<p class="wp-block-paragraph">well I guess this thing is kind of short I could copy paste the whole thing all at once.</p>



<p class="wp-block-paragraph">So let&#8217;s do yeah let&#8217;s just jump let&#8217;s just call the whole thing. Okay I&#8217;m going to copy paste the</p>



<p class="wp-block-paragraph">whole thing then I&#8217;ll explain it a little bit to you. So there is a function that I have in here</p>



<p class="wp-block-paragraph">It&#8217;s just a convenience function that I made so I can print a carriage return line feed.</p>



<p class="wp-block-paragraph">The real interesting thing here is the jump test function.</p>



<p class="wp-block-paragraph">So we were just making a call to jump test.</p>



<p class="wp-block-paragraph">Now we&#8217;re making the actual jump test function.</p>



<p class="wp-block-paragraph">It&#8217;s got a signature of just void with no arguments.</p>



<p class="wp-block-paragraph">So it&#8217;s not super interesting from the caller&#8217;s perspective, but it does some stuff.</p>



<p class="wp-block-paragraph">So for starters, it has an intro message.</p>



<p class="wp-block-paragraph">So this will print, you know, hello, welcome to the jump test.</p>



<p class="wp-block-paragraph">jump test. In fact, if I do a return call here,</p>



<p class="wp-block-paragraph">it should actually just print that and do nothing else. Right. Okay.</p>



<p class="wp-block-paragraph">Notice how it printed, begin the jump test.</p>



<p class="wp-block-paragraph">And then right after that,</p>



<p class="wp-block-paragraph">there&#8217;s a jump instruction just proving to you that we can jump over other</p>



<p class="wp-block-paragraph">instructions. So look at this,</p>



<p class="wp-block-paragraph">this piece of code should never actually be called because we&#8217;re going to jump</p>



<p class="wp-block-paragraph">over it. What it is, is it&#8217;s printing that jump shouldn&#8217;t happen message.</p>



<p class="wp-block-paragraph">at the top here jumps shouldn&#8217;t happen so it&#8217;s trying to print out this message should not appear</p>



<p class="wp-block-paragraph">but we&#8217;re going to jump over that by using this jump instruction here on line 66.</p>



<p class="wp-block-paragraph">Again note that the jump instruction is just jmp followed by a label the label specified has to be</p>



<p class="wp-block-paragraph">where you want to jump it&#8217;s never going to return from that place unless you specifically jump back</p>



<p class="wp-block-paragraph">somehow later on like i guess if we wanted to we could put a label on line 67 call it the return</p>



<p class="wp-block-paragraph">call it the return point and then jump back from it after the jump point in fact maybe that would</p>



<p class="wp-block-paragraph">be kind of interesting to do at the end of this video but otherwise we&#8217;re gonna you know just</p>



<p class="wp-block-paragraph">let&#8217;s see we&#8217;re gonna end up jumping over so let me reduce the front size just for a second here</p>



<p class="wp-block-paragraph">so imagine execution uh comes into this program you know we&#8217;re executing uh instructions we&#8217;re</p>



<p class="wp-block-paragraph">calling crlf we&#8217;re just executing executing as soon as we hit this jump instruction then execution</p>



<p class="wp-block-paragraph">then execution jumps over into the label that I specified.</p>



<p class="wp-block-paragraph">So this whole code section here just never even gets called.</p>



<p class="wp-block-paragraph">So that&#8217;s why we will not see that message.</p>



<p class="wp-block-paragraph">And then at the very end, all I&#8217;m doing is I&#8217;m just properly,</p>



<p class="wp-block-paragraph">you know, I&#8217;m printing the exit message.</p>



<p class="wp-block-paragraph">So I&#8217;m just printing another string saying the exit or the jump test is done.</p>



<p class="wp-block-paragraph">I return to the caller execution goes all the way back up to just you know right here right after</p>



<p class="wp-block-paragraph">call jump test was executed and then the cool function will return to the caller and that&#8217;s</p>



<p class="wp-block-paragraph">just a c++ main function that does nothing so at this point we should see the whole entire point of</p>



<p class="wp-block-paragraph">the program and then I&#8217;ll start tweaking it so you can kind of see the difference with the jump</p>



<p class="wp-block-paragraph">instruction uh there and not there so let&#8217;s run one more time and notice how it says begin the</p>



<p class="wp-block-paragraph">says begin the jump test and then end jump test and then it goes back to the driver that is</p>



<p class="wp-block-paragraph">regain control it never says this message should not be printed so this whole section was just</p>



<p class="wp-block-paragraph">skipped let&#8217;s comment out line 66 so that we don&#8217;t actually jump over that code and then now you&#8217;ll</p>



<p class="wp-block-paragraph">see that that message does get printed so notice how it says this message should not appear okay</p>



<p class="wp-block-paragraph">and then run the program one more time.</p>



<p class="wp-block-paragraph">Now that message does not appear.</p>



<p class="wp-block-paragraph">Pretty cool.</p>



<p class="wp-block-paragraph">Now let&#8217;s do that double jumping thing just to show you.</p>



<p class="wp-block-paragraph">I mean, this is not something that you actually want to do.</p>



<p class="wp-block-paragraph">You probably want to write functions and function calls,</p>



<p class="wp-block-paragraph">but if you wanted to, you could do something like this.</p>



<p class="wp-block-paragraph">Here&#8217;s the exiting.</p>



<p class="wp-block-paragraph">And maybe right after this, let&#8217;s make another label.</p>



<p class="wp-block-paragraph">Let&#8217;s do, oh gosh, what am I going to do?</p>



<p class="wp-block-paragraph">what am I going to do? Because if I jump after the exiting label and I jump back up to some label</p>



<p class="wp-block-paragraph">up here, it&#8217;s just going to be an infinite loop. So maybe, um, I don&#8217;t know, let&#8217;s make a, I mean,</p>



<p class="wp-block-paragraph">if I make another label down at the bottom, you&#8217;ll kind of think it&#8217;s a function just without</p>



<p class="wp-block-paragraph">a return statement. So let&#8217;s actually jump within the same function. Let&#8217;s do, um,</p>



<p class="wp-block-paragraph">over the never area.</p>



<p class="wp-block-paragraph">So I&#8217;m going to say jump test and I&#8217;m going to write never.</p>



<p class="wp-block-paragraph">So now we have a label that tells us where the never printed message actually starts.</p>



<p class="wp-block-paragraph">So if we jump over it to the exiting, then we&#8217;re good.</p>



<p class="wp-block-paragraph">But then if I up here, if I say jump instruction that subverts</p>



<p class="wp-block-paragraph">never message so I&#8217;m just I&#8217;m just leaving a comment not code I could then</p>



<p class="wp-block-paragraph">say let&#8217;s jump to jump test never and what will happen now is we&#8217;ll still see</p>



<p class="wp-block-paragraph">the never message because what will happen is execution comes down you know</p>



<p class="wp-block-paragraph">through here all these instructions are executing and then we see a jump that</p>



<p class="wp-block-paragraph">tells us to go to the the never label so we actually jump over this exiting jump</p>



<p class="wp-block-paragraph">over this exiting jump or this like the skipping jump,</p>



<p class="wp-block-paragraph">the jump that skips the message.</p>



<p class="wp-block-paragraph">And then we actually do print the never message</p>



<p class="wp-block-paragraph">and we just keep going down and down and down</p>



<p class="wp-block-paragraph">until we&#8217;re finished with that.</p>



<p class="wp-block-paragraph">And we end up just sort of exiting normally.</p>



<p class="wp-block-paragraph">So that means the only code that doesn&#8217;t get executed</p>



<p class="wp-block-paragraph">in this case is the one right here</p>



<p class="wp-block-paragraph">that skips over the never message.</p>



<p class="wp-block-paragraph">Hopefully that makes sense.</p>



<p class="wp-block-paragraph">Let&#8217;s run the program just to prove it real fast.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do this again.</p>



<p class="wp-block-paragraph">And now you see the message should not appear.</p>



<p class="wp-block-paragraph">This message should not appear.</p>



<p class="wp-block-paragraph">You see that message.</p>



<p class="wp-block-paragraph">So again, if we comment out that jump that subverts the skip, then execution will fall</p>



<p class="wp-block-paragraph">through and we&#8217;ll end up executing line 69, the skipping instruction.</p>



<p class="wp-block-paragraph">again now that message does not appear.</p>



<p class="wp-block-paragraph">We could jump back and forth if we wanted to.</p>



<p class="wp-block-paragraph">I don&#8217;t know.</p>



<p class="wp-block-paragraph">Should I do a back and forth?</p>



<p class="wp-block-paragraph">I don&#8217;t really want to.</p>



<p class="wp-block-paragraph">I think at this point you understand we can jump anywhere we want, right?</p>



<p class="wp-block-paragraph">I could take a bunch of time in this video to rewrite this program and have it say,</p>



<p class="wp-block-paragraph">let&#8217;s jump downward and then let&#8217;s jump upward again and let&#8217;s let it fall through</p>



<p class="wp-block-paragraph">and then let&#8217;s jump over something and whatever.</p>



<p class="wp-block-paragraph">let&#8217;s jump over something and whatever. I mean, just wherever you want to jump,</p>



<p class="wp-block-paragraph">just make a label and then jump to it. Then you have to figure out what your execution path is</p>



<p class="wp-block-paragraph">actually going to be. And maybe that&#8217;ll be complicated, but I hope I&#8217;ve made my point by</p>



<p class="wp-block-paragraph">now. Anyway, so that&#8217;s the basics of just jumping around. It&#8217;s not super useful. Conditional</p>



<p class="wp-block-paragraph">branching is a lot better. So see my next video. And I thank you for watching this and I hope you</p>



<p class="wp-block-paragraph">learned a little bit and had a little fun. See you soon.</p>



<p class="wp-block-paragraph">See you soon.</p>



<p class="wp-block-paragraph">longer videos, better videos, or just I&#8217;ll be able to keep making videos in general.</p>



<p class="wp-block-paragraph">So please do me a kindness and subscribe. You know sometimes I&#8217;m sleeping in the</p>



<p class="wp-block-paragraph">middle of the night and I just wake up because I know somebody subscribed or</p>



<p class="wp-block-paragraph">followed. It just wakes me up and I get filled with joy. That&#8217;s exactly what</p>



<p class="wp-block-paragraph">happens every single time. So you could do it as a nice favor to me or you could</p>



<p class="wp-block-paragraph">you could troll me if you want to just wake me up in the middle of the night.</p>



<p class="wp-block-paragraph">Just subscribe and then I&#8217;ll just wake up. I promise that&#8217;s what will happen.</p>



<p class="wp-block-paragraph">Also, if you look at the middle of the screen right now, you should see a QR code which you can scan in order to go to the website</p>



<p class="wp-block-paragraph">which I think is also named somewhere at the bottom of this video 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">Clarifications or errata or just future videos that you want to see</p>



<p class="wp-block-paragraph">please leave a comment or if you just want to say, Hey, what&#8217;s up, what&#8217;s going on?</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 and I&#8217;m like, it would really, it really mean the world to me.</p>



<p class="wp-block-paragraph">I would really appreciate it. So again, thank you so much for watching this video and, um,</p>



<p class="wp-block-paragraph">enjoy the cool music as, as I fade into the darkness, which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-jump-instructions-explained-unconditional-jmp-with-full-example-in-yasm/">x86-64 Assembly Jump Instructions Explained: Unconditional JMP with Full Example in Yasm</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-jump-instructions-explained-unconditional-jmp-with-full-example-in-yasm/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>x86-64 Assembly: Integer Data Movement, Pointers, and Dereferencing Explained (YASM on Ubuntu)</title>
		<link>https://www.NeuralLantern.com/x86-64-assembly-integer-data-movement-pointers-and-dereferencing-explained-yasm-on-ubuntu/</link>
					<comments>https://www.NeuralLantern.com/x86-64-assembly-integer-data-movement-pointers-and-dereferencing-explained-yasm-on-ubuntu/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Tue, 23 Dec 2025 13:22:17 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[dereferencing assembly]]></category>
		<category><![CDATA[gdb debugging assembly]]></category>
		<category><![CDATA[general purpose registers]]></category>
		<category><![CDATA[integer data movement]]></category>
		<category><![CDATA[lea instruction]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[mov instruction assembly]]></category>
		<category><![CDATA[pointer arithmetic assembly]]></category>
		<category><![CDATA[pointers in assembly]]></category>
		<category><![CDATA[systems programming]]></category>
		<category><![CDATA[ubuntu assembly programming]]></category>
		<category><![CDATA[x86 assembly tutorial]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=269</guid>

					<description><![CDATA[<p>This practical x86-64 assembly language tutorial explains integer data movement between registers and memory, the importance of data size specifiers (byte, word, dword, qword), the difference between pointers and dereferenced values, pointer arithmetic for array access, and using LEA for address calculation - all demonstrated with YASM on Ubuntu Linux and inspected via GDB.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-integer-data-movement-pointers-and-dereferencing-explained-yasm-on-ubuntu/">x86-64 Assembly: Integer Data Movement, Pointers, and Dereferencing Explained (YASM on Ubuntu)</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="x86-64 Assembly: Integer Data Movement, Pointers, and Dereferencing Explained (YASM on Ubuntu)" width="1380" height="776" src="https://www.youtube.com/embed/zGrnEPrpmeg?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">In this hands-on x86-64 assembly tutorial, we dive deep into moving integer data between registers and memory, working with pointers, dereferencing, and pointer arithmetic using YASM on Ubuntu.</p>



<p class="wp-block-paragraph">You&#8217;ll see practical examples of:</p>



<ul class="wp-block-list">
<li>Moving immediates and data between general-purpose registers</li>



<li>Loading different sized integers (byte, word, dword, qword) from memory</li>



<li>Why data size specifiers matter and what happens with overflow/underflow</li>



<li>The difference between moving a pointer vs dereferencing it</li>



<li>Pointer arithmetic to access array elements</li>



<li>Using LEA for address calculation without dereferencing</li>



<li>Debugging with GDB to inspect registers step-by-step</li>
</ul>



<p class="wp-block-paragraph">Perfect for anyone learning low-level programming, systems programming, or wanting to truly understand how pointers work at the assembly level. No prior expert knowledge required &#8211; we build it up with clear examples.</p>



<p class="wp-block-paragraph">Code and examples are shown live, compiled with YASM, linked with LD, and debugged with GDB.</p>



<p class="wp-block-paragraph">If you&#8217;re into assembly, reverse engineering, operating systems, or just love understanding how computers really work, this one is for you.</p>



<p class="wp-block-paragraph">Introduction to Integer Data Movement and Pointers 00:00:00<br>Program Setup and Makefile 00:01:16<br>Data Section Definitions 00:01:38<br>System Calls for Output and Exit 00:01:52<br>Text Section and Entry Point 00:04:21<br>Printing Hello String 00:05:05<br>Moving Immediates to Registers 00:06:04<br>Copying Between Registers 00:06:27<br>Dereferencing Pointers from Memory 00:07:16<br>Specifying Data Sizes 00:08:01<br>Register Size Variants 00:09:45<br>Reference Book Explanation 00:10:50<br>Loading Words and Bytes 00:13:25<br>Debugger Breakpoint and Registers 00:14:28<br>Overflow Demonstration 00:16:25<br>Makefile Adjustments for Warnings 00:17:44<br>Proving Data Size Importance 00:20:32<br>Underflow and Overflow Examples 00:21:34<br>Symbols as Pointers 00:24:16<br>Array Definitions and Access 00:25:20<br>Pointer to Array Items 00:26:08<br>LEA Instruction for Addresses 00:27:32<br>Dereferencing Manipulated Pointers 00:29:01<br>Final Register Inspection 00:29:50<br>Conclusion and Subscribe Request 00:33:26</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>
</ul>



<p class="wp-block-paragraph">Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



<li>Leaving a comment or suggestion</li>



<li>Subscribing to our Blog</li>



<li>Watching the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p class="wp-block-paragraph">Hey everybody! In this video I&#8217;m going to talk to you about integer data movement and pointers</p>



<p class="wp-block-paragraph">in an x86-64 YASM assembly program within Ubuntu but this should work for all of you x86 YASM people.</p>



<p class="wp-block-paragraph">Okay so what am I talking about for starters? I&#8217;m just talking about moving data pretty much. If you</p>



<p class="wp-block-paragraph">from one integer register to another or from one general purpose register to another.</p>



<p class="wp-block-paragraph">And if you know how to manipulate pointers in assembly and all that stuff,</p>



<p class="wp-block-paragraph">you probably don&#8217;t need this video.</p>



<p class="wp-block-paragraph">But if you are not an expert, then this will probably help you.</p>



<p class="wp-block-paragraph">Let me start off by saying, of course, that there are going to be lots of concepts in this video</p>



<p class="wp-block-paragraph">that I don&#8217;t actually explain because I&#8217;ve explained them in other videos.</p>



<p class="wp-block-paragraph">For example, this is not a Hello World tutorial where I show you how to actually,</p>



<p class="wp-block-paragraph">you know, build a basic Yasm assembly program.</p>



<p class="wp-block-paragraph">a basic yasm assembly program you can probably infer that just from this video but i&#8217;m not going</p>



<p class="wp-block-paragraph">to explain it also this is not a debugging video so i&#8217;m not really going to explain too much about</p>



<p class="wp-block-paragraph">gdb which is something i&#8217;m going to use to look at the registers but it might be nice for you to</p>



<p class="wp-block-paragraph">get a little preview i will record a full gdb tutorial in another video anyway so let&#8217;s see</p>



<p class="wp-block-paragraph">here uh for starters i&#8217;ve just kind of like created a little sample program here um well</p>



<p class="wp-block-paragraph">a make file which will just compile and execute my program again this is not a</p>



<p class="wp-block-paragraph">make file video tutorial check my other videos for that I&#8217;ve already made one</p>



<p class="wp-block-paragraph">for now I&#8217;m just gonna start making a program and I&#8217;m gonna give it a data</p>



<p class="wp-block-paragraph">section so first thing I&#8217;m gonna give it is like well let&#8217;s say I&#8217;ll give it a</p>



<p class="wp-block-paragraph">bunch of data then I&#8217;ll kind of explain what I gave it again this is not a</p>



<p class="wp-block-paragraph">basics Yasum tutorial so see my other videos but you know just simply put I&#8217;m</p>



<p class="wp-block-paragraph">You know just simply put I&#8217;m gonna write to standard output so I do I remember the system call code for for writing</p>



<p class="wp-block-paragraph">And then I&#8217;m gonna exit because this is a pure assembly program that I&#8217;m compiling. There&#8217;s this is not a hybrid program</p>



<p class="wp-block-paragraph">We&#8217;re not doing GCC. We&#8217;re just doing LD</p>



<p class="wp-block-paragraph">So I&#8217;m just gonna say a call code 60 to exit a file descriptor for stdin actually I don&#8217;t really need that</p>



<p class="wp-block-paragraph">Let me just double check that I don&#8217;t need that in my solution</p>



<p class="wp-block-paragraph">Yeah, I like to reuse these things so we don&#8217;t really need standard input</p>



<p class="wp-block-paragraph">standard input. We&#8217;re just going to do standard output, you know, printing to the terminal.</p>



<p class="wp-block-paragraph">We&#8217;re going to exit with a success code of zero. And then I made a couple of, well, just one C</p>



<p class="wp-block-paragraph">string where I&#8217;m just basically saying, hello, my name is so-and-so. So you can know that the</p>



<p class="wp-block-paragraph">program actually started in case it crashes later. That&#8217;s not my name. I just love those kinds of</p>



<p class="wp-block-paragraph">names. And then I&#8217;m just going to make a bunch of integers of different data sizes. So this is kind</p>



<p class="wp-block-paragraph">sizes but just real fast because these are going to be directly used in our video we uh we have a</p>



<p class="wp-block-paragraph">quad word here so i&#8217;m defining an integer called my long int and i&#8217;m saying that it&#8217;s a quad word</p>



<p class="wp-block-paragraph">with dq q for quad word and then i&#8217;m just giving a pretty big integer same thing for a regular d</p>



<p class="wp-block-paragraph">word which is just a double word so that&#8217;s half of a quad word and then another thing with a whoops</p>



<p class="wp-block-paragraph">w not a q let me just fix that in my solution oh dear then i&#8217;m going to do a word which is a six two</p>



<p class="wp-block-paragraph">one one one a word is two bytes of data we&#8217;re starting to get dangerously into overflow territory</p>



<p class="wp-block-paragraph">when i was originally writing this solution i had like a bunch of nines up here and the number that</p>



<p class="wp-block-paragraph">i ended up seeing on the screen was totally different and i forgot oh a word is just two bytes</p>



<p class="wp-block-paragraph">so even if it&#8217;s unsigned the highest thing that it can actually represent is um</p>



<ol start="65535" class="wp-block-list">
<li>So that&#8217;s not good. Then I&#8217;m going to just store a byte here with DB and put some twos there.</li>
</ol>



<p class="wp-block-paragraph">And then I&#8217;m going to make an array of integers. Putting an array, making an array in the regular</p>



<p class="wp-block-paragraph">data section, that&#8217;s not as good as putting it in the BSS section if you want a lot of integers.</p>



<p class="wp-block-paragraph">But I&#8217;m just going to specify a small number of integers so I can just put it directly into the</p>



<p class="wp-block-paragraph">BSS is for like giant arrays of uninitialized data and the data section is for whatever you can handle typing pretty much of initialized data.</p>



<p class="wp-block-paragraph">So now I&#8217;m going to start my text section and my program&#8217;s entry point.</p>



<p class="wp-block-paragraph">Again, this is not a Yasm basics tutorial. See my other videos.</p>



<p class="wp-block-paragraph">And so we&#8217;re going to start by just saying here&#8217;s my entry point where the operating system can sort of jump into my program.</p>



<p class="wp-block-paragraph">to just you know exit the program let&#8217;s actually see if that works so far it</p>



<p class="wp-block-paragraph">should do nothing but but at least exit with success I&#8217;m gonna go clear and make</p>



<p class="wp-block-paragraph">run and notice how it just kind of like ran it and then nothing happened okay so</p>



<p class="wp-block-paragraph">that&#8217;s great how come it didn&#8217;t print though what did I do wrong oh I forgot</p>



<p class="wp-block-paragraph">to copy paste the code that prints something okay let me get my solution up</p>



<p class="wp-block-paragraph">here so the first thing we&#8217;re really going to do is just use a system call to</p>



<p class="wp-block-paragraph">call to print the word hello again this is not a system call video see my other videos</p>



<p class="wp-block-paragraph">and um so now we&#8217;re just we&#8217;re just going to print our hello string</p>



<p class="wp-block-paragraph">with a system call let me run it one more time we should see like a quick hello</p>



<p class="wp-block-paragraph">yeah okay my name is uh chaplain mondover it&#8217;s not maybe i should change that to it&#8217;s not my name is</p>



<p class="wp-block-paragraph">not no i don&#8217;t want to do that sometimes i like to pretend that my name is chaplain mondover</p>



<p class="wp-block-paragraph">name is check lane Mondover you should too it&#8217;s fun anyway so the first thing that I&#8217;m going to</p>



<p class="wp-block-paragraph">do here is I&#8217;m going to move an immediate well let me copy paste a whole chunk of code and then</p>



<p class="wp-block-paragraph">I&#8217;ll explain it one by one I&#8217;m basically going to use data movement instructions to move a bunch of</p>



<p class="wp-block-paragraph">data into a bunch of registers and then I&#8217;m going to hit a break point or I&#8217;m going to I&#8217;m going to</p>



<p class="wp-block-paragraph">sort of do like a non-operation piece of code so that I can easily break on it in my debugger so</p>



<p class="wp-block-paragraph">the results. Okay so first off I&#8217;m going to move an immediate into R12. The</p>



<p class="wp-block-paragraph">instruction for moving data into general purpose registers or the integer</p>



<p class="wp-block-paragraph">registers is just MOV so that&#8217;s fine probably you should know that by now but</p>



<p class="wp-block-paragraph">I&#8217;m going to move an immediate into R10 and I&#8217;m going to move 12876 into R10.</p>



<p class="wp-block-paragraph">So that&#8217;s how you move an immediate no problem. Then I&#8217;m going to first clear</p>



<p class="wp-block-paragraph">out R11 and then I&#8217;m going to move R10 into it. The reason I&#8217;m clearing out R11</p>



<p class="wp-block-paragraph">reason i&#8217;m clearing out r11 first is just to prove to you that we are actually moving data from one</p>



<p class="wp-block-paragraph">register into another because if i just i don&#8217;t know if i didn&#8217;t do that you might be tempted to</p>



<p class="wp-block-paragraph">to think well um maybe r11 already had that data in it so first it&#8217;s going to be just a zero and</p>



<p class="wp-block-paragraph">then it&#8217;s going to actually be whatever r10 had so we should see r10 and 11 both have one two eight</p>



<p class="wp-block-paragraph">register or a pointer register from one to another. And by the way, integers, those are</p>



<p class="wp-block-paragraph">pointers, or rather, should I say, pointers are integers. So if you&#8217;re moving an integer or you&#8217;re</p>



<p class="wp-block-paragraph">moving a pointer, the system just kind of sees that as a number either way. So you can use the</p>



<p class="wp-block-paragraph">move instruction for it. So now let&#8217;s use the pointer that was assigned to our long integer</p>



<p class="wp-block-paragraph">to move that number into R12. So let me just go up real fast. My long int up here,</p>



<p class="wp-block-paragraph">my long int up here it&#8217;s like a gigantic number and remember I said before that my long int is</p>



<p class="wp-block-paragraph">actually just a pointer to the memory location that begins to hold the quad word so my long</p>



<p class="wp-block-paragraph">int is really a pointer to one byte and there&#8217;s an eight byte allocation because it&#8217;s a quad word</p>



<p class="wp-block-paragraph">so when you sort of put this into your code and you let the system know that you want to move a</p>



<p class="wp-block-paragraph">then the system will scan eight bytes and interpret that as an integer so</p>



<p class="wp-block-paragraph">putting the the keyword quad word here I don&#8217;t think that&#8217;s necessary I usually</p>



<p class="wp-block-paragraph">do it anyway but you&#8217;ll see in the next few instructions that it&#8217;s a really</p>



<p class="wp-block-paragraph">really good idea to specify the data size because if you don&#8217;t you&#8217;re gonna</p>



<p class="wp-block-paragraph">actually get something wrong in other words if for some reason I moved I don&#8217;t</p>



<p class="wp-block-paragraph">into R12 I could do it if I if I wrote it correctly but then I would be taking</p>



<p class="wp-block-paragraph">part of a quad word and trying to interpret that as the entire number and</p>



<p class="wp-block-paragraph">it&#8217;s probably going to be totally wrong we&#8217;ll have an example for that in a</p>



<p class="wp-block-paragraph">moment but anyway so that&#8217;s how we move a quad word we just specify keyword right</p>



<p class="wp-block-paragraph">before the memory location and we use the brackets to dereference if we</p>



<p class="wp-block-paragraph">didn&#8217;t dereference then we would actually be moving a pointer to the long</p>



<p class="wp-block-paragraph">integer into R12 we&#8217;ll do that soon too I&#8217;m going to try to do everything in this</p>



<p class="wp-block-paragraph">to do everything in this video then we&#8217;ll move a d word from uh from memory into r13 this is a</p>



<p class="wp-block-paragraph">little bit different notice how uh here on line 60 i kind of have to specify d word because that&#8217;s</p>



<p class="wp-block-paragraph">the data size that i&#8217;m trying to move into the r13 register so this is telling the system that</p>



<p class="wp-block-paragraph">there&#8217;s about four bytes that i need to scan not eight bytes and interpret those four bytes as an</p>



<p class="wp-block-paragraph">Moving it into R13, you know, I could have sworn a long time ago, you could just move that sort of thing directly into R13.</p>



<p class="wp-block-paragraph">But when I tried my solution before recording this video, I kept getting assembler errors until I added the D at the end.</p>



<p class="wp-block-paragraph">And so I just want you to know, oh, you know what, let&#8217;s look at a book real fast.</p>



<p class="wp-block-paragraph">Let&#8217;s look at my favorite book.</p>



<p class="wp-block-paragraph">I just want you to know that there are other forms of every register that can specify the register&#8217;s data size.</p>



<p class="wp-block-paragraph">What do I mean by that?</p>



<p class="wp-block-paragraph">itself then the system thinks you&#8217;re talking about all 64 bits of the 13 register the r13 register</p>



<p class="wp-block-paragraph">but if you type whoops if you type r13d then the system thinks you&#8217;re only talking about the lowest</p>



<p class="wp-block-paragraph">32 bits of that register so you&#8217;re also able to specify the data size of a register again i thought</p>



<p class="wp-block-paragraph">you should should a long time ago i thought i remembered moving a d word directly into r13 and</p>



<p class="wp-block-paragraph">it would just clear out the extra bits but i got warnings this time maybe that&#8217;s for the best</p>



<p class="wp-block-paragraph">that&#8217;s for the best forcing us to be more precise, but this will solve it. R13 just says,</p>



<p class="wp-block-paragraph">let&#8217;s only talk about a D word. So let me go, let me go to my favorite book here.</p>



<p class="wp-block-paragraph">Where the heck is my favorite book? Okay. I guess I have to like, there we go.</p>



<p class="wp-block-paragraph">Okay. There&#8217;s my favorite book. So, um, I&#8217;m going to go to, uh, let&#8217;s, let me just search for,</p>



<p class="wp-block-paragraph">just search for I can&#8217;t remember where it is our 13 D yeah there it is okay so</p>



<p class="wp-block-paragraph">what we&#8217;re looking for is 2.3.1.1 let me show you real fast the top of the book</p>



<p class="wp-block-paragraph">just to give a shout out to the person who wrote this book I did not write this</p>



<p class="wp-block-paragraph">book this is a free and open source book that you can download for free</p>



<p class="wp-block-paragraph">everybody should get a copy to turn yourselves into an expert it&#8217;s called</p>



<p class="wp-block-paragraph">x86 64 assembly language programming with Ubuntu it&#8217;s written by a brilliant</p>



<p class="wp-block-paragraph">professor. This is like a semi recent version. There&#8217;s probably a newer one now, but yeah,</p>



<p class="wp-block-paragraph">it&#8217;s a great book. I love it. So I&#8217;m going to go back to what the heck was I just doing?</p>



<p class="wp-block-paragraph">Was it 2.3.1.1? Yeah. Okay. So in this book section 2.3.1.1, the section entitled general</p>



<p class="wp-block-paragraph">purpose registers, notice how there are a bunch of different versions of each register. So if you</p>



<p class="wp-block-paragraph">So if you look in this column right here, we have, oh, I get to use my annotator.</p>



<p class="wp-block-paragraph">We have a, what were we just looking at?</p>



<p class="wp-block-paragraph">R12.</p>



<p class="wp-block-paragraph">Notice how R12 here is a designated as a 64 bit register.</p>



<p class="wp-block-paragraph">But if you wanted to use the R12 register and only use 32 bits of it, the lowest 32</p>



<p class="wp-block-paragraph">bits, then this is the form you would use for the R12 register.</p>



<p class="wp-block-paragraph">So 32 bits.</p>



<p class="wp-block-paragraph">And if you wanted to use only a word&#8217;s worth, you know, 16 bits.</p>



<p class="wp-block-paragraph">Well there you go.</p>



<p class="wp-block-paragraph">two bytes on these systems or 16 bits and then if you just wanted to use one</p>



<p class="wp-block-paragraph">byte of the register well you can you just put a B at the end of it it&#8217;s</p>



<p class="wp-block-paragraph">pretty sweet and convenient it&#8217;s not too hard to remember that D is for D word and</p>



<p class="wp-block-paragraph">W is for word and B is for bytes it just gets a little confusing when you&#8217;re</p>



<p class="wp-block-paragraph">using the other registers like the the RAX register it&#8217;s got EAX as its 32-bit</p>



<p class="wp-block-paragraph">version and then AX as its 16-bit version and AL as its 8-bit version if</p>



<p class="wp-block-paragraph">it&#8217;s a 8-bit version if you can remember that there&#8217;s an L I guess that&#8217;s not too bad but you</p>



<p class="wp-block-paragraph">know remembering the X and then it changes to an I over here and a P over here I&#8217;m personally not</p>



<p class="wp-block-paragraph">a fan so I always come back to this book to try and remember what I&#8217;m supposed to be putting for</p>



<p class="wp-block-paragraph">these registers because I can&#8217;t remember all the time or rather I can almost never remember but we</p>



<p class="wp-block-paragraph">So, I want to take a D word from memory and load it up into the R13 register.</p>



<p class="wp-block-paragraph">So I got to specify the R13D.</p>



<p class="wp-block-paragraph">And, you know, maybe I&#8217;ll take that out and show you that it doesn&#8217;t compile later, if I can remember.</p>



<p class="wp-block-paragraph">But I&#8217;m going to now take a D word, do the same thing into R13.</p>



<p class="wp-block-paragraph">Oh, sorry, no.</p>



<p class="wp-block-paragraph">I&#8217;m going to take a regular word and do the same thing basically into R14.</p>



<p class="wp-block-paragraph">But it&#8217;s just going to be a word instead of a D word.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to take a single byte and load it up into R15.</p>



<p class="wp-block-paragraph">single byte and load it up into R15. Then I&#8217;m going to call Nope, which is an instruction that</p>



<p class="wp-block-paragraph">just does nothing. And it&#8217;s useful sometimes if you want to have a place where you can stop your</p>



<p class="wp-block-paragraph">program and sort of inspect the results of your program in your debugger. So there&#8217;s a lot more</p>



<p class="wp-block-paragraph">to this program. I&#8217;m going to go ahead and just run it right now, just to make sure that it compiles.</p>



<p class="wp-block-paragraph">And then maybe I&#8217;ll add a breakpoint here. Let&#8217;s go clear and make run. Okay, so it ran,</p>



<p class="wp-block-paragraph">Okay, so it ran, it compiled, it was fine.</p>



<p class="wp-block-paragraph">And now I&#8217;m going to open up a new little window and I&#8217;m going to launch the program.</p>



<p class="wp-block-paragraph">Notice how the program that has been compiled is called main.</p>



<p class="wp-block-paragraph">So I&#8217;m going to launch the program into the GDB debugger.</p>



<p class="wp-block-paragraph">This is not a GDB video.</p>



<p class="wp-block-paragraph">So I&#8217;m going to be skimming over this, check future videos for a full GDB tutorial.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do a break point right there at line 72.</p>



<p class="wp-block-paragraph">So that I can just kind of inspect the state of the register.</p>



<p class="wp-block-paragraph">break at main.asm line 72 and then just to be sure I did that correctly I&#8217;m going to say info</p>



<p class="wp-block-paragraph">breakpoints or just a b is fine and it&#8217;s like looks like I set it up correctly then I can type</p>



<p class="wp-block-paragraph">run to see what&#8217;s up notice how it starts to run and immediately stops at that breakpoint</p>



<p class="wp-block-paragraph">you can tell it stops there because it&#8217;s at line 72 then I&#8217;m just going to do info registers or</p>



<p class="wp-block-paragraph">info r just to print out all my registers and we should see a confirmation of what we were hoping</p>



<p class="wp-block-paragraph">so if I kind of like maybe I&#8217;ll pin this to the top for a second always on top how about that</p>



<p class="wp-block-paragraph">so first we moved an immediate into r10 so notice how 12876 is in r10 right there and then on line</p>



<p class="wp-block-paragraph">53 we basically copied r10 into r11 so that means in r11 we see the same value</p>



<p class="wp-block-paragraph">and then we&#8217;ll move a quad word from the long integer we&#8217;ll dereference that pointer</p>



<p class="wp-block-paragraph">at R12 it&#8217;s like this gigantic number if I scroll all the way up it&#8217;s the same number here as in our</p>



<p class="wp-block-paragraph">source code so that seemed to work and then we&#8217;re going to move the D word next I guess into R13</p>



<p class="wp-block-paragraph">so that&#8217;s this number right here so that seemed to have worked it&#8217;s this number right here the</p>



<p class="wp-block-paragraph">D word and I&#8217;m going to get lost so fast scrolling up and down like this now we&#8217;re going to move a</p>



<p class="wp-block-paragraph">one one and there it is right there and then r15 is just gonna take a bite and</p>



<p class="wp-block-paragraph">it&#8217;s gonna take a bite oh now I&#8217;m hungry now I&#8217;m hungry that activated it it&#8217;s</p>



<p class="wp-block-paragraph">only been like think four or five hours since I ate I guess that is usually when</p>



<p class="wp-block-paragraph">I need to eat again even though I gorge and fall asleep on the floor because I</p>



<p class="wp-block-paragraph">home r15 anyway holds the correct value it&#8217;s 222 i just want to show you overflow real fast</p>



<p class="wp-block-paragraph">we look at r14 uh we&#8217;re using the word version so it&#8217;s only going to read you know uh two bytes</p>



<p class="wp-block-paragraph">from that memory location and it&#8217;s only going to consider uh you know 16 bits as being valid in uh</p>



<p class="wp-block-paragraph">in that register so if i use a number that&#8217;s too big it&#8217;ll overflow two bytes the maximum number</p>



<p class="wp-block-paragraph">three, five or from zero to that number or six, five, five, three, six combinations.</p>



<p class="wp-block-paragraph">So if I just stick a nine in front of that right now, that should overflow the system.</p>



<p class="wp-block-paragraph">So let me try it one more time.</p>



<p class="wp-block-paragraph">I want to do Q because that was the last thing we needed to look at.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to do make.</p>



<p class="wp-block-paragraph">Oh gosh, what&#8217;s happening here?</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">How about I do clear and make build, which is in my make file and GDB main.</p>



<p class="wp-block-paragraph">So I don&#8217;t have to type all this stuff out again every single time.</p>



<p class="wp-block-paragraph">have to type all this stuff out again every single time what did i just do oh nice okay you know what</p>



<p class="wp-block-paragraph">i swear it didn&#8217;t used to do this before it&#8217;s giving me a nice warning saying that value does</p>



<p class="wp-block-paragraph">not fit in a 16-bit field i guess if i turned warnings off which i don&#8217;t want to do then the</p>



<p class="wp-block-paragraph">program would run and it would overflow eh do you want to turn it off let&#8217;s turn off warnings okay</p>



<p class="wp-block-paragraph">much attention to it. Um, where the heck are the warnings? Where&#8217;s the assembler running? There it</p>



<p class="wp-block-paragraph">is. Yasum flags. And that&#8217;s, so that&#8217;s my variable up there. There we go. Okay. So I&#8217;m just going to</p>



<p class="wp-block-paragraph">make a copy of this line, comment out the original, and then I&#8217;m going to take out, uh, the command</p>



<p class="wp-block-paragraph">that says, or the flag that says convert warnings into errors. Your compiler is your friend. It was</p>



<p class="wp-block-paragraph">So I am now going to do a breakpoint at 72.</p>



<p class="wp-block-paragraph">So break at main.asm972 and then run and then it breaks.</p>



<p class="wp-block-paragraph">And then I go input registers.</p>



<p class="wp-block-paragraph">Then if I look at, what was I just talking about?</p>



<p class="wp-block-paragraph">R14, was that it?</p>



<p class="wp-block-paragraph">Notice how 44607 for the word, that&#8217;s a totally different value, right?</p>



<p class="wp-block-paragraph">So this is what happens when you overflow or underflow.</p>



<p class="wp-block-paragraph">You got to be careful about your data sizes.</p>



<p class="wp-block-paragraph">I&#8217;m just going to do this again one more time just to make sure.</p>



<p class="wp-block-paragraph">one more time just to make sure that it&#8217;s fixed now that I moved it and I&#8217;m</p>



<p class="wp-block-paragraph">gonna I&#8217;m gonna read that part about warnings because I love converting</p>



<p class="wp-block-paragraph">warnings to errors as just a way to help myself write better code okay so let&#8217;s</p>



<p class="wp-block-paragraph">do that and you know what I&#8217;m gonna do too I added another target here again</p>



<p class="wp-block-paragraph">this is not a Yasm or sorry this is not this is kind of a Yasm video this is not</p>



<p class="wp-block-paragraph">extra file called gdb.txt where I can stick gdb commands and so I don&#8217;t want</p>



<p class="wp-block-paragraph">to continue to type my breakpoints over and over over again every single time so</p>



<p class="wp-block-paragraph">I know for sure I&#8217;m not going to change the program anymore at least I&#8217;m not</p>



<p class="wp-block-paragraph">going to shift the lines up and down before 72 so I&#8217;m going to do breakpoint</p>



<p class="wp-block-paragraph">you can type like break or I think you can type either break or the full word</p>



<p class="wp-block-paragraph">breakpoint but I just type B name of the source code file a.asm at line 72 and so</p>



<p class="wp-block-paragraph">And so this file right here, again, this is not a GDB tutorial, but in my make file, I have it set up so that GDB will execute whatever commands I put into this text file.</p>



<p class="wp-block-paragraph">So it just kind of saves me typing.</p>



<p class="wp-block-paragraph">So let&#8217;s put a breakpoint there and then we&#8217;ll say info breakpoints and then we&#8217;ll just run the program in that way.</p>



<p class="wp-block-paragraph">I can have a little bit more information.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do make build instead of make build and then running GDB.</p>



<p class="wp-block-paragraph">I&#8217;m going to say make debug.</p>



<p class="wp-block-paragraph">That&#8217;s another target in my make file.</p>



<p class="wp-block-paragraph">make file so then you can see it added the breakpoint for me and then it</p>



<p class="wp-block-paragraph">immediately broke on that and you know what yeah I guess I&#8217;ll just leave it in</p>



<p class="wp-block-paragraph">where I have to type info registers okay so we&#8217;re just making sure I think that</p>



<p class="wp-block-paragraph">R14 is still a valid value and that it was fixed okay so we&#8217;ve gotten this far</p>



<p class="wp-block-paragraph">the next thing we need to do is prove that data size matters by specifying a</p>



<p class="wp-block-paragraph">specifying a bad data size for our long and then for our byte. So I&#8217;m going to copy paste a little</p>



<p class="wp-block-paragraph">proof here that is followed by another non-operation. So I&#8217;m going to do this. And so</p>



<p class="wp-block-paragraph">I&#8217;m going to try to prove to you that the data size matters by specifying a bad data size. I</p>



<p class="wp-block-paragraph">just proved to you that it kind of mattered or it definitely mattered in terms of overflow and</p>



<p class="wp-block-paragraph">underflow, but I&#8217;m trying to prove to you now that it matters when you&#8217;re reading. So I&#8217;m going to</p>



<p class="wp-block-paragraph">I&#8217;m going to clear out r12 and then I&#8217;m going to move a byte from the long integer into r12b</p>



<p class="wp-block-paragraph">and you know this would be a warning or I think it this wouldn&#8217;t compile or wouldn&#8217;t assemble if</p>



<p class="wp-block-paragraph">I didn&#8217;t have the b there so I&#8217;m basically telling the computer all right I want you to use only a</p>



<p class="wp-block-paragraph">byte of r12 and I want you to take one byte from memory and just put it in there but I&#8217;m taking it</p>



<p class="wp-block-paragraph">awful. And then right after that, I&#8217;m going to do another naughty thing. I&#8217;m going to move</p>



<p class="wp-block-paragraph">something into R15 and I&#8217;m going to word, I&#8217;m going to move a keyword of memory into R15,</p>



<p class="wp-block-paragraph">but I&#8217;m going to be taking it from the bytes memory. So remember a byte is just one byte.</p>



<p class="wp-block-paragraph">And so the next seven bytes that come after it definitely have nothing to do with that byte,</p>



<p class="wp-block-paragraph">but I&#8217;m going to interpret them all as a quad word. So we should get nonsense data</p>



<p class="wp-block-paragraph">also R15. I don&#8217;t really need to zero out R15 because the fact that I&#8217;m specifying R15 means</p>



<p class="wp-block-paragraph">I want to use all 64 bits. So it is going to definitely zero it out. And whatever we see in</p>



<p class="wp-block-paragraph">R15 is going to be the real thing. But in terms of R12B, we&#8217;re only going to be loading up one</p>



<p class="wp-block-paragraph">byte&#8217;s worth of that register. So that&#8217;s why I&#8217;m zeroing out the rest of it just to prove to you</p>



<p class="wp-block-paragraph">that we are truly getting nonsense. So now there&#8217;s going to be another break point at 82.</p>



<p class="wp-block-paragraph">I&#8217;m not going to break anymore at 72. I&#8217;m going to break at 82. So let&#8217;s do that.</p>



<p class="wp-block-paragraph">What&#8217;s going on here? We got that.</p>



<p class="wp-block-paragraph">Oh gosh. Okay. Quit. I guess that means I need to comment this out. Let me do that.</p>



<p class="wp-block-paragraph">We&#8217;ll do break at main.asm at 82. And you could, you could do both of these two. Actually,</p>



<p class="wp-block-paragraph">let me do it first. We could, we could do multiple break points. Yeah, I&#8217;ll just do it first,</p>



<p class="wp-block-paragraph">So notice how it first breaks at the 72 and then I do C to continue and then suddenly</p>



<p class="wp-block-paragraph">it breaks at the 82.</p>



<p class="wp-block-paragraph">But I don&#8217;t want to have to hit continue a bunch of times so I&#8217;m going to comment that</p>



<p class="wp-block-paragraph">out for the future.</p>



<p class="wp-block-paragraph">Let me quit real fast to make sure I remember what GDB expects as a comment.</p>



<p class="wp-block-paragraph">Yeah, okay.</p>



<p class="wp-block-paragraph">82.</p>



<p class="wp-block-paragraph">Okay, so then I&#8217;m going to print the registers info registers.</p>



<p class="wp-block-paragraph">And we&#8217;re looking at R12 and R15.</p>



<p class="wp-block-paragraph">You can see R12 has the number 144, which definitely doesn&#8217;t make sense because there&#8217;s</p>



<p class="wp-block-paragraph">no 144 anywhere in our original data.</p>



<p class="wp-block-paragraph">in our original data so hopefully that&#8217;s proof to you that we&#8217;ve grabbed some junk</p>



<p class="wp-block-paragraph">sometimes maybe when you&#8217;re doing this at home if you&#8217;re just going to grab one bite</p>



<p class="wp-block-paragraph">maybe accidentally um no no that would definitely still look bad but for the r15</p>



<p class="wp-block-paragraph">if you&#8217;re going to grab a q words worth of data i don&#8217;t know maybe the junk data is like</p>



<p class="wp-block-paragraph">accidentally correct in a way where it would it would still make it look like the intended value</p>



<p class="wp-block-paragraph">the intended value no it wouldn&#8217;t there&#8217;s no way that would even happen no i guess the junk data</p>



<p class="wp-block-paragraph">could could work that way anyway so we&#8217;ll look at r15 two eight and a bunch of fours and then a two</p>



<p class="wp-block-paragraph">three eight um that value does not exist up here so again junk data mixed in with real data is is</p>



<p class="wp-block-paragraph">bad news okay so now let&#8217;s do another proof i want to prove to you that symbols are pointers</p>



<p class="wp-block-paragraph">you know like I&#8217;ve been saying and they need to be dereferenced so let&#8217;s first start off by</p>



<p class="wp-block-paragraph">moving the raw pointer for my long int into r10 and then into r11 we will dereference that</p>



<p class="wp-block-paragraph">pointer so in r10 we should see something that looks like a memory location or at least a relative</p>



<p class="wp-block-paragraph">memory location and then in r11 we should see the actual long integer like we saw before</p>



<p class="wp-block-paragraph">And then I&#8217;m going to mess around with some arrays.</p>



<p class="wp-block-paragraph">So remember up here we made an array.</p>



<p class="wp-block-paragraph">Let me minimize that real fast.</p>



<p class="wp-block-paragraph">We made an array, a very small array.</p>



<p class="wp-block-paragraph">I just specified a bunch of ones and then specified a bunch of twos.</p>



<p class="wp-block-paragraph">So it should be contiguous memory since it&#8217;s been allocated as an array with that comma</p>



<p class="wp-block-paragraph">there.</p>



<p class="wp-block-paragraph">We should see if we looked inside of the system inside of memory, we should see eight bytes</p>



<p class="wp-block-paragraph">representing the ones.</p>



<p class="wp-block-paragraph">And then right next to that, we should see eight bytes representing the twos.</p>



<p class="wp-block-paragraph">easy for us to sort of move our pointer around a little bit. So what else do I need to copy paste</p>



<p class="wp-block-paragraph">here? We got that long int. Okay. So I&#8217;m going to grab the first item and the second item in those</p>



<p class="wp-block-paragraph">arrays just using some pointer manipulation. So you know how to do this already. You just take</p>



<p class="wp-block-paragraph">like the pointer to some number and you just dereference it and you end up getting a number.</p>



<p class="wp-block-paragraph">I understand your feeling because you might be thinking, wait a minute, wait a minute.</p>



<p class="wp-block-paragraph">I understood it when it was dereferencing just one number, but didn&#8217;t you say this is an array?</p>



<p class="wp-block-paragraph">Why is it we can dereference the array and just get one number still?</p>



<p class="wp-block-paragraph">Well, remember, a pointer to an array is really a pointer to the first item in the array.</p>



<p class="wp-block-paragraph">That&#8217;s kind of how it works.</p>



<p class="wp-block-paragraph">So either way, whether you consider that to be a pointer to one number or a pointer to array,</p>



<p class="wp-block-paragraph">when we dereference it, we&#8217;ll get one number.</p>



<p class="wp-block-paragraph">If you want to get the other numbers, then you have to start manipulating the pointer</p>



<p class="wp-block-paragraph">or derefing in different ways.</p>



<p class="wp-block-paragraph">So here&#8217;s how we can get the second item.</p>



<p class="wp-block-paragraph">We deref the original pointer plus 8.</p>



<p class="wp-block-paragraph">8 because it&#8217;s a quad word.</p>



<p class="wp-block-paragraph">So, you know, it probably would be a little smarter to put that 8 up into a define somewhere</p>



<p class="wp-block-paragraph">so you&#8217;re not hard coding numbers, but I&#8217;m not going to do it.</p>



<p class="wp-block-paragraph">So basically in R12, we should see the 1s.</p>



<p class="wp-block-paragraph">In R13, we should see the 2s.</p>



<p class="wp-block-paragraph">And then a couple more things.</p>



<p class="wp-block-paragraph">Next, we will grab a pointer to the first item in the small array.</p>



<p class="wp-block-paragraph">And we&#8217;ll just stick that into R14.</p>



<p class="wp-block-paragraph">So, you know, we kind of did that with R10 already.</p>



<p class="wp-block-paragraph">But now I just want a pointer to the small array.</p>



<p class="wp-block-paragraph">And I&#8217;m going to keep it in R14 just so you can see the memory location.</p>



<p class="wp-block-paragraph">And then after that, we&#8217;re going to grab an actual pointer to the second item.</p>



<p class="wp-block-paragraph">hey how do you get a pointer to the second item well it&#8217;s that but then what</p>



<p class="wp-block-paragraph">if you didn&#8217;t want to dereference the pointer like what if R13 you know here</p>



<p class="wp-block-paragraph">clearly because we have these brackets for dereferencing you&#8217;re allowed to put</p>



<p class="wp-block-paragraph">the formulas inside of the brackets what if I wanted to get the address to the</p>



<p class="wp-block-paragraph">second item and not actually the second item we would not be allowed to remove</p>



<p class="wp-block-paragraph">the brackets the assembler wouldn&#8217;t like that so without another instruction</p>



<p class="wp-block-paragraph">way to actually get a pointer with a complicated mathematical formula. You know, you can do like</p>



<p class="wp-block-paragraph">multiplication inside of here and other things in parentheses. So we wouldn&#8217;t be able to do that</p>



<p class="wp-block-paragraph">without the other instruction that I&#8217;m introducing now called LEA. LEA just means, yeah, sure, we&#8217;re</p>



<p class="wp-block-paragraph">still going to use the brackets to figure out where the pointer is, like figure out what memory</p>



<p class="wp-block-paragraph">location we want with some sort of a formula. But then what will be stored in the first operand is</p>



<p class="wp-block-paragraph">This LEA won&#8217;t dereference just because there are brackets.</p>



<p class="wp-block-paragraph">LEA will keep the pointer.</p>



<p class="wp-block-paragraph">And then finally, once we actually have a pointer to the second item in R15,</p>



<p class="wp-block-paragraph">then we can move the second item into another register.</p>



<p class="wp-block-paragraph">And this is like an LOL because I ran out of registers already.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to use RDI, which is usually the first argument.</p>



<p class="wp-block-paragraph">It doesn&#8217;t matter. It&#8217;s fine.</p>



<p class="wp-block-paragraph">So if we dereference R15, it&#8217;s the same thing as dereferencing this.</p>



<p class="wp-block-paragraph">We could have also just dereferenced R14,</p>



<p class="wp-block-paragraph">and it would have been the same thing as just getting the value of the first item.</p>



<p class="wp-block-paragraph">But I hope you can see now that, you know,</p>



<p class="wp-block-paragraph">that expression is going to be a pointer to the second item in the array,</p>



<p class="wp-block-paragraph">and that&#8217;s going to be stuck into R15.</p>



<p class="wp-block-paragraph">So if we dereference that, we can treat R15 like a pointer.</p>



<p class="wp-block-paragraph">It&#8217;s just, you know, it&#8217;s a general purpose register.</p>



<p class="wp-block-paragraph">it doesn&#8217;t only need to hold integer values it can also hold pointers because pointers are integers</p>



<p class="wp-block-paragraph">they&#8217;re just unsigned so we deref the pointer that is stored inside of r15 now put it inside</p>



<p class="wp-block-paragraph">of rdi and then we should see that rdi actually holds the second item which should be a bunch of</p>



<p class="wp-block-paragraph">twos so the last thing that i&#8217;m going to copy paste in here is another nope so i&#8217;m going to</p>



<p class="wp-block-paragraph">break at 108 just so we can see the rest of it so i&#8217;m going to open up this right here and i&#8217;m</p>



<p class="wp-block-paragraph">open up this right here and I&#8217;m going to comment that out and I&#8217;m gonna say break at 108.</p>



<p class="wp-block-paragraph">Whoops.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">There we go.</p>



<p class="wp-block-paragraph">But, but, but quitsies.</p>



<p class="wp-block-paragraph">Now I&#8217;m just going to run make debug again so that I can enter GDB.</p>



<p class="wp-block-paragraph">And we&#8217;ve, we&#8217;ve, we&#8217;ve hit our break points.</p>



<p class="wp-block-paragraph">And now I&#8217;m going to go info registers and just talk about the stuff that we&#8217;re seeing.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So the first thing I think this is all on one page, right?</p>



<p class="wp-block-paragraph">I don&#8217;t need to scroll.</p>



<p class="wp-block-paragraph">Yeah.</p>



<p class="wp-block-paragraph">one page right I don&#8217;t need to scroll yeah okay this is all on one page first thing we see is r10</p>



<p class="wp-block-paragraph">holds this value right here that&#8217;s a pointer to the my long int that kind of looks like a pointer</p>



<p class="wp-block-paragraph">if you consider that it could be relative and then r11 holds the actual integer and if you recall</p>



<p class="wp-block-paragraph">that is the gigantic long that we put in there great so now that&#8217;s the difference between a</p>



<p class="wp-block-paragraph">pointer and its value or in its dereference value so then we&#8217;re going to grab the first item in the</p>



<p class="wp-block-paragraph">So then we&#8217;re going to grab the first item in the small array.</p>



<p class="wp-block-paragraph">So R12 should have the ones.</p>



<p class="wp-block-paragraph">If we look at R12 here, it&#8217;s got the ones.</p>



<p class="wp-block-paragraph">Again, just trying to prove to you that if you dereference a pointer to an array,</p>



<p class="wp-block-paragraph">you&#8217;re actually dereferencing a pointer to the first item.</p>



<p class="wp-block-paragraph">And then on R13, we&#8217;re going to make a pointer to the second item.</p>



<p class="wp-block-paragraph">Then with the brackets and the move instruction, we dereference that and it should become the twos.</p>



<p class="wp-block-paragraph">So if we look at R13, notice how it&#8217;s the twos.</p>



<p class="wp-block-paragraph">Then we&#8217;re going to grab a pointer to the first item in the small array.</p>



<p class="wp-block-paragraph">Hang on a second.</p>



<p class="wp-block-paragraph">What did I want to say about that?</p>



<p class="wp-block-paragraph">In the small array to R14.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So again, we&#8217;re just going to grab an actual pointer to the small array.</p>



<p class="wp-block-paragraph">And so the difference between R12 and R14 is the dereft value or the pointer.</p>



<p class="wp-block-paragraph">So if we look at R12, that&#8217;s the actual value that we&#8217;re pointing at.</p>



<p class="wp-block-paragraph">And then R14 is the memory location that we have in there.</p>



<p class="wp-block-paragraph">And then notice how it&#8217;s kind of similar.</p>



<p class="wp-block-paragraph">Notice how it&#8217;s kind of similar.</p>



<p class="wp-block-paragraph">Maybe this proves a little bit more that these are pointers that we&#8217;re looking at.</p>



<p class="wp-block-paragraph">R10 was supposed to be a pointer and now R14 is also a pointer.</p>



<p class="wp-block-paragraph">Notice how those numbers are pretty close together.</p>



<p class="wp-block-paragraph">I&#8217;m sure if we looked back up in the data section and we calculated an offset based</p>



<p class="wp-block-paragraph">on how big each data item was, we could probably predict what the memory location of R14 should</p>



<p class="wp-block-paragraph">have been based on R10 because you can see it&#8217;s like only increased by a little bit,</p>



<p class="wp-block-paragraph">by a little bit right it was like 534 to 49 what is that 10 plus 5 so that&#8217;s like 15 is that correct</p>



<p class="wp-block-paragraph">oh I guess I had a byte in there so maybe it&#8217;s that&#8217;s why it&#8217;s kind of odd but yeah we can we</p>



<p class="wp-block-paragraph">can just compute the offset and then we&#8217;ll grab a pointer to the second item using the LEA</p>



<p class="wp-block-paragraph">instruction remember we have to use brackets if we&#8217;re going to do an expression but then the</p>



<p class="wp-block-paragraph">dereferencing in the move instruction so we just use LEA to not dereference.</p>



<p class="wp-block-paragraph">So we should see that R15 is about 8 bytes further along in RAM than R14 because both of those items</p>



<p class="wp-block-paragraph">are contiguous in memory and they should only be about you know a quad word apart 8 bytes.</p>



<p class="wp-block-paragraph">So if we look at R14 it&#8217;s got this number so it&#8217;s a pointer and then if we look at R15</p>



<p class="wp-block-paragraph">you know what&#8217;s 49 plus 8 it&#8217;s 57 so you can see that the pointer to the second item is only</p>



<p class="wp-block-paragraph">pointer to the second item is only eight bytes further in memory yay contiguousness and then</p>



<p class="wp-block-paragraph">sorry i can&#8217;t resist so then finally we will uh we&#8217;ll look at the dereference to r15 because it&#8217;s</p>



<p class="wp-block-paragraph">now a pointer what was it it was the it was a pointer to the second item that should be a bunch</p>



<p class="wp-block-paragraph">of twos right because remember in our array we had a bunch of twos here as our second item</p>



<p class="wp-block-paragraph">have a bunch of twos inside of it so if i look at rdi where the heck is that right there has a</p>



<p class="wp-block-paragraph">bunch of twos inside of it rdi baby oh my gosh why did i say that anyway so that&#8217;s our non-operation</p>



<p class="wp-block-paragraph">instruction and i think that&#8217;s everything that i wanted to talk to you about in this video so now</p>



<p class="wp-block-paragraph">i hope you feel like you&#8217;re an expert in moving integers and moving pointers and</p>



<p class="wp-block-paragraph">dereferencing pointers and manipulating pointers so you can get a pointer to something else and</p>



<p class="wp-block-paragraph">pointer to something else and de-referencing those pointers or not whenever you choose.</p>



<p class="wp-block-paragraph">Thank you so much for watching this video. I hope you learned a little bit of stuff</p>



<p class="wp-block-paragraph">and you had a little bit of fun. I&#8217;ll see you in the next video. Limit out. I&#8217;ll never say that</p>



<p class="wp-block-paragraph">again. Hey everybody, thanks for watching this video again from the bottom of my heart. I really</p>



<p class="wp-block-paragraph">appreciate it. I do hope you did learn something and have some fun. If you could do me a please,</p>



<p class="wp-block-paragraph">could do me a please a small little favor could you please subscribe and</p>



<p class="wp-block-paragraph">follow this channel or these videos or whatever it is you do on the current</p>



<p class="wp-block-paragraph">social media website that you&#8217;re looking at right now it would really mean the</p>



<p class="wp-block-paragraph">world to me and it&#8217;ll help make more videos and grow this community so we&#8217;ll</p>



<p class="wp-block-paragraph">be able to do more videos longer videos better videos or just I&#8217;ll be able to</p>



<p class="wp-block-paragraph">keep making videos in general so please do do me a kindness and and subscribe</p>



<p class="wp-block-paragraph">you know sometimes I&#8217;m sleeping in the middle of the night and I just wake up</p>



<p class="wp-block-paragraph">night and I just wake up because I know somebody subscribed or followed it just</p>



<p class="wp-block-paragraph">wakes me up and I get filled with joy that&#8217;s exactly what happens every single</p>



<p class="wp-block-paragraph">time so you could do it as a nice favor to me or you could you control me if you</p>



<p class="wp-block-paragraph">want to just wake me up in the middle of night just subscribe and then I&#8217;ll just</p>



<p class="wp-block-paragraph">wake up I promise that&#8217;s what will happen also if you look at the middle of</p>



<p class="wp-block-paragraph">the screen right now you should see a QR code which you can scan in order to go</p>



<p class="wp-block-paragraph">to the website which I think is also named somewhere at the bottom of this</p>



<p class="wp-block-paragraph">video and it&#8217;ll take you to my main website where you can just kind of like</p>



<p class="wp-block-paragraph">my main website where you can just kind of like see all the videos I published and the services</p>



<p class="wp-block-paragraph">and tutorials and things that I offer and all that good stuff and uh if you have a suggestion</p>



<p class="wp-block-paragraph">for uh uh clarifications or errata or just future videos that you want to see please leave a comment</p>



<p class="wp-block-paragraph">or if you just want to say hey what&#8217;s up what&#8217;s going on you know just send me a comment whatever</p>



<p class="wp-block-paragraph">I also wake up for those in the middle of the night I get I wake up in a cold sweat and I&#8217;m like</p>



<p class="wp-block-paragraph">It would really mean the world to me.</p>



<p class="wp-block-paragraph">I would really appreciate it.</p>



<p class="wp-block-paragraph">So again, thank you so much for watching this video.</p>



<p class="wp-block-paragraph">And enjoy the cool music as I fade into the darkness, which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-integer-data-movement-pointers-and-dereferencing-explained-yasm-on-ubuntu/">x86-64 Assembly: Integer Data Movement, Pointers, and Dereferencing Explained (YASM on Ubuntu)</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/x86-64-assembly-integer-data-movement-pointers-and-dereferencing-explained-yasm-on-ubuntu/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
