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

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

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



<p>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>Great for people moving from NASM/GAS or trying to mix assembly with higher-level code.</p>



<p>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>=-=-=-=-=-=-=-=-=</p>



<p>Thanks for watching!</p>



<p>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>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>Hello there.</p>



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



<p>of Ubuntu, although probably any YASM assembler will be fine.</p>



<p>I&#8217;ve covered these topics before in other videos, but I thought it would be nice to</p>



<p>put them all here in one single video to make it a little bit easier to understand.</p>



<p>Okay, so first off, what the heck am I talking about?</p>



<p>Well, you know, when you have a program, let&#8217;s say we have a higher level language program</p>



<p>and we have like, you know, void or actually probably not void main, let&#8217;s say int main</p>



<p>and we&#8217;ll forget about the arguments and inside of main, it just sort of calls f and then</p>



<p>we have an f function over here and it, you know, does stuff, right?</p>



<p>So that&#8217;s the basic idea of having a function that you can call and calling it.</p>



<p>We sort of take the whole process for granted in higher level languages, but in assembly,</p>



<p>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>arguments how to pass integer arguments pointer arguments floating point arguments return types</p>



<p>you know double like floating return types integer return types pointer return types</p>



<p>how to use special registers to actually pass the arguments into the functions how to write a</p>



<p>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>out that um this video is not about the basics of assembly nor is it about the basics of um</p>



<p>make files or some of the other related technologies that i&#8217;m going to show</p>



<p>i&#8217;ve covered everything in this video in a previous video um so if you if you find yourself</p>



<p>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>look at my previous videos because this is sort of a summary video to kind of help you</p>



<p>of help you help you really lock down on the concepts of writing functions okay so for starters</p>



<p>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>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>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>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>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>that I&#8217;m showing you now as a as a hybrid program a program with modules of different languages so</p>



<p>this module right here is going to be c++ and then we&#8217;re going to have an assembly module and</p>



<p>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>going to name some functions that are going to be inside of my assembly module and also name some</p>



<p>module and also name some functions that are going to well name a function that&#8217;s</p>



<p>going to be just inside of the C++ module but it should be callable by the</p>



<p>assembly module so for that we use a little block called extern C and long</p>



<p>story short that disables name mangling which C++ does in order to provide</p>



<p>overloading functionality so we&#8217;re basically going to disable overload</p>



<p>functionality in order to have simple function names that way assembly can</p>



<p>and also when we attempt to call a function inside of assembly we&#8217;ll just call it by its</p>



<p>simple name rather than try to mangle the name based on the the arguments and return type and</p>



<p>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>going to move on so basically you just take you just take the keyword extern and you put a c in</p>



<p>a quote and then you make a block right a little braces scope and then you just list prototypes of</p>



<p>types of either the functions inside of the current module which you would like to demangle</p>



<p>or other modules functions that you would like to be able to call in a demangled or</p>



<p>I guess a non-mangled way.</p>



<p>Mangled meaning name mangling.</p>



<p>Okay, so then I&#8217;m going to copy paste the main function that I&#8217;m going to do.</p>



<p>So this is a hybrid program, which means you know there are several different modules of</p>



<p>different languages.</p>



<p>driver which just launches the program and it contains like the entry point</p>



<p>like see how there&#8217;s like the the main entry point which if you know how to do</p>



<p>other languages this is usually where you start in C++ or like the GCC</p>



<p>libraries so this is the entry point for our whole program and then once we&#8217;re</p>



<p>inside of here we&#8217;re just kind of print out a message to the user we&#8217;re gonna</p>



<p>make a C string and then we&#8217;re gonna call on the assembly function we&#8217;re</p>



<p>gonna give it you know some numbers some integers and some floats and we&#8217;re</p>



<p>gonna give it our C string and then when we&#8217;re inside of the assembly</p>



<p>and then when we&#8217;re inside of the assembly function we&#8217;re just going to print those things to prove</p>



<p>that we know how to call and we know how to receive data from c or c plus plus into assembly</p>



<p>and then we&#8217;re just going to print you know goodbye basically okay so uh i&#8217;m going to make</p>



<p>another uh let&#8217;s see another function in the driver so that assembly has something to call</p>



<p>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>put in something that we can call on from assembly here.</p>



<p>I&#8217;m going to call it my CPP function.</p>



<p>And it&#8217;s just going to have like some arguments,</p>



<p>just some nonsense arguments.</p>



<p>And all it&#8217;s going to do is just print out that it&#8217;s entering,</p>



<p>print out the arguments and then just sort of say goodbye.</p>



<p>And then just return a value to the caller.</p>



<p>We&#8217;re going to return a floating point value to the call.</p>



<p>And we&#8217;re going to return a floating point value to the caller.</p>



<p>floating point value to the color okay so notice how at the top here i have uh in the extern c my</p>



<p>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>uh this normally would be compiled and linked with name mangling and so because it&#8217;s in the</p>



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



<p>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>whatever okay so we have like the basics for our driver it&#8217;s a simple module the hard work is going</p>



<p>to be inside of assembly so uh maybe before we write assembly oh you know what let&#8217;s write the</p>



<p>skeleton of the assembly and then i&#8217;ll start explaining uh what it means to call back and</p>



<p>forth and to make functions and things like that okay so for starters i&#8217;m just going to make a data</p>



<p>section with a bunch of stuff inside of it and i&#8217;ll explain it just very briefly again this video is</p>



<p>basics of assembly in Yasm. If you want to learn the basics, go see my other videos.</p>



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



<p>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>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>message when we begin the program. Here&#8217;s a message to announce we&#8217;re about to print some integers,</p>



<p>to print a C string, to print some floats. Here&#8217;s another message for this, another message for</p>



<p>messages right then a crlf that&#8217;s just you know taking the cursor down to the</p>



<p>next line and then back to the very beginning so it&#8217;s like a basically a new</p>



<p>line and then I&#8217;m gonna have some floating point numbers and I&#8217;m gonna</p>



<p>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>somewhere else and so I&#8217;m gonna populate them with numbers from the start the B</p>



<p>and the D that just are not being sent they start at zero because we&#8217;re gonna</p>



<p>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>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>like making a bunch of room to store some from some variables and then here we&#8217;re just going</p>



<p>to do system right and we&#8217;re going to send to standard output that&#8217;s covered in other videos</p>



<p>but basically we&#8217;re just going to be printing stuff okay so section text now this is where</p>



<p>actually starts. Well, sort of. This is where the program contains instructions to execute.</p>



<p>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>that I have for myself just to help me print integers and floats. So, you know, if you&#8217;ve</p>



<p>seen my other videos, you know that you can do a wide variety of things to sort of print characters</p>



<p>and numbers if you were clever. And you can also call on C library functions like printf.</p>



<p>functions like printf if you really wanted to print floating point numbers</p>



<p>and signed integers and stuff so you don&#8217;t have access to this library so</p>



<p>don&#8217;t worry about putting this part in your code if you&#8217;re working at this from</p>



<p>home unless you&#8217;re someone that has taken a class that I&#8217;ve taught somewhere</p>



<p>and for now just just assume that this just lets me print very easily no</p>



<p>problem you don&#8217;t really need that if you&#8217;re just kind of experimenting with</p>



<p>function this is the name of the function that we made in the driver my cpp function notice how it&#8217;s</p>



<p>over here my cpp function we&#8217;re just letting our assembly module know that there&#8217;s a function in</p>



<p>another module that we should be able to call on even though all these modules are going to be</p>



<p>linked into the same program we still have to do this just to let the assembly module know</p>



<p>okay so now we need to make a function called my assembly function if you look at the top right here</p>



<p>We had this other D name mingled prototype, my assembly function.</p>



<p>So we have to actually write that somewhere.</p>



<p>It&#8217;s not in the driver.</p>



<p>Let&#8217;s write it inside of assembly.</p>



<p>So my assembly function is going to start right here.</p>



<p>Boom, right there.</p>



<p>So this is when we kind of start on the basics of writing a function.</p>



<p>So for starters, hopefully you understand labels.</p>



<p>If you don&#8217;t understand labels by now, you might want to see my other videos.</p>



<p>just some sort of an alphanumeric symbol that you just kind of write as sort of</p>



<p>sort of like a variable name.</p>



<p>You can&#8217;t use every symbol available.</p>



<p>I would just start with using letters and numbers in the underscore.</p>



<p>Of course, you can always experiment if you want to see what&#8217;s allowed and what&#8217;s</p>



<p>not just type something and see if it compiles.</p>



<p>So the label starts with just, you know, this name and then a colon, right?</p>



<p>So that&#8217;s a label.</p>



<p>You can jump into labels.</p>



<p>Like if we were somewhere else in the program right now, we could like, if we</p>



<p>we could like if we wanted to do an infinite loop we could say jump and then the name of the label</p>



<p>and what would happen is execution would go back up to this label and then it would jump again</p>



<p>it would go back up it would be an infinite loop but we wouldn&#8217;t technically be calling a function</p>



<p>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>actually make a function so we have an assembly function which right now is just a label and in</p>



<p>side we have to mark it as global so remember when we marked my CPP function</p>



<p>as extern that means we can call a function that lives somewhere else now</p>



<p>we&#8217;re marking this function as global which means it lives here but we want</p>



<p>other modules to be able to call on our function or our label okay so how do we</p>



<p>actually make a function let&#8217;s see the first thing that you should do is try to</p>



<p>understand that if that all functions return even if they don&#8217;t return any any</p>



<p>Under the hood, what this is actually going to do is crash the program if you just jumped</p>



<p>into this label and didn&#8217;t call it like a function, because the return instruction will</p>



<p>look onto the call stack and try to find a return address for wherever this function</p>



<p>was called from and then go jump, do a jump instruction to return there.</p>



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



<p>something up the stack that&#8217;s not actually a return address and probably crash the program.</p>



<p>So that means now that we have a return statement, we definitely have to call this like a function.</p>



<p>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>let me just show you real fast.</p>



<p>I&#8217;ll say some other function.</p>



<p>If we were inside of this label, for some reason, we could call the above function by just saying call.</p>



<p>And then the label.</p>



<p>So now it&#8217;s being treated like a function.</p>



<p>it&#8217;s being treated like a function.</p>



<p>What the call instruction really does is it just looks at the address of the next instruction</p>



<p>that would have been executed.</p>



<p>So, you know, whatever, let&#8217;s say we have like a nope on the next line, whatever the</p>



<p>memory location or the relative memory location is of that nope instruction, it&#8217;s going to</p>



<p>push that onto the call stack.</p>



<p>And then it&#8217;s going to just do a jump instruction to this label.</p>



<p>So then later when we hit this return statement, then it&#8217;s going to look onto the call stack</p>



<p>and it&#8217;s going to find that return address, which is going to correspond with the nope</p>



<p>instruction and the return instruction is going to do a jump back to that address.</p>



<p>So basically you have to use the call instruction to get there and then use a return instruction</p>



<p>to return from there.</p>



<p>And then now we can actually call it a function.</p>



<p>But then there&#8217;s other things that functions have to do to behave themselves without crashing</p>



<p>the program.</p>



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



<p>Oh, I think I forgot to update that.</p>



<p>I forgot to update that. Let&#8217;s see. Call on this. Yeah. It&#8217;s supposed to be called my assembly</p>



<p>function now. Hold on. Let me update my, uh, my solution. Whoops. Hold on. Call on.</p>



<p>There we go. My assembly. Okay. So I&#8217;ve got my solution up above on another monitor.</p>



<p>So I have the prototype of the function here. Uh, it&#8217;s just going to return nothing. And it&#8217;s</p>



<p>going to like take in a long and a double and a long and a double and a character pointer.</p>



<p>And then my intention here is to save these arguments with some registers.</p>



<p>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>And we&#8217;ll talk about why we&#8217;re not saving the other ones with those registers in a second.</p>



<p>But basically we&#8217;re going to use those registers.</p>



<p>So the thing about the registers is we have to do something called respecting the ABI.</p>



<p>Respect the ABI.</p>



<p>The ABI stands for the Application Binary Interface.</p>



<p>interface and it&#8217;s just sort of like a standard that governs all of the things that you can do</p>



<p>and you&#8217;re supposed to do when you&#8217;re working with x86 64 assembly so the abi is pretty cool</p>



<p>hang on a second the abi is pretty cool because it actually standardizes things</p>



<p>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>plus or vice versa because c plus plus the higher level languages they&#8217;re going to use the abi so if</p>



<p>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>coming up with two different ways of doing things when you could have just done it the abi way</p>



<p>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>module calling it would only work internally to your own program so we&#8217;re going to respect the abi</p>



<p>does the abi say about these registers and again i&#8217;ve talked about this extensively in other videos</p>



<p>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>write it uh the book here is written by a really wonderful uh professor dr ed jorgensen phd and um</p>



<p>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>to this this professor&#8217;s website and download his book for free and he&#8217;s already given me permission</p>



<p>and he&#8217;s already given me permission to just tell everybody to just share it with everybody if you</p>



<p>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>source just sort of like sharing knowledge and stuff so it&#8217;s awesome so I suggest everybody you</p>



<p>know follow the link that I hopefully put in the video and and grab a copy of this book but</p>



<p>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>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>notice how it lists all of the registers and kind of how they are typically used</p>



<p>this falls under the scope of the abi so this is not all the abi is but this is one of the</p>



<p>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>are supposed to be used. Notice R12, which is one of the registers that we&#8217;re going to use,</p>



<p>is designated as callee saved. So that means whoever is being called has to preserve that</p>



<p>register if they intend to mess it up. So for example, if I just had this function here,</p>



<p>move some value into, you know, R12, I have now basically broken the program. If any, you know,</p>



<p>C function or other library that you didn&#8217;t write, just any anywhere else calls on this function,</p>



<p>anywhere else calls on this function then their version of r12 is going to get destroyed by what</p>



<p>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>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>to screw up so respecting the ai means you have to preserve any registered mark as callee saved or</p>



<p>that&#8217;s one of the things that it means so notice how r12 through r15 are callee saved so we have</p>



<p>the abi now and sort of preserve 12 13 and 14. we can do that pretty easily with some push and pop</p>



<p>statements so i&#8217;m going to do push r12 push r13 push r14 and now what happens is those values</p>



<p>are actually on the stack now and i can retrieve them later at the end of the function even if i</p>



<p>destroy them while i&#8217;m inside of the function i&#8217;ll just restore them right before i return so</p>



<p>because it happens before the function does anything.</p>



<p>And then we have to make sure that we pop those values</p>



<p>because you have to be careful with the stack.</p>



<p>If you just start pushing values</p>



<p>and you don&#8217;t restore the stack to its original state</p>



<p>by the time you return,</p>



<p>you&#8217;ve basically broken the program.</p>



<p>It&#8217;s either going to crash right away</p>



<p>or whoever called it is not going to function correctly anymore.</p>



<p>So let&#8217;s do some pops.</p>



<p>We&#8217;re going to do three pops</p>



<p>and we&#8217;ll call this the epilogue,</p>



<p>which means uh you know something we do right before we exit the function and keep in mind</p>



<p>that the order of the pops should be reverse of the order of the pushes notice how we&#8217;re popping</p>



<p>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>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>the pushes then uh you&#8217;re still going to end up destroying data for the caller because you&#8217;re</p>



<p>because you&#8217;re going to be restoring data to the wrong registers.</p>



<p>So just keep that in mind.</p>



<p>OK, so now the ABI is being respected.</p>



<p>Let me see, by the way, do we have enough to actually even run the driver right now?</p>



<p>We&#8217;re just calling it and then it didn&#8217;t really do anything.</p>



<p>Yeah, I think we could probably this might compile.</p>



<p>OK, let me let me just check this out.</p>



<p>I want to say clear and make run.</p>



<p>Yeah, it compiles.</p>



<p>OK, so the driver printed its hello message.</p>



<p>again it&#8217;s just sort of saying like hello from the driver and then when the driver comes</p>



<p>back it says the driver regained control and nothing really happened because well we didn&#8217;t</p>



<p>do anything in our assembly function yet but at least we&#8217;re calling multiple modules and again</p>



<p>if you want to know how to do hybrid programs and linking and compiling and all that stuff</p>



<p>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>have to try to understand like how are we going to receive these arguments so this is one of the</p>



<p>other you know building blocks to making functions notice how the assembly function i&#8217;m sending in an</p>



<p>integer and then a float and then another integer and then another floats i&#8217;m kind of mixing the</p>



<p>arguments then at the very end i&#8217;m sending in a pointer if i look back up at the prototype here</p>



<p>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>longs and doubles, they actually, they&#8217;re called mixed arguments and they don&#8217;t actually count</p>



<p>against each other when you&#8217;re looking at the order of the registers to stuff them into.</p>



<p>So for example, let me show you here. We have, what did I just do? I clicked on the wrong</p>



<p>computer. Okay. I&#8217;m on the wrong computer. Okay. Let me close this real fast. So</p>



<p>When you think about registers for incoming arguments, you basically start to think about this.</p>



<p>RDI and RSI, those registers represent the first and the second integer arguments.</p>



<p>If we look back to the book real fast, we can see in that same section, RDI is the first argument,</p>



<p>and then RSI is the second argument, and then RDX is the third, and then the fourth.</p>



<p>We can do up to six arguments with R9, and then after that, we have to start pushing arguments to the stack.</p>



<p>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>but basically we for for now we can just use six registers to push arguments but if you think</p>



<p>about it these registers these are not floating point registers these are general purpose registers</p>



<p>they&#8217;re meant for integers and pointers the reason they&#8217;re used for in it for pointers also is because</p>



<p>a pointer is just an integer a pointer is just a 64-bit integer unsigned which represents a memory</p>



<p>can use these registers only for integers and pointers but we can&#8217;t use them for floats so that</p>



<p>means the first integer argument is going to be RDI and the second integer argument is going to be</p>



<p>RSI but if there was a float argument in between then RSI would still be the second integer argument</p>



<p>the floats don&#8217;t count against the integers and vice versa so for example if we&#8217;re talking about</p>



<p>float registers you know the first one available is XMM0 and then we have XMM1 and then we have</p>



<p>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>point registers. So we could pass in 16 floating point arguments just using these registers if we</p>



<p>wanted to and then if we want to do even more than that we probably have to get you know funky with</p>



<p>the stack or something or maybe hopefully you just have an array somewhere and you&#8217;re just going to</p>



<p>pass in a pointer. But the way you have to think of these arguments is that even though they might</p>



<p>they might be mixed in the prototype of the function that you&#8217;re calling from a higher level</p>



<p>language you shouldn&#8217;t think of them as being mixed when you&#8217;re actually loading up registers so</p>



<p>again if we just kind of like go back here to this prototype</p>



<p>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>but then right after that there&#8217;s a double b we would not skip rsi or assign b to rsi</p>



<p>b would just be the first float argument and then when we go back to long c long c is actually the</p>



<p>second integer argument because that double doesn&#8217;t count against the integer so it&#8217;s going to be c</p>



<p>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>because there was a an integer we&#8217;re going to go straight to saying that xmm1 is the second float</p>



<p>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>it was going to be e and remember pointers are integers they&#8217;re just unsigned uh 64-bit integers</p>



<p>we have to go on to the next one which i think i recall is like rdx let me just double check</p>



<p>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>that we&#8217;ve kind of like mapped this out we we know now what what registers we should be looking for</p>



<p>we should be looking for when the function comes in in order to receive</p>



<p>our data we should look at those registers for those variables another</p>



<p>thing to keep in mind by the way is that usually when we return something in</p>



<p>assembly we will move a value into RAX right like some value if you&#8217;ve been</p>



<p>following my assembly videos so far but that only counts if you want to return</p>



<p>an integer or a pointer if you instead wanted to return a floating point number</p>



<p>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>point number in assembly like this or at least in the ASM so we&#8217;ll just pretend that there&#8217;s</p>



<p>like a float somewhere and I&#8217;ll just load it from memory and I&#8217;ll say like the float</p>



<p>something like that so notice how we&#8217;re using a different instruction we&#8217;re not using the regular</p>



<p>move instruction that works with integers we&#8217;re instead using the floating point version we&#8217;re</p>



<p>saying let&#8217;s move a single piece of data and let&#8217;s move a double precision floating point number</p>



<p>xmm zero and then we&#8217;ll just grab from memory whatever whatever that variable has we&#8217;ll just</p>



<p>take that floating point number and stick it into xmm zero so uh if you want to return an integer</p>



<p>or float you use rax if you want to re sorry if you want to return an integer or a pointer</p>



<p>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>if you have like two assembly functions calling each other you might be tempted to do that and i</p>



<p>you know very standard and it wouldn&#8217;t work very well with other people&#8217;s code or library code or</p>



<p>higher level language code so only one or the other and it just has to match your prototype</p>



<p>so notice how here inside of my cpp function notice how it&#8217;s going to return a double</p>



<p>right so when the assembly module is done calling on this function it should expect xmm0 to be</p>



<p>loaded up with that double precision loading point number okay so that&#8217;s the basic idea</p>



<p>Okay, so that&#8217;s the basic idea.</p>



<p>So now let&#8217;s maybe let me pin this.</p>



<p>Let&#8217;s kind of fill this out a little bit more.</p>



<p>So the first thing that we should do</p>



<p>is we should save our integer or pointer arguments.</p>



<p>So I&#8217;m gonna leave the respect ABI thing there.</p>



<p>And notice how I&#8217;m just looking at RDI and RSI and RDX.</p>



<p>And well, I guess, you know,</p>



<p>we have it in a comment up here, A, C and E,</p>



<p>but I&#8217;ll just make another comment here.</p>



<p>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>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>pointer it would still be an integer because all pointers are integers no matter what they&#8217;re</p>



<p>pointing to so just keep that in mind anyway so we&#8217;re going to save r12 r13 and 14 with the</p>



<p>14 with the incoming arguments and the reason we want to save those right away is because rdi rsi</p>



<p>and rdx those are not callee saved which means the moment we call in another function they&#8217;ll</p>



<p>possibly be destroyed because we don&#8217;t really know what&#8217;s going on in other functions that we might</p>



<p>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>in this case just registers being faster okay so we have that now we have to save our float arguments</p>



<p>b and d were xmm0 and xmm1 so I&#8217;m just going to save both of those</p>



<p>and if you&#8217;re wondering what this float b and float d are that&#8217;s just</p>



<p>you know up here I just have a global variable so I can save them to memory</p>



<p>easily and not worry about the stack this is not a stack video so much</p>



<p>but yeah okay so I&#8217;m just saving all of the incoming arguments that&#8217;s all I&#8217;ve</p>



<p>done so far and let&#8217;s see it should probably still</p>



<p>Let me see if this works.</p>



<p>I&#8217;m going to go make run.</p>



<p>Okay.</p>



<p>So nothing happens, but it at least worked.</p>



<p>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>And now that we&#8217;re done saving our float arguments, let&#8217;s, let&#8217;s print a welcome message.</p>



<p>So I&#8217;m going to do, you know, welcome.</p>



<p>And I&#8217;m going to use a special function that I&#8217;ve made in previous videos called print</p>



<p>null terminated string, which means I should probably copy paste that into this program</p>



<p>now.</p>



<p>What is print null terminated string?</p>



<p>It&#8217;s just another convenience function that I wrote.</p>



<p>I&#8217;m not going to explain it too much because it&#8217;s in other videos and</p>



<p>I&#8217;m already here trying to explain functions to you in general.</p>



<p>So long story short, it takes in a C string and a file handle, you know, to like where</p>



<p>you want to write, like if you want to write to a file or you want to write to standard</p>



<p>output or standard error.</p>



<p>And it just takes those arguments and then it sort of says, all right, how long is the</p>



<p>string?</p>



<p>And it uses another function called string length to figure out how long the string is.</p>



<p>how long the string is and then it just uses a system call to actually print the</p>



<p>string and again system calls are covered in other videos this one looks</p>



<p>really convoluted because it&#8217;s like you know customized for this function but</p>



<p>just trust me on this this prints a string next function I got a paste in</p>



<p>real fast again explained in other videos is the string length function so</p>



<p>all this does is it just takes a pointer to a string and it sort of scans the</p>



<p>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>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>know about this covered in other videos then i&#8217;m going to make a convenience function here called</p>



<p>crlf and all that&#8217;s going to do is just print the new line that we talked about earlier so just</p>



<p>just a bunch of convenience functions on top of the real part of the program so now that we have</p>



<p>the convenience functions in there we should be able to see the welcome message let me just double</p>



<p>yeah now inside assembly entry point oh what did I do wrong now inside I keep forgetting to change</p>



<p>the strings on this now inside where&#8217;s that now inside my assembly function okay let me change</p>



<p>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>I keep changing my mind about what they should be named and then I I get some inconsistencies okay</p>



<p>all right so then uh we printed the welcome message and now let&#8217;s print the integer arguments</p>



<p>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>to print the integer so that&#8217;s just this other string a message saying we&#8217;re about to print the</p>



<p>integers nothing really that complicated so far it says now we&#8217;re printing all the integer arguments</p>



<p>so now we can actually print the integer arguments so we have two integer arguments</p>



<p>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>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>was r12 and r13 so I&#8217;m just going to paste some code here to actually print those and</p>



<p>talk about this library in other videos but basically I&#8217;m moving r12 which is the first</p>



<p>integer argument that functions typically receive and once that&#8217;s loaded up i&#8217;m just going to call</p>



<p>on my special function to just print the integer and again you could use printf from the c libraries</p>



<p>if you actually wanted to print it and not just experiment and stuff like that so i&#8217;m going to use</p>



<p>r12 and r13 and i&#8217;m just going to print both of those integer arguments and then after each one</p>



<p>is printed notice i&#8217;m calling the crlf function which is just the convenience function of just</p>



<p>which is just the convenience function of just like doing a new line.</p>



<p>So now we should see two numbers. Yeah.</p>



<p>Now printing all integer arguments,</p>



<p>we&#8217;ve got an 88 there and then that other giant number there.</p>



<p>Let&#8217;s just double check that that&#8217;s actually what we&#8217;re supposed to be seeing.</p>



<p>So I&#8217;m going to do this and I&#8217;m going to say,</p>



<p>the driver called the assembly module with these numbers.</p>



<p>It gave it an 88 and then for the next integer,</p>



<p>it gave it the 287 giant number. So great.</p>



<p>so great we&#8217;re printing the integers now let&#8217;s print the floats so we should see</p>



<p>like a 99 point something in a 32 point something next okay so let&#8217;s continue</p>



<p>with printing oh sorry actually let&#8217;s print the the C string because that&#8217;s a</p>



<p>pointer that&#8217;s still more closely related than the floats so the first</p>



<p>thing I&#8217;m going to do is I&#8217;m going to</p>



<p>print the received c string how about like announce that we will print the c string because</p>



<p>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>actually print the received c string next so same stuff as before first we call print null terminated</p>



<p>string to print out a little welcome message or just like an intro message like we are going to</p>



<p>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>it the c string so it just prints the whole c string out and this should prove to you that we</p>



<p>are indeed receiving a pointer to some data owned by the c plus plus module so if we run this real</p>



<p>fast it should just tell us two more things it&#8217;ll give us the announcement now printing the received</p>



<p>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>that&#8217;s exactly what string is inside of that variable so hello this is a c string owned by</p>



<p>main and we gave it to the function by just kind of passing it in and we know that character arrays</p>



<p>are basically character pointers or any array is just a pointer to the first item in the array so</p>



<p>my c string is really a pointer to that h character so if we pass that in then a pointer</p>



<p>based print function should be able to work and that&#8217;s what happened okay so we&#8217;ve done that</p>



<p>So we&#8217;ve done that.</p>



<p>And then the next thing we should do is let&#8217;s print the floats.</p>



<p>So first let&#8217;s announce that we&#8217;re going to print the floats.</p>



<p>Same thing we&#8217;d before we&#8217;re just printing like an announcement message.</p>



<p>If we run the program again, it&#8217;s just like now printing the floats, but it doesn&#8217;t actually</p>



<p>do anything.</p>



<p>So then the next step is let&#8217;s, let&#8217;s grab the first float into XMM zero.</p>



<p>And then let&#8217;s call a function to print it.</p>



<p>so right here we have like that 99 number that we expected from before by</p>



<p>the way so why am I doing it this way why am I not just keeping XMM zero</p>



<p>because you remember before we had XMM zero had that had the float that we</p>



<p>received and then we&#8217;re using it again down here but remember XMM zero and all</p>



<p>the other float registers they&#8217;re not designated as callee saved which means</p>



<p>the moment we call any other function we should expect that that data has been</p>



<p>so I can&#8217;t actually count on XMM 0 surviving just this little simple function instead I have to save</p>



<p>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>a global variable so it&#8217;s sitting in float underscore B right now and then we saved it at</p>



<p>the beginning to float underscore B and if you just kind of look up to the data area well it was</p>



<p>just float underscore B was just a little quad word you know eight bytes of memory that can hold</p>



<p>hold our float. So we have like float allocations for B and D, the first and second float arguments.</p>



<p>So we&#8217;re saving it there. And then we&#8217;re recalling it here. And remember, the first function argument</p>



<p>is going to be XMM0, regardless of where that data originally came from. So if we look at the next</p>



<p>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>still going to load it into XMM zero because right now it&#8217;s not about what we originally</p>



<p>received as an argument.</p>



<p>It&#8217;s what this function expects as an argument.</p>



<p>This function only takes one argument.</p>



<p>It just wants a float so that it can print it and that&#8217;s it.</p>



<p>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>line.</p>



<p>Okay.</p>



<p>So let me just run the program one more time and we should now see we&#8217;ve got two floats</p>



<p>and they should match what the driver tried to send in.</p>



<p>Right?</p>



<p>99, that&#8217;s the first one.</p>



<p>And then 32 point something, that&#8217;s the second one.</p>



<p>So cool, we have received integers and pointers and floats.</p>



<p>We&#8217;ve recalled them and then we&#8217;ve printed them.</p>



<p>Pretty slick, what do you think?</p>



<p>Anyway, so we&#8217;ve done that.</p>



<p>And the next thing that we should probably do is…</p>



<p>Well, at this point, we just maybe have to mess with return types.</p>



<p>Even though I&#8217;ve told you about it, we&#8217;ll just mess with it a little bit.</p>



<p>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>of announce that we&#8217;re going to call on the C++ module. Again, typical design pattern, let&#8217;s just</p>



<p>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>module will now call on the C++ module. So nothing really too complicated. So now we&#8217;re going to call</p>



<p>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>I want to put this one right here.</p>



<p>It&#8217;s the my CPP function function.</p>



<p>So if we look back at the driver</p>



<p>and look at the signature for my CPP function,</p>



<p>whoops, it has this signature.</p>



<p>And if you wanted to look up higher,</p>



<p>you totally can just look into the name mangling section.</p>



<p>Whoops, the my CPP function, it returns a double.</p>



<p>It takes in a long, a double, a long, a double,</p>



<p>and a character pointer.</p>



<p>Basically the same thing as the other one,</p>



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



<p>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>like some random double that i decided to type because this is not an arithmetic video okay so</p>



<p>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>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>and just sort of like paste it right where I&#8217;m about to call the function.</p>



<p>So I&#8217;m going to do this just to remind myself of what I&#8217;m actually calling.</p>



<p>Let me add that to my solution, by the way.</p>



<p>Okay.</p>



<p>So we&#8217;re going to call myCPP function,</p>



<p>which means it&#8217;s expecting some registers to be loaded up with arguments.</p>



<p>If we don&#8217;t actually load up anything right now,</p>



<p>it&#8217;ll probably do some sort of nonsense.</p>



<p>Let&#8217;s actually see what happens right now.</p>



<p>If we don&#8217;t load up the appropriate registers,</p>



<p>then C++ will still look at those registers expecting to see valid data.</p>



<p>Let&#8217;s see what happens.</p>



<p>I don&#8217;t know if it&#8217;s going to be good or bad.</p>



<p>This would probably be something called undefined behavior,</p>



<p>meaning you did something wrong,</p>



<p>and probably sometimes your program will work,</p>



<p>and sometimes it won&#8217;t.</p>



<p>Sometimes you won&#8217;t understand what&#8217;s going on.</p>



<p>So I&#8217;m going to do this right now,</p>



<p>and it&#8217;s saying, oh, it&#8217;s segfaulted.</p>



<p>Okay.</p>



<p>Why did it segfault?</p>



<p>all right well I guess maybe because I did something naughty I don&#8217;t know</p>



<p>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>so uh we&#8217;re going to enter into my cpp function and then it says we got a variable a</p>



<p>which was a long we didn&#8217;t give it a one a one was just probably sitting in there</p>



<p>before we even called that function so like one is definitely not it and then b was like some</p>



<p>floating point number and then C was just like another seemingly random value</p>



<p>that kind of looks a little bit more like a pointer I&#8217;m not really sure it&#8217;s</p>



<p>probably not but it&#8217;s just some junk data coming from somewhere and then D</p>



<p>which I think D was supposed to be a character pointer it says not a number</p>



<p>so we just got like a a really bad value for D let me upgrade this real fast</p>



<p>because maybe we should be printing what printing it as a memory location so E is</p>



<p>So E is, let me bring this down real fast.</p>



<p>So E is supposed to be a character pointer.</p>



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



<p>And I&#8217;ll just say memory.</p>



<p>And then we&#8217;ll do a static cast.</p>



<p>Oh, I wonder if this will actually change anything.</p>



<p>I wonder.</p>



<p>Let&#8217;s static cast both of them just to see what happens.</p>



<p>print e and uh c string here and then it&#8217;s going to be um</p>



<p>hmm no i don&#8217;t think that&#8217;s actually going to change anything</p>



<p>because if i cast it as a point as a character that&#8217;s definitely wrong</p>



<p>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>location hmm oh i know what to do i can static cast it maybe as a as an unsigned long okay</p>



<p>and unsigned long. Okay, so a character pointer, we&#8217;ll leave that for the C string. Unsigned long,</p>



<p>long, just in case. And I think I should see the memory location first and then the actual C</p>



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



<p>a character pointer to a type long, long unsigned integer. What have I done? How about unsigned long?</p>



<p>long see if that works character pointer long int i guess i forgot how to cast pointers to longs</p>



<p>i&#8217;ll look that up and post another video in the future but i guess for now we&#8217;ll just</p>



<p>we&#8217;ll deal with this humiliation and i&#8217;ll just print e by itself</p>



<p>all right so i&#8217;m just going to run it one more time it should say fault again</p>



<p>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>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>what else do i have i&#8217;ve got three move instructions so the first argument is going to get</p>



<p>77118 the second argument is going to get 1111 and the third argument looks like it&#8217;s going to</p>



<p>get a pointer that i&#8217;ve defined inside of the assembly module so if you look back up at the</p>



<p>look back up at the top let&#8217;s see message string inside asm message string inside asm so basically</p>



<p>i&#8217;m going to be sending a character pointer to this t right here which is just this string says</p>



<p>this string is owned by the assembly module and it&#8217;s a null terminated string which uh c definitely</p>



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



<p>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>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>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>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>I&#8217;m going to do this real fast. Remember the first float argument is going to be XMM zero.</p>



<p>And the second one is going to be XMM one. And you can see that send B corresponds to the first</p>



<p>corresponds to the first float argument.</p>



<p>That&#8217;s why it&#8217;s XMM zero.</p>



<p>And then the float send D corresponds</p>



<p>to the second argument.</p>



<p>That&#8217;s why it&#8217;s XMM one.</p>



<p>And that&#8217;s the D right here.</p>



<p>And there&#8217;s no other floats.</p>



<p>And if we just kind of look back up real fast,</p>



<p>the float send B and D,</p>



<p>I just defined those arbitrarily</p>



<p>to just these two random numbers.</p>



<p>So we should see like a two one nine</p>



<p>and then a nine nine eight eight seven seven six</p>



<p>sort of, you know, weird number.</p>



<p>number then when we&#8217;re done loading all those things you know all of these registers up now</p>



<p>the function should be able to receive something i bet you the reason it crashes because we</p>



<p>we had like a bad address for the the string previously because we didn&#8217;t load it</p>



<p>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>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>of ones and then the string which was rdx which was the e so this string is owned by the assembly</p>



<p>module nice we&#8217;re now able to print a string that&#8217;s owned by assembly in another module in</p>



<p>the c plus plus module and then xmm0 and xmm1 those are the two floats so if we look back up</p>



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



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



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



<p>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>how to transport data. This is not a C++ video. Anyway, if we go back down to the end of that,</p>



<p>so we called the function, but then this function, if you notice, it returns a double,</p>



<p>we should expect to see something loaded into the XMM zero register.</p>



<p>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>point number.</p>



<p>And I&#8217;m just going to store it into my float got return value global variable,</p>



<p>which I defined up above.</p>



<p>So let me scroll up real fast.</p>



<p>So where is that?</p>



<p>Where is that?</p>



<p>Float got return value.</p>



<p>I initialized it with a zero just because we&#8217;re going to receive something in there.</p>



<p>I could have used the BSS section if I wanted to, probably would have been a little bit</p>



<p>bit smarter and more performant but i don&#8217;t know i personally don&#8217;t like using the bss section in</p>



<p>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>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>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>return the double to us inside of XMM0.</p>



<p>So I&#8217;m just moving that into my data section into the global variable.</p>



<p>And then now that we have finally returned from the C++ module,</p>



<p>I&#8217;m just going to announce that we have indeed returned from the C++ module.</p>



<p>Let&#8217;s get that far real fast.</p>



<p>So this string is owned and then my CPP function exiting.</p>



<p>exiting and then now it says the assembly module received this return value from the</p>



<p>C++ module and then it says the driver has regained control because we were supposed</p>



<p>to print the float and do a line feed after that and we didn&#8217;t so basically assembly just</p>



<p>printed this string that was the last part we just added and then it returned right away</p>



<p>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>move the float value back into XMM0.</p>



<p>Remember, anytime you call a function, all your float registers could be destroyed.</p>



<p>So it makes sense that we saved XMM0 and then we loaded it only several instructions later</p>



<p>because you don&#8217;t know what&#8217;s happening under the hood inside of print null terminated string</p>



<p>or any of the system calls that it uses.</p>



<p>Your XMM0 is likely destroyed, but you don&#8217;t know for sure, right?</p>



<p>If you count on it not being destroyed, you might be introducing undefined behavior to your program.</p>



<p>So that&#8217;s not good.</p>



<p>Anyway, so we&#8217;re just loading back up from that global into XMM zero.</p>



<p>And then we&#8217;re calling that function again to just sort of print the float.</p>



<p>And it wants the float as argument zero.</p>



<p>So we&#8217;re just sticking into XMM zero.</p>



<p>And then we&#8217;re printing a new line.</p>



<p>So then when we&#8217;re done here, run it.</p>



<p>and now it says the assembly module received this value from the C++ module and notice how it&#8217;s that</p>



<p>special value that I hard-coded 112222 whatever so that&#8217;s this value right here</p>



<p>and again if we had this cpp function returning along then all we would have to do is look at</p>



<p>the RAX register and if we had let&#8217;s let&#8217;s say for some reason that this assembly function we</p>



<p>function we wanted to get something from it we could say you know double temp</p>



<p>equals that then all we&#8217;d have to do is make sure that we returned sorry we</p>



<p>loaded XMM zero with our return value in the assembly module because XMM zero is</p>



<p>the return register for floats or if we wanted to do like a long then just load</p>



<p>RAX with something at the end of my assembly function and that&#8217;s how C would</p>



<p>C and C++ they&#8217;re following the ABI so if you personally don&#8217;t follow the ABI</p>



<p>you&#8217;re just going to end up memorizing two different ways of doing things and</p>



<p>your way is not going to be compatible with all the other modules written in</p>



<p>higher level languages so it&#8217;s kind of a waste of time and your modules won&#8217;t be</p>



<p>compatible with other people&#8217;s libraries and functions it&#8217;s just a huge waste of</p>



<p>time so just follow the ABI in the first place I&#8217;ve literally known people who</p>



<p>said you know what I don&#8217;t want to learn the ABI that&#8217;s dumb then as they wrote</p>



<p>they wrote a bunch of assembly they just started thinking like oh this is too confusing because i</p>



<p>keep forgetting what registers i was going to pass back and forth between my functions</p>



<p>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>through all my code again so then they started accidentally inventing their own little scheme</p>



<p>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>second argument in this all they were doing is just reinventing the abi from scratch and then</p>



<p>got to that point and then later wasting more time when they realized they just needed to learn the</p>



<p>ABI and forget about their way so don&#8217;t let that happen to you and you should probably just try to</p>



<p>do it the right way the first time right um a lot of things in coding and computer science in general</p>



<p>are just time savers that feel like wastes of time at first okay what else we have anything</p>



<p>that I wanted to show you that was actually I think the entirety of that program let&#8217;s run it</p>



<p>while I check to see if there&#8217;s any other stuff that I was supposed to show you.</p>



<p>So let&#8217;s do this.</p>



<p>Everything seems to be working.</p>



<p>Let&#8217;s see, I wanted to tell you about labels, return, call,</p>



<p>the stack, the return address, prologue and epilog,</p>



<p>pushing and popping, respecting the ABI.</p>



<p>I wanted to show you that textbook function arguments, mixed arguments,</p>



<p>int or pointer return types and float return types.</p>



<p>I guess I got through everything without forgetting something huge, which, hey,</p>



<p>that&#8217;s probably a first not that i recall at the moment but it probably is</p>



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



<p>also known as a amd64 assembly in ubuntu okay thank you so much for watching this video i</p>



<p>hope you learned a little bit and had a little bit of fun i&#8217;ll see you in the next video</p>



<p>Hey everybody, thanks for watching this video again from the bottom of my heart. I really</p>



<p>appreciate it. I do hope you did learn something and have some fun. If you could do me a please,</p>



<p>a small little favor, could you please subscribe and follow this channel or these videos or whatever</p>



<p>it is you do on the current social media website that you&#8217;re looking at right now.</p>



<p>It would really mean the world to me and it&#8217;ll help make more videos and grow this community.</p>



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



<p>kindness and subscribe. You know, sometimes I&#8217;m sleeping in the middle of the night and I just</p>



<p>wake up because I know somebody subscribed or followed. It just wakes me up and I get filled</p>



<p>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>you could troll me if you want to just wake me up in the middle of the night, just subscribe</p>



<p>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>if you look at the middle of the screen right now you should see a QR code which</p>



<p>you can scan in order to go to the website which I think is also named</p>



<p>somewhere at the bottom of this video and it&#8217;ll take you to my main website</p>



<p>where you can just kind of like see all the videos I published and the services</p>



<p>and tutorials and things that I offer and all that good stuff and if you have</p>



<p>a suggestion for clarifications or errata or just future videos that you</p>



<p>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>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>like it would really it really mean the world to me i would really appreciate it so again thank you</p>



<p>so much for watching this video and um enjoy the cool music as as i fade into the darkness</p>



<p>which is coming for us all</p>



<p>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>Mixed Function Arguments in x86-64 Assembly &#8211; Integers &#038; Floats Explained</title>
		<link>https://www.NeuralLantern.com/mixed-function-arguments-in-x86-64-assembly-integers-floats-explained/</link>
					<comments>https://www.NeuralLantern.com/mixed-function-arguments-in-x86-64-assembly-integers-floats-explained/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 01 Mar 2026 02:14:39 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[abi calling convention]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[Ed Jorgensen]]></category>
		<category><![CDATA[floating point registers]]></category>
		<category><![CDATA[function calling convention]]></category>
		<category><![CDATA[integer float arguments]]></category>
		<category><![CDATA[linux assembly]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[mixed arguments]]></category>
		<category><![CDATA[nasm]]></category>
		<category><![CDATA[rax return]]></category>
		<category><![CDATA[rdi rsi xmm0]]></category>
		<category><![CDATA[system v abi]]></category>
		<category><![CDATA[ubuntu assembly]]></category>
		<category><![CDATA[x86 assembly]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[xmm registers]]></category>
		<category><![CDATA[xmm0 return]]></category>
		<category><![CDATA[yasm]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=310</guid>

					<description><![CDATA[<p>In the System V x86-64 ABI, integer and floating-point arguments use separate register assignment sequences. Integer arguments go into rdi, rsi, rdx, rcx, r8, r9 (then stack), while floating-point arguments use xmm0 through xmm7 independently. This means the first integer you see — even if it appears after several doubles — always goes into rdi, and the second float always goes into xmm1 regardless of how many integers came before it.</p>
<p>The post <a href="https://www.NeuralLantern.com/mixed-function-arguments-in-x86-64-assembly-integers-floats-explained/">Mixed Function Arguments in x86-64 Assembly &#8211; Integers &amp; Floats Explained</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Mixed Function Arguments in x86-64 Assembly - Integers &amp; Floats Explained" width="1380" height="776" src="https://www.youtube.com/embed/ZAY3srLRx8c?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p>Quick guide to passing mixed int + float arguments in x86-64 assembly (System V ABI). See why rdi can still be the first integer after several doubles, why xmm registers count separately, and how return values switch between rax and xmm0. Perfect for YASM/NASM programmers interfacing with C/C++.</p>



<p>Intro to Mixed Arguments 00:00<br>Simple Integer-Only Functions 00:46<br>Adding Arguments and Return Values 01:01<br>Integer Arguments in RDI and RSI 01:30<br>Pointers Treated as Integers 02:06<br>Introducing Floating-Point Returns 02:31<br>Returning Double in XMM0 03:06<br>First Float Argument in XMM0 03:36<br>Float Registers Count Separately 04:03<br>Integer Register Order Explained 04:16<br>Separate Counting for Integers and Floats 05:21<br>RSI as First Integer After Float 05:38<br>Reference to Ed Jorgensen&#8217;s Book 07:00<br>Callee-Saved Registers Overview 07:56<br>Complex Mixed Argument Examples 09:48<br>Inserting Integer Among Floats 10:57<br>Skipping Float Registers on Integer 11:18<br>Calling C from Assembly Notes 11:56<br>Name Mangling Reminder 12:21<br>Closing Remarks and Thanks 12:30<br>Call to Subscribe and Support 13:04<br>Website and QR Code Mention 13:38<br>Final Thanks and Outro Music 14:19</p>



<p>=-=-=-=-=-=-=-=-=</p>



<p>Thanks for watching!</p>



<p>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>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>Hey everybody! In this video I&#8217;m going to talk to you a little bit about using functions with</p>



<p>mixed arguments in a YASM x86-64 assembly language program. Although this</p>



<p>video will probably still be useful to you if you&#8217;re using NASM or MASM because the</p>



<p>mixed arguments and their order and how they work is governed by the ABI which is a system</p>



<p>so what the heck am i talking about okay so the first thing that i want to show you is uh</p>



<p>imagine we have a function here let&#8217;s say void f this function doesn&#8217;t take any arguments and it</p>



<p>doesn&#8217;t return anything congratulations you&#8217;re done that was the easiest thing that you&#8217;ve ever</p>



<p>done in your entire life but as soon as you start adding arguments to it you have to uh</p>



<p>start understanding how to uh how to call registers and stuff so imagine we have like a</p>



<p>a long a and a long b so we give it two arguments and maybe we want it to return a long</p>



<p>this is kind of like level one of writing functions in assembly right we just have</p>



<p>a bunch of integers and we realize okay um this long the return value is going to come to the</p>



<p>Maybe I&#8217;ll do a comment up here, we&#8217;ll do rax, we&#8217;ll say the long is actually coming</p>



<p>back to the user in rax and the long is coming into the function with rdi, the long a and</p>



<p>the long b is coming into the function with rsi.</p>



<p>So if you understand this then you can write a function in assembly where basically you</p>



<p>two incoming integer arguments and then you return to the caller a long value using rax</p>



<p>keep in mind also that if this second argument was a pointer long pointer then it would be the</p>



<p>same thing rsi because pointers are integers they&#8217;re 64-bit unsigned integers even if the</p>



<p>pointer was not to a long even if the pointer was to a double or a character or whatever it would</p>



<p>pointers are integers. It starts getting a little bit more complicated when you want to mix between</p>



<p>floats and numeric arguments. So what I&#8217;ve been saying so far, it&#8217;s all just integers.</p>



<p>Even if we&#8217;re mixing pointers with regular longs, it&#8217;s just, you know, they&#8217;re all longs basically,</p>



<p>right? So, but as soon as we start introducing floats, then things get a little bit more</p>



<p>complicated. So for starters, what if we did a function that was, that returned a double?</p>



<p>that returned a double. So let&#8217;s say we have a function G and it returns a double. Maybe it</p>



<p>still takes in a long and a double pointer. If it returns a double, let&#8217;s see, is this still</p>



<p>lining up? I don&#8217;t think it&#8217;s lining up anymore. There we go. If it&#8217;s returning a double, then</p>



<p>that means we have to return to the caller with the XMM0 register. So XMM0 is always designated as</p>



<p>the well the first float argument but also the float return value so keep in</p>



<p>mind if I&#8217;m returning a double to the caller I&#8217;m not going to use our a X at</p>



<p>all I&#8217;m just going to use XMM zero load it up with the return value and then</p>



<p>return to the caller and that&#8217;s it if I wanted to do another function let&#8217;s say</p>



<p>hmm how about H and then we&#8217;ll say I don&#8217;t know maybe the first argument is a</p>



<p>argument is a float we call it a double a that means rdi is not going to be the argument that</p>



<p>or the register that we look at to see our incoming double it has to be a float register</p>



<p>xmm0 is the float register that we look at for the first argument the float registers are really</p>



<p>easy you just kind of go in order like xmm0 is the first argument xmm1 is the next argument xmm2</p>



<p>the general purpose uh integer registers they&#8217;re a little more complicated you have to remember</p>



<p>their their labels uh you know like the letters have an ordering to it uh suppose for the sake</p>



<p>of argument that we&#8217;re going to return a double from this also uh in that case let&#8217;s see did i</p>



<p>just mess the formatting up again i think that&#8217;s okay um again we&#8217;re going to return xmm0</p>



<p>notice how we&#8217;re returning xmm0 oh that&#8217;s why i spaced it forward notice how we are</p>



<p>forward notice how we are returning xmm0 and we&#8217;re also taking xmm0 these float registers tend to be</p>



<p>reused all over the place none of them are designated as callee saved within the abi</p>



<p>so you always have to stash their values somewhere if you&#8217;re ever going to make a function call or</p>



<p>anything or a system call the other thing to keep in mind is notice how rsi i still have rsi there</p>



<p>for the second incoming argument you would imagine that rsi is supposed to be the second argument</p>



<p>supposed to be the second argument even if it uh you know comes after a double but that&#8217;s not</p>



<p>actually true because the the order i guess like the the ordering of the uh of the registers they</p>



<p>only count against their own class like the float registers the float arguments they only count</p>



<p>against the position of the float and the uh of the float registers and and and the general purpose</p>



<p>they only count against themselves.</p>



<p>What I&#8217;m trying to say is that RSI is the second integer argument,</p>



<p>but if you look at the signature, we only have one integer argument, right?</p>



<p>So RSI is not the second integer argument.</p>



<p>It&#8217;s actually the first integer argument.</p>



<p>So I&#8217;m going to put an RDI there.</p>



<p>You probably can infer that the second float argument should be XMM1 at this point, I hope.</p>



<p>and that&#8217;s true if we just say that there&#8217;s going to be another double</p>



<p>that this function takes then it&#8217;s going to be xmm1</p>



<p>not xmm2 because if you&#8217;re just counting the arguments</p>



<p>and you&#8217;re sort of grouping the integers with the floats together</p>



<p>you might think well first argument is xmm0 second argument would have been xmm1</p>



<p>third argument would have been xmm2 therefore if I see</p>



<p>a float coming into the third argument position it&#8217;ll be xmm2</p>



<p>No, the integers don&#8217;t count against the floats and the floats don&#8217;t count against the integers.</p>



<p>That&#8217;s why RDI is the first argument for integers, even though it&#8217;s in the second position,</p>



<p>it&#8217;s still the first integer that we see.</p>



<p>And XMM1 is the second float argument that we see.</p>



<p>That&#8217;s why it&#8217;s XMM1 instead of XMM2, because we&#8217;re looking at it in the third position.</p>



<p>So you&#8217;re not really looking at, whoops, you&#8217;re not looking at overall position.</p>



<p>at position only counting that type of register let me pull open my favorite book to get a little</p>



<p>bit more clarity on this um this book is written by a wonderful professor dr ed jorgensen phd</p>



<p>this book i did not write it ed jorgensen wrote this book it&#8217;s called x86 64 assembly language</p>



<p>programming with ubuntu this book can turn you into an expert with yasm assembly so uh</p>



<p>I recommend everybody grab a copy. It&#8217;s also free and it&#8217;s got a copy left license.</p>



<p>You can see like the license here. But anyway, I wanted to open up this book real fast</p>



<p>just to show you the registers. Okay. I&#8217;m going to search for Kali saved so I don&#8217;t have to fumble</p>



<p>around the book too long. Kali saved and that brings us to section 12.8.2 register usage.</p>



<p>let&#8217;s see callee saved you&#8217;ll see rbx is callee saved rbp is callee saved probably shouldn&#8217;t</p>



<p>mess with that one too much r12 13 14 15 all callee saved there&#8217;s a couple temporary registers</p>



<p>but if you look carefully rdi is designated as the first argument but it should say really</p>



<p>the first integer argument so rdi is the first integer argument that we see that&#8217;s why</p>



<p>argument that we see that&#8217;s why in all of these examples the first integer argument that we</p>



<p>actually see is rdi so like for function f long a that was the first integer argument</p>



<p>and then in the function g it&#8217;s also the first integer argument and then in function h rdi is</p>



<p>the double pointer b because that is the first integer argument that we actually see</p>



<p>so keep that in mind you would just repeat that pattern you know the second integer argument you</p>



<p>second integer argument you see that&#8217;s RSI. That&#8217;s why RSI is the pointer here because</p>



<p>pointers are integers and the pointer here and we don&#8217;t have anything else but if I guess if we</p>



<p>added another you know let&#8217;s say a long D then it would be RSI right there. Hopefully that makes</p>



<p>sense and you can you can carry this logic forward for the first argument the second argument the</p>



<p>and then R8 for the fifth argument R9 for the sixth argument and then you can</p>



<p>look for further arguments on the stack if you need more than that but basically</p>



<p>that&#8217;s how you count them up and then the float registers are just a lot</p>



<p>easier they always start at zero and they go all the way up to 15 and so I</p>



<p>don&#8217;t know maybe I should just copy paste well maybe I&#8217;ll start it from</p>



<p>scratch we&#8217;ll say dub no let&#8217;s let&#8217;s mix it up a little bit more we&#8217;ll say long</p>



<p>And the long is going to be returned with the RAX.</p>



<p>And I guess I have to space this forward a little bit.</p>



<p>And then if we have a bunch of floats, say double A.</p>



<p>Can I maybe just copy paste this a little bit so I don&#8217;t have to do so much typing?</p>



<p>Okay, so we&#8217;ll do double A, double B, double C, double D, double E.</p>



<p>and then this is going to be xmm zero why did i put the extra comment there i don&#8217;t know</p>



<p>and then we&#8217;re going to have xmm one and then we&#8217;re going to have xmm two xmm three and so forth</p>



<p>and just to make sure you&#8217;re paying attention ask yourself this real fast if i were to insert</p>



<p>z what register would we use for that would it be the one two three four five the fifth argument</p>



<p>would it be uh would it be r8 or would it be something else hopefully you understand now</p>



<p>let&#8217;s say we do a character pointer that it would be rdi because that&#8217;s the first integer argument</p>



<p>that we actually see all right and then you know to return we would return in rax and then the</p>



<p>return in RAX and then the floats just keep counting notice how it skipped from XMM2 or it</p>



<p>went from XMM2 to XMM3 even though we crossed over an integer argument in between because the</p>



<p>float arguments don&#8217;t count against the integers and the ints don&#8217;t count against the floats</p>



<p>let&#8217;s see I think</p>



<p>well if you want you can look at section 18.2 which just sort of talks about the floating</p>



<p>which just sort of talks about the floating point registers we have 0 through 15 but I don&#8217;t think</p>



<p>we really need to do that here I think we pretty much have everything we need to know to to call</p>



<p>on functions with mixed arguments and to have a function within assembly that supports mixed</p>



<p>arguments so again if your C or C++ modules are expecting some kind of signature like this now you</p>



<p>know how to calculate what registers they&#8217;re actually going to populate by the time execution</p>



<p>execution jumps into your assembly function.</p>



<p>And if you want to call a C or a C++ function from assembly,</p>



<p>now you know which registers to populate so that the the other function that</p>



<p>you&#8217;re calling can receive the right data from you.</p>



<p>Don&#8217;t forget about name mangling, which I talked about in another video.</p>



<p>Okay, I think that&#8217;s all I really have to say.</p>



<p>This video was pretty short.</p>



<p>Thank you so much for watching.</p>



<p>I hope you learned a little bit of stuff and had a little bit of fun.</p>



<p>I&#8217;ll see you in the next video.</p>



<p>make more videos and grow this community so we&#8217;ll be able to do more videos longer videos better</p>



<p>videos or just i&#8217;ll be able to keep making videos in general so please do do me a kindness and uh</p>



<p>and subscribe you know sometimes i&#8217;m sleeping in the middle of the night and i just wake up because</p>



<p>i know somebody subscribed or followed it just wakes me up and i get filled with joy that&#8217;s</p>



<p>exactly what happens every single time so you could do it as a nice favor to me or you could</p>



<p>you could troll me if you want to just wake me up in the middle of the night just subscribe</p>



<p>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>the screen right now, you should see a QR code, which you can scan in order to go to the website,</p>



<p>which I think is also named somewhere at the bottom of this video. And it&#8217;ll take you to my</p>



<p>main website where you can just kind of like see all the videos I published and the services and</p>



<p>tutorials and things that I offer and all that good stuff. And if you have a suggestion for</p>



<p>clarifications or errata or just future videos that you want to see please leave a comment or</p>



<p>if you just want to say hey what&#8217;s up what&#8217;s going on you know just send me a comment whatever i also</p>



<p>wake up for those in the middle of the night i get i wake up in a cold sweat and i&#8217;m like it would</p>



<p>really it really mean the world to me i would really appreciate it so again thank you so much</p>



<p>for watching this video and um enjoy the cool music as as i fade into the darkness which is</p>



<p>for us all.</p>



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/mixed-function-arguments-in-x86-64-assembly-integers-floats-explained/">Mixed Function Arguments in x86-64 Assembly &#8211; Integers &amp; Floats Explained</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/mixed-function-arguments-in-x86-64-assembly-integers-floats-explained/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>x86-64 Assembly: Signed Integer Multiplication and Addition with IMUL &#038; ADD (YASM on Ubuntu Linux)</title>
		<link>https://www.NeuralLantern.com/x86-64-assembly-signed-integer-multiplication-and-addition-with-imul-add-yasm-on-ubuntu-linux/</link>
					<comments>https://www.NeuralLantern.com/x86-64-assembly-signed-integer-multiplication-and-addition-with-imul-add-yasm-on-ubuntu-linux/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 30 Nov 2025 07:30:56 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly arithmetic]]></category>
		<category><![CDATA[assembly language programming]]></category>
		<category><![CDATA[cpu registers assembly]]></category>
		<category><![CDATA[imul instruction]]></category>
		<category><![CDATA[intel imul]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[nasm imul]]></category>
		<category><![CDATA[reverse engineering basics]]></category>
		<category><![CDATA[signed multiplication assembly]]></category>
		<category><![CDATA[systems programming]]></category>
		<category><![CDATA[ubuntu assembly]]></category>
		<category><![CDATA[x86_64 tutorial]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[x86-64 multiplication]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=259</guid>

					<description><![CDATA[<p>Hands-on x86-64 assembly tutorial showing how to perform signed integer multiplication using the IMUL instruction in YASM/NASM on Linux. Covers immediate and global operands, two/three-operand forms, basic addition, with complete working code and live execution.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-signed-integer-multiplication-and-addition-with-imul-add-yasm-on-ubuntu-linux/">x86-64 Assembly: Signed Integer Multiplication and Addition with IMUL &amp; ADD (YASM on Ubuntu Linux)</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="x86-64 Assembly: Signed Integer Multiplication and Addition with IMUL &amp; ADD (YASM on Ubuntu Linux)" width="1380" height="776" src="https://www.youtube.com/embed/QmIdCYsxTTM?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>Want to actually understand how signed integer multiplication works at the CPU level? In this straight-to-the-point x86-64 assembly tutorial we dive into the IMUL instruction &#8211; the proper way to multiply signed integers in YASM/NASM on Linux.</p>



<p>We cover:</p>



<ul class="wp-block-list">
<li>The difference between unsigned (MUL) and signed (IMUL) multiplication</li>



<li>Two-operand vs three-operand IMUL forms</li>



<li>Multiplying immediate values vs global variables</li>



<li>Loading values into registers (mov) and performing fast multiplication</li>



<li>Basic addition with ADD and INC</li>



<li>Full working example that prints results so you can see it in action</li>



<li>Why you should respect the ABI and save callee-saved registers (R12-R15)</li>
</ul>



<p>Everything is built with YASM on Ubuntu, linked with a tiny C driver, and run instantly. No fluff, just real assembly code you can copy and run right now.</p>



<p>Code on screen, calculator verification, and clear explanation of every line. Perfect if you&#8217;re learning low-level programming, reverse engineering, or just want to know what really happens when you write a = b * c; in C.</p>



<p>Introduction to Integer Arithmetic 00:00:00<br>Recommended Book and Resources 00:00:39<br>Instruction Set Overview 00:01:17<br>Addition Instruction (ADD) 00:01:43<br>Unsigned vs Signed Multiplication 00:02:20<br>Signed Multiplication with IMUL 00:03:05<br>IMUL Three-Operand Form 00:03:40<br>IMUL Two-Operand Form 00:04:12<br>Squaring with IMUL 00:04:50<br>Setting Up the Sample Program 00:05:08<br>Data Section and Strings 00:06:13<br>Text Section and External Functions 00:06:58<br>Math Function Entry Point 00:08:01<br>Multiply Test Function Setup 00:08:42<br>Multiplying Immediate Values 00:09:48<br>Printing the Immediate Result 00:10:21<br>Running and Verifying Immediates 00:12:53<br>Multiplying Global Variables 00:13:49<br>Loading Globals into Registers 00:14:07<br>IMUL with Globals Demo 00:14:37<br>Addition with INC and ADD 00:15:36<br>Final Results and Verification 00:17:09<br>Wrap-Up and Closing 00:17:22<br>Outro and Subscribe Request 00:17:44</p>



<p>Thanks for watching!</p>



<p>Find us on other social media here:</p>



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



<p>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>okay</p>



<p>hey everybody in this video i&#8217;d like to talk to you a little bit about</p>



<p>integer arithmetic in yasm assembly on an x86-64 system</p>



<p>so what am i talking about well integer arithmetic i want to take integers and i want to add them</p>



<p>together multiply them together whatever this video is mostly going to focus on signed integer</p>



<p>but I&#8217;m going to briefly skim through unsigned integer multiplication and also</p>



<p>addition and anyway so for starters let me go up to this the top of this book</p>



<p>here that I&#8217;ve got I&#8217;m going to show you pages from a book that I really really</p>



<p>love I mentioned it in a lot of my other videos it&#8217;s called x86 64 assembly</p>



<p>language programming with Ubuntu and it is written by a brilliant doctor</p>



<p>professor who is just interested in everyone learning this is an open source</p>



<p>you can get yourself a copy for free just look that title and name up you can find his website</p>



<p>you can grab a copy and you too can become an assembly expert just by reading this book</p>



<p>honestly this book has everything that you really need to to really level yourself off</p>



<p>level yourself up well beyond i think the master&#8217;s level of college probably</p>



<p>not as much as the person who wrote the book but you never know anyway let me uh</p>



<p>I want to go down to section 7 here and it&#8217;s called instruction set overview.</p>



<p>So here you can have like an overview of like all different types of instructions, but I&#8217;m</p>



<p>going to focus on the integer arithmetic instructions.</p>



<p>So I&#8217;m going to go to 7.5.</p>



<p>Notice how the first section in here is addition.</p>



<p>I&#8217;m not going to give you an addition example because it&#8217;s just too easy.</p>



<p>You pretty much use just use the add instruction and then you have a source and destination</p>



<p>comma source in x86 yasm assembly it&#8217;s pretty much the destination is the first operand so uh</p>



<p>that&#8217;s kind of implied usually when you have just like a two operand instruction now we could add</p>



<p>using three operands and the the first one would be the destination and then the other two would</p>



<p>be the sources but i&#8217;m going to scroll down past that to the integer multiplication section 7.5.3</p>



<p>unsigned multiplication is a little tricky and</p>



<p>You kind of have to decide what data size you&#8217;re going to go with and then you use the instruction</p>



<p>uh, and then you end up with some some uh, some answers that are spread across multiple registers</p>



<p>I&#8217;m going to eventually make other videos where I can talk to you about how to manipulate multiple registers or at least know that they&#8217;re there like</p>



<p>not 64-bit registers so if we were clever we could just sort of manipulate those so that</p>



<p>the whole answer showed up on one single 64-bit register if we wanted to but just know you can</p>



<p>multiply unsigned integers it&#8217;s a little bit more complicated than signed integers so in this video</p>



<p>i&#8217;m just going to talk about signed integers let&#8217;s see so signed integer multiplication</p>



<p>so in order that&#8217;s a section 7.5.3.2 if you want to multiply two integers and they&#8217;re signed</p>



<p>and they&#8217;re signed meaning they have a plus or a minus sign meaning they&#8217;re designated as either</p>



<p>positive or negative rather than not designated at all which would usually just mean positive</p>



<p>you can use imall as an instruction and well you can just provide you know one operand two operands</p>



<p>or three operands let&#8217;s do the three operand version first because that&#8217;s probably the easiest</p>



<p>So we would take a number that&#8217;s sitting in a register and then we would multiply it by some immediate.</p>



<p>Notice how IMM is sitting here.</p>



<p>The result of multiplying a register by some number would just go into the destination operand.</p>



<p>So that means you&#8217;d put a register here.</p>



<p>You&#8217;d put a register here.</p>



<p>You&#8217;d put an immediate here in one instruction.</p>



<p>Then you&#8217;d have some sort of an answer on what these two numbers were multiplied against each other.</p>



<p>then you&#8217;re allowed to specify a register for the destination and a register for the source.</p>



<p>But notice how the destination gets overwritten here.</p>



<p>So, you know, it&#8217;s going to multiply this number by this number</p>



<p>and then just store the result in the first register.</p>



<p>So you will kind of overwrite something.</p>



<p>It&#8217;s maybe more convenient for you to use the three operand version</p>



<p>if you only want to multiply by an immediate.</p>



<p>But, you know, anyway, and then for the, let&#8217;s see, let&#8217;s see, I think you can square a number</p>



<p>just by specifying its source here.</p>



<p>It&#8217;ll multiply a number by itself.</p>



<p>And then let me just go down a little bit.</p>



<p>I&#8217;m starting to get lost in this book.</p>



<p>Let me go down a little bit.</p>



<p>Well, I guess that&#8217;s all we really had to say.</p>



<p>Okay, so let me show you a sample program that I&#8217;ve prepared.</p>



<p>that I&#8217;ve prepared so that we can multiply. Okay, sample program. I think I need to rehearse myself</p>



<p>a little bit more before I record videos. Anyway, I&#8217;ve got this empty source code file here. Keep in</p>



<p>mind that I don&#8217;t need that. Keep in mind that I have a sample program here already. I&#8217;m not going</p>



<p>to really focus on the contents of it because this is not what did I do wrong here? Main and math.</p>



<p>main empty no no where&#8217;s my make file oh I forgot to open it this is not a video</p>



<p>about make files or hybrid programs or anything like that that&#8217;s covered in my</p>



<p>other videos so I&#8217;m just gonna say hey I&#8217;ve got a make file that compiles my</p>



<p>assembly source code and then I&#8217;ve got a driver C source code file which its</p>



<p>entire job is just to have the main entry point because I&#8217;m linking against</p>



<p>the GCC libraries and then its job is just a call on a math function which</p>



<p>So now my assembly module is blank.</p>



<p>What should we do?</p>



<p>First I&#8217;m going to copy paste from my solution here, just a bunch of strings.</p>



<p>So I&#8217;ve got a data section that I&#8217;m going to get set up.</p>



<p>This is not a video about the basics of assembly programming.</p>



<p>See my other videos, but for now I&#8217;m just going to say, well, I&#8217;ve got a bunch of strings.</p>



<p>I&#8217;m going to say the result of multiplying immediates is this, the result of multiplying</p>



<p>globals is that.</p>



<p>multiplications and then see the results. Okay, so then a couple more variables</p>



<p>before we&#8217;re finished is I&#8217;m gonna now say that we have a system call for</p>



<p>system right which is not what this video is about. See my other videos for</p>



<p>system calls. Same thing for file descriptors it&#8217;s there I&#8217;m not gonna</p>



<p>explain it too much I&#8217;m just gonna print a standard output and then from this</p>



<p>program we&#8217;re gonna I&#8217;m gonna return just an integer for whatever reason I&#8217;m</p>



<p>for whatever reason. I&#8217;m just saving it as a variable. And then we want to multiply some</p>



<p>integers. So I&#8217;m storing two integers in the global section, you know, still inside of the</p>



<p>dot data section. So that&#8217;s, these are going to be global variables. They&#8217;re going to be both</p>



<p>be quad words. So I can load up into a quad registers or quad word registers. And now I&#8217;m</p>



<p>ready to start my text section. So the first thing that I&#8217;m going to do is just, you know,</p>



<p>that&#8217;s where all of our instructions go and then I&#8217;m going to name two external</p>



<p>symbols don&#8217;t worry about these symbols they are not the point of this video but</p>



<p>you know I have a little library that I wrote that helps me print and and take</p>



<p>input from the user I&#8217;m gonna print an integer I&#8217;m gonna take an input from the</p>



<p>from the user so don&#8217;t worry about that that&#8217;s not the point of this video</p>



<p>really I just want to make it so that when I demo I can just easily type in an</p>



<p>and then just print it to the user.</p>



<p>So let&#8217;s look at our main entry point here.</p>



<p>Remember the driver calls on a function called math.</p>



<p>So I&#8217;m just gonna make an entry point called math,</p>



<p>mark it as global so the driver can call on it.</p>



<p>And then I&#8217;m gonna, whoops, why did I put load here?</p>



<p>Do, I&#8217;m gonna put, I&#8217;m gonna say do it right there.</p>



<p>Let&#8217;s do it.</p>



<p>I&#8217;m just gonna call on a function I wrote</p>



<p>called multiply test, kind of pointless</p>



<p>if you think about the fact that there&#8217;s like no code</p>



<p>in the main function here.</p>



<p>like no code in the main function here but that&#8217;s the way i like to do it when we&#8217;re done we&#8217;re just</p>



<p>going to return our return value that&#8217;s already been defined and so now i&#8217;m ready to start setting</p>



<p>up our multiply test maybe i&#8217;ll just copy paste the header here and i&#8217;ll say this is going to be</p>



<p>a label called multiply test because i&#8217;m going to call on it and try to use it like a function</p>



<p>i&#8217;m going to put a return instruction at the very end so now it really is a function</p>



<p>I&#8217;m going to use these registers.</p>



<p>So I&#8217;m going to use R12, R13, R14, and R15 to hold temporary immediates and globals</p>



<p>just for multiplication.</p>



<p>So I&#8217;m basically going to be using the registers to hold my data.</p>



<p>And because I&#8217;m going to use all of those registers, remember you have to respect the</p>



<p>ABI.</p>



<p>So that means we have to have a push pop pair on all of those registers because they&#8217;re</p>



<p>designated as callee saved.</p>



<p>saved if anyone calls on your functions and you&#8217;re not respecting the abi then you&#8217;re probably about</p>



<p>to crash the program in some way or even if you are the only person who writes any code that your</p>



<p>code calls on or gets called from you&#8217;re probably still going to regret not respecting the abi</p>



<p>eventually when you forget what&#8217;s going on anyway so let&#8217;s multiply some immediates first thing i&#8217;m</p>



<p>and move it into R12 because that&#8217;s where we&#8217;re going to hold some temporary</p>



<p>immediates and then the number 256 put that into R13 and then I&#8217;m going to use</p>



<p>the I&#8217;m all instruction to just multiply those two values together because this</p>



<p>is the two operand version of that instruction the result will also be</p>



<p>stored in R12 so that means 233 will be erased from R12 and the answer that will</p>



<p>the results for the immediate because I like my programs to be pretty. I&#8217;m not just going to print</p>



<p>the numbers and then hope I can remember which number comes first. That&#8217;s usually a huge mistake</p>



<p>if you&#8217;re trying to debug something even a little bit complicated. So I&#8217;m going to print my string,</p>



<p>which I&#8217;m calling the immediate prefix. And if you look back up at the top right here,</p>



<p>it&#8217;s just going to say the result of multiplying immediates is, and then it&#8217;s going to put three</p>



<p>asterisks. And then I&#8217;m going to print the actual result. And then the suffix is just going to be</p>



<p>And then the suffix is just going to be three more asterisks.</p>



<p>So we should basically see the results surrounded by asterisks.</p>



<p>So I&#8217;m doing that.</p>



<p>And then I&#8217;m going to use my library</p>



<p>to call a special printing function</p>



<p>so that I can just print to the result.</p>



<p>So R12 is where the result of the multiplication is.</p>



<p>I&#8217;m going to give that as the first argument,</p>



<p>which is the RDI register, if we&#8217;re talking about integers.</p>



<p>And then I&#8217;m just going to call my function</p>



<p>that&#8217;ll print for me.</p>



<p>for me and then I&#8217;m going to print the suffix so the suffix is just that other</p>



<p>string we talked about just it trails with asterisks I&#8217;m then going to call</p>



<p>on a custom function called crlf all that&#8217;s going to do is just print a new</p>



<p>line I don&#8217;t know why I do that I could easily put that into the string but it&#8217;s</p>



<p>more fun to call functions although to be fair every time you call a function</p>



<p>you are jumping to an instruction elsewhere and so the CPU does pay a</p>



<p>trying to write programs for high performance you might not want to do that anyway so this is pretty</p>



<p>fast because i&#8217;m really just using an immediate and then i&#8217;m loading it into a register i&#8217;m not</p>



<p>actually touching global memory so this should be a lightning fast multiplication operation</p>



<p>let&#8217;s see if this actually works if i didn&#8217;t screw this up whoops what did i what did i do there oh no</p>



<p>i have too many videos now</p>



<p>gosh okay clear and make run oh what have I done undefined symbols CRLF did I</p>



<p>forget oh I forgot to copy paste my CRLF function yeah so again don&#8217;t worry</p>



<p>about CRLF all I&#8217;m doing is just printing a new line carriage return it&#8217;s</p>



<p>just you know this is from another example where I was trying to prove to</p>



<p>somebody you need to preserve registers and respect the ABI so don&#8217;t even worry</p>



<p>you know what I&#8217;m going to leave it as is because if I take that out then I have to take the time</p>



<p>to remove the push pop pair because there&#8217;s no point at that I&#8217;m doing that if I&#8217;m not</p>



<p>messing with those registers forget about that it&#8217;s not part of this video</p>



<p>anyway I&#8217;m going to run the program again and you can see that the result of multiplying</p>



<p>immediates is and so this whole string right here was my prefix so again not really the point of</p>



<p>the point of this video and then this is the result of multiplying those two different numbers</p>



<p>and then the suffix with the stars after so let me just prove this to you where&#8217;s the dang calculator</p>



<p>oh man i need to work on my icons like i just did something screwy and i have like no good icons</p>



<p>anymore on this virtual machine so we were going to multiply 233 by whoops by what was it 256.</p>



<p>five nine six four eight and you can see that&#8217;s on the screen five nine six four eight so we have</p>



<p>successfully multiplied signed integers and since they&#8217;re signed we could multiply negative numbers</p>



<p>if we wanted to i&#8217;m not going to here but you can now the next thing let&#8217;s try is let&#8217;s multiply</p>



<p>stuff sitting in global memory okay so i&#8217;m just going to copy this um where the heck is it</p>



<p>at the top we made two global integers we said integer a is equal to 233 and then 256</p>



<p>those were the same numbers that we just multiplied so we should probably get the same result</p>



<p>if we&#8217;re lucky so i&#8217;m saying i&#8217;m going to move both of these into registers so i&#8217;m going to move</p>



<p>a into r14 and i&#8217;m going to move b into r15 just to prove to you that we can and then</p>



<p>instruction we did before the result is going to go into r14 so let&#8217;s see i&#8217;m gonna print the</p>



<p>prefix real fast here same kind of concept that we talked about before there&#8217;s just going to be</p>



<p>a prefix before the result and then i&#8217;m actually going to print the result with this line right</p>



<p>here or these lines i&#8217;m just going to print r14 which is holding the result at this point</p>



<p>and hey we can trust that R14 wasn&#8217;t killed by the system call because the</p>



<p>system call respects the ABI and then I&#8217;m gonna print my suffix right here and</p>



<p>then a CRLF and that&#8217;s basically the idea let&#8217;s just double check here that</p>



<p>we get the same result twice make run notice how it prints the same result</p>



<p>twice once with globals once with immediates so if you were wondering</p>



<p>before this video how to multiply immediates or how to multiply globals</p>



<p>well there you go just put them into registers first and then call I&#8217;m all</p>



<p>pretty fast let&#8217;s just do one other thing for fun I&#8217;m going to let&#8217;s see add</p>



<p>one number to our 14 so to the result of the multiplication and then I&#8217;m going to</p>



<p>add five to it just to prove to you that we can increase an integer by one and we</p>



<p>we print it before we do the prefix extra stuff just for fun so here I&#8217;m gonna increase r14 and</p>



<p>I&#8217;m gonna say that it is a q word I guess that&#8217;s implied already because r14 is considered a 64-bit</p>



<p>register but if we wanted to increase I don&#8217;t know just for the sake of argument if we wanted</p>



<p>to like increase this right here let me just show you real fast how we do it we would say increase</p>



<p>would say increase q word and then name the integer in global memory so in global memory</p>



<p>the system doesn&#8217;t really know by default if it&#8217;s a one byte integer a two byte integer or four or</p>



<p>eight so you just have to specify the data size so it knows which integers to look at when it&#8217;s</p>



<p>considering what the original value is and how to overflow and when to overflow but you know just</p>



<p>for fun I&#8217;m putting that in here so it&#8217;s going to increase the result by one and then it&#8217;s going to</p>



<p>and it&#8217;s going to do that using the addition instruction.</p>



<p>So we&#8217;re going to add R14 with 5.</p>



<p>We can put an immediate there,</p>



<p>and the result is going to be stored in R14.</p>



<p>So basically R14 is equal to R14 plus 5.</p>



<p>And so overall, when we add those two things in there,</p>



<p>the second result should now be about 6 higher than the first result.</p>



<p>So if you kind of look at this right here,</p>



<p>we have 648, and then we have 644,</p>



<p>which is 6 numbers higher than the original result.</p>



<p>than the original result. So these are the basics of integer arithmetic with</p>



<p>signed multiplication and addition. I think that&#8217;s all I really wanted to show</p>



<p>you. Yeah, okay. So I hope you learned a little bit of stuff from this video. I</p>



<p>hope you had a little bit of fun. Come back, I&#8217;ll see you in the next video and</p>



<p>happy coding! Hey everybody! Thanks for watching this video again from the</p>



<p>appreciate it. I do hope you did learn something and have some fun. If you could do me a please,</p>



<p>a small little favor, could you please subscribe and follow this channel or these videos or</p>



<p>whatever it is you do on the current social media website that you&#8217;re looking at right now.</p>



<p>It would really mean the world to me and it&#8217;ll help make more videos and grow this community.</p>



<p>So we&#8217;ll be able to do more videos, longer videos, better videos, or just I&#8217;ll be able to keep making</p>



<p>kindness and uh and subscribe you know sometimes i&#8217;m sleeping in the middle of the night and i just</p>



<p>wake up because i know somebody subscribed or followed it just wakes me up and i get filled</p>



<p>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>you could you could troll me if you want to just wake me up in the middle of the night just subscribe</p>



<p>and then i&#8217;ll i&#8217;ll just wake up i promise that&#8217;s what will happen also uh if you look at the middle</p>



<p>of the screen right now you should see a qr code which you can scan in order to go to the website</p>



<p>which I think is also named somewhere at the bottom of this video.</p>



<p>And it&#8217;ll take you to my main website where you can just kind of like see</p>



<p>all the videos I published and the services and tutorials and things that I offer</p>



<p>and all that good stuff.</p>



<p>And if you have a suggestion for clarifications or errata or just future videos that you want to see,</p>



<p>please leave a comment.</p>



<p>Or if you just want to say, hey, what&#8217;s up? What&#8217;s going on?</p>



<p>You know, just send me a comment, whatever.</p>



<p>I also wake up for those in the middle of the night.</p>



<p>middle of the night I get I wake up in a cold sweat and I&#8217;m like it would really it really</p>



<p>mean the world to me I would really appreciate it so again thank you so much for watching this video</p>



<p>and um enjoy the cool music as as I fade into the darkness which is coming for us all</p>



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-signed-integer-multiplication-and-addition-with-imul-add-yasm-on-ubuntu-linux/">x86-64 Assembly: Signed Integer Multiplication and Addition with IMUL &amp; ADD (YASM on Ubuntu Linux)</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/x86-64-assembly-signed-integer-multiplication-and-addition-with-imul-add-yasm-on-ubuntu-linux/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
