<?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>general purpose registers Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/general-purpose-registers/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/general-purpose-registers/</link>
	<description></description>
	<lastBuildDate>Tue, 23 Dec 2025 13:22:18 +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>general purpose registers Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/general-purpose-registers/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>x86-64 Assembly: Integer Data Movement, Pointers, and Dereferencing Explained (YASM on Ubuntu)</title>
		<link>https://www.NeuralLantern.com/x86-64-assembly-integer-data-movement-pointers-and-dereferencing-explained-yasm-on-ubuntu/</link>
					<comments>https://www.NeuralLantern.com/x86-64-assembly-integer-data-movement-pointers-and-dereferencing-explained-yasm-on-ubuntu/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Tue, 23 Dec 2025 13:22:17 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[dereferencing assembly]]></category>
		<category><![CDATA[gdb debugging assembly]]></category>
		<category><![CDATA[general purpose registers]]></category>
		<category><![CDATA[integer data movement]]></category>
		<category><![CDATA[lea instruction]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[mov instruction assembly]]></category>
		<category><![CDATA[pointer arithmetic assembly]]></category>
		<category><![CDATA[pointers in assembly]]></category>
		<category><![CDATA[systems programming]]></category>
		<category><![CDATA[ubuntu assembly programming]]></category>
		<category><![CDATA[x86 assembly tutorial]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=269</guid>

					<description><![CDATA[<p>This practical x86-64 assembly language tutorial explains integer data movement between registers and memory, the importance of data size specifiers (byte, word, dword, qword), the difference between pointers and dereferenced values, pointer arithmetic for array access, and using LEA for address calculation - all demonstrated with YASM on Ubuntu Linux and inspected via GDB.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-integer-data-movement-pointers-and-dereferencing-explained-yasm-on-ubuntu/">x86-64 Assembly: Integer Data Movement, Pointers, and Dereferencing Explained (YASM on Ubuntu)</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="x86-64 Assembly: Integer Data Movement, Pointers, and Dereferencing Explained (YASM on Ubuntu)" width="1380" height="776" src="https://www.youtube.com/embed/zGrnEPrpmeg?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p>In this hands-on x86-64 assembly tutorial, we dive deep into moving integer data between registers and memory, working with pointers, dereferencing, and pointer arithmetic using YASM on Ubuntu.</p>



<p>You&#8217;ll see practical examples of:</p>



<ul class="wp-block-list">
<li>Moving immediates and data between general-purpose registers</li>



<li>Loading different sized integers (byte, word, dword, qword) from memory</li>



<li>Why data size specifiers matter and what happens with overflow/underflow</li>



<li>The difference between moving a pointer vs dereferencing it</li>



<li>Pointer arithmetic to access array elements</li>



<li>Using LEA for address calculation without dereferencing</li>



<li>Debugging with GDB to inspect registers step-by-step</li>
</ul>



<p>Perfect for anyone learning low-level programming, systems programming, or wanting to truly understand how pointers work at the assembly level. No prior expert knowledge required &#8211; we build it up with clear examples.</p>



<p>Code and examples are shown live, compiled with YASM, linked with LD, and debugged with GDB.</p>



<p>If you&#8217;re into assembly, reverse engineering, operating systems, or just love understanding how computers really work, this one is for you.</p>



<p>Introduction to Integer Data Movement and Pointers 00:00:00<br>Program Setup and Makefile 00:01:16<br>Data Section Definitions 00:01:38<br>System Calls for Output and Exit 00:01:52<br>Text Section and Entry Point 00:04:21<br>Printing Hello String 00:05:05<br>Moving Immediates to Registers 00:06:04<br>Copying Between Registers 00:06:27<br>Dereferencing Pointers from Memory 00:07:16<br>Specifying Data Sizes 00:08:01<br>Register Size Variants 00:09:45<br>Reference Book Explanation 00:10:50<br>Loading Words and Bytes 00:13:25<br>Debugger Breakpoint and Registers 00:14:28<br>Overflow Demonstration 00:16:25<br>Makefile Adjustments for Warnings 00:17:44<br>Proving Data Size Importance 00:20:32<br>Underflow and Overflow Examples 00:21:34<br>Symbols as Pointers 00:24:16<br>Array Definitions and Access 00:25:20<br>Pointer to Array Items 00:26:08<br>LEA Instruction for Addresses 00:27:32<br>Dereferencing Manipulated Pointers 00:29:01<br>Final Register Inspection 00:29:50<br>Conclusion and Subscribe Request 00:33:26</p>



<p>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>Hey everybody! In this video I&#8217;m going to talk to you about integer data movement and pointers</p>



<p>in an x86-64 YASM assembly program within Ubuntu but this should work for all of you x86 YASM people.</p>



<p>Okay so what am I talking about for starters? I&#8217;m just talking about moving data pretty much. If you</p>



<p>from one integer register to another or from one general purpose register to another.</p>



<p>And if you know how to manipulate pointers in assembly and all that stuff,</p>



<p>you probably don&#8217;t need this video.</p>



<p>But if you are not an expert, then this will probably help you.</p>



<p>Let me start off by saying, of course, that there are going to be lots of concepts in this video</p>



<p>that I don&#8217;t actually explain because I&#8217;ve explained them in other videos.</p>



<p>For example, this is not a Hello World tutorial where I show you how to actually,</p>



<p>you know, build a basic Yasm assembly program.</p>



<p>a basic yasm assembly program you can probably infer that just from this video but i&#8217;m not going</p>



<p>to explain it also this is not a debugging video so i&#8217;m not really going to explain too much about</p>



<p>gdb which is something i&#8217;m going to use to look at the registers but it might be nice for you to</p>



<p>get a little preview i will record a full gdb tutorial in another video anyway so let&#8217;s see</p>



<p>here uh for starters i&#8217;ve just kind of like created a little sample program here um well</p>



<p>a make file which will just compile and execute my program again this is not a</p>



<p>make file video tutorial check my other videos for that I&#8217;ve already made one</p>



<p>for now I&#8217;m just gonna start making a program and I&#8217;m gonna give it a data</p>



<p>section so first thing I&#8217;m gonna give it is like well let&#8217;s say I&#8217;ll give it a</p>



<p>bunch of data then I&#8217;ll kind of explain what I gave it again this is not a</p>



<p>basics Yasum tutorial so see my other videos but you know just simply put I&#8217;m</p>



<p>You know just simply put I&#8217;m gonna write to standard output so I do I remember the system call code for for writing</p>



<p>And then I&#8217;m gonna exit because this is a pure assembly program that I&#8217;m compiling. There&#8217;s this is not a hybrid program</p>



<p>We&#8217;re not doing GCC. We&#8217;re just doing LD</p>



<p>So I&#8217;m just gonna say a call code 60 to exit a file descriptor for stdin actually I don&#8217;t really need that</p>



<p>Let me just double check that I don&#8217;t need that in my solution</p>



<p>Yeah, I like to reuse these things so we don&#8217;t really need standard input</p>



<p>standard input. We&#8217;re just going to do standard output, you know, printing to the terminal.</p>



<p>We&#8217;re going to exit with a success code of zero. And then I made a couple of, well, just one C</p>



<p>string where I&#8217;m just basically saying, hello, my name is so-and-so. So you can know that the</p>



<p>program actually started in case it crashes later. That&#8217;s not my name. I just love those kinds of</p>



<p>names. And then I&#8217;m just going to make a bunch of integers of different data sizes. So this is kind</p>



<p>sizes but just real fast because these are going to be directly used in our video we uh we have a</p>



<p>quad word here so i&#8217;m defining an integer called my long int and i&#8217;m saying that it&#8217;s a quad word</p>



<p>with dq q for quad word and then i&#8217;m just giving a pretty big integer same thing for a regular d</p>



<p>word which is just a double word so that&#8217;s half of a quad word and then another thing with a whoops</p>



<p>w not a q let me just fix that in my solution oh dear then i&#8217;m going to do a word which is a six two</p>



<p>one one one a word is two bytes of data we&#8217;re starting to get dangerously into overflow territory</p>



<p>when i was originally writing this solution i had like a bunch of nines up here and the number that</p>



<p>i ended up seeing on the screen was totally different and i forgot oh a word is just two bytes</p>



<p>so even if it&#8217;s unsigned the highest thing that it can actually represent is um</p>



<ol start="65535" class="wp-block-list">
<li>So that&#8217;s not good. Then I&#8217;m going to just store a byte here with DB and put some twos there.</li>
</ol>



<p>And then I&#8217;m going to make an array of integers. Putting an array, making an array in the regular</p>



<p>data section, that&#8217;s not as good as putting it in the BSS section if you want a lot of integers.</p>



<p>But I&#8217;m just going to specify a small number of integers so I can just put it directly into the</p>



<p>BSS is for like giant arrays of uninitialized data and the data section is for whatever you can handle typing pretty much of initialized data.</p>



<p>So now I&#8217;m going to start my text section and my program&#8217;s entry point.</p>



<p>Again, this is not a Yasm basics tutorial. See my other videos.</p>



<p>And so we&#8217;re going to start by just saying here&#8217;s my entry point where the operating system can sort of jump into my program.</p>



<p>to just you know exit the program let&#8217;s actually see if that works so far it</p>



<p>should do nothing but but at least exit with success I&#8217;m gonna go clear and make</p>



<p>run and notice how it just kind of like ran it and then nothing happened okay so</p>



<p>that&#8217;s great how come it didn&#8217;t print though what did I do wrong oh I forgot</p>



<p>to copy paste the code that prints something okay let me get my solution up</p>



<p>here so the first thing we&#8217;re really going to do is just use a system call to</p>



<p>call to print the word hello again this is not a system call video see my other videos</p>



<p>and um so now we&#8217;re just we&#8217;re just going to print our hello string</p>



<p>with a system call let me run it one more time we should see like a quick hello</p>



<p>yeah okay my name is uh chaplain mondover it&#8217;s not maybe i should change that to it&#8217;s not my name is</p>



<p>not no i don&#8217;t want to do that sometimes i like to pretend that my name is chaplain mondover</p>



<p>name is check lane Mondover you should too it&#8217;s fun anyway so the first thing that I&#8217;m going to</p>



<p>do here is I&#8217;m going to move an immediate well let me copy paste a whole chunk of code and then</p>



<p>I&#8217;ll explain it one by one I&#8217;m basically going to use data movement instructions to move a bunch of</p>



<p>data into a bunch of registers and then I&#8217;m going to hit a break point or I&#8217;m going to I&#8217;m going to</p>



<p>sort of do like a non-operation piece of code so that I can easily break on it in my debugger so</p>



<p>the results. Okay so first off I&#8217;m going to move an immediate into R12. The</p>



<p>instruction for moving data into general purpose registers or the integer</p>



<p>registers is just MOV so that&#8217;s fine probably you should know that by now but</p>



<p>I&#8217;m going to move an immediate into R10 and I&#8217;m going to move 12876 into R10.</p>



<p>So that&#8217;s how you move an immediate no problem. Then I&#8217;m going to first clear</p>



<p>out R11 and then I&#8217;m going to move R10 into it. The reason I&#8217;m clearing out R11</p>



<p>reason i&#8217;m clearing out r11 first is just to prove to you that we are actually moving data from one</p>



<p>register into another because if i just i don&#8217;t know if i didn&#8217;t do that you might be tempted to</p>



<p>to think well um maybe r11 already had that data in it so first it&#8217;s going to be just a zero and</p>



<p>then it&#8217;s going to actually be whatever r10 had so we should see r10 and 11 both have one two eight</p>



<p>register or a pointer register from one to another. And by the way, integers, those are</p>



<p>pointers, or rather, should I say, pointers are integers. So if you&#8217;re moving an integer or you&#8217;re</p>



<p>moving a pointer, the system just kind of sees that as a number either way. So you can use the</p>



<p>move instruction for it. So now let&#8217;s use the pointer that was assigned to our long integer</p>



<p>to move that number into R12. So let me just go up real fast. My long int up here,</p>



<p>my long int up here it&#8217;s like a gigantic number and remember I said before that my long int is</p>



<p>actually just a pointer to the memory location that begins to hold the quad word so my long</p>



<p>int is really a pointer to one byte and there&#8217;s an eight byte allocation because it&#8217;s a quad word</p>



<p>so when you sort of put this into your code and you let the system know that you want to move a</p>



<p>then the system will scan eight bytes and interpret that as an integer so</p>



<p>putting the the keyword quad word here I don&#8217;t think that&#8217;s necessary I usually</p>



<p>do it anyway but you&#8217;ll see in the next few instructions that it&#8217;s a really</p>



<p>really good idea to specify the data size because if you don&#8217;t you&#8217;re gonna</p>



<p>actually get something wrong in other words if for some reason I moved I don&#8217;t</p>



<p>into R12 I could do it if I if I wrote it correctly but then I would be taking</p>



<p>part of a quad word and trying to interpret that as the entire number and</p>



<p>it&#8217;s probably going to be totally wrong we&#8217;ll have an example for that in a</p>



<p>moment but anyway so that&#8217;s how we move a quad word we just specify keyword right</p>



<p>before the memory location and we use the brackets to dereference if we</p>



<p>didn&#8217;t dereference then we would actually be moving a pointer to the long</p>



<p>integer into R12 we&#8217;ll do that soon too I&#8217;m going to try to do everything in this</p>



<p>to do everything in this video then we&#8217;ll move a d word from uh from memory into r13 this is a</p>



<p>little bit different notice how uh here on line 60 i kind of have to specify d word because that&#8217;s</p>



<p>the data size that i&#8217;m trying to move into the r13 register so this is telling the system that</p>



<p>there&#8217;s about four bytes that i need to scan not eight bytes and interpret those four bytes as an</p>



<p>Moving it into R13, you know, I could have sworn a long time ago, you could just move that sort of thing directly into R13.</p>



<p>But when I tried my solution before recording this video, I kept getting assembler errors until I added the D at the end.</p>



<p>And so I just want you to know, oh, you know what, let&#8217;s look at a book real fast.</p>



<p>Let&#8217;s look at my favorite book.</p>



<p>I just want you to know that there are other forms of every register that can specify the register&#8217;s data size.</p>



<p>What do I mean by that?</p>



<p>itself then the system thinks you&#8217;re talking about all 64 bits of the 13 register the r13 register</p>



<p>but if you type whoops if you type r13d then the system thinks you&#8217;re only talking about the lowest</p>



<p>32 bits of that register so you&#8217;re also able to specify the data size of a register again i thought</p>



<p>you should should a long time ago i thought i remembered moving a d word directly into r13 and</p>



<p>it would just clear out the extra bits but i got warnings this time maybe that&#8217;s for the best</p>



<p>that&#8217;s for the best forcing us to be more precise, but this will solve it. R13 just says,</p>



<p>let&#8217;s only talk about a D word. So let me go, let me go to my favorite book here.</p>



<p>Where the heck is my favorite book? Okay. I guess I have to like, there we go.</p>



<p>Okay. There&#8217;s my favorite book. So, um, I&#8217;m going to go to, uh, let&#8217;s, let me just search for,</p>



<p>just search for I can&#8217;t remember where it is our 13 D yeah there it is okay so</p>



<p>what we&#8217;re looking for is 2.3.1.1 let me show you real fast the top of the book</p>



<p>just to give a shout out to the person who wrote this book I did not write this</p>



<p>book this is a free and open source book that you can download for free</p>



<p>everybody should get a copy to turn yourselves into an expert it&#8217;s called</p>



<p>x86 64 assembly language programming with Ubuntu it&#8217;s written by a brilliant</p>



<p>professor. This is like a semi recent version. There&#8217;s probably a newer one now, but yeah,</p>



<p>it&#8217;s a great book. I love it. So I&#8217;m going to go back to what the heck was I just doing?</p>



<p>Was it 2.3.1.1? Yeah. Okay. So in this book section 2.3.1.1, the section entitled general</p>



<p>purpose registers, notice how there are a bunch of different versions of each register. So if you</p>



<p>So if you look in this column right here, we have, oh, I get to use my annotator.</p>



<p>We have a, what were we just looking at?</p>



<p>R12.</p>



<p>Notice how R12 here is a designated as a 64 bit register.</p>



<p>But if you wanted to use the R12 register and only use 32 bits of it, the lowest 32</p>



<p>bits, then this is the form you would use for the R12 register.</p>



<p>So 32 bits.</p>



<p>And if you wanted to use only a word&#8217;s worth, you know, 16 bits.</p>



<p>Well there you go.</p>



<p>two bytes on these systems or 16 bits and then if you just wanted to use one</p>



<p>byte of the register well you can you just put a B at the end of it it&#8217;s</p>



<p>pretty sweet and convenient it&#8217;s not too hard to remember that D is for D word and</p>



<p>W is for word and B is for bytes it just gets a little confusing when you&#8217;re</p>



<p>using the other registers like the the RAX register it&#8217;s got EAX as its 32-bit</p>



<p>version and then AX as its 16-bit version and AL as its 8-bit version if</p>



<p>it&#8217;s a 8-bit version if you can remember that there&#8217;s an L I guess that&#8217;s not too bad but you</p>



<p>know remembering the X and then it changes to an I over here and a P over here I&#8217;m personally not</p>



<p>a fan so I always come back to this book to try and remember what I&#8217;m supposed to be putting for</p>



<p>these registers because I can&#8217;t remember all the time or rather I can almost never remember but we</p>



<p>So, I want to take a D word from memory and load it up into the R13 register.</p>



<p>So I got to specify the R13D.</p>



<p>And, you know, maybe I&#8217;ll take that out and show you that it doesn&#8217;t compile later, if I can remember.</p>



<p>But I&#8217;m going to now take a D word, do the same thing into R13.</p>



<p>Oh, sorry, no.</p>



<p>I&#8217;m going to take a regular word and do the same thing basically into R14.</p>



<p>But it&#8217;s just going to be a word instead of a D word.</p>



<p>And then I&#8217;m going to take a single byte and load it up into R15.</p>



<p>single byte and load it up into R15. Then I&#8217;m going to call Nope, which is an instruction that</p>



<p>just does nothing. And it&#8217;s useful sometimes if you want to have a place where you can stop your</p>



<p>program and sort of inspect the results of your program in your debugger. So there&#8217;s a lot more</p>



<p>to this program. I&#8217;m going to go ahead and just run it right now, just to make sure that it compiles.</p>



<p>And then maybe I&#8217;ll add a breakpoint here. Let&#8217;s go clear and make run. Okay, so it ran,</p>



<p>Okay, so it ran, it compiled, it was fine.</p>



<p>And now I&#8217;m going to open up a new little window and I&#8217;m going to launch the program.</p>



<p>Notice how the program that has been compiled is called main.</p>



<p>So I&#8217;m going to launch the program into the GDB debugger.</p>



<p>This is not a GDB video.</p>



<p>So I&#8217;m going to be skimming over this, check future videos for a full GDB tutorial.</p>



<p>So I&#8217;m going to do a break point right there at line 72.</p>



<p>So that I can just kind of inspect the state of the register.</p>



<p>break at main.asm line 72 and then just to be sure I did that correctly I&#8217;m going to say info</p>



<p>breakpoints or just a b is fine and it&#8217;s like looks like I set it up correctly then I can type</p>



<p>run to see what&#8217;s up notice how it starts to run and immediately stops at that breakpoint</p>



<p>you can tell it stops there because it&#8217;s at line 72 then I&#8217;m just going to do info registers or</p>



<p>info r just to print out all my registers and we should see a confirmation of what we were hoping</p>



<p>so if I kind of like maybe I&#8217;ll pin this to the top for a second always on top how about that</p>



<p>so first we moved an immediate into r10 so notice how 12876 is in r10 right there and then on line</p>



<p>53 we basically copied r10 into r11 so that means in r11 we see the same value</p>



<p>and then we&#8217;ll move a quad word from the long integer we&#8217;ll dereference that pointer</p>



<p>at R12 it&#8217;s like this gigantic number if I scroll all the way up it&#8217;s the same number here as in our</p>



<p>source code so that seemed to work and then we&#8217;re going to move the D word next I guess into R13</p>



<p>so that&#8217;s this number right here so that seemed to have worked it&#8217;s this number right here the</p>



<p>D word and I&#8217;m going to get lost so fast scrolling up and down like this now we&#8217;re going to move a</p>



<p>one one and there it is right there and then r15 is just gonna take a bite and</p>



<p>it&#8217;s gonna take a bite oh now I&#8217;m hungry now I&#8217;m hungry that activated it it&#8217;s</p>



<p>only been like think four or five hours since I ate I guess that is usually when</p>



<p>I need to eat again even though I gorge and fall asleep on the floor because I</p>



<p>home r15 anyway holds the correct value it&#8217;s 222 i just want to show you overflow real fast</p>



<p>we look at r14 uh we&#8217;re using the word version so it&#8217;s only going to read you know uh two bytes</p>



<p>from that memory location and it&#8217;s only going to consider uh you know 16 bits as being valid in uh</p>



<p>in that register so if i use a number that&#8217;s too big it&#8217;ll overflow two bytes the maximum number</p>



<p>three, five or from zero to that number or six, five, five, three, six combinations.</p>



<p>So if I just stick a nine in front of that right now, that should overflow the system.</p>



<p>So let me try it one more time.</p>



<p>I want to do Q because that was the last thing we needed to look at.</p>



<p>And then I&#8217;m going to do make.</p>



<p>Oh gosh, what&#8217;s happening here?</p>



<p>Okay.</p>



<p>How about I do clear and make build, which is in my make file and GDB main.</p>



<p>So I don&#8217;t have to type all this stuff out again every single time.</p>



<p>have to type all this stuff out again every single time what did i just do oh nice okay you know what</p>



<p>i swear it didn&#8217;t used to do this before it&#8217;s giving me a nice warning saying that value does</p>



<p>not fit in a 16-bit field i guess if i turned warnings off which i don&#8217;t want to do then the</p>



<p>program would run and it would overflow eh do you want to turn it off let&#8217;s turn off warnings okay</p>



<p>much attention to it. Um, where the heck are the warnings? Where&#8217;s the assembler running? There it</p>



<p>is. Yasum flags. And that&#8217;s, so that&#8217;s my variable up there. There we go. Okay. So I&#8217;m just going to</p>



<p>make a copy of this line, comment out the original, and then I&#8217;m going to take out, uh, the command</p>



<p>that says, or the flag that says convert warnings into errors. Your compiler is your friend. It was</p>



<p>So I am now going to do a breakpoint at 72.</p>



<p>So break at main.asm972 and then run and then it breaks.</p>



<p>And then I go input registers.</p>



<p>Then if I look at, what was I just talking about?</p>



<p>R14, was that it?</p>



<p>Notice how 44607 for the word, that&#8217;s a totally different value, right?</p>



<p>So this is what happens when you overflow or underflow.</p>



<p>You got to be careful about your data sizes.</p>



<p>I&#8217;m just going to do this again one more time just to make sure.</p>



<p>one more time just to make sure that it&#8217;s fixed now that I moved it and I&#8217;m</p>



<p>gonna I&#8217;m gonna read that part about warnings because I love converting</p>



<p>warnings to errors as just a way to help myself write better code okay so let&#8217;s</p>



<p>do that and you know what I&#8217;m gonna do too I added another target here again</p>



<p>this is not a Yasm or sorry this is not this is kind of a Yasm video this is not</p>



<p>extra file called gdb.txt where I can stick gdb commands and so I don&#8217;t want</p>



<p>to continue to type my breakpoints over and over over again every single time so</p>



<p>I know for sure I&#8217;m not going to change the program anymore at least I&#8217;m not</p>



<p>going to shift the lines up and down before 72 so I&#8217;m going to do breakpoint</p>



<p>you can type like break or I think you can type either break or the full word</p>



<p>breakpoint but I just type B name of the source code file a.asm at line 72 and so</p>



<p>And so this file right here, again, this is not a GDB tutorial, but in my make file, I have it set up so that GDB will execute whatever commands I put into this text file.</p>



<p>So it just kind of saves me typing.</p>



<p>So let&#8217;s put a breakpoint there and then we&#8217;ll say info breakpoints and then we&#8217;ll just run the program in that way.</p>



<p>I can have a little bit more information.</p>



<p>So I&#8217;m going to do make build instead of make build and then running GDB.</p>



<p>I&#8217;m going to say make debug.</p>



<p>That&#8217;s another target in my make file.</p>



<p>make file so then you can see it added the breakpoint for me and then it</p>



<p>immediately broke on that and you know what yeah I guess I&#8217;ll just leave it in</p>



<p>where I have to type info registers okay so we&#8217;re just making sure I think that</p>



<p>R14 is still a valid value and that it was fixed okay so we&#8217;ve gotten this far</p>



<p>the next thing we need to do is prove that data size matters by specifying a</p>



<p>specifying a bad data size for our long and then for our byte. So I&#8217;m going to copy paste a little</p>



<p>proof here that is followed by another non-operation. So I&#8217;m going to do this. And so</p>



<p>I&#8217;m going to try to prove to you that the data size matters by specifying a bad data size. I</p>



<p>just proved to you that it kind of mattered or it definitely mattered in terms of overflow and</p>



<p>underflow, but I&#8217;m trying to prove to you now that it matters when you&#8217;re reading. So I&#8217;m going to</p>



<p>I&#8217;m going to clear out r12 and then I&#8217;m going to move a byte from the long integer into r12b</p>



<p>and you know this would be a warning or I think it this wouldn&#8217;t compile or wouldn&#8217;t assemble if</p>



<p>I didn&#8217;t have the b there so I&#8217;m basically telling the computer all right I want you to use only a</p>



<p>byte of r12 and I want you to take one byte from memory and just put it in there but I&#8217;m taking it</p>



<p>awful. And then right after that, I&#8217;m going to do another naughty thing. I&#8217;m going to move</p>



<p>something into R15 and I&#8217;m going to word, I&#8217;m going to move a keyword of memory into R15,</p>



<p>but I&#8217;m going to be taking it from the bytes memory. So remember a byte is just one byte.</p>



<p>And so the next seven bytes that come after it definitely have nothing to do with that byte,</p>



<p>but I&#8217;m going to interpret them all as a quad word. So we should get nonsense data</p>



<p>also R15. I don&#8217;t really need to zero out R15 because the fact that I&#8217;m specifying R15 means</p>



<p>I want to use all 64 bits. So it is going to definitely zero it out. And whatever we see in</p>



<p>R15 is going to be the real thing. But in terms of R12B, we&#8217;re only going to be loading up one</p>



<p>byte&#8217;s worth of that register. So that&#8217;s why I&#8217;m zeroing out the rest of it just to prove to you</p>



<p>that we are truly getting nonsense. So now there&#8217;s going to be another break point at 82.</p>



<p>I&#8217;m not going to break anymore at 72. I&#8217;m going to break at 82. So let&#8217;s do that.</p>



<p>What&#8217;s going on here? We got that.</p>



<p>Oh gosh. Okay. Quit. I guess that means I need to comment this out. Let me do that.</p>



<p>We&#8217;ll do break at main.asm at 82. And you could, you could do both of these two. Actually,</p>



<p>let me do it first. We could, we could do multiple break points. Yeah, I&#8217;ll just do it first,</p>



<p>So notice how it first breaks at the 72 and then I do C to continue and then suddenly</p>



<p>it breaks at the 82.</p>



<p>But I don&#8217;t want to have to hit continue a bunch of times so I&#8217;m going to comment that</p>



<p>out for the future.</p>



<p>Let me quit real fast to make sure I remember what GDB expects as a comment.</p>



<p>Yeah, okay.</p>



<p>82.</p>



<p>Okay, so then I&#8217;m going to print the registers info registers.</p>



<p>And we&#8217;re looking at R12 and R15.</p>



<p>You can see R12 has the number 144, which definitely doesn&#8217;t make sense because there&#8217;s</p>



<p>no 144 anywhere in our original data.</p>



<p>in our original data so hopefully that&#8217;s proof to you that we&#8217;ve grabbed some junk</p>



<p>sometimes maybe when you&#8217;re doing this at home if you&#8217;re just going to grab one bite</p>



<p>maybe accidentally um no no that would definitely still look bad but for the r15</p>



<p>if you&#8217;re going to grab a q words worth of data i don&#8217;t know maybe the junk data is like</p>



<p>accidentally correct in a way where it would it would still make it look like the intended value</p>



<p>the intended value no it wouldn&#8217;t there&#8217;s no way that would even happen no i guess the junk data</p>



<p>could could work that way anyway so we&#8217;ll look at r15 two eight and a bunch of fours and then a two</p>



<p>three eight um that value does not exist up here so again junk data mixed in with real data is is</p>



<p>bad news okay so now let&#8217;s do another proof i want to prove to you that symbols are pointers</p>



<p>you know like I&#8217;ve been saying and they need to be dereferenced so let&#8217;s first start off by</p>



<p>moving the raw pointer for my long int into r10 and then into r11 we will dereference that</p>



<p>pointer so in r10 we should see something that looks like a memory location or at least a relative</p>



<p>memory location and then in r11 we should see the actual long integer like we saw before</p>



<p>And then I&#8217;m going to mess around with some arrays.</p>



<p>So remember up here we made an array.</p>



<p>Let me minimize that real fast.</p>



<p>We made an array, a very small array.</p>



<p>I just specified a bunch of ones and then specified a bunch of twos.</p>



<p>So it should be contiguous memory since it&#8217;s been allocated as an array with that comma</p>



<p>there.</p>



<p>We should see if we looked inside of the system inside of memory, we should see eight bytes</p>



<p>representing the ones.</p>



<p>And then right next to that, we should see eight bytes representing the twos.</p>



<p>easy for us to sort of move our pointer around a little bit. So what else do I need to copy paste</p>



<p>here? We got that long int. Okay. So I&#8217;m going to grab the first item and the second item in those</p>



<p>arrays just using some pointer manipulation. So you know how to do this already. You just take</p>



<p>like the pointer to some number and you just dereference it and you end up getting a number.</p>



<p>I understand your feeling because you might be thinking, wait a minute, wait a minute.</p>



<p>I understood it when it was dereferencing just one number, but didn&#8217;t you say this is an array?</p>



<p>Why is it we can dereference the array and just get one number still?</p>



<p>Well, remember, a pointer to an array is really a pointer to the first item in the array.</p>



<p>That&#8217;s kind of how it works.</p>



<p>So either way, whether you consider that to be a pointer to one number or a pointer to array,</p>



<p>when we dereference it, we&#8217;ll get one number.</p>



<p>If you want to get the other numbers, then you have to start manipulating the pointer</p>



<p>or derefing in different ways.</p>



<p>So here&#8217;s how we can get the second item.</p>



<p>We deref the original pointer plus 8.</p>



<p>8 because it&#8217;s a quad word.</p>



<p>So, you know, it probably would be a little smarter to put that 8 up into a define somewhere</p>



<p>so you&#8217;re not hard coding numbers, but I&#8217;m not going to do it.</p>



<p>So basically in R12, we should see the 1s.</p>



<p>In R13, we should see the 2s.</p>



<p>And then a couple more things.</p>



<p>Next, we will grab a pointer to the first item in the small array.</p>



<p>And we&#8217;ll just stick that into R14.</p>



<p>So, you know, we kind of did that with R10 already.</p>



<p>But now I just want a pointer to the small array.</p>



<p>And I&#8217;m going to keep it in R14 just so you can see the memory location.</p>



<p>And then after that, we&#8217;re going to grab an actual pointer to the second item.</p>



<p>hey how do you get a pointer to the second item well it&#8217;s that but then what</p>



<p>if you didn&#8217;t want to dereference the pointer like what if R13 you know here</p>



<p>clearly because we have these brackets for dereferencing you&#8217;re allowed to put</p>



<p>the formulas inside of the brackets what if I wanted to get the address to the</p>



<p>second item and not actually the second item we would not be allowed to remove</p>



<p>the brackets the assembler wouldn&#8217;t like that so without another instruction</p>



<p>way to actually get a pointer with a complicated mathematical formula. You know, you can do like</p>



<p>multiplication inside of here and other things in parentheses. So we wouldn&#8217;t be able to do that</p>



<p>without the other instruction that I&#8217;m introducing now called LEA. LEA just means, yeah, sure, we&#8217;re</p>



<p>still going to use the brackets to figure out where the pointer is, like figure out what memory</p>



<p>location we want with some sort of a formula. But then what will be stored in the first operand is</p>



<p>This LEA won&#8217;t dereference just because there are brackets.</p>



<p>LEA will keep the pointer.</p>



<p>And then finally, once we actually have a pointer to the second item in R15,</p>



<p>then we can move the second item into another register.</p>



<p>And this is like an LOL because I ran out of registers already.</p>



<p>So I&#8217;m just going to use RDI, which is usually the first argument.</p>



<p>It doesn&#8217;t matter. It&#8217;s fine.</p>



<p>So if we dereference R15, it&#8217;s the same thing as dereferencing this.</p>



<p>We could have also just dereferenced R14,</p>



<p>and it would have been the same thing as just getting the value of the first item.</p>



<p>But I hope you can see now that, you know,</p>



<p>that expression is going to be a pointer to the second item in the array,</p>



<p>and that&#8217;s going to be stuck into R15.</p>



<p>So if we dereference that, we can treat R15 like a pointer.</p>



<p>It&#8217;s just, you know, it&#8217;s a general purpose register.</p>



<p>it doesn&#8217;t only need to hold integer values it can also hold pointers because pointers are integers</p>



<p>they&#8217;re just unsigned so we deref the pointer that is stored inside of r15 now put it inside</p>



<p>of rdi and then we should see that rdi actually holds the second item which should be a bunch of</p>



<p>twos so the last thing that i&#8217;m going to copy paste in here is another nope so i&#8217;m going to</p>



<p>break at 108 just so we can see the rest of it so i&#8217;m going to open up this right here and i&#8217;m</p>



<p>open up this right here and I&#8217;m going to comment that out and I&#8217;m gonna say break at 108.</p>



<p>Whoops.</p>



<p>Okay.</p>



<p>There we go.</p>



<p>But, but, but quitsies.</p>



<p>Now I&#8217;m just going to run make debug again so that I can enter GDB.</p>



<p>And we&#8217;ve, we&#8217;ve, we&#8217;ve hit our break points.</p>



<p>And now I&#8217;m going to go info registers and just talk about the stuff that we&#8217;re seeing.</p>



<p>Okay.</p>



<p>So the first thing I think this is all on one page, right?</p>



<p>I don&#8217;t need to scroll.</p>



<p>Yeah.</p>



<p>one page right I don&#8217;t need to scroll yeah okay this is all on one page first thing we see is r10</p>



<p>holds this value right here that&#8217;s a pointer to the my long int that kind of looks like a pointer</p>



<p>if you consider that it could be relative and then r11 holds the actual integer and if you recall</p>



<p>that is the gigantic long that we put in there great so now that&#8217;s the difference between a</p>



<p>pointer and its value or in its dereference value so then we&#8217;re going to grab the first item in the</p>



<p>So then we&#8217;re going to grab the first item in the small array.</p>



<p>So R12 should have the ones.</p>



<p>If we look at R12 here, it&#8217;s got the ones.</p>



<p>Again, just trying to prove to you that if you dereference a pointer to an array,</p>



<p>you&#8217;re actually dereferencing a pointer to the first item.</p>



<p>And then on R13, we&#8217;re going to make a pointer to the second item.</p>



<p>Then with the brackets and the move instruction, we dereference that and it should become the twos.</p>



<p>So if we look at R13, notice how it&#8217;s the twos.</p>



<p>Then we&#8217;re going to grab a pointer to the first item in the small array.</p>



<p>Hang on a second.</p>



<p>What did I want to say about that?</p>



<p>In the small array to R14.</p>



<p>Okay.</p>



<p>So again, we&#8217;re just going to grab an actual pointer to the small array.</p>



<p>And so the difference between R12 and R14 is the dereft value or the pointer.</p>



<p>So if we look at R12, that&#8217;s the actual value that we&#8217;re pointing at.</p>



<p>And then R14 is the memory location that we have in there.</p>



<p>And then notice how it&#8217;s kind of similar.</p>



<p>Notice how it&#8217;s kind of similar.</p>



<p>Maybe this proves a little bit more that these are pointers that we&#8217;re looking at.</p>



<p>R10 was supposed to be a pointer and now R14 is also a pointer.</p>



<p>Notice how those numbers are pretty close together.</p>



<p>I&#8217;m sure if we looked back up in the data section and we calculated an offset based</p>



<p>on how big each data item was, we could probably predict what the memory location of R14 should</p>



<p>have been based on R10 because you can see it&#8217;s like only increased by a little bit,</p>



<p>by a little bit right it was like 534 to 49 what is that 10 plus 5 so that&#8217;s like 15 is that correct</p>



<p>oh I guess I had a byte in there so maybe it&#8217;s that&#8217;s why it&#8217;s kind of odd but yeah we can we</p>



<p>can just compute the offset and then we&#8217;ll grab a pointer to the second item using the LEA</p>



<p>instruction remember we have to use brackets if we&#8217;re going to do an expression but then the</p>



<p>dereferencing in the move instruction so we just use LEA to not dereference.</p>



<p>So we should see that R15 is about 8 bytes further along in RAM than R14 because both of those items</p>



<p>are contiguous in memory and they should only be about you know a quad word apart 8 bytes.</p>



<p>So if we look at R14 it&#8217;s got this number so it&#8217;s a pointer and then if we look at R15</p>



<p>you know what&#8217;s 49 plus 8 it&#8217;s 57 so you can see that the pointer to the second item is only</p>



<p>pointer to the second item is only eight bytes further in memory yay contiguousness and then</p>



<p>sorry i can&#8217;t resist so then finally we will uh we&#8217;ll look at the dereference to r15 because it&#8217;s</p>



<p>now a pointer what was it it was the it was a pointer to the second item that should be a bunch</p>



<p>of twos right because remember in our array we had a bunch of twos here as our second item</p>



<p>have a bunch of twos inside of it so if i look at rdi where the heck is that right there has a</p>



<p>bunch of twos inside of it rdi baby oh my gosh why did i say that anyway so that&#8217;s our non-operation</p>



<p>instruction and i think that&#8217;s everything that i wanted to talk to you about in this video so now</p>



<p>i hope you feel like you&#8217;re an expert in moving integers and moving pointers and</p>



<p>dereferencing pointers and manipulating pointers so you can get a pointer to something else and</p>



<p>pointer to something else and de-referencing those pointers or not whenever you choose.</p>



<p>Thank you so much for watching this video. I hope you learned a little bit of stuff</p>



<p>and you had a little bit of fun. I&#8217;ll see you in the next video. Limit out. I&#8217;ll never say that</p>



<p>again. 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>could do me a please a small little favor could you please subscribe and</p>



<p>follow this channel or these videos or whatever it is you do on the current</p>



<p>social media website that you&#8217;re looking at right now it would really mean the</p>



<p>world to me and it&#8217;ll help make more videos and grow this community so we&#8217;ll</p>



<p>be able to do more videos longer videos better videos or just I&#8217;ll be able to</p>



<p>keep making videos in general so please do do me a kindness and and subscribe</p>



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



<p>night and I just wake up because I know somebody subscribed or followed it just</p>



<p>wakes me up and I get filled with joy that&#8217;s exactly what happens every single</p>



<p>time so you could do it as a nice favor to me or you could you control me if you</p>



<p>want to just wake me up in the middle of night just subscribe and then I&#8217;ll just</p>



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



<p>to the website which I think is also named somewhere at the bottom of this</p>



<p>video and it&#8217;ll take you to my main website where you can just kind of like</p>



<p>my main website 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 uh if you have a suggestion</p>



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



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



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



<p>It would really mean the world to me.</p>



<p>I would really appreciate it.</p>



<p>So again, thank you so much for watching this video.</p>



<p>And enjoy the cool music 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-integer-data-movement-pointers-and-dereferencing-explained-yasm-on-ubuntu/">x86-64 Assembly: Integer Data Movement, Pointers, and Dereferencing Explained (YASM on Ubuntu)</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/x86-64-assembly-integer-data-movement-pointers-and-dereferencing-explained-yasm-on-ubuntu/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Mastering x86-64 Assembly: A Beginner&#8217;s Guide to CPU Registers</title>
		<link>https://www.NeuralLantern.com/mastering-x86-64-assembly-a-beginners-guide-to-cpu-registers/</link>
					<comments>https://www.NeuralLantern.com/mastering-x86-64-assembly-a-beginners-guide-to-cpu-registers/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 27 Jul 2025 00:12:09 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[ABI]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[callee saved registers]]></category>
		<category><![CDATA[coding optimization]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[CPU registers]]></category>
		<category><![CDATA[general purpose registers]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[programming tutorial]]></category>
		<category><![CDATA[RAX register]]></category>
		<category><![CDATA[RBX register]]></category>
		<category><![CDATA[stack pointer]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=190</guid>

					<description><![CDATA[<p>Learn x86-64 assembly with this beginner’s guide to CPU registers. Master RAX, RBX, ABI rules, and optimize code. Perfect for low-level programming enthusiasts.</p>
<p>The post <a href="https://www.NeuralLantern.com/mastering-x86-64-assembly-a-beginners-guide-to-cpu-registers/">Mastering x86-64 Assembly: A Beginner&#8217;s Guide to CPU Registers</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Mastering x86-64 Assembly: A Beginner&#039;s Guide to CPU Registers" width="1380" height="776" src="https://www.youtube.com/embed/jPysL_y4rRA?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p>Ready to dive into x86-64 assembly? This beginner-friendly video breaks down CPU registers like RAX, RBX, and more in Yasm assembly. Learn how to use general-purpose registers, respect the ABI to avoid debugging nightmares, and optimize code by minimizing RAM hits. We’ll cover callee-saved registers, function arguments, and why you shouldn’t mess with the stack pointer. Perfect for coders looking to master low-level programming. Grab the free book mentioned and subscribe for more assembly tips! #AssemblyProgramming #x86</p>



<p>Introduction to Registers 00:00:00<br>x86-64 Yasm Assembly 00:00:04<br>Recommended Book 00:00:09<br>Move Instruction Example 00:01:04<br>64-bit Registers Overview 00:01:52<br>General Purpose Registers 00:02:24<br>Two&#8217;s Complement for Integers 00:03:37<br>Floating Point Registers Introduction 00:03:54<br>ABI Importance 00:04:55<br>Function Return Values 00:08:01<br>C++ to Assembly Translation 00:08:43<br>Avoiding System RAM 00:11:45<br>Optimizing with Registers 00:13:04<br>Callee Saved Registers 00:14:16<br>Preserving Registers with Push/Pop 00:16:40<br>Stack and Local Variables 00:22:00<br>Function Arguments in Registers 00:25:12<br>Stack Pointer and Base Pointer 00:31:29<br>Instruction Pointer (RIP) 00:36:30<br>Temporary Registers (R10, R11) 00:36:49<br>Accessing 32-bit Register Portions 00:38:29<br>Floating Point Registers Details 00:42:53<br>Conclusion and Call to Subscribe 00:45:35</p>



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



<p>Let&#8217;s talk about registers in assembly.</p>



<p>More specifically, x8664 Yasm assembly.</p>



<p>Okay, so first off, check out this book.</p>



<p>It&#8217;s a wonderful book.</p>



<p>The author is a genius.</p>



<p>It&#8217;s called x8664 assembly language programming with Ubuntu.</p>



<p>It&#8217;s free.</p>



<p>website and you know grab your copy it&#8217;s wonderful it&#8217;s released under a copy left license</p>



<p>author&#8217;s a genius whenever i&#8217;m trying to remember things about cpu registers for assembly</p>



<p>i always go to this section i say callie saved i search for callie saved i already had it there</p>



<p>callie saved and it ends up being section 12.8.2 register usage so before we do that let me just</p>



<p>that let me just give you a little quick example so you kind of know what I&#8217;m</p>



<p>talking about here hopefully everybody&#8217;s seen this instruction by now this is a</p>



<p>move instruction in Yasm x86 64 assembly it just moves data from one place to</p>



<p>another it takes two operands so you know we have like operand one and</p>



<p>operand two basically operand one is the destination operand so we could put</p>



<p>destination there if we wanted to operand two is the source we could put</p>



<p>that actually won&#8217;t, you know, compile or assemble,</p>



<p>but I&#8217;m just saying that&#8217;s what those positions are for.</p>



<p>So here&#8217;s an example of a register receiving the value 10.</p>



<p>We use the move instruction and we say,</p>



<p>we would like to move something into the REX register.</p>



<p>We would like to move the value 10 into the REX register.</p>



<p>And there you have it. There&#8217;s a move instruction. Okay.</p>



<p>So REX is a register.</p>



<p>that it is a 64-bit register in our 64-bit CPUs every single register or</p>



<p>sorry every single general purpose register and the plot registers they</p>



<p>have 64 bits available for us to use 64 bits like you know ones and zeros it&#8217;s</p>



<p>like a very very very big number a long time ago we had 32 bit registers and so</p>



<p>these are like you know twice as mathematically robust or whatever you</p>



<p>allows the registers to address, you know, the 64 bit memory addressing space,</p>



<p>which helps us go beyond four gigabytes of RAM in our modern computers,</p>



<p>helps us go far beyond what we can even achieve right now.</p>



<p>So we have a register called RAX and there&#8217;s a list of other registers on this</p>



<p>page here. So basically, you know,</p>



<p>we have the RAX register and the RBX register and the RCX register and so</p>



<p>forth. I have a hard time remembering all these names,</p>



<p>remembering all these names so I go to this book when I forget but otherwise</p>



<p>you know you can kind of think of like ABCD and then just surround them with</p>



<p>RNX you know our kind of for register and X for extra big maybe so we&#8217;ve got</p>



<p>the RAX RBX RCX and RDX registers and then we have a bunch of other ones</p>



<p>through R15 but first before we do that let me just emphasize that these are</p>



<p>general purpose registers to be used for integers or data you can put</p>



<p>characters in there for strings you can put just like you know whatever data you</p>



<p>want inside the machine we use twos complement to represent integers so it&#8217;s</p>



<p>it&#8217;s fine the instructions that you will do for addition and other mathematical</p>



<p>but don&#8217;t put floating point numbers in these registers because the computer the</p>



<p>system the machine won&#8217;t know how to operate on that data the thing is</p>



<p>integers are stored as twos complement I&#8217;m going to talk about that more in</p>



<p>another video and floats are stored in something called I triple E seven five</p>



<p>four standard which is just like a crazy different way of storing floating point</p>



<p>numbers that makes more sense for floating point numbers but because the</p>



<p>differently in the machine you can&#8217;t really use normal instructions like you</p>



<p>can&#8217;t add two floats together the same with the same instruction that you would</p>



<p>add two integers together you would have to use special floating point registers</p>



<p>and special floating point instructions I&#8217;ll talk about floats at the end of</p>



<p>this video I hope but basically just keep in mind these are general purpose</p>



<p>registers to be used for integers in data only okay so what am I talking</p>



<p>Okay, so what am I talking about?</p>



<p>What are we seeing here on the side here?</p>



<p>Why does it say return value?</p>



<p>What is going on with Kali saved and fourth argument and third argument and so forth?</p>



<p>So there&#8217;s a standard that you&#8217;re supposed to use when you program.</p>



<p>You can get yourself into a lot of hot water or you can dramatically increase your debugging</p>



<p>time if you don&#8217;t do this.</p>



<p>standard called the ABI which I think is short for application binary interface but basically I just</p>



<p>like to say the ABI respect the ABI there&#8217;s a plan that people came up with that says this is how you</p>



<p>should use these registers don&#8217;t use them in a different way and that way if everybody is writing</p>



<p>you know modules and they&#8217;re all going to interact together like I write a module you write a module</p>



<p>the abi then it&#8217;s it&#8217;s pretty much guaranteed that we can we can expect certain things like</p>



<p>our registers won&#8217;t become corrupted after a function call or that we&#8217;re not going to corrupt</p>



<p>someone else&#8217;s uh function or you know whatever right so you can also use this to benefit yourself</p>



<p>even if you&#8217;re the only person who is writing your code and you&#8217;re not calling on anyone else&#8217;s</p>



<p>function um then you know like how are you going to remember like what was i using this register</p>



<p>Was I supposed to save that one?</p>



<p>Was I supposed to preserve it?</p>



<p>How was I going to return data from that function?</p>



<p>Was I going to use this register or the other register?</p>



<p>It&#8217;s hard to remember what you,</p>



<p>what you yourself even did like a month ago.</p>



<p>So if you respect this plan,</p>



<p>then your functions will work not only</p>



<p>with other people&#8217;s functions,</p>



<p>but with your own functions from a month ago.</p>



<p>The other thing too, is we have system calls, right?</p>



<p>So you have like a system call.</p>



<p>Let&#8217;s say we want to move something into REX.</p>



<p>something into REX, maybe I want to exit the system or something.</p>



<p>Just forget about this.</p>



<p>This is not a system call video.</p>



<p>Suppose I want to do a system call, right?</p>



<p>The system call itself is also going to respect the ABI.</p>



<p>So if you start calling other</p>



<p>services in the system call instruction, like if you want to open a file,</p>



<p>close a file, read a file, write a file, whatever.</p>



<p>If you don&#8217;t respect the ABI and the system call is respecting the ABI,</p>



<p>might have some of your data corrupted so it&#8217;s a really really good idea to respect the abi</p>



<p>in fact think about it this way what if uh what if you wrote a bunch of functions for a long long</p>



<p>time and then like a year later you couldn&#8217;t you couldn&#8217;t remember what you were doing you know</p>



<p>last year oh what was the return register uh and it costs you a bunch of extra time debugging</p>



<p>eventually you&#8217;ll probably decide to write your write your own standard okay from now on i&#8217;m always</p>



<p>going to use this register for return values and i&#8217;m going to use this register for the first</p>



<p>and i&#8217;m going to use this register for the first argument and i&#8217;m going to write you probably</p>



<p>come up with a plan right that&#8217;s basically the abi the abi covers more than just how to use</p>



<p>registers but um register usage is covered in the abi so why would you put yourself through</p>



<p>a bunch of heartache and then eventually come up with your own plan when you could have just</p>



<p>been following the correct plan in the first place right so everybody should respect the abi</p>



<p>I wanted to talk next about the return value.</p>



<p>Okay, so let me write up a little function here.</p>



<p>Let me just say that we have a C++ function.</p>



<p>And we&#8217;ll just say it&#8217;s like void f.</p>



<p>And we&#8217;ll say it calls on g.</p>



<p>And then we&#8217;ll have another function called g.</p>



<p>And actually, maybe it&#8217;s not void.</p>



<p>Maybe it actually does return a long.</p>



<p>returns the number five or something. Okay.</p>



<p>Hopefully you&#8217;ve seen some kind of a higher level language before like C++</p>



<p>so that you can understand what&#8217;s going on here.</p>



<p>We&#8217;re just making two functions and one is calling the other.</p>



<p>And the second one is just returning a value to the first one.</p>



<p>So in assembly, the equivalent of this would be,</p>



<p>let&#8217;s make a label with the name of the function and then a little colon,</p>



<p>and then just put a return instruction at the end of it. Wham,</p>



<p>it, wham, you&#8217;ve got a function. It&#8217;s not going to work very well, but you do have a function at</p>



<p>this point. So F, uh, I&#8217;m going to try to copy the C++, uh, function. So it&#8217;s a really good idea.</p>



<p>Whenever you&#8217;re writing a function in assembly, try to imagine what the prototype would be for</p>



<p>C++ and just put it as a comment at the top. So I know how the F function is going to behave in</p>



<p>my assembly module because I put a comment up there just kind of reminding me what the prototype</p>



<p>for C++. It doesn&#8217;t really do anything. It just sort of runs. So that&#8217;s okay. And then I&#8217;ll make</p>



<p>another function down here. I&#8217;ll do the long G again in a comment, and then I&#8217;ll do the actual</p>



<p>assembly version. I&#8217;ll say G colon, and then I&#8217;ll return. So just by putting that comment up,</p>



<p>I kind of can remind myself now that the G function is supposed to return some kind of a value.</p>



<p>up here, you know, do how about let&#8217;s say, I don&#8217;t want to start adding a bunch of local</p>



<p>variables right now.</p>



<p>Pretend that we&#8217;re going to print the return value of the call to G somehow we&#8217;re going</p>



<p>to use C out or whatever.</p>



<p>We&#8217;re going to send it to a variable, whatever.</p>



<p>I&#8217;m just going to try to keep the assembly as simple as possible.</p>



<p>But anyway, the point is G returns something.</p>



<p>you know, a value in your assembly function.</p>



<p>Well, remember that higher level languages,</p>



<p>part of why they&#8217;re so awesome</p>



<p>is they do a lot of extra work for us under the hood</p>



<p>and they actually provide illusions to us</p>



<p>that make programming easier.</p>



<p>For example, there are no functions</p>



<p>going on inside the actual machine.</p>



<p>I mean, I guess from a certain point of view there are,</p>



<p>but basically the machine is just sort of like moving data</p>



<p>and jumping to instructions</p>



<p>and jumping back from somewhere.</p>



<p>And, you know, it&#8217;s just sort of like jumping around</p>



<p>and executing instructions.</p>



<p>and executing instructions there&#8217;s no actual function it&#8217;s so it&#8217;s like if you want to pretend</p>



<p>that you have a function in assembly well there is a return instruction that will help you jump</p>



<p>back to wherever you came from most recently but we have to we have to implement more is what i&#8217;m</p>



<p>saying so how do we return a value in an assembly function we just have to load up the return value</p>



<p>it as return value that just means we have to load rax up with something so for example we wanted to</p>



<p>return the number five so i&#8217;m just going to load it up with the number five and then return</p>



<p>now we&#8217;ve pretty much translated a c plus plus function into uh an assembly function i mean</p>



<p>that&#8217;s really what&#8217;s happening under the hood when you compile your c plus plus program</p>



<p>it&#8217;s um it&#8217;s just you know translating all the c plus plus into assembly</p>



<p>Another thing that I should probably point out is that you should use registers as often as possible</p>



<p>and you should try your best not to touch memory.</p>



<p>Of course at some point you have to touch memory when you want to save your final result or send</p>



<p>it off somewhere or whatever but you know imagine that you have an assembly function and you&#8217;re</p>



<p>doing lots and lots of calculations you&#8217;re performing an algorithm or something your</p>



<p>program will be so much more efficient by like a factor of a hundred or probably more if you don&#8217;t</p>



<p>if you don&#8217;t hit system ram because every time you hit system ram like a global variable or the</p>



<p>stack or something then um your cpu you know typically in the uncashed uh scenario your cpu</p>



<p>has to go talk to the system bus on your motherboard and then you know send a message</p>



<p>to system ram and then wait for the system ram to figure out what it&#8217;s doing and then get a response</p>



<p>back so when your program is executing on the cpu you can encounter a stall which is like your</p>



<p>like a hundred clock cycles or more like it&#8217;s a long time at least in the most basic case</p>



<p>and how do you avoid that just use registers only when you&#8217;re doing lots of calculations the</p>



<p>registers are built into the cpu they&#8217;re part of the cpu&#8217;s hardware so they are lightning fast</p>



<p>compared to system ram or your disk or whatever</p>



<p>i probably say this a lot but you know part of the reason you would even want to code an assembly</p>



<p>Maybe you have a big program that does a lot of stuff and eventually you profile the program</p>



<p>and you realize, Hey, this one part of the program is really slow because it gets called</p>



<p>constantly and it&#8217;s not super efficient.</p>



<p>That might be a good use case for writing a hybrid program where you have multiple modules.</p>



<p>Some of your modules are in a higher level language and some of your modules are in assembly.</p>



<p>So you take your most important function that slows down your program that gets called all</p>



<p>like a big loop or something and you rewrite it to assembly so that you can</p>



<p>have more control over how often you touch system RAM to try to make the</p>



<p>whole thing more efficient and you know reduce the number of instructions and</p>



<p>just like whatever and you can improve the product of the efficiency of your</p>



<p>program so anyway you want to try to avoid hitting system RAM your registers</p>



<p>are built in the CPU there&#8217;s 64 bits that gives us 64 bits of address space</p>



<p>can reach you know back in the day we had 32 bit systems that means you could only have a memory</p>



<p>stick that was about four gigabytes so now we can go much further so um yeah that&#8217;s what&#8217;s going on</p>



<p>with our registers on the cpu back to the return value okay so we have return value here goes into</p>



<p>the rax register and then the next thing here is it&#8217;s saying that the rbx register is something</p>



<p>remember we said that uh we have to respect the abi right like you have to respect this convention</p>



<p>uh because if you don&#8217;t you&#8217;re going to cost yourself debug time and your</p>



<p>functions probably won&#8217;t be interoperable interoperable with other people&#8217;s functions</p>



<p>or the system call uh instruction well so one of the things about the abi is some of the registers</p>



<p>are designated as the function that is being called has to be the thing that saves the register&#8217;s</p>



<p>registers value so that it doesn&#8217;t corrupt for the caller. Imagine this if I had a function</p>



<p>called f I&#8217;m going to do like f and g here suppose the function f is going to move something into</p>



<p>let&#8217;s say r12 or actually let&#8217;s do rbx move the value 10 into rbx and then it&#8217;ll call g for some</p>



<p>reason maybe g doesn&#8217;t take any arguments maybe g is just kind of doing stuff let&#8217;s just say that</p>



<p>g just moves a different value into rbx so the thing is when the call comes back like after we</p>



<p>get to this next line let&#8217;s say we&#8217;re going to do something with do something do something with</p>



<p>rbx by the time we get to this line dude can i get the line numbers on this oh yeah i forgot okay</p>



<p>so by the time we get to line seven uh the register rbx is ruined these registers are not</p>



<p>local variables they&#8217;re not like uh you know tied to any scope or function call these registers are</p>



<p>just basically global variables that are sitting on the cpu for lightning fast performance but that</p>



<p>means the rbx i use inside of f is the same rbx i use inside of g so that means if g messes up the</p>



<p>value of rbx and then if f calls g then f now has a bad value for rbx this is a broken program</p>



<p>broken program because we did not respect the ABI.</p>



<p>The ABI says that RBX is callee saved,</p>



<p>which means if I use RBX in the G,</p>



<p>then I have to preserve the value so that by the time I return,</p>



<p>the value is the same as it was when the call first came in.</p>



<p>So how do we do that?</p>



<p>We&#8217;ll do that with a push pop pair.</p>



<p>I&#8217;ll probably make more videos in the future about pushing and popping,</p>



<p>but I&#8217;m going to try to keep it simple for now.</p>



<p>We just say, let&#8217;s push RBX onto the stack.</p>



<p>at the beginning and then we&#8217;ll pop it off the stack at the very end.</p>



<p>This basically means we&#8217;re going to take the value of RBX.</p>



<p>We&#8217;re going to send it onto the stack.</p>



<p>So, so yeah, we are hitting system RAM.</p>



<p>It&#8217;s a little slower at that point.</p>



<p>The ABI will save us a little time for the other registers.</p>



<p>I&#8217;ll try to explain that in a second, but basically we&#8217;re preserving the value here</p>



<p>with push and then we&#8217;re popping it back off.</p>



<p>So we&#8217;re restoring it.</p>



<p>So that means even though we ruined the value at line 14, when F does its line</p>



<p>When F does its line 7, it&#8217;s going to have the correct value of RBX.</p>



<p>It&#8217;s going to have the value 10.</p>



<p>The book and I like to call this the prologue, just meaning this is a little section of code</p>



<p>where we&#8217;re going to set things up.</p>



<p>We&#8217;re going to start getting ready to do more instructions.</p>



<p>And then this at the bottom is going to be called the epilogue.</p>



<p>This is just like the finale.</p>



<p>We&#8217;re like, we&#8217;re kind of cleaning up.</p>



<p>We&#8217;re finishing up right before we return.</p>



<p>If you wanted to return a value from G, then you could put, you know, the move inside of</p>



<p>RAX somewhere else.</p>



<p>Like you could put it like below the epilogue or just above the epilogue.</p>



<p>It&#8217;s fine.</p>



<p>As long as you&#8217;re sure that RAX is not getting trampled upon.</p>



<p>So it&#8217;s important to note also that if I do a, let&#8217;s say a system call right after that,</p>



<p>let&#8217;s say I call G and then I do a system call with, you know, like, you know, some</p>



<p>you know some other number as the call code i want to open a file i want to close a file</p>



<p>i want to read or write a file whatever i want to ask the system to do something for me the</p>



<p>system is also going to respect the abi which means i&#8217;m guaranteed that rbx is not modified</p>



<p>when the system call comes back if if the system call you know if the syscall instruction didn&#8217;t</p>



<p>respect the abi then my rbx could be ruined i&#8217;d have to do a bunch of stuff to preserve it so this</p>



<p>some of these registers let me go down a little bit here like this R10 the</p>



<p>temporary register and also the RDI if you decided to use that in the body of</p>



<p>your program which you&#8217;re allowed to these are not designated as callee saved</p>



<p>which means system call could actually ruin the value of those registers so</p>



<p>suppose for the sake of argument I really decided that I needed to use I</p>



<p>10 because that&#8217;s marked as a temporary I&#8217;ll put a value in our 10 and then I&#8217;ll say you know</p>



<p>do something</p>



<p>With our 10 I&#8217;m sorry our 10</p>



<p>Then that means by the time I&#8217;m done with my call to G</p>



<p>I should assume that our 10 is ruined because even though you&#8217;re you can see right here that that G doesn&#8217;t actually do anything</p>



<p>To our 10 we should assume that we have to respect the ABI which means the other function could have ruined it</p>



<p>Ruined it. This is especially true if you call someone else&#8217;s module or system call or whatever same thing for the system call</p>



<p>so if I call system call</p>



<p>and I use</p>



<p>R10 at some point before and then after then I have to assume R10 is destroyed by the time I get back in system call</p>



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



<p>The cure to that when we&#8217;re talking about a temporary or something that is not designated as callee saved is that the caller has to save</p>



<p>hit a system RAM just to make a call.</p>



<p>Anything that we think we need when we&#8217;re finished.</p>



<p>So I&#8217;m going to do a push.</p>



<p>This is not the only way to do it, but I&#8217;m going to do a push R10 here.</p>



<p>And then afterwards, I&#8217;m going to do pop R10.</p>



<p>So that sucks.</p>



<p>And then I have to do the same thing for the system call.</p>



<p>I can go push R10 and then pop R10.</p>



<p>And of course, if you were clever, you probably would, you know, put the call to G inside</p>



<p>of that system call push pop pair.</p>



<p>So you could have one less push pop pair.</p>



<p>push pop pair but I&#8217;m just saying there&#8217;s no guarantee r10 is going to survive so you always</p>



<p>have to preserve it if you ever want to use it again so this is like another way that the abi</p>



<p>kind of can help you save time notice how here in the uh in the g function which I probably should</p>



<p>have prototyped let me just say it looks like to me it&#8217;s a void and it doesn&#8217;t take any arguments</p>



<p>to prototype your functions in comments.</p>



<p>Maybe let me do the same thing to F up here.</p>



<p>So F looks like it&#8217;s not returning anything and it&#8217;s not taking anything.</p>



<p>Okay. Whoops.</p>



<p>Those are C++ comments that would not compile.</p>



<p>All right. So notice how I&#8217;m using RBX.</p>



<p>So I decided to preserve RBX, but there&#8217;s also other registers that are marked as</p>



<p>marked as Kali saved like R12 through 15 and RBP and whatever,</p>



<p>but I&#8217;m not preserving those.</p>



<p>The reason I don&#8217;t have to preserve those is because I&#8217;m not using them.</p>



<p>So the, uh, the, uh, the ABI can save you a little time here.</p>



<p>Like for example, I&#8217;m preserving R10 because I&#8217;m supposed to assume that I,</p>



<p>that I, that they could be destroyed by the time the function comes back.</p>



<p>What if instead of using R10, I used R12.</p>



<p>have to surround any calls with a push pop pair because I can trust that whoever is going to</p>



<p>modify R12 will preserve it for me. But then notice down here G is not actually using R12.</p>



<p>So G doesn&#8217;t even do push pop on R12. So we save hits to memory. We don&#8217;t even have to touch memory</p>



<p>to preserve R12 because the ABI is helping us sort of stay a little bit more efficient.</p>



<p>So this is great, right? F is a broken function at this point though, because I&#8217;m using</p>



<p>at this point though because I&#8217;m using two registers that are callee saved I</p>



<p>can&#8217;t assume that whoever called F is gonna know that I&#8217;m modifying RBX and</p>



<p>R12 so I should do a prolog and an epilog up here I&#8217;m gonna say push RBX</p>



<p>and then I&#8217;m gonna push R12 and then down here at the bottom I&#8217;m gonna do an</p>



<p>epilog epilog and I&#8217;m gonna pop RBX and I&#8217;m gonna pop R12</p>



<p>but I&#8217;ve done something wrong.</p>



<p>The thing is, I&#8217;m not going to talk about the stack too much in this video,</p>



<p>but the stack is a particular type of data structure</p>



<p>that will return to you data in a reverse order than you sent into it.</p>



<p>This is great for helping us keep track of function calls and return addresses,</p>



<p>as I&#8217;ll probably try to explain in this video.</p>



<p>But basically, the data comes out backwards.</p>



<p>The data comes out backwards. So if I do it like this</p>



<p>Then actually by the time the return statement instruction gets executed</p>



<p>RBX will have the value that was intended for R12 and R12 will have the value that was intended for RBX</p>



<p>It&#8217;ll be backwards. So you have to do your push pops in the reverse order to notice how</p>



<p>It&#8217;s kind of like a shell that goes outwards. It&#8217;s like R12 is first on the inside and then RBX is next on the outside</p>



<p>That&#8217;s just what you have to do to preserve everything.</p>



<p>So the F function works now.</p>



<p>The G function, I think it works now.</p>



<p>We have a prologue and epilogue.</p>



<p>Okay, so we talked about the ABI.</p>



<p>We talked about calls.</p>



<p>We talked about the registers.</p>



<p>Talked about callee saved.</p>



<p>We talked about the fact that we don&#8217;t have to save the other ones.</p>



<p>I think I can talk about…</p>



<p>um so uh one thing that i should make sure to mention is that these are general purpose registers</p>



<p>they&#8217;re to be used for integers integer data or like characters or just like regular data</p>



<p>they use certain instructions that are not to be used with floating point data so you actually</p>



<p>should not store floating point numbers inside of your general purpose registers you should only</p>



<p>store floating points in special registers which i&#8217;ll talk about at the end of this video</p>



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



<p>Floating point numbers are stored differently in the system.</p>



<p>They&#8217;re stored with a scheme called IEEE 754 floating point.</p>



<p>So like the numbers wouldn&#8217;t make sense.</p>



<p>Integers and floats are stored differently.</p>



<p>So the hardware is wired differently to operate on two different types of data.</p>



<p>So anyway, I just want to say that this is only for integers and general purpose data.</p>



<p>So now let&#8217;s look at the other types of registers.</p>



<p>rc x and rd x and rsi and rdi are designated as arguments first second third and fourth</p>



<p>okay so let me uh let me write like a quick c plus function here and this is going to be uh let&#8217;s</p>



<p>say we have like a function that returns some value we&#8217;ll call it uh we&#8217;ll call it f and we&#8217;ll</p>



<p>say that f takes in some arguments let&#8217;s say that it takes in three arguments um again uh</p>



<p>integers right now because we&#8217;re only using integer registers.</p>



<p>We can&#8217;t use floating point arguments or return values.</p>



<p>Just keep that in mind.</p>



<p>So I&#8217;m just going to do like three arguments long A and B and C.</p>



<p>OK, so we can take in three arguments and maybe we&#8217;re just going to return,</p>



<p>you know, a plus B plus C.</p>



<p>OK, simple C++ function.</p>



<p>Let&#8217;s do this in assembly.</p>



<p>We&#8217;ll make a label and we&#8217;ll do a little comment that just reminds us.</p>



<p>of sorry of the</p>



<p>Function that we&#8217;re trying to implement we stick a little return instruction at the end of it so that</p>



<p>it will jump back to the caller and</p>



<p>then we&#8217;ll say</p>



<p>Well when the caller of this function</p>



<p>Called us and gave us a and B and C. How do we get a and B and C?</p>



<p>Well a is the first integer argument, so it&#8217;s just going to be the RDI register</p>



<p>So we&#8217;ll literally just use a move instruction</p>



<p>just use a move instruction.</p>



<p>Let&#8217;s say we want to, for whatever reason, use the R12 register</p>



<p>and we&#8217;ll do something with it later.</p>



<p>We&#8217;ll print it, we&#8217;ll save it, we&#8217;ll do whatever.</p>



<p>Let&#8217;s just grab the incoming argument.</p>



<p>So we&#8217;ll just grab RDI.</p>



<p>So I&#8217;m going to put a little comment up above here, you know, grab</p>



<p>a and do something</p>



<p>with it and I&#8217;ll just write down here more instructions just to just to denote</p>



<p>Just to denote that we&#8217;re doing something with the a value, but I&#8217;m not going to write it down here because I don&#8217;t want this to get huge.</p>



<p>So we&#8217;ll do the same thing with the b.</p>



<p>Maybe I should remember that programming is case sensitive.</p>



<p>We&#8217;ll do something with the b.</p>



<p>It&#8217;s not RDI.</p>



<p>It&#8217;s the second argument.</p>



<p>So that&#8217;s actually RSI.</p>



<p>And then we&#8217;ll do something else.</p>



<p>We could have used R13 or other registers if we wanted to.</p>



<p>I&#8217;m just showing you how to grab incoming arguments.</p>



<p>So then we&#8217;ll grab the c and do something with it also.</p>



<p>C and do something with it also it&#8217;s not going to be RDI it&#8217;s going to be RDX and you can do this</p>



<p>up to like I think six arguments let me just double check here yeah so like R9 ends up being</p>



<p>the sixth argument and if you want more than that then the caller will have to put stuff on the stack</p>



<p>and in fact if you if you kind of like understand what&#8217;s happening here let me just do like a main</p>



<p>Let&#8217;s say we do cout whatever f returns.</p>



<p>You know, this is like a typical function call, right?</p>



<p>So we&#8217;ll call f.</p>



<p>f will jump in there.</p>



<p>It&#8217;ll jump into its body.</p>



<p>It&#8217;ll do some sort of a manipulation with the incoming arguments.</p>



<p>It&#8217;ll return a final value.</p>



<p>Then that final value gets printed out.</p>



<p>So what&#8217;s 5 plus 6 plus 7?</p>



<p>That&#8217;s 11 plus 7.</p>



<p>What is that, like 18?</p>



<p>I&#8217;m bad at math.</p>



<p>So let&#8217;s just say that&#8217;s 18 gets printed.</p>



<p>So what happens is, um, we return, Oh, I forgot to return.</p>



<p>I&#8217;m going to say compute the, uh,</p>



<p>compute the sum of the things.</p>



<p>Uh, and I&#8217;ll just say like block cause I don&#8217;t want to put a bunch of addition</p>



<p>instructions here. Uh, and we&#8217;ll just like store in, uh,</p>



<p>in R13, let&#8217;s say. So then when we have finally computed our result, I&#8217;ll just move R13 into the</p>



<p>return value. And what I&#8217;m really trying to say here is if you imagine a function that behaves</p>



<p>this way in C++, then what I&#8217;ve written down below is really what&#8217;s actually happening in</p>



<p>assembly. Your compiler compiles the code down to assembly language first, and then it assembles it</p>



<p>down to machine code. So really this is what&#8217;s happening under the hood. It&#8217;s not like a special</p>



<p>under the hood it&#8217;s not like a special trick when you call a function in C++</p>



<p>literally the A B and C have been loaded up just before the the jump</p>



<p>instruction to go to that you know line 9 they&#8217;ve been loaded up with the</p>



<p>appropriate values so that&#8217;s part of like you know the magic of what higher</p>



<p>level languages give us it&#8217;s just makes things a little bit easier okay so I&#8217;ve</p>



<p>If I had only used RDI and RSI and RDX, those are not designated as callee saved.</p>



<p>So I wouldn&#8217;t have had to preserve those, but because I chose to use R12 and R13, I</p>



<p>have to preserve those for the caller, even if the caller is not going to use them.</p>



<p>So I&#8217;m going to do, I&#8217;m going to do push R12 and I&#8217;m going to do push R13.</p>



<p>And then at the bottom, let me do like a prologue here.</p>



<p>is kind of helpful because it kind of helps you remember like, oh, did I forget the prologue?</p>



<p>Did I forget the epilogue? They got a match. And then I guess the fact that you can see the words</p>



<p>prologue and epilogue also helps remind you that everything is supposed to be in reverse order</p>



<p>in the epilogue. So again, notice how R13 is on the inside and R12 is on the outside</p>



<p>of the push pop pair. So now my program is good. And we&#8217;ve, you know, successfully translated</p>



<p>We&#8217;ve, you know, successfully translated a nice function call there.</p>



<p>And we&#8217;ve done a bunch of nonsense in the middle that I&#8217;m not writing down at this point.</p>



<p>We are respecting the ABI.</p>



<p>So if somebody else uses our function in their module, then they can be pretty confident</p>



<p>that we&#8217;re not going to ruin their registers and so forth.</p>



<p>We talked about the return value.</p>



<p>We talked about that these general purpose registers are not for floats.</p>



<p>We talked about pushes and pops.</p>



<p>and pops. We&#8217;ve talked about function arguments,</p>



<p>which right now we can only do integer arguments and return values.</p>



<p>We&#8217;ve talked about, uh, well, he talked about the stack a little bit.</p>



<p>So let me, let me just point out that, um,</p>



<p>suppose you have a function. I just want to mention this briefly.</p>



<p>Suppose you have a function F let&#8217;s say it&#8217;s, it&#8217;s void.</p>



<p>So it doesn&#8217;t return anything and it has an integer a equals five.</p>



<p>I think the reason that I wanted to do this is to show you how dangerous it is to mess</p>



<p>with the wrong register like the stack register.</p>



<p>So suppose we have two variables and then we&#8217;ll do something with a and b.</p>



<p>So now suppose we have two threads.</p>



<p>We have an execution thread one and an execution thread two.</p>



<p>If you&#8217;re not familiar with threads, that just basically means your computer is literally</p>



<p>executing your function or sorry, it&#8217;s executing twice at the exact same time.</p>



<p>Maybe not the exact same time if you only have a single core on your CPU, but you could</p>



<p>imagine that that&#8217;s possible, especially if you have many cores, that&#8217;s an operating systems</p>



<p>video.</p>



<p>But basically pretend that we have an execution thread.</p>



<p>So it&#8217;s like going through your program, executing one instruction at a time.</p>



<p>Then you launch another execution thread that also executes your program one instruction</p>



<p>at a time.</p>



<p>at a time so at some point it&#8217;s possible that um thread one and thread two could be executing f</p>



<p>at the exact same time right so i&#8217;m not going to talk too much about this because this is not an</p>



<p>os video but basically this could create a race condition where thread one and thread two kind</p>



<p>of step on each other&#8217;s toes and they both try to modify the value of a uh at the wrong time and</p>



<p>because the other thread modified it.</p>



<p>So this is why local variables in a function actually live on the stack.</p>



<p>A and B are not global variables.</p>



<p>They&#8217;re actually temporary local variables that are sitting on the stack.</p>



<p>And the way the stack works, I&#8217;m not going to talk about it too much in this video,</p>



<p>is that, well, every function kind of has its own area of the stack.</p>



<p>So when the function comes in, on the stack sits the return address</p>



<p>return address so that the function knows where to jump back to when it returns and any local</p>



<p>variable that the function makes. So if you think about it, when thread one calls F, we actually</p>



<p>end up with a different version of A and a different version of B. We could imagine them</p>



<p>as being, you know, A sub one and B sub one. But when thread two comes in and, you know,</p>



<p>creates those local variables, we can imagine that we get two different versions of A and B also.</p>



<p>of a and b also so this is how the computer prevents multiple threads from stepping on each</p>



<p>other&#8217;s toes if you&#8217;re using local variables local variables are just local to the function</p>



<p>this also helps if a function calls itself a bunch of times maybe one call or another might want to</p>



<p>see a different version of a or b and it would be just incredibly difficult to keep track of that</p>



<p>if you were using globals but when you use locals well they&#8217;re just basically different variables</p>



<p>trying to lead up to is the stack is pretty important if you corrupt the</p>



<p>stack then you probably are going to crash the entire program you might make</p>



<p>it so that a function doesn&#8217;t know where to return from like when you do the</p>



<p>return statement it might jump to some other part of the code that doesn&#8217;t even</p>



<p>make sense and then everything crashes or you might mess up some data in a local</p>



<p>variable like if you mess up if you mess up the stack you might be messing up the</p>



<p>that the caller is depending on and so then the caller continues to execute and</p>



<p>it sees the wrong value and it just crashes or you might even mess up your</p>



<p>own local variables so don&#8217;t miss with the stack the reason I&#8217;m saying that now</p>



<p>is because this very this register right here the RBP sorry that&#8217;s not it the</p>



<p>RSP register that&#8217;s the stack pointer eventually you can learn how to</p>



<p>manipulate the stack pointer in order to create your own local variables that&#8217;s</p>



<p>variables that&#8217;s okay once you know what you&#8217;re doing but unless you know exactly what you&#8217;re</p>



<p>doing you probably shouldn&#8217;t touch the stack pointer especially you shouldn&#8217;t mess with it</p>



<p>right before you call a function or right before you return from your own function be very careful</p>



<p>with that the rbp uh register is similarly dangerous uh it won&#8217;t like automatically destroy</p>



<p>your program but it is uh it&#8217;s usually i mean it&#8217;s quite often used as sort of like a bookmark for</p>



<p>mark for where the stack pointer was pointing in other modules and other</p>



<p>functions.</p>



<p>So if you fail to preserve the base pointer,</p>



<p>the RBP and then you returned from your function,</p>



<p>then you might have actually messed up the stack pointer for the caller or some</p>



<p>other caller somewhere in like the ancestry of your call graph.</p>



<p>So be very careful. I mean,</p>



<p>I guess all callee preserved variables or registers you should be very careful,</p>



<p>careful about, but these are like kind of the two worst ones to forget about.</p>



<p>first ones to forget about.</p>



<p>There&#8217;s also another register that is not listed on this page called the</p>



<p>instruction pointer. It&#8217;s RIP.</p>



<p>It basically is a register that holds</p>



<p>the memory location of the next instruction to be executed.</p>



<p>So if you modify that, then you just told your program to go execute</p>



<p>in some random crazy place and probably everything is going to crash.</p>



<p>R10 and R11.</p>



<p>I can&#8217;t remember already if I explain this, but I&#8217;ll just say it again.</p>



<p>But I&#8217;ll just say it again, R10 and R11 are just temporary registers.</p>



<p>You don&#8217;t have to preserve them.</p>



<p>But the callee, if you call another function, they don&#8217;t have to preserve the registers either,</p>



<p>which means it can be really fast to use R10 and R12 if you&#8217;re kind of like already running out of registers.</p>



<p>If you don&#8217;t have to make a function call anywhere, so you know no one is going to ruin your R10 and R11,</p>



<p>then you can also use R10 and R11 to sort of like, you know,</p>



<p>reduce your hits to system RAM to speed up your program.</p>



<p>So R10 and R11, they should probably be used last.</p>



<p>R12, 13, 14, 15, I usually use those first.</p>



<p>Once I start running out of those, Kali saved.</p>



<p>Then I&#8217;ll start eating into the arguments.</p>



<p>I&#8217;ll say, you know, I&#8217;m going to start using RBX</p>



<p>and then I&#8217;m going to use RDI and RSI and whatever.</p>



<p>It&#8217;s okay if you use, you know, the argument registers.</p>



<p>as long as you&#8217;ve already done something with your incoming arguments or you don&#8217;t have any.</p>



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



<p>You&#8217;re allowed to use them.</p>



<p>I mean, this is all just a standard.</p>



<p>As long as you obey what the rule is, then you&#8217;ll be all right.</p>



<p>The rule is you got to callee save those.</p>



<p>The rule is that one&#8217;s just temporary.</p>



<p>The rule is this one&#8217;s an argument and so forth.</p>



<p>Okay.</p>



<p>locals and how we use these registers. Let me try to just show you one more</p>



<p>thing real fast. So I said before that the size of the REX register is 64 bits</p>



<p>because it takes up the whole available register bits in the CPU but there are</p>



<p>other versions of this register that we could use. What if you only wanted to use</p>



<p>32 bits in your register? What if you wanted to pack like two different 32</p>



<p>32-bit numbers in the same 64-bit register.</p>



<p>You could do that.</p>



<p>There are also older modules and instructions</p>



<p>that will operate only on 32-bits.</p>



<p>So how do you reference only 32-bits of a register?</p>



<p>Well, if I search for, oh shoot, what was it?</p>



<p>Is it EDI?</p>



<p>No, no.</p>



<p>Shoot.</p>



<p>It&#8217;s in here somewhere.</p>



<p>This is like a good lesson in actually preparing before you record a video.</p>



<p>So I&#8217;m going to do architecture overview, CPU registers.</p>



<p>Oh, it was at the beginning of this section.</p>



<p>I always skip down to 2.4, 12 point something, and I should have gone to 2.3.1.1.</p>



<p>So obviously I&#8217;m not going to remember all of this, but basically notice at the top,</p>



<p>we have the REX register, all 64 bits of it.</p>



<p>But if you wanted to access the lowest six, sorry, the lowest 32 bits, then in your code,</p>



<p>instead of putting REX, you put EAX.</p>



<p>So for example, you know, move REX, whoops, REX, let&#8217;s say the number five, but over</p>



<p>here we&#8217;re going to move EAX, the number five.</p>



<p>five uh here it&#8217;s going to set the whole 64 bits to just like a bunch of zeros and then there&#8217;s</p>



<p>going to be a few bits that help represent five the lowest bits so it&#8217;s going to erase like the</p>



<p>whole thing all 64 bits but if you use the eax instruction then only the lowest 32 bits will</p>



<p>actually be modified which means whatever data you had in the higher 60 the higher 32 bits of the</p>



<p>of the register itself they&#8217;re just going to stay there it&#8217;s going to be considered</p>



<p>point so sometimes if you&#8217;re going to work with something that&#8217;s going to be using only 32 bits</p>



<p>of your register you better make sure that the other bits are uh are cleared out if you intend</p>



<p>to use rax right after that and and vice versa so keep in mind that we have other forms of the same</p>



<p>register a designation but just to let the cpu know that we only want to touch 32 bits worth we</p>



<p>And this, this kind of table covers all of the registers,</p>



<p>REX, RBX, RCX, and so forth.</p>



<p>I think it doesn&#8217;t cover the instruction pointer</p>



<p>and something else in there, but you know, most of them.</p>



<p>However, you know, for my purposes,</p>



<p>I usually just use 64 bits</p>



<p>because I&#8217;m not like super advanced</p>



<p>unless I have to divide or do something.</p>



<p>Okay, so that&#8217;s the last thing I wanted to mention</p>



<p>about the general purpose registers.</p>



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



<p>Callie saved.</p>



<p>So the not sure if I said this before already, but basically the.</p>



<p>So just to emphasize the, uh, the RBP register is the base pointer register.</p>



<p>You&#8217;re probably going to mess up, uh, the caller because the caller often uses RBP as</p>



<p>a bookmark for the stack pointer.</p>



<p>mark for the stack pointer you definitely don&#8217;t want to mess with a stack pointer unless you</p>



<p>actually know what you&#8217;re doing in order to create local variables um there&#8217;s another variable</p>



<p>another register in here called rip which is the instruction pointer register so if you</p>



<p>if you mess that one up then the program is going to start executing in some crazy place that doesn&#8217;t</p>



<p>even make sense okay so now that we&#8217;ve talked about general purpose registers by the way i</p>



<p>called special purpose registers, not general purpose.</p>



<p>General purpose is like RAX, RBX, all these ones that you can just randomly use as long</p>



<p>as you respect the ABI.</p>



<p>But the base pointer and the stack pointer, those are kind of special.</p>



<p>And the RIP, the instruction pointer, that&#8217;s a special register.</p>



<p>You shouldn&#8217;t mess with those unless you really know what you&#8217;re doing or you&#8217;ll get yourself</p>



<p>in trouble.</p>



<p>But now that we&#8217;ve talked about those, let&#8217;s talk about the floating point registers just</p>



<p>real fast.</p>



<p>about using floats until a lot later but basically</p>



<p>i at least want to mention that we do have registers that will work with floating point</p>



<p>operations let me see there we go okay so it&#8217;s 18.2 floating point registers um essentially</p>



<p>we use xmm0 through xmm13 for floating point numbers there&#8217;s not going to be any</p>



<p>not going to be any crazy letters like RDI, RSI that you have to memorize. You can just use zero</p>



<p>through 15 and it&#8217;s totally fine. When you want to return a floating point number from a function,</p>



<p>you will load up XMM0 as the return value. You won&#8217;t even touch RAX. The arguments are the same.</p>



<p>They&#8217;re just kind of ordered. It&#8217;s like the first argument is going to be XMM0. The second argument</p>



<p>is going to be XMM1 and so forth all the way up to XMM15. If you need more than 15 arguments,</p>



<p>some sorcery with the stack or with some kind of system like the global variable or something</p>



<p>and also for floating point registers we use different instructions so what I really want</p>



<p>you to know because this is mostly about the regular registers is that if you have floating</p>



<p>point numbers and you want to multiply them or do some other operation on them the normal</p>



<p>instructions for regular registers won&#8217;t actually work you&#8217;ll have to look up a different set of</p>



<p>and i&#8217;m going to do this in another video some other time uh if you want to move uh data with</p>



<p>a floating point register you can&#8217;t just use the regular move instruction you got to use either</p>



<p>move ss or move sd um and and both operands have to be floats or actually i think the one on the</p>



<p>right can be memory but um move ss basically means a single piece of data single precision</p>



<p>So you can move like multiple pieces of data at a time.</p>



<p>I&#8217;m not going to really cover that.</p>



<p>Um, but you can move single precision floating points, which means there are 32 bit floating</p>



<p>points or you can move double precision floating points, which means they&#8217;re a full 64 bit</p>



<p>floating point number.</p>



<p>There&#8217;s also bigger registers, but I&#8217;m not going to talk about that.</p>



<p>Yeah.</p>



<p>Well, yeah.</p>



<p>Later processors 256.</p>



<p>I think mine has that, but I&#8217;m not going to talk about that.</p>



<p>I think mine are like YMM or something.</p>



<p>So yeah, with that, those are the basics of registers and some stuff to keep in mind.</p>



<p>In future videos, I&#8217;m going to talk more in depth about making functions and calling functions</p>



<p>and all that stuff, respecting the ABI.</p>



<p>But for now, here is your primer.</p>



<p>I hope it was useful on CPU registers.</p>



<p>Thank you for watching this video.</p>



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



<p>I hope you had a little bit of fun and you learned a little bit of stuff.</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 a</p>



<p>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 it would really</p>



<p>mean the world to me and it&#8217;ll help make more videos and grow this community so we&#8217;ll be able</p>



<p>longer videos, better videos, or just I&#8217;ll be able to keep making videos in general.</p>



<p>So please do me a kindness and subscribe. You know sometimes I&#8217;m sleeping in the</p>



<p>middle of the night and I just wake up because I know somebody subscribed or</p>



<p>followed. It just wakes me up and I get filled with joy. That&#8217;s exactly what</p>



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



<p>Just subscribe and then I&#8217;ll just wake up. I promise that&#8217;s what will happen.</p>



<p>Also, if you look at the middle of the screen right now, you should see a QR code which you can scan in order to go to the website</p>



<p>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 all the videos I published and</p>



<p>services and tutorials and things that I offer and all that good stuff and</p>



<p>If you have a suggestion for</p>



<p>Clarifications or errata or just future videos that you want to see</p>



<p>please leave a comment or if you just want to say, Hey, what&#8217;s up, what&#8217;s going on? You know,</p>



<p>just send me a comment, whatever. I also wake up for those in the middle of the night. I get,</p>



<p>I wake up in a cold sweat and I&#8217;m like, it would really, it really mean the world to me. I would</p>



<p>really appreciate it. So again, thank you so much for watching this video and, um, enjoy the cool</p>



<p>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/mastering-x86-64-assembly-a-beginners-guide-to-cpu-registers/">Mastering x86-64 Assembly: A Beginner&#8217;s Guide to CPU Registers</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/mastering-x86-64-assembly-a-beginners-guide-to-cpu-registers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
