<?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>C++ assembly interop Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/c-assembly-interop/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/c-assembly-interop/</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>C++ assembly interop Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/c-assembly-interop/</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>Mix C++, C, and Assembly into One Program &#8211; Hybrid Program Full Build Tutorial</title>
		<link>https://www.NeuralLantern.com/mix-c-c-and-assembly-into-one-program-hybrid-program-full-build-tutorial/</link>
					<comments>https://www.NeuralLantern.com/mix-c-c-and-assembly-into-one-program-hybrid-program-full-build-tutorial/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 09 Nov 2025 01:47:40 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly tutorial]]></category>
		<category><![CDATA[build system]]></category>
		<category><![CDATA[C and assembly]]></category>
		<category><![CDATA[C++ assembly interop]]></category>
		<category><![CDATA[c++ tutorial]]></category>
		<category><![CDATA[extern C]]></category>
		<category><![CDATA[hybrid programming]]></category>
		<category><![CDATA[linking object files]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[Makefile tutorial]]></category>
		<category><![CDATA[mixed language programming]]></category>
		<category><![CDATA[name mangling]]></category>
		<category><![CDATA[object files]]></category>
		<category><![CDATA[systems programming]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=248</guid>

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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<li>Subscribing to our Blog</li>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">so the object file c.o depends on c.c and so forth.</p>



<p class="wp-block-paragraph">So I&#8217;m just gonna go cpp.o, circle it,</p>



<p class="wp-block-paragraph">and then down here we&#8217;ll do hello.o.</p>



<p class="wp-block-paragraph">Notice how all of these modules, regardless of the language they are written in, compile</p>



<p class="wp-block-paragraph">down to an object file.</p>



<p class="wp-block-paragraph">That&#8217;s really what it is.</p>



<p class="wp-block-paragraph">The higher level languages are just an illusion for us humans.</p>



<p class="wp-block-paragraph">So then at the very top, we want our actual executable file.</p>



<p class="wp-block-paragraph">We&#8217;ll call it main because that&#8217;s kind of a standard thing sometimes.</p>



<p class="wp-block-paragraph">So it&#8217;s like our main, we&#8217;ll have a file named main that we can execute.</p>



<p class="wp-block-paragraph">is going to be the main function.</p>



<p class="wp-block-paragraph">Spoiler, it&#8217;s going to be inside the driver.</p>



<p class="wp-block-paragraph">Remember, the main function, if you&#8217;re using GCC libraries,</p>



<p class="wp-block-paragraph">has to appear in exactly one place, one module in your program.</p>



<p class="wp-block-paragraph">It should appear one time and one time alone, not zero or more than one.</p>



<p class="wp-block-paragraph">So the main executable depends on the driver object file,</p>



<p class="wp-block-paragraph">the C object file, and the C++ object file,</p>



<p class="wp-block-paragraph">and the assembly hello object file.</p>



<p class="wp-block-paragraph">Looks complicated at first until you stare at it a while,</p>



<p class="wp-block-paragraph">complicated at first until you stare at it a while and then you just realize,</p>



<p class="wp-block-paragraph">oh, every source code is going to compile to its own object file.</p>



<p class="wp-block-paragraph">And then after we have all the object files,</p>



<p class="wp-block-paragraph">we&#8217;re going to have our executable just sort of linked together at the final</p>



<p class="wp-block-paragraph">linking stage. So with that in mind,</p>



<p class="wp-block-paragraph">I&#8217;m just going to copy paste a make file that I already wrote.</p>



<p class="wp-block-paragraph">It&#8217;s like my pre solution because this is not a make file video.</p>



<p class="wp-block-paragraph">And I just want you to know just real fast what it looks like.</p>



<p class="wp-block-paragraph">All I&#8217;m doing is I&#8217;m going to make some variables, uh,</p>



<p class="wp-block-paragraph">for my C++ compiler flags and my C compiler flags and my Yasm assembler flags.</p>



<p class="wp-block-paragraph">I&#8217;m really not doing anything fancy.</p>



<p class="wp-block-paragraph">I&#8217;m just sort of setting up some variables so I don&#8217;t have to type.</p>



<p class="wp-block-paragraph">The newer Ubuntu stuffs need a no executable stack flag and a no pi flag.</p>



<p class="wp-block-paragraph">So here we&#8217;re just like making variables for linking.</p>



<p class="wp-block-paragraph">My executable&#8217;s name is going to be main over here.</p>



<p class="wp-block-paragraph">I personally like to make make files that default to a cute help menu.</p>



<p class="wp-block-paragraph">help menu. I know most people just default to building, but, uh, you know, for educational</p>



<p class="wp-block-paragraph">purposes, I think this is a little more fun, a little easier. So this way later I can just type</p>



<p class="wp-block-paragraph">make menu or make build or make run or make clean or whatever I want to do. And, um,</p>



<p class="wp-block-paragraph">I&#8217;m going to make a target called run, which is what we&#8217;re going to be using. And then a target</p>



<p class="wp-block-paragraph">called build, which just only builds everything. The run target obviously depends on build so that</p>



<p class="wp-block-paragraph">to run it and and if you don&#8217;t know make files at this point it&#8217;s okay this is not a make file video</p>



<p class="wp-block-paragraph">i&#8217;m going to make some make file videos in the future</p>



<p class="wp-block-paragraph">then my binary the main program is just going to depend on all these object files notice how it</p>



<p class="wp-block-paragraph">depends on the driver the hello the c plus plus and the c object files so really uh at the linking</p>



<p class="wp-block-paragraph">stage we&#8217;re just going to use g plus plus to just sort of link all the object files together</p>



<p class="wp-block-paragraph">Then we have a little section to compile each source code file.</p>



<p class="wp-block-paragraph">So in order to make driver.o, I&#8217;m going to compile,</p>



<p class="wp-block-paragraph">I&#8217;m going to depend on the driver.cpp.</p>



<p class="wp-block-paragraph">And then when it comes to actually compiling, compiling it,</p>



<p class="wp-block-paragraph">I&#8217;m just going to type the commands to compile it here.</p>



<p class="wp-block-paragraph">Don&#8217;t worry about these crazy variables.</p>



<p class="wp-block-paragraph">I&#8217;m going to make more videos in the future.</p>



<p class="wp-block-paragraph">And then each module gets compiled in its own way.</p>



<p class="wp-block-paragraph">Then I just have a little target at the bottom called clean because I like to be</p>



<p class="wp-block-paragraph">able to just clean my build area of object files.</p>



<p class="wp-block-paragraph">I don&#8217;t know about you, but I like a clean build area.</p>



<p class="wp-block-paragraph">I&#8217;m a clean build area guy.</p>



<p class="wp-block-paragraph">I don&#8217;t like a dirty build area.</p>



<p class="wp-block-paragraph">So that was the make file.</p>



<p class="wp-block-paragraph">Again, this is not a make file video,</p>



<p class="wp-block-paragraph">so don&#8217;t worry too much about it right now.</p>



<p class="wp-block-paragraph">The next thing we should do is make a driver.</p>



<p class="wp-block-paragraph">Again, I&#8217;m gonna copy paste from my solution here</p>



<p class="wp-block-paragraph">because the driver should be easy.</p>



<p class="wp-block-paragraph">This is really, this video is mostly gonna be</p>



<p class="wp-block-paragraph">for people who have never done assembly before.</p>



<p class="wp-block-paragraph">So I&#8217;m just gonna assume you know a little bit</p>



<p class="wp-block-paragraph">about C++ and C already.</p>



<p class="wp-block-paragraph">So I&#8217;m going to say touch, let&#8217;s see, driver.cpp so that I can get the driver file in there.</p>



<p class="wp-block-paragraph">And I&#8217;m going to go nano driver, probably put it down in the first place.</p>



<p class="wp-block-paragraph">Then I&#8217;m just going to paste my solution.</p>



<p class="wp-block-paragraph">Oops, you can tell I copy pasted this from my IDE because I have the extra tabs in there, which I love.</p>



<p class="wp-block-paragraph">One thing to keep in mind is that, I guess like one of the first rules that I want to tell you for hybrid programs</p>



<p class="wp-block-paragraph">is that assembly doesn&#8217;t understand something called name mangling.</p>



<p class="wp-block-paragraph">mangling. So what is name mangling? Name mangling is basically when you have a,</p>



<p class="wp-block-paragraph">oh, this is not going to get called. So this is maybe not a great idea to show you name</p>



<p class="wp-block-paragraph">mangling. Let me, let me start on one of the other ones real fast too. Let&#8217;s do</p>



<p class="wp-block-paragraph">the C++ module. Okay. So I&#8217;m going to go nano CPP dot CPP. I know it&#8217;s a silly name. I know it.</p>



<p class="wp-block-paragraph">So CPP dot CPP, this is just a simple C++ program, which is meant to be called by</p>



<p class="wp-block-paragraph">by the assembly module just to show you that we can.</p>



<p class="wp-block-paragraph">And so we just have a little function here,</p>



<p class="wp-block-paragraph">void, you know, the CPP function.</p>



<p class="wp-block-paragraph">So it&#8217;s just a function that doesn&#8217;t really return anything.</p>



<p class="wp-block-paragraph">Inside of it, I&#8217;m just going to print,</p>



<p class="wp-block-paragraph">hello, this is the C++ function, no problem.</p>



<p class="wp-block-paragraph">Notice at the top,</p>



<p class="wp-block-paragraph">this is the first important thing that you should understand.</p>



<p class="wp-block-paragraph">In C++, we have overloads, right?</p>



<p class="wp-block-paragraph">You can have the same function name,</p>



<p class="wp-block-paragraph">but with different argument lists.</p>



<p class="wp-block-paragraph">And so that gets resolved under the hood</p>



<p class="wp-block-paragraph">by the name actually kind of changing</p>



<p class="wp-block-paragraph">based on what the argument signature is,</p>



<p class="wp-block-paragraph">what the prototype signature is.</p>



<p class="wp-block-paragraph">So that&#8217;s great and all,</p>



<p class="wp-block-paragraph">but in assembly, at least at this level,</p>



<p class="wp-block-paragraph">there&#8217;s no way to really differentiate between overloads.</p>



<p class="wp-block-paragraph">In assembly, we&#8217;ll just call a name of some function.</p>



<p class="wp-block-paragraph">But if we try to call a name,</p>



<p class="wp-block-paragraph">let&#8217;s say of just the CPP function,</p>



<p class="wp-block-paragraph">if we say call the CPP function in assembly,</p>



<p class="wp-block-paragraph">it&#8217;s not going to work</p>



<p class="wp-block-paragraph">because the actual function has not been compiled to have that name.</p>



<p class="wp-block-paragraph">It&#8217;s been compiled to have the CPP function</p>



<p class="wp-block-paragraph">and a bunch of extra symbols indicating what the argument list is.</p>



<p class="wp-block-paragraph">This is great for C++ programs, but it&#8217;s poor.</p>



<p class="wp-block-paragraph">It&#8217;s bad for other modules to be able to call the C++ function</p>



<p class="wp-block-paragraph">because they won&#8217;t really know what the actual name is</p>



<p class="wp-block-paragraph">after the mangling has been done.</p>



<p class="wp-block-paragraph">So what you want to do is make a little block called Xtern C,</p>



<p class="wp-block-paragraph">just like this.</p>



<p class="wp-block-paragraph">You say extern and then C in quotes and then do a scope with braces.</p>



<p class="wp-block-paragraph">And inside of that, just put the prototypes of any function that you want to be available to other modules.</p>



<p class="wp-block-paragraph">So, for example, we want the CPP function to be available to assembly modules and C modules, not just other C++ modules.</p>



<p class="wp-block-paragraph">So, I&#8217;m just going to put the prototype of it right there.</p>



<p class="wp-block-paragraph">You can put as many prototypes as you want.</p>



<p class="wp-block-paragraph">I think they should come from the same source file.</p>



<p class="wp-block-paragraph">different source file where you want something to be available elsewhere,</p>



<p class="wp-block-paragraph">you would put an extern C block for its prototype.</p>



<p class="wp-block-paragraph">This disables name mangling,</p>



<p class="wp-block-paragraph">which means the compiled name of the CPP function will actually be the CPP</p>



<p class="wp-block-paragraph">function. So it&#8217;s pretty good. This will make it available to assembly.</p>



<p class="wp-block-paragraph">So now that I&#8217;ve kind of explained that I can maybe close this and go back to</p>



<p class="wp-block-paragraph">what I was doing before. So this is the main driver program.</p>



<p class="wp-block-paragraph">We don&#8217;t need to engage in a dis,</p>



<p class="wp-block-paragraph">we don&#8217;t need to disable name mangling for the main function because no one is going to call main</p>



<p class="wp-block-paragraph">except the GCC libraries or like the OS. But when the C++ module calls other modules, it needs to</p>



<p class="wp-block-paragraph">be able to call the right name. And remember in C++, there&#8217;s name mangling enabled by default.</p>



<p class="wp-block-paragraph">So if I tell it, let&#8217;s say on this line right here, or you can do alt N to get line numbers on nano.</p>



<p class="wp-block-paragraph">we want to call on the hello module will C++ by default will assume that hello has name</p>



<p class="wp-block-paragraph">mangling applied to it, which means it will actually try to call some name that is totally</p>



<p class="wp-block-paragraph">different based on the prototype signature, the incoming arguments and things like that.</p>



<p class="wp-block-paragraph">But so we want to disable that because we wanted to just call the actual name because</p>



<p class="wp-block-paragraph">hello is going to be a function that we make inside of assembly.</p>



<p class="wp-block-paragraph">So it&#8217;s not going to be name mangled.</p>



<p class="wp-block-paragraph">So that&#8217;s why up here on line seven, we enter its signature.</p>



<p class="wp-block-paragraph">Enter its signature. Oh, actually this is from a different version of something that I was doing. I&#8217;m not going to return anything from hello</p>



<p class="wp-block-paragraph">We&#8217;ll do name a demangling with our extern C block just void hello</p>



<p class="wp-block-paragraph">And now when we call hello, it&#8217;s actually going to call a function called hello without any other</p>



<p class="wp-block-paragraph">mangling same thing for the C S E A</p>



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



<p class="wp-block-paragraph">That was from a different version. I wrote we&#8217;re not going to be calling C in this module. Don&#8217;t worry</p>



<p class="wp-block-paragraph">We&#8217;re going to keep it simple</p>



<p class="wp-block-paragraph">So pretty much we&#8217;re going to print hello and then we&#8217;re going to print,</p>



<p class="wp-block-paragraph">then we&#8217;re going to call the hello and then we&#8217;re going to say goodbye.</p>



<p class="wp-block-paragraph">And then that&#8217;s going to be the end of the program.</p>



<p class="wp-block-paragraph">So let me close out of that and just double check what I have written right now.</p>



<p class="wp-block-paragraph">So we&#8217;ve got CPP and okay, we need to do C, nano C dot C.</p>



<p class="wp-block-paragraph">Okay, so let me get my solution real fast here.</p>



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



<p class="wp-block-paragraph">Literally, it&#8217;s just a C program that prints something.</p>



<p class="wp-block-paragraph">Remember in C++, we&#8217;ve got the IO stream, but we don&#8217;t have that in C.</p>



<p class="wp-block-paragraph">that in C. So I&#8217;m just going to use printf. Oh, by the way, this is a C function. Hi.</p>



<p class="wp-block-paragraph">So done with that. Pretty easy. Shoot. You know what? I need to start learning how to do</p>



<p class="wp-block-paragraph">clear and LSA. So I can just go up, up, up. So C++, C and the driver</p>



<p class="wp-block-paragraph">and the make file, which is huge. I just realized that. And now I guess we can start</p>



<p class="wp-block-paragraph">or hello function.</p>



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



<p class="wp-block-paragraph">So maybe I want to write this in the Genie program.</p>



<p class="wp-block-paragraph">So I&#8217;m going to, well, maybe I&#8217;ll first, I&#8217;ll say touch,</p>



<p class="wp-block-paragraph">and I&#8217;ll say hello.assembly.</p>



<p class="wp-block-paragraph">And then now that it&#8217;s there, I can try to open it up.</p>



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



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



<p class="wp-block-paragraph">Okay, so now let&#8217;s write an assembly program.</p>



<p class="wp-block-paragraph">I&#8217;m not going to spend too much time talking about this,</p>



<p class="wp-block-paragraph">but I&#8217;ll say like data section here,</p>



<p class="wp-block-paragraph">and we&#8217;ll say section.data.</p>



<p class="wp-block-paragraph">We&#8217;ll say section.data and then we&#8217;ll say system calls.</p>



<p class="wp-block-paragraph">This is not a system calls video.</p>



<p class="wp-block-paragraph">I&#8217;m just gonna do this very quickly without explaining.</p>



<p class="wp-block-paragraph">System write is gonna be code one, I guess.</p>



<p class="wp-block-paragraph">And then we&#8217;re gonna do file descriptors, one for stdout.</p>



<p class="wp-block-paragraph">Whoops, stdout, it&#8217;s gonna be equal to one.</p>



<p class="wp-block-paragraph">This is not a file descriptors video.</p>



<p class="wp-block-paragraph">I&#8217;ll put FDs, I guess.</p>



<p class="wp-block-paragraph">And then we&#8217;ll make some C strings.</p>



<p class="wp-block-paragraph">C strings, you know, just character arrays, basically.</p>



<p class="wp-block-paragraph">I&#8217;ll say, well, you know what?</p>



<p class="wp-block-paragraph">I don&#8217;t want to type this all out</p>



<p class="wp-block-paragraph">because this is not a lesson on variables.</p>



<p class="wp-block-paragraph">I&#8217;m just going to copy paste my existing solution here.</p>



<p class="wp-block-paragraph">And, you know, long story short,</p>



<p class="wp-block-paragraph">I&#8217;m going to try to make this a pretty program</p>



<p class="wp-block-paragraph">that prints hello and goodbye and things.</p>



<p class="wp-block-paragraph">And so we just have like some variables</p>



<p class="wp-block-paragraph">that sort of just you know old strings and hold the strings length and we&#8217;re</p>



<p class="wp-block-paragraph">gonna do one that calls a new line and so forth actually that&#8217;ll be a different</p>



<p class="wp-block-paragraph">video but I&#8217;ll leave it in there for now so now let&#8217;s make the text section</p>



<p class="wp-block-paragraph">remember the text section in Yasm is just for the actual code of your program</p>



<p class="wp-block-paragraph">and then we need to set up some external symbols. So right now, at least for the purposes of this</p>



<p class="wp-block-paragraph">source code, this source code is inside of the hello module. So you can imagine we&#8217;re inside of</p>



<p class="wp-block-paragraph">hello.o. All the o&#8217;s get linked together in the executable, but between themselves, there&#8217;s kind</p>



<p class="wp-block-paragraph">of a little bit of like a border situation happening. There&#8217;s like some separation. So</p>



<p class="wp-block-paragraph">are going to be available to the other modules,</p>



<p class="wp-block-paragraph">even if they&#8217;re part of the same executable.</p>



<p class="wp-block-paragraph">It&#8217;s kind of convenient because you would like sometimes</p>



<p class="wp-block-paragraph">to be able to write tons and tons of functions</p>



<p class="wp-block-paragraph">that are only available inside of your assembly module,</p>



<p class="wp-block-paragraph">and they might not be available to other modules,</p>



<p class="wp-block-paragraph">and therefore you don&#8217;t want to pollute the namespace,</p>



<p class="wp-block-paragraph">I guess, of the global namespace of the executable.</p>



<p class="wp-block-paragraph">So by default, if I make a function called hello,</p>



<p class="wp-block-paragraph">how you make a function, just a quick recap here.</p>



<p class="wp-block-paragraph">This is not a functions video.</p>



<p class="wp-block-paragraph">make a symbol with the name of the function you want and then you just put a ret after it and then</p>



<p class="wp-block-paragraph">you hope that whoever got you there is going to use the call instruction but anyway for now</p>



<p class="wp-block-paragraph">we&#8217;re just going to say this is our hello function okay i cannot call the hello function now from</p>



<p class="wp-block-paragraph">another module i have to make it visible to the other modules with a special keyword called global</p>



<p class="wp-block-paragraph">our program the hello function will be available to the other modules but not anything else that</p>



<p class="wp-block-paragraph">I type without marking it as global so let&#8217;s go ahead and do let&#8217;s do a welcome message and this</p>



<p class="wp-block-paragraph">is not a system call a video maybe I should just copy paste this so just looking at it this is a</p>



<p class="wp-block-paragraph">going to quickly you know breeze through what we&#8217;re doing in case you&#8217;re interested but you</p>



<p class="wp-block-paragraph">don&#8217;t have to care too much about this right now we&#8217;re going to tell rax what system call we want</p>



<p class="wp-block-paragraph">to make the code for that is what i put up in system right which is just code one means let&#8217;s</p>



<p class="wp-block-paragraph">write to a file we&#8217;ll give it the file handle uh that&#8217;s going to be the variable we made called fd</p>



<p class="wp-block-paragraph">std out which means we&#8217;re going to be giving it the file handle of one which means we&#8217;re telling</p>



<p class="wp-block-paragraph">to print to standard output because file handle one is always standard output.</p>



<p class="wp-block-paragraph">And then we just give it the a pointer to the first character of the message</p>



<p class="wp-block-paragraph">string that we made and then tell it how long that message is. Then we do system call this should</p>



<p class="wp-block-paragraph">actually um this this should actually print a string. Okay so then uh let&#8217;s see down here I&#8217;m</p>



<p class="wp-block-paragraph">going to make a function called crlf because I&#8217;m lazy and I don&#8217;t like constantly adding extra stuff</p>



<p class="wp-block-paragraph">constantly adding extra stuff to the string so that there will be like a new line line feed</p>



<p class="wp-block-paragraph">situation so i usually like to make a little function somewhere called crlf carriage return</p>



<p class="wp-block-paragraph">line feed is what it stands for and it&#8217;s the same thing it&#8217;s just like load up the system call</p>



<p class="wp-block-paragraph">registers so that it knows i want to print the string which is just a line feed</p>



<p class="wp-block-paragraph">carriage return line feed and then actually call system call and then return to the caller so this</p>



<p class="wp-block-paragraph">back up here let&#8217;s see crlf i guess i am going to be doing it it&#8217;s just a string with the the</p>



<p class="wp-block-paragraph">slash r slash n or the carriage return line feed characters in ascii those are 13 comma 10 so i can</p>



<p class="wp-block-paragraph">put that there instead of a literal quoted string okay so we got that and then that means after the</p>



<p class="wp-block-paragraph">welcome message i can just like call crlf in order to have uh the cursor jump down to the next uh</p>



<p class="wp-block-paragraph">after that string is notice how I don&#8217;t have any line feeds after my strings that&#8217;s kind of why I</p>



<p class="wp-block-paragraph">like to do crlf and it lets me just call crlf a bunch of times if I want to just like space things</p>



<p class="wp-block-paragraph">out temporarily okay so we&#8217;ve got the hello function and now we&#8217;re going to print another</p>



<p class="wp-block-paragraph">message here I&#8217;ll just explain this real fast so I&#8217;m going to print a message about calling a c</p>



<p class="wp-block-paragraph">So same thing, we&#8217;re just going to tell the system call service that we want to print a string and it&#8217;s going to be our string here that says, you know, we&#8217;re about to print this.</p>



<p class="wp-block-paragraph">We&#8217;re about to, what do we say?</p>



<p class="wp-block-paragraph">We&#8217;re about to call the C function and then later we&#8217;re going to call the C++ function.</p>



<p class="wp-block-paragraph">So we&#8217;re just going to print a message announcing and do CRLF.</p>



<p class="wp-block-paragraph">So that&#8217;s really nothing new at all.</p>



<p class="wp-block-paragraph">And then down here we actually call the C function, which is not going to be in this module.</p>



<p class="wp-block-paragraph">It&#8217;s going to be in the C module, the other source code.</p>



<p class="wp-block-paragraph">Somewhere in this program, we need to let our assembly module know</p>



<p class="wp-block-paragraph">that the C function is available because it kind of goes both ways.</p>



<p class="wp-block-paragraph">In assembly, I have to mark a function as global if I want other modules to be able</p>



<p class="wp-block-paragraph">to call that function, but I also have to mark a function as external</p>



<p class="wp-block-paragraph">if I want to be able to call some other modules function.</p>



<p class="wp-block-paragraph">So we have to go back up to the top.</p>



<p class="wp-block-paragraph">I mean, somewhere in the text section, I think would be fine.</p>



<p class="wp-block-paragraph">But for me, I just like to put it at the top.</p>



<p class="wp-block-paragraph">put it at the top at the top so I&#8217;ll say externals and then I&#8217;ll go extern</p>



<p class="wp-block-paragraph">keyword and then just name the function that I intend to call this way when the</p>



<p class="wp-block-paragraph">linker is trying to link all the objects together it doesn&#8217;t get upset because</p>



<p class="wp-block-paragraph">you tried to call a C function which is not in the current module it&#8217;ll</p>



<p class="wp-block-paragraph">understand that it&#8217;s gonna happen you know like it&#8217;s gonna call other modules</p>



<p class="wp-block-paragraph">for that it usually gets upset right away and then while we&#8217;re at it let&#8217;s do</p>



<p class="wp-block-paragraph">Let&#8217;s turn the CPP function because that&#8217;s going to come from the C++ module.</p>



<p class="wp-block-paragraph">So now we have both of our external functions set up.</p>



<p class="wp-block-paragraph">We&#8217;ve marked our global hello function so other modules can call us.</p>



<p class="wp-block-paragraph">And then let&#8217;s see, oh, what did I do wrong?</p>



<p class="wp-block-paragraph">I put the C message in the wrong spot.</p>



<p class="wp-block-paragraph">I was inside of the CRLF function.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to copy paste that back up here.</p>



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



<p class="wp-block-paragraph">Then we&#8217;ll do the same thing for the C++ message,</p>



<p class="wp-block-paragraph">or for the C++ stuff.</p>



<p class="wp-block-paragraph">I will first print a C++ message</p>



<p class="wp-block-paragraph">with these lines up here.</p>



<p class="wp-block-paragraph">That&#8217;s just like a regular system call,</p>



<p class="wp-block-paragraph">and then I&#8217;ll do a CRLF, so nothing new,</p>



<p class="wp-block-paragraph">just printing something.</p>



<p class="wp-block-paragraph">And then I will actually call the C++ function</p>



<p class="wp-block-paragraph">called the CPP function.</p>



<p class="wp-block-paragraph">Then I&#8217;ll say goodbye with another block of text.</p>



<p class="wp-block-paragraph">Again, not super important or complicated,</p>



<p class="wp-block-paragraph">so I&#8217;m just gonna paste it.</p>



<p class="wp-block-paragraph">Just another string.</p>



<p class="wp-block-paragraph">string. And then when we&#8217;re done, I&#8217;ll just say, you know, return, we&#8217;re done. So that means if</p>



<p class="wp-block-paragraph">you notice how we don&#8217;t have like any symbols in here called underscore start, that means this is</p>



<p class="wp-block-paragraph">not a pure assembly program. And we don&#8217;t even have a symbol inside of here that is called main.</p>



<p class="wp-block-paragraph">So that means this hello ASM module is not going to be the entry point of our program. It&#8217;s just</p>



<p class="wp-block-paragraph">going to expose a function called hello, which does some stuff. The entry point of our program</p>



<p class="wp-block-paragraph">really, let&#8217;s see, where is it? Oh, gosh, I think I already closed it for some reason. It&#8217;s the</p>



<p class="wp-block-paragraph">driver program, which we talked about. So remember the driver program, it&#8217;s got a main here. So that</p>



<p class="wp-block-paragraph">means this is going to be what the GCC libraries or the operating system calls, however you want</p>



<p class="wp-block-paragraph">to think of it. So this is the entry point of our program, it&#8217;ll just print something quickly,</p>



<p class="wp-block-paragraph">then it&#8217;ll call the hello function, which means execution will jump in to the hello module. So</p>



<p class="wp-block-paragraph">module and and also it would be really easy to apply this logic to have a c module call on an</p>



<p class="wp-block-paragraph">assembly module and in fact you can use this logic to have every type of module call on any other i</p>



<p class="wp-block-paragraph">mean just if you have a c++ function either being defined in the current module or being referenced</p>



<p class="wp-block-paragraph">in another module just make sure that you have an extern c block and then if you&#8217;re inside of a c</p>



<p class="wp-block-paragraph">and you&#8217;re going to call a non-name mingled module like C or assembly,</p>



<p class="wp-block-paragraph">then just do this block here.</p>



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



<p class="wp-block-paragraph">What else can I really tell you?</p>



<p class="wp-block-paragraph">The C module, we looked at that.</p>



<p class="wp-block-paragraph">The C++, we looked at that.</p>



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



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



<p class="wp-block-paragraph">I mean, I guess we&#8217;re kind of done already.</p>



<p class="wp-block-paragraph">So what we should see now is a little intro here.</p>



<p class="wp-block-paragraph">Hello, my name is whatever.</p>



<p class="wp-block-paragraph">And then when we jump into the hello module,</p>



<p class="wp-block-paragraph">we should see a little welcome message about being in the assembly module.</p>



<p class="wp-block-paragraph">And then we should see a message about calling the C function.</p>



<p class="wp-block-paragraph">And then the C function should print.</p>



<p class="wp-block-paragraph">Let me open that up for you.</p>



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



<p class="wp-block-paragraph">The C function should print.</p>



<p class="wp-block-paragraph">Just, oh, by the way, this is the C function.</p>



<p class="wp-block-paragraph">And then when it returns at the very end, remember, even in a void function, there is</p>



<p class="wp-block-paragraph">a return statement implicit at the end of scope.</p>



<p class="wp-block-paragraph">So when it returns, it&#8217;ll come back down here.</p>



<p class="wp-block-paragraph">And then the C++ message will get printed.</p>



<p class="wp-block-paragraph">printed and then the C++ function will get called which is going to end up being</p>



<p class="wp-block-paragraph">just it just prints you know another message this is the C++ function</p>



<p class="wp-block-paragraph">then when it returns implicitly at the end of the function</p>



<p class="wp-block-paragraph">then we get down to here where we just basically say goodbye and then return to the caller</p>



<p class="wp-block-paragraph">the caller in this case is going to be the driver program because this whole time we were just inside</p>



<p class="wp-block-paragraph">called. So then the driver will say goodbye and then it will return control to the GCC libraries</p>



<p class="wp-block-paragraph">or the operating system ending the program. Let&#8217;s see if that works. Okay, clear. And so,</p>



<p class="wp-block-paragraph">you know, the way I wreck my make files, I&#8217;d like to do a little menu. I type make and it&#8217;s like,</p>



<p class="wp-block-paragraph">oh, the menu, make menu, make build, just in case, just to see what&#8217;s up. Okay, clear.</p>



<p class="wp-block-paragraph">say, let&#8217;s do clear and make run because that&#8217;s really what we&#8217;re after.</p>



<p class="wp-block-paragraph">Again, run will implicitly call build.</p>



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



<p class="wp-block-paragraph">And it seems to have succeeded.</p>



<p class="wp-block-paragraph">It wasn&#8217;t actually very exciting.</p>



<p class="wp-block-paragraph">Let me do a make clean so you can see it compile real fast.</p>



<p class="wp-block-paragraph">We run it.</p>



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



<p class="wp-block-paragraph">That was still kind of fast.</p>



<p class="wp-block-paragraph">So up here is basically the make file, all these lines.</p>



<p class="wp-block-paragraph">They&#8217;re just calling your normal compiler and assembler tools and linker tools.</p>



<p class="wp-block-paragraph">linker tools and then here&#8217;s the driver like we said it prints the hello message</p>



<p class="wp-block-paragraph">you know my name is whatever I love that name I have a strange name generator</p>



<p class="wp-block-paragraph">that I found online that&#8217;s not my name you can call me that if you want to and</p>



<p class="wp-block-paragraph">then we have the message that we&#8217;re going to call the hello module so now</p>



<p class="wp-block-paragraph">we&#8217;ve called into the hello function inside of the assembly module assembly</p>



<p class="wp-block-paragraph">module prints hello you know I&#8217;m inside of here now and then the assembly module</p>



<p class="wp-block-paragraph">Now we&#8217;re going to call the C function and then the C function actually</p>



<p class="wp-block-paragraph">executes and it is now printing with its print F function call that it did,</p>



<p class="wp-block-paragraph">you know, by the way, this is a C function. Hi.</p>



<p class="wp-block-paragraph">And then it returns to the caller.</p>



<p class="wp-block-paragraph">Maybe I could have added another print statement if I felt like I wanted to</p>



<p class="wp-block-paragraph">spend more time, but it returns to the caller, which is the hello function.</p>



<p class="wp-block-paragraph">Then we get to the next part of the hello function where it&#8217;s like now calling</p>



<p class="wp-block-paragraph">the C function as a print statement.</p>



<p class="wp-block-paragraph">Then it actually does call the C function. The C function prints itself out.</p>



<p class="wp-block-paragraph">prints itself out hello this is the C function and then the assembly module</p>



<p class="wp-block-paragraph">says goodbye it says it&#8217;s finished and then it returns to the caller so finally</p>



<p class="wp-block-paragraph">at the end of this print statement the there&#8217;s like that little return statement</p>



<p class="wp-block-paragraph">at the bottom of the hello module that returns to the caller which was the</p>



<p class="wp-block-paragraph">driver dot CPP program so then the driver prints the driver is now back in</p>



<p class="wp-block-paragraph">the driver is now back in control. You can see that message behind the terminal here.</p>



<p class="wp-block-paragraph">The driver is now back in control. Then it says goodbye. Then it returns zero to the operating</p>



<p class="wp-block-paragraph">system and the whole program is done. So it&#8217;s important to understand that if you want to</p>



<p class="wp-block-paragraph">pass arguments, right now I&#8217;m just going to say, you know, I&#8217;m going to make another video that</p>



<p class="wp-block-paragraph">is more robust in the future about passing arguments back and forth between these. It&#8217;s</p>



<p class="wp-block-paragraph">important to understand for now that you can pass arguments between all these different</p>



<p class="wp-block-paragraph">between all these different module functions if you want.</p>



<p class="wp-block-paragraph">Like if I wanted to give the.</p>



<p class="wp-block-paragraph">If I wanted to give the hello function.</p>



<p class="wp-block-paragraph">An incoming argument, let&#8217;s say like an integer or a character value or a pointer</p>



<p class="wp-block-paragraph">or something like that, I could just load up appropriate registers.</p>



<p class="wp-block-paragraph">Remember, we have argument registers in assembly.</p>



<p class="wp-block-paragraph">I think I can never remember if RSI or RDI are the first one.</p>



<p class="wp-block-paragraph">I&#8217;m going to say RDI is the first argument just off the top of my head.</p>



<p class="wp-block-paragraph">I always look that up.</p>



<p class="wp-block-paragraph">So if I wanted to call, let&#8217;s say give an integer argument to hello when I originally</p>



<p class="wp-block-paragraph">called it, then from the driver, I literally would just have to type an integer argument</p>



<p class="wp-block-paragraph">in there and then under the hood, the RDI register, assuming that I&#8217;m right about RDI</p>



<p class="wp-block-paragraph">being the first argument, the RDI register would get loaded up with that value.</p>



<p class="wp-block-paragraph">your assembly program all you&#8217;d have to do is do something with the RDI register</p>



<p class="wp-block-paragraph">you know load it up save it do whatever it is that you think you&#8217;re gonna do so</p>



<p class="wp-block-paragraph">that&#8217;s how you pass an integer or like a character or a pointer from C++ to</p>



<p class="wp-block-paragraph">assembly it works the same exact way if you want to pass arguments when calling</p>



<p class="wp-block-paragraph">from C to assembly and when if you want to pass integers from assembly to C or</p>



<p class="wp-block-paragraph">or C++, it&#8217;s the same thing just backwards.</p>



<p class="wp-block-paragraph">First, you load up RDI as the first argument register with whatever value you want to pass</p>



<p class="wp-block-paragraph">to C or C++.</p>



<p class="wp-block-paragraph">And then in the signature of the C++ function, you just expect, you know, a long or, you</p>



<p class="wp-block-paragraph">know, whatever you were going to get and the register will already be loaded up.</p>



<p class="wp-block-paragraph">Remember, the C++ is not really happening on the machine.</p>



<p class="wp-block-paragraph">It gets compiled down to assembly.</p>



<p class="wp-block-paragraph">So this source code that we&#8217;re looking at, it&#8217;s another illusion for our benefit.</p>



<p class="wp-block-paragraph">benefit. It&#8217;s not happening. It&#8217;s getting assembled and then compiled down to machine code. And then</p>



<p class="wp-block-paragraph">that is what&#8217;s actually happening on the machine. So when you write that you have an argument in a</p>



<p class="wp-block-paragraph">C++ function signature prototype, you&#8217;re not really writing that at all. You&#8217;re just saying</p>



<p class="wp-block-paragraph">that the A variable should get loaded from whatever the RDI register had.</p>



<p class="wp-block-paragraph">Or if you previously loaded the RDI register and you want to call a C++ function, that&#8217;s</p>



<p class="wp-block-paragraph">a C++ function that&#8217;s what it would expect the RDI register so it can work pretty seamlessly</p>



<p class="wp-block-paragraph">just calling things back and forth um so yeah I guess that&#8217;s all I really had to say</p>



<p class="wp-block-paragraph">this is the basics of writing a hybrid program thank you for watching I hope you learned a</p>



<p class="wp-block-paragraph">little bit of stuff and had a little bit of fun see you in the next video</p>



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



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



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



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



<p class="wp-block-paragraph">website that you&#8217;re looking at right now it would really mean the world to me and</p>



<p class="wp-block-paragraph">it&#8217;ll help make more videos and grow this community so we&#8217;ll be able to do</p>



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



<p class="wp-block-paragraph">videos in general. So please do me a kindness and subscribe. You know, sometimes I&#8217;m sleeping</p>



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



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



<p class="wp-block-paragraph">So you could do it as a nice favor to me or you could troll me if you want to just wake me up in</p>



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



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



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



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



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



<p class="wp-block-paragraph">good stuff and if you have a suggestion for clarifications or errata or just future videos</p>



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



<p class="wp-block-paragraph">you know just send me a comment whatever i also wake up for those in the middle of the night i get</p>



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



<p class="wp-block-paragraph">really appreciate it so again thank you so much for watching this video and um enjoy the cool music</p>



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



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/mix-c-c-and-assembly-into-one-program-hybrid-program-full-build-tutorial/">Mix C++, C, and Assembly into One Program &#8211; Hybrid Program Full Build Tutorial</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/mix-c-c-and-assembly-into-one-program-hybrid-program-full-build-tutorial/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
