<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

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

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>ubuntu assembly programming Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/ubuntu-assembly-programming/</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 class="wp-block-paragraph">In this hands-on x86-64 assembly tutorial, we dive deep into moving integer data between registers and memory, working with pointers, dereferencing, and pointer arithmetic using YASM on Ubuntu.</p>



<p class="wp-block-paragraph">You&#8217;ll see practical examples of:</p>



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">Code and examples are shown live, compiled with YASM, linked with LD, and debugged with GDB.</p>



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



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



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



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



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



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



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



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



<li>Subscribing to our Blog</li>



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



<p class="wp-block-paragraph">Hey everybody! In this video I&#8217;m going to talk to you about integer data movement and pointers</p>



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



<p class="wp-block-paragraph">Okay so what am I talking about for starters? I&#8217;m just talking about moving data pretty much. If you</p>



<p class="wp-block-paragraph">from one integer register to another or from one general purpose register to another.</p>



<p class="wp-block-paragraph">And if you know how to manipulate pointers in assembly and all that stuff,</p>



<p class="wp-block-paragraph">you probably don&#8217;t need this video.</p>



<p class="wp-block-paragraph">But if you are not an expert, then this will probably help you.</p>



<p class="wp-block-paragraph">Let me start off by saying, of course, that there are going to be lots of concepts in this video</p>



<p class="wp-block-paragraph">that I don&#8217;t actually explain because I&#8217;ve explained them in other videos.</p>



<p class="wp-block-paragraph">For example, this is not a Hello World tutorial where I show you how to actually,</p>



<p class="wp-block-paragraph">you know, build a basic Yasm assembly program.</p>



<p class="wp-block-paragraph">a basic yasm assembly program you can probably infer that just from this video but i&#8217;m not going</p>



<p class="wp-block-paragraph">to explain it also this is not a debugging video so i&#8217;m not really going to explain too much about</p>



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



<p class="wp-block-paragraph">get a little preview i will record a full gdb tutorial in another video anyway so let&#8217;s see</p>



<p class="wp-block-paragraph">here uh for starters i&#8217;ve just kind of like created a little sample program here um well</p>



<p class="wp-block-paragraph">a make file which will just compile and execute my program again this is not a</p>



<p class="wp-block-paragraph">make file video tutorial check my other videos for that I&#8217;ve already made one</p>



<p class="wp-block-paragraph">for now I&#8217;m just gonna start making a program and I&#8217;m gonna give it a data</p>



<p class="wp-block-paragraph">section so first thing I&#8217;m gonna give it is like well let&#8217;s say I&#8217;ll give it a</p>



<p class="wp-block-paragraph">bunch of data then I&#8217;ll kind of explain what I gave it again this is not a</p>



<p class="wp-block-paragraph">basics Yasum tutorial so see my other videos but you know just simply put I&#8217;m</p>



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



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



<p class="wp-block-paragraph">We&#8217;re not doing GCC. We&#8217;re just doing LD</p>



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



<p class="wp-block-paragraph">Let me just double check that I don&#8217;t need that in my solution</p>



<p class="wp-block-paragraph">Yeah, I like to reuse these things so we don&#8217;t really need standard input</p>



<p class="wp-block-paragraph">standard input. We&#8217;re just going to do standard output, you know, printing to the terminal.</p>



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



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



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



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



<p class="wp-block-paragraph">sizes but just real fast because these are going to be directly used in our video we uh we have a</p>



<p class="wp-block-paragraph">quad word here so i&#8217;m defining an integer called my long int and i&#8217;m saying that it&#8217;s a quad word</p>



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



<p class="wp-block-paragraph">word which is just a double word so that&#8217;s half of a quad word and then another thing with a whoops</p>



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



<p class="wp-block-paragraph">one one one a word is two bytes of data we&#8217;re starting to get dangerously into overflow territory</p>



<p class="wp-block-paragraph">when i was originally writing this solution i had like a bunch of nines up here and the number that</p>



<p class="wp-block-paragraph">i ended up seeing on the screen was totally different and i forgot oh a word is just two bytes</p>



<p class="wp-block-paragraph">so even if it&#8217;s unsigned the highest thing that it can actually represent is um</p>



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



<p class="wp-block-paragraph">And then I&#8217;m going to make an array of integers. Putting an array, making an array in the regular</p>



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



<p class="wp-block-paragraph">But I&#8217;m just going to specify a small number of integers so I can just put it directly into the</p>



<p class="wp-block-paragraph">BSS is for like giant arrays of uninitialized data and the data section is for whatever you can handle typing pretty much of initialized data.</p>



<p class="wp-block-paragraph">So now I&#8217;m going to start my text section and my program&#8217;s entry point.</p>



<p class="wp-block-paragraph">Again, this is not a Yasm basics tutorial. See my other videos.</p>



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



<p class="wp-block-paragraph">to just you know exit the program let&#8217;s actually see if that works so far it</p>



<p class="wp-block-paragraph">should do nothing but but at least exit with success I&#8217;m gonna go clear and make</p>



<p class="wp-block-paragraph">run and notice how it just kind of like ran it and then nothing happened okay so</p>



<p class="wp-block-paragraph">that&#8217;s great how come it didn&#8217;t print though what did I do wrong oh I forgot</p>



<p class="wp-block-paragraph">to copy paste the code that prints something okay let me get my solution up</p>



<p class="wp-block-paragraph">here so the first thing we&#8217;re really going to do is just use a system call to</p>



<p class="wp-block-paragraph">call to print the word hello again this is not a system call video see my other videos</p>



<p class="wp-block-paragraph">and um so now we&#8217;re just we&#8217;re just going to print our hello string</p>



<p class="wp-block-paragraph">with a system call let me run it one more time we should see like a quick hello</p>



<p class="wp-block-paragraph">yeah okay my name is uh chaplain mondover it&#8217;s not maybe i should change that to it&#8217;s not my name is</p>



<p class="wp-block-paragraph">not no i don&#8217;t want to do that sometimes i like to pretend that my name is chaplain mondover</p>



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



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



<p class="wp-block-paragraph">I&#8217;ll explain it one by one I&#8217;m basically going to use data movement instructions to move a bunch of</p>



<p class="wp-block-paragraph">data into a bunch of registers and then I&#8217;m going to hit a break point or I&#8217;m going to I&#8217;m going to</p>



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



<p class="wp-block-paragraph">the results. Okay so first off I&#8217;m going to move an immediate into R12. The</p>



<p class="wp-block-paragraph">instruction for moving data into general purpose registers or the integer</p>



<p class="wp-block-paragraph">registers is just MOV so that&#8217;s fine probably you should know that by now but</p>



<p class="wp-block-paragraph">I&#8217;m going to move an immediate into R10 and I&#8217;m going to move 12876 into R10.</p>



<p class="wp-block-paragraph">So that&#8217;s how you move an immediate no problem. Then I&#8217;m going to first clear</p>



<p class="wp-block-paragraph">out R11 and then I&#8217;m going to move R10 into it. The reason I&#8217;m clearing out R11</p>



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



<p class="wp-block-paragraph">register into another because if i just i don&#8217;t know if i didn&#8217;t do that you might be tempted to</p>



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



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



<p class="wp-block-paragraph">register or a pointer register from one to another. And by the way, integers, those are</p>



<p class="wp-block-paragraph">pointers, or rather, should I say, pointers are integers. So if you&#8217;re moving an integer or you&#8217;re</p>



<p class="wp-block-paragraph">moving a pointer, the system just kind of sees that as a number either way. So you can use the</p>



<p class="wp-block-paragraph">move instruction for it. So now let&#8217;s use the pointer that was assigned to our long integer</p>



<p class="wp-block-paragraph">to move that number into R12. So let me just go up real fast. My long int up here,</p>



<p class="wp-block-paragraph">my long int up here it&#8217;s like a gigantic number and remember I said before that my long int is</p>



<p class="wp-block-paragraph">actually just a pointer to the memory location that begins to hold the quad word so my long</p>



<p class="wp-block-paragraph">int is really a pointer to one byte and there&#8217;s an eight byte allocation because it&#8217;s a quad word</p>



<p class="wp-block-paragraph">so when you sort of put this into your code and you let the system know that you want to move a</p>



<p class="wp-block-paragraph">then the system will scan eight bytes and interpret that as an integer so</p>



<p class="wp-block-paragraph">putting the the keyword quad word here I don&#8217;t think that&#8217;s necessary I usually</p>



<p class="wp-block-paragraph">do it anyway but you&#8217;ll see in the next few instructions that it&#8217;s a really</p>



<p class="wp-block-paragraph">really good idea to specify the data size because if you don&#8217;t you&#8217;re gonna</p>



<p class="wp-block-paragraph">actually get something wrong in other words if for some reason I moved I don&#8217;t</p>



<p class="wp-block-paragraph">into R12 I could do it if I if I wrote it correctly but then I would be taking</p>



<p class="wp-block-paragraph">part of a quad word and trying to interpret that as the entire number and</p>



<p class="wp-block-paragraph">it&#8217;s probably going to be totally wrong we&#8217;ll have an example for that in a</p>



<p class="wp-block-paragraph">moment but anyway so that&#8217;s how we move a quad word we just specify keyword right</p>



<p class="wp-block-paragraph">before the memory location and we use the brackets to dereference if we</p>



<p class="wp-block-paragraph">didn&#8217;t dereference then we would actually be moving a pointer to the long</p>



<p class="wp-block-paragraph">integer into R12 we&#8217;ll do that soon too I&#8217;m going to try to do everything in this</p>



<p class="wp-block-paragraph">to do everything in this video then we&#8217;ll move a d word from uh from memory into r13 this is a</p>



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



<p class="wp-block-paragraph">the data size that i&#8217;m trying to move into the r13 register so this is telling the system that</p>



<p class="wp-block-paragraph">there&#8217;s about four bytes that i need to scan not eight bytes and interpret those four bytes as an</p>



<p class="wp-block-paragraph">Moving it into R13, you know, I could have sworn a long time ago, you could just move that sort of thing directly into R13.</p>



<p class="wp-block-paragraph">But when I tried my solution before recording this video, I kept getting assembler errors until I added the D at the end.</p>



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



<p class="wp-block-paragraph">Let&#8217;s look at my favorite book.</p>



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



<p class="wp-block-paragraph">What do I mean by that?</p>



<p class="wp-block-paragraph">itself then the system thinks you&#8217;re talking about all 64 bits of the 13 register the r13 register</p>



<p class="wp-block-paragraph">but if you type whoops if you type r13d then the system thinks you&#8217;re only talking about the lowest</p>



<p class="wp-block-paragraph">32 bits of that register so you&#8217;re also able to specify the data size of a register again i thought</p>



<p class="wp-block-paragraph">you should should a long time ago i thought i remembered moving a d word directly into r13 and</p>



<p class="wp-block-paragraph">it would just clear out the extra bits but i got warnings this time maybe that&#8217;s for the best</p>



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



<p class="wp-block-paragraph">let&#8217;s only talk about a D word. So let me go, let me go to my favorite book here.</p>



<p class="wp-block-paragraph">Where the heck is my favorite book? Okay. I guess I have to like, there we go.</p>



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



<p class="wp-block-paragraph">just search for I can&#8217;t remember where it is our 13 D yeah there it is okay so</p>



<p class="wp-block-paragraph">what we&#8217;re looking for is 2.3.1.1 let me show you real fast the top of the book</p>



<p class="wp-block-paragraph">just to give a shout out to the person who wrote this book I did not write this</p>



<p class="wp-block-paragraph">book this is a free and open source book that you can download for free</p>



<p class="wp-block-paragraph">everybody should get a copy to turn yourselves into an expert it&#8217;s called</p>



<p class="wp-block-paragraph">x86 64 assembly language programming with Ubuntu it&#8217;s written by a brilliant</p>



<p class="wp-block-paragraph">professor. This is like a semi recent version. There&#8217;s probably a newer one now, but yeah,</p>



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



<p class="wp-block-paragraph">Was it 2.3.1.1? Yeah. Okay. So in this book section 2.3.1.1, the section entitled general</p>



<p class="wp-block-paragraph">purpose registers, notice how there are a bunch of different versions of each register. So if you</p>



<p class="wp-block-paragraph">So if you look in this column right here, we have, oh, I get to use my annotator.</p>



<p class="wp-block-paragraph">We have a, what were we just looking at?</p>



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



<p class="wp-block-paragraph">Notice how R12 here is a designated as a 64 bit register.</p>



<p class="wp-block-paragraph">But if you wanted to use the R12 register and only use 32 bits of it, the lowest 32</p>



<p class="wp-block-paragraph">bits, then this is the form you would use for the R12 register.</p>



<p class="wp-block-paragraph">So 32 bits.</p>



<p class="wp-block-paragraph">And if you wanted to use only a word&#8217;s worth, you know, 16 bits.</p>



<p class="wp-block-paragraph">Well there you go.</p>



<p class="wp-block-paragraph">two bytes on these systems or 16 bits and then if you just wanted to use one</p>



<p class="wp-block-paragraph">byte of the register well you can you just put a B at the end of it it&#8217;s</p>



<p class="wp-block-paragraph">pretty sweet and convenient it&#8217;s not too hard to remember that D is for D word and</p>



<p class="wp-block-paragraph">W is for word and B is for bytes it just gets a little confusing when you&#8217;re</p>



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



<p class="wp-block-paragraph">version and then AX as its 16-bit version and AL as its 8-bit version if</p>



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



<p class="wp-block-paragraph">know remembering the X and then it changes to an I over here and a P over here I&#8217;m personally not</p>



<p class="wp-block-paragraph">a fan so I always come back to this book to try and remember what I&#8217;m supposed to be putting for</p>



<p class="wp-block-paragraph">these registers because I can&#8217;t remember all the time or rather I can almost never remember but we</p>



<p class="wp-block-paragraph">So, I want to take a D word from memory and load it up into the R13 register.</p>



<p class="wp-block-paragraph">So I got to specify the R13D.</p>



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



<p class="wp-block-paragraph">But I&#8217;m going to now take a D word, do the same thing into R13.</p>



<p class="wp-block-paragraph">Oh, sorry, no.</p>



<p class="wp-block-paragraph">I&#8217;m going to take a regular word and do the same thing basically into R14.</p>



<p class="wp-block-paragraph">But it&#8217;s just going to be a word instead of a D word.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to take a single byte and load it up into R15.</p>



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



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



<p class="wp-block-paragraph">program and sort of inspect the results of your program in your debugger. So there&#8217;s a lot more</p>



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



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



<p class="wp-block-paragraph">Okay, so it ran, it compiled, it was fine.</p>



<p class="wp-block-paragraph">And now I&#8217;m going to open up a new little window and I&#8217;m going to launch the program.</p>



<p class="wp-block-paragraph">Notice how the program that has been compiled is called main.</p>



<p class="wp-block-paragraph">So I&#8217;m going to launch the program into the GDB debugger.</p>



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



<p class="wp-block-paragraph">So I&#8217;m going to be skimming over this, check future videos for a full GDB tutorial.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do a break point right there at line 72.</p>



<p class="wp-block-paragraph">So that I can just kind of inspect the state of the register.</p>



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



<p class="wp-block-paragraph">breakpoints or just a b is fine and it&#8217;s like looks like I set it up correctly then I can type</p>



<p class="wp-block-paragraph">run to see what&#8217;s up notice how it starts to run and immediately stops at that breakpoint</p>



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



<p class="wp-block-paragraph">info r just to print out all my registers and we should see a confirmation of what we were hoping</p>



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



<p class="wp-block-paragraph">so first we moved an immediate into r10 so notice how 12876 is in r10 right there and then on line</p>



<p class="wp-block-paragraph">53 we basically copied r10 into r11 so that means in r11 we see the same value</p>



<p class="wp-block-paragraph">and then we&#8217;ll move a quad word from the long integer we&#8217;ll dereference that pointer</p>



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



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



<p class="wp-block-paragraph">so that&#8217;s this number right here so that seemed to have worked it&#8217;s this number right here the</p>



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



<p class="wp-block-paragraph">one one and there it is right there and then r15 is just gonna take a bite and</p>



<p class="wp-block-paragraph">it&#8217;s gonna take a bite oh now I&#8217;m hungry now I&#8217;m hungry that activated it it&#8217;s</p>



<p class="wp-block-paragraph">only been like think four or five hours since I ate I guess that is usually when</p>



<p class="wp-block-paragraph">I need to eat again even though I gorge and fall asleep on the floor because I</p>



<p class="wp-block-paragraph">home r15 anyway holds the correct value it&#8217;s 222 i just want to show you overflow real fast</p>



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



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



<p class="wp-block-paragraph">in that register so if i use a number that&#8217;s too big it&#8217;ll overflow two bytes the maximum number</p>



<p class="wp-block-paragraph">three, five or from zero to that number or six, five, five, three, six combinations.</p>



<p class="wp-block-paragraph">So if I just stick a nine in front of that right now, that should overflow the system.</p>



<p class="wp-block-paragraph">So let me try it one more time.</p>



<p class="wp-block-paragraph">I want to do Q because that was the last thing we needed to look at.</p>



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



<p class="wp-block-paragraph">Oh gosh, what&#8217;s happening here?</p>



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



<p class="wp-block-paragraph">How about I do clear and make build, which is in my make file and GDB main.</p>



<p class="wp-block-paragraph">So I don&#8217;t have to type all this stuff out again every single time.</p>



<p class="wp-block-paragraph">have to type all this stuff out again every single time what did i just do oh nice okay you know what</p>



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



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



<p class="wp-block-paragraph">program would run and it would overflow eh do you want to turn it off let&#8217;s turn off warnings okay</p>



<p class="wp-block-paragraph">much attention to it. Um, where the heck are the warnings? Where&#8217;s the assembler running? There it</p>



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



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



<p class="wp-block-paragraph">that says, or the flag that says convert warnings into errors. Your compiler is your friend. It was</p>



<p class="wp-block-paragraph">So I am now going to do a breakpoint at 72.</p>



<p class="wp-block-paragraph">So break at main.asm972 and then run and then it breaks.</p>



<p class="wp-block-paragraph">And then I go input registers.</p>



<p class="wp-block-paragraph">Then if I look at, what was I just talking about?</p>



<p class="wp-block-paragraph">R14, was that it?</p>



<p class="wp-block-paragraph">Notice how 44607 for the word, that&#8217;s a totally different value, right?</p>



<p class="wp-block-paragraph">So this is what happens when you overflow or underflow.</p>



<p class="wp-block-paragraph">You got to be careful about your data sizes.</p>



<p class="wp-block-paragraph">I&#8217;m just going to do this again one more time just to make sure.</p>



<p class="wp-block-paragraph">one more time just to make sure that it&#8217;s fixed now that I moved it and I&#8217;m</p>



<p class="wp-block-paragraph">gonna I&#8217;m gonna read that part about warnings because I love converting</p>



<p class="wp-block-paragraph">warnings to errors as just a way to help myself write better code okay so let&#8217;s</p>



<p class="wp-block-paragraph">do that and you know what I&#8217;m gonna do too I added another target here again</p>



<p class="wp-block-paragraph">this is not a Yasm or sorry this is not this is kind of a Yasm video this is not</p>



<p class="wp-block-paragraph">extra file called gdb.txt where I can stick gdb commands and so I don&#8217;t want</p>



<p class="wp-block-paragraph">to continue to type my breakpoints over and over over again every single time so</p>



<p class="wp-block-paragraph">I know for sure I&#8217;m not going to change the program anymore at least I&#8217;m not</p>



<p class="wp-block-paragraph">going to shift the lines up and down before 72 so I&#8217;m going to do breakpoint</p>



<p class="wp-block-paragraph">you can type like break or I think you can type either break or the full word</p>



<p class="wp-block-paragraph">breakpoint but I just type B name of the source code file a.asm at line 72 and so</p>



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



<p class="wp-block-paragraph">So it just kind of saves me typing.</p>



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



<p class="wp-block-paragraph">I can have a little bit more information.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do make build instead of make build and then running GDB.</p>



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



<p class="wp-block-paragraph">That&#8217;s another target in my make file.</p>



<p class="wp-block-paragraph">make file so then you can see it added the breakpoint for me and then it</p>



<p class="wp-block-paragraph">immediately broke on that and you know what yeah I guess I&#8217;ll just leave it in</p>



<p class="wp-block-paragraph">where I have to type info registers okay so we&#8217;re just making sure I think that</p>



<p class="wp-block-paragraph">R14 is still a valid value and that it was fixed okay so we&#8217;ve gotten this far</p>



<p class="wp-block-paragraph">the next thing we need to do is prove that data size matters by specifying a</p>



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



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



<p class="wp-block-paragraph">I&#8217;m going to try to prove to you that the data size matters by specifying a bad data size. I</p>



<p class="wp-block-paragraph">just proved to you that it kind of mattered or it definitely mattered in terms of overflow and</p>



<p class="wp-block-paragraph">underflow, but I&#8217;m trying to prove to you now that it matters when you&#8217;re reading. So I&#8217;m going to</p>



<p class="wp-block-paragraph">I&#8217;m going to clear out r12 and then I&#8217;m going to move a byte from the long integer into r12b</p>



<p class="wp-block-paragraph">and you know this would be a warning or I think it this wouldn&#8217;t compile or wouldn&#8217;t assemble if</p>



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



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



<p class="wp-block-paragraph">awful. And then right after that, I&#8217;m going to do another naughty thing. I&#8217;m going to move</p>



<p class="wp-block-paragraph">something into R15 and I&#8217;m going to word, I&#8217;m going to move a keyword of memory into R15,</p>



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



<p class="wp-block-paragraph">And so the next seven bytes that come after it definitely have nothing to do with that byte,</p>



<p class="wp-block-paragraph">but I&#8217;m going to interpret them all as a quad word. So we should get nonsense data</p>



<p class="wp-block-paragraph">also R15. I don&#8217;t really need to zero out R15 because the fact that I&#8217;m specifying R15 means</p>



<p class="wp-block-paragraph">I want to use all 64 bits. So it is going to definitely zero it out. And whatever we see in</p>



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



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



<p class="wp-block-paragraph">that we are truly getting nonsense. So now there&#8217;s going to be another break point at 82.</p>



<p class="wp-block-paragraph">I&#8217;m not going to break anymore at 72. I&#8217;m going to break at 82. So let&#8217;s do that.</p>



<p class="wp-block-paragraph">What&#8217;s going on here? We got that.</p>



<p class="wp-block-paragraph">Oh gosh. Okay. Quit. I guess that means I need to comment this out. Let me do that.</p>



<p class="wp-block-paragraph">We&#8217;ll do break at main.asm at 82. And you could, you could do both of these two. Actually,</p>



<p class="wp-block-paragraph">let me do it first. We could, we could do multiple break points. Yeah, I&#8217;ll just do it first,</p>



<p class="wp-block-paragraph">So notice how it first breaks at the 72 and then I do C to continue and then suddenly</p>



<p class="wp-block-paragraph">it breaks at the 82.</p>



<p class="wp-block-paragraph">But I don&#8217;t want to have to hit continue a bunch of times so I&#8217;m going to comment that</p>



<p class="wp-block-paragraph">out for the future.</p>



<p class="wp-block-paragraph">Let me quit real fast to make sure I remember what GDB expects as a comment.</p>



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



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



<p class="wp-block-paragraph">Okay, so then I&#8217;m going to print the registers info registers.</p>



<p class="wp-block-paragraph">And we&#8217;re looking at R12 and R15.</p>



<p class="wp-block-paragraph">You can see R12 has the number 144, which definitely doesn&#8217;t make sense because there&#8217;s</p>



<p class="wp-block-paragraph">no 144 anywhere in our original data.</p>



<p class="wp-block-paragraph">in our original data so hopefully that&#8217;s proof to you that we&#8217;ve grabbed some junk</p>



<p class="wp-block-paragraph">sometimes maybe when you&#8217;re doing this at home if you&#8217;re just going to grab one bite</p>



<p class="wp-block-paragraph">maybe accidentally um no no that would definitely still look bad but for the r15</p>



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



<p class="wp-block-paragraph">accidentally correct in a way where it would it would still make it look like the intended value</p>



<p class="wp-block-paragraph">the intended value no it wouldn&#8217;t there&#8217;s no way that would even happen no i guess the junk data</p>



<p class="wp-block-paragraph">could could work that way anyway so we&#8217;ll look at r15 two eight and a bunch of fours and then a two</p>



<p class="wp-block-paragraph">three eight um that value does not exist up here so again junk data mixed in with real data is is</p>



<p class="wp-block-paragraph">bad news okay so now let&#8217;s do another proof i want to prove to you that symbols are pointers</p>



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



<p class="wp-block-paragraph">moving the raw pointer for my long int into r10 and then into r11 we will dereference that</p>



<p class="wp-block-paragraph">pointer so in r10 we should see something that looks like a memory location or at least a relative</p>



<p class="wp-block-paragraph">memory location and then in r11 we should see the actual long integer like we saw before</p>



<p class="wp-block-paragraph">And then I&#8217;m going to mess around with some arrays.</p>



<p class="wp-block-paragraph">So remember up here we made an array.</p>



<p class="wp-block-paragraph">Let me minimize that real fast.</p>



<p class="wp-block-paragraph">We made an array, a very small array.</p>



<p class="wp-block-paragraph">I just specified a bunch of ones and then specified a bunch of twos.</p>



<p class="wp-block-paragraph">So it should be contiguous memory since it&#8217;s been allocated as an array with that comma</p>



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



<p class="wp-block-paragraph">We should see if we looked inside of the system inside of memory, we should see eight bytes</p>



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



<p class="wp-block-paragraph">And then right next to that, we should see eight bytes representing the twos.</p>



<p class="wp-block-paragraph">easy for us to sort of move our pointer around a little bit. So what else do I need to copy paste</p>



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



<p class="wp-block-paragraph">arrays just using some pointer manipulation. So you know how to do this already. You just take</p>



<p class="wp-block-paragraph">like the pointer to some number and you just dereference it and you end up getting a number.</p>



<p class="wp-block-paragraph">I understand your feeling because you might be thinking, wait a minute, wait a minute.</p>



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



<p class="wp-block-paragraph">Why is it we can dereference the array and just get one number still?</p>



<p class="wp-block-paragraph">Well, remember, a pointer to an array is really a pointer to the first item in the array.</p>



<p class="wp-block-paragraph">That&#8217;s kind of how it works.</p>



<p class="wp-block-paragraph">So either way, whether you consider that to be a pointer to one number or a pointer to array,</p>



<p class="wp-block-paragraph">when we dereference it, we&#8217;ll get one number.</p>



<p class="wp-block-paragraph">If you want to get the other numbers, then you have to start manipulating the pointer</p>



<p class="wp-block-paragraph">or derefing in different ways.</p>



<p class="wp-block-paragraph">So here&#8217;s how we can get the second item.</p>



<p class="wp-block-paragraph">We deref the original pointer plus 8.</p>



<p class="wp-block-paragraph">8 because it&#8217;s a quad word.</p>



<p class="wp-block-paragraph">So, you know, it probably would be a little smarter to put that 8 up into a define somewhere</p>



<p class="wp-block-paragraph">so you&#8217;re not hard coding numbers, but I&#8217;m not going to do it.</p>



<p class="wp-block-paragraph">So basically in R12, we should see the 1s.</p>



<p class="wp-block-paragraph">In R13, we should see the 2s.</p>



<p class="wp-block-paragraph">And then a couple more things.</p>



<p class="wp-block-paragraph">Next, we will grab a pointer to the first item in the small array.</p>



<p class="wp-block-paragraph">And we&#8217;ll just stick that into R14.</p>



<p class="wp-block-paragraph">So, you know, we kind of did that with R10 already.</p>



<p class="wp-block-paragraph">But now I just want a pointer to the small array.</p>



<p class="wp-block-paragraph">And I&#8217;m going to keep it in R14 just so you can see the memory location.</p>



<p class="wp-block-paragraph">And then after that, we&#8217;re going to grab an actual pointer to the second item.</p>



<p class="wp-block-paragraph">hey how do you get a pointer to the second item well it&#8217;s that but then what</p>



<p class="wp-block-paragraph">if you didn&#8217;t want to dereference the pointer like what if R13 you know here</p>



<p class="wp-block-paragraph">clearly because we have these brackets for dereferencing you&#8217;re allowed to put</p>



<p class="wp-block-paragraph">the formulas inside of the brackets what if I wanted to get the address to the</p>



<p class="wp-block-paragraph">second item and not actually the second item we would not be allowed to remove</p>



<p class="wp-block-paragraph">the brackets the assembler wouldn&#8217;t like that so without another instruction</p>



<p class="wp-block-paragraph">way to actually get a pointer with a complicated mathematical formula. You know, you can do like</p>



<p class="wp-block-paragraph">multiplication inside of here and other things in parentheses. So we wouldn&#8217;t be able to do that</p>



<p class="wp-block-paragraph">without the other instruction that I&#8217;m introducing now called LEA. LEA just means, yeah, sure, we&#8217;re</p>



<p class="wp-block-paragraph">still going to use the brackets to figure out where the pointer is, like figure out what memory</p>



<p class="wp-block-paragraph">location we want with some sort of a formula. But then what will be stored in the first operand is</p>



<p class="wp-block-paragraph">This LEA won&#8217;t dereference just because there are brackets.</p>



<p class="wp-block-paragraph">LEA will keep the pointer.</p>



<p class="wp-block-paragraph">And then finally, once we actually have a pointer to the second item in R15,</p>



<p class="wp-block-paragraph">then we can move the second item into another register.</p>



<p class="wp-block-paragraph">And this is like an LOL because I ran out of registers already.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to use RDI, which is usually the first argument.</p>



<p class="wp-block-paragraph">It doesn&#8217;t matter. It&#8217;s fine.</p>



<p class="wp-block-paragraph">So if we dereference R15, it&#8217;s the same thing as dereferencing this.</p>



<p class="wp-block-paragraph">We could have also just dereferenced R14,</p>



<p class="wp-block-paragraph">and it would have been the same thing as just getting the value of the first item.</p>



<p class="wp-block-paragraph">But I hope you can see now that, you know,</p>



<p class="wp-block-paragraph">that expression is going to be a pointer to the second item in the array,</p>



<p class="wp-block-paragraph">and that&#8217;s going to be stuck into R15.</p>



<p class="wp-block-paragraph">So if we dereference that, we can treat R15 like a pointer.</p>



<p class="wp-block-paragraph">It&#8217;s just, you know, it&#8217;s a general purpose register.</p>



<p class="wp-block-paragraph">it doesn&#8217;t only need to hold integer values it can also hold pointers because pointers are integers</p>



<p class="wp-block-paragraph">they&#8217;re just unsigned so we deref the pointer that is stored inside of r15 now put it inside</p>



<p class="wp-block-paragraph">of rdi and then we should see that rdi actually holds the second item which should be a bunch of</p>



<p class="wp-block-paragraph">twos so the last thing that i&#8217;m going to copy paste in here is another nope so i&#8217;m going to</p>



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



<p class="wp-block-paragraph">open up this right here and I&#8217;m going to comment that out and I&#8217;m gonna say break at 108.</p>



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



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



<p class="wp-block-paragraph">There we go.</p>



<p class="wp-block-paragraph">But, but, but quitsies.</p>



<p class="wp-block-paragraph">Now I&#8217;m just going to run make debug again so that I can enter GDB.</p>



<p class="wp-block-paragraph">And we&#8217;ve, we&#8217;ve, we&#8217;ve hit our break points.</p>



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



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



<p class="wp-block-paragraph">So the first thing I think this is all on one page, right?</p>



<p class="wp-block-paragraph">I don&#8217;t need to scroll.</p>



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



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



<p class="wp-block-paragraph">holds this value right here that&#8217;s a pointer to the my long int that kind of looks like a pointer</p>



<p class="wp-block-paragraph">if you consider that it could be relative and then r11 holds the actual integer and if you recall</p>



<p class="wp-block-paragraph">that is the gigantic long that we put in there great so now that&#8217;s the difference between a</p>



<p class="wp-block-paragraph">pointer and its value or in its dereference value so then we&#8217;re going to grab the first item in the</p>



<p class="wp-block-paragraph">So then we&#8217;re going to grab the first item in the small array.</p>



<p class="wp-block-paragraph">So R12 should have the ones.</p>



<p class="wp-block-paragraph">If we look at R12 here, it&#8217;s got the ones.</p>



<p class="wp-block-paragraph">Again, just trying to prove to you that if you dereference a pointer to an array,</p>



<p class="wp-block-paragraph">you&#8217;re actually dereferencing a pointer to the first item.</p>



<p class="wp-block-paragraph">And then on R13, we&#8217;re going to make a pointer to the second item.</p>



<p class="wp-block-paragraph">Then with the brackets and the move instruction, we dereference that and it should become the twos.</p>



<p class="wp-block-paragraph">So if we look at R13, notice how it&#8217;s the twos.</p>



<p class="wp-block-paragraph">Then we&#8217;re going to grab a pointer to the first item in the small array.</p>



<p class="wp-block-paragraph">Hang on a second.</p>



<p class="wp-block-paragraph">What did I want to say about that?</p>



<p class="wp-block-paragraph">In the small array to R14.</p>



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



<p class="wp-block-paragraph">So again, we&#8217;re just going to grab an actual pointer to the small array.</p>



<p class="wp-block-paragraph">And so the difference between R12 and R14 is the dereft value or the pointer.</p>



<p class="wp-block-paragraph">So if we look at R12, that&#8217;s the actual value that we&#8217;re pointing at.</p>



<p class="wp-block-paragraph">And then R14 is the memory location that we have in there.</p>



<p class="wp-block-paragraph">And then notice how it&#8217;s kind of similar.</p>



<p class="wp-block-paragraph">Notice how it&#8217;s kind of similar.</p>



<p class="wp-block-paragraph">Maybe this proves a little bit more that these are pointers that we&#8217;re looking at.</p>



<p class="wp-block-paragraph">R10 was supposed to be a pointer and now R14 is also a pointer.</p>



<p class="wp-block-paragraph">Notice how those numbers are pretty close together.</p>



<p class="wp-block-paragraph">I&#8217;m sure if we looked back up in the data section and we calculated an offset based</p>



<p class="wp-block-paragraph">on how big each data item was, we could probably predict what the memory location of R14 should</p>



<p class="wp-block-paragraph">have been based on R10 because you can see it&#8217;s like only increased by a little bit,</p>



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



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



<p class="wp-block-paragraph">can just compute the offset and then we&#8217;ll grab a pointer to the second item using the LEA</p>



<p class="wp-block-paragraph">instruction remember we have to use brackets if we&#8217;re going to do an expression but then the</p>



<p class="wp-block-paragraph">dereferencing in the move instruction so we just use LEA to not dereference.</p>



<p class="wp-block-paragraph">So we should see that R15 is about 8 bytes further along in RAM than R14 because both of those items</p>



<p class="wp-block-paragraph">are contiguous in memory and they should only be about you know a quad word apart 8 bytes.</p>



<p class="wp-block-paragraph">So if we look at R14 it&#8217;s got this number so it&#8217;s a pointer and then if we look at R15</p>



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



<p class="wp-block-paragraph">pointer to the second item is only eight bytes further in memory yay contiguousness and then</p>



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



<p class="wp-block-paragraph">now a pointer what was it it was the it was a pointer to the second item that should be a bunch</p>



<p class="wp-block-paragraph">of twos right because remember in our array we had a bunch of twos here as our second item</p>



<p class="wp-block-paragraph">have a bunch of twos inside of it so if i look at rdi where the heck is that right there has a</p>



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



<p class="wp-block-paragraph">instruction and i think that&#8217;s everything that i wanted to talk to you about in this video so now</p>



<p class="wp-block-paragraph">i hope you feel like you&#8217;re an expert in moving integers and moving pointers and</p>



<p class="wp-block-paragraph">dereferencing pointers and manipulating pointers so you can get a pointer to something else and</p>



<p class="wp-block-paragraph">pointer to something else and de-referencing those pointers or not whenever you choose.</p>



<p class="wp-block-paragraph">Thank you so much for watching this video. I hope you learned a little bit of stuff</p>



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">keep making videos in general so please do do me a kindness and and subscribe</p>



<p class="wp-block-paragraph">you know sometimes I&#8217;m sleeping in the middle of the night and I just wake up</p>



<p class="wp-block-paragraph">night and I just wake up because I know somebody subscribed or followed it just</p>



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



<p class="wp-block-paragraph">time so you could do it as a nice favor to me or you could you control me if you</p>



<p class="wp-block-paragraph">want to just wake me up in the middle of night just subscribe and then I&#8217;ll just</p>



<p class="wp-block-paragraph">wake up I promise that&#8217;s what will happen also if you look at the middle of</p>



<p class="wp-block-paragraph">the screen right now you should see a QR code which you can scan in order to go</p>



<p class="wp-block-paragraph">to the website which I think is also named somewhere at the bottom of this</p>



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



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



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



<p class="wp-block-paragraph">for uh uh clarifications or errata or just future videos that you want to see please leave a comment</p>



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



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



<p class="wp-block-paragraph">It would really mean the world to me.</p>



<p class="wp-block-paragraph">I would really appreciate it.</p>



<p class="wp-block-paragraph">So again, thank you so much for watching this video.</p>



<p class="wp-block-paragraph">And enjoy the cool music as I fade into the darkness, which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-integer-data-movement-pointers-and-dereferencing-explained-yasm-on-ubuntu/">x86-64 Assembly: Integer Data Movement, Pointers, and Dereferencing Explained (YASM on Ubuntu)</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/x86-64-assembly-integer-data-movement-pointers-and-dereferencing-explained-yasm-on-ubuntu/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Signed Integer Division in x86-64 Assembly with IDIV and CQO</title>
		<link>https://www.NeuralLantern.com/signed-integer-division-in-x86-64-assembly-with-idiv-and-cqo/</link>
					<comments>https://www.NeuralLantern.com/signed-integer-division-in-x86-64-assembly-with-idiv-and-cqo/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 14 Dec 2025 09:12:38 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[64-bit assembly]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[assembly language programming]]></category>
		<category><![CDATA[assembly tutorial]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[cqo instruction]]></category>
		<category><![CDATA[idiv instruction]]></category>
		<category><![CDATA[integer division assembly]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[rax rdx division]]></category>
		<category><![CDATA[signed division]]></category>
		<category><![CDATA[ubuntu assembly programming]]></category>
		<category><![CDATA[x86 assembly]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=266</guid>

					<description><![CDATA[<p>Step-by-step tutorial on performing signed integer division in x86-64 assembly language using the IDIV instruction and CQO for sign extension. Includes complete working Yasm code on Ubuntu/Linux.</p>
<p>The post <a href="https://www.NeuralLantern.com/signed-integer-division-in-x86-64-assembly-with-idiv-and-cqo/">Signed Integer Division in x86-64 Assembly with IDIV and CQO</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="Signed Integer Division in x86-64 Assembly with IDIV and CQO" width="1380" height="776" src="https://www.youtube.com/embed/axasPyjJFAA?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">In this video, I walk through how to perform signed integer division in x86-64 assembly language using Yasm on Ubuntu/Linux.</p>



<p class="wp-block-paragraph">We focus on 64-bit signed integers and the IDIV instruction. I explain the setup: loading the dividend into RAX, using CQO for proper sign extension into RDX, loading the divisor, and executing IDIV. After division, the quotient ends up in RAX and the remainder in RDX.</p>



<p class="wp-block-paragraph">I reference the excellent free book &#8220;x86-64 Assembly Language Programming with Ubuntu&#8221; and show practical code examples, including a complete working program that prints both the quotient and remainder.</p>



<p class="wp-block-paragraph">We test with small numbers (256 / 233) and larger ones to see it in action. I also touch on the difference between signed (IDIV) and unsigned (DIV) division, and why CQO is needed for correct sign handling.</p>



<p class="wp-block-paragraph">Perfect for anyone learning low-level programming, assembly language, or wanting to understand how integer division really works under the hood.</p>



<p class="wp-block-paragraph">Introduction to Signed Division 00:00:00<br>Referencing the Assembly Book 00:00:28<br>IDIV Instruction Overview 00:01:16<br>Dividend and Divisor Setup 00:01:24<br>Sign Extension and CQO 00:04:18<br>Quotient and Remainder Results 00:04:40<br>Setting Up the Program 00:05:13<br>Data Section and Strings 00:06:22<br>Function Prologue and Preservation 00:09:20<br>Loading the Dividend into RAX 00:10:00<br>Sign Extension with CQO 00:11:24<br>Loading the Divisor 00:12:36<br>Performing IDIV 00:14:19<br>Saving Quotient and Remainder 00:16:48<br>Printing the Results 00:17:20<br>Running and Testing the Program 00:18:51<br>Testing Larger Numbers 00:19:48<br>Returning the Remainder 00:21:42<br>Signed vs Unsigned Division 00:23:48<br>Conclusion and Thanks 00:25:02</p>



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



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



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



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



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



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



<li>Subscribing to our Blog</li>



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



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



<p class="wp-block-paragraph">In this video, I&#8217;d like to talk to you about dividing signed integers in an x86-64 Yasm</p>



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



<p class="wp-block-paragraph">I&#8217;m going to be focusing on 64-bit integers and I&#8217;m going to use an instruction called</p>



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



<p class="wp-block-paragraph">Okay, so for starters, I just want to give you an overview of the process.</p>



<p class="wp-block-paragraph">I&#8217;m going to use an awesome book that I love to reference.</p>



<p class="wp-block-paragraph">awesome book that i love to reference i did not write this book the book is titled x86 64 assembly</p>



<p class="wp-block-paragraph">language programming with ubuntu i mention it in a lot of other videos the author here</p>



<p class="wp-block-paragraph">is a genius and this book is open source and freely available so go look it up get yourself a</p>



<p class="wp-block-paragraph">copy and become an assembly expert too anyway so what i&#8217;m going to do is go into this book</p>



<p class="wp-block-paragraph">instruction set overview and then inside of that i&#8217;m going to click on 7.5 integer arithmetic</p>



<p class="wp-block-paragraph">instructions or arithmetic i don&#8217;t know whatever you prefer to say it as but and then i&#8217;m going to</p>



<p class="wp-block-paragraph">go inside of there and i&#8217;m going to click on 754 integer division so you know the basic idea for</p>



<p class="wp-block-paragraph">dividing integers in a yasm assembly program on x86 64 is just to load up your dividend you know</p>



<p class="wp-block-paragraph">top portion I guess that is the numerator and then the divisor which</p>



<p class="wp-block-paragraph">would be the denominator with separate registers so load up the dividend with a</p>



<p class="wp-block-paragraph">register or put the dividend into a register and then put the divisor into</p>



<p class="wp-block-paragraph">another register they&#8217;re going to be special registers we&#8217;re going to be</p>



<p class="wp-block-paragraph">using the A and D registers you can kind of see that at the bottom already and</p>



<p class="wp-block-paragraph">then once that&#8217;s set up properly you just sort of use the IDIV function or</p>



<p class="wp-block-paragraph">instruction and there&#8217;s another instruction we can use to make things a</p>



<p class="wp-block-paragraph">little bit more simple because if you kind of notice what what this is saying</p>



<p class="wp-block-paragraph">here it&#8217;s saying that the the dividend usually needs to be stacked between two</p>



<p class="wp-block-paragraph">other registers at the same time and that&#8217;s a little bit confusing</p>



<p class="wp-block-paragraph">especially if these registers right here notice how a DX colon ax that means it</p>



<p class="wp-block-paragraph">16-bit versions of two different registers just sort of like pushed up against each other so</p>



<p class="wp-block-paragraph">Obviously if you know how to manipulate bits which I&#8217;m gonna do other videos for in the future</p>



<p class="wp-block-paragraph">Then you could do that with any size, but just I&#8217;m gonna make the easiest example that I can here. I</p>



<p class="wp-block-paragraph">Love this line. I just found it today</p>



<p class="wp-block-paragraph">As always division by zero will crash the program and damage the space-time continuum. So try not to divide by zero. I love it</p>



<p class="wp-block-paragraph">nifty little table inside of the book that sort of shows you how you&#8217;re supposed to set up the</p>



<p class="wp-block-paragraph">numerator and denominator or the divisor what was the other word that they used dividend and divisor</p>



<p class="wp-block-paragraph">i usually say numerator and denominator anyway so remember in in if you watched my previous videos</p>



<p class="wp-block-paragraph">then you&#8217;ll know that we have smaller versions of our registers like for example uh this eax right</p>



<p class="wp-block-paragraph">here that&#8217;s actually the rax register but it&#8217;s only about half of it uh half of those bytes are</p>



<p class="wp-block-paragraph">half of those bytes are being ignored so all of these different versions that you&#8217;re seeing are</p>



<p class="wp-block-paragraph">just different sizes of the a and d registers so i&#8217;m going to go to the simplest example here and</p>



<p class="wp-block-paragraph">just kind of show you that basically what they&#8217;re trying to do is force you to have a numerator that</p>



<p class="wp-block-paragraph">is bigger than the denominator that way it&#8217;s guaranteed you&#8217;ll have some kind of a quotient</p>



<p class="wp-block-paragraph">or you know more likely that you&#8217;ll have some kind of a quotient and and a remainder that kind of</p>



<p class="wp-block-paragraph">that kind of makes sense because if you divide a number that&#8217;s much smaller than</p>



<p class="wp-block-paragraph">the denominator then you&#8217;ll just end up with zero remainder the original number</p>



<p class="wp-block-paragraph">pretty much in integer division this video is not about float division float</p>



<p class="wp-block-paragraph">division is more precise so in other words if I want to divide something by a</p>



<p class="wp-block-paragraph">64-bit integer then I need to first set up a 128-bit integer and that means I</p>



<p class="wp-block-paragraph">and that means I have to take two 64-bit integers, the D register and the A register.</p>



<p class="wp-block-paragraph">I have to load both of those registers up with 64 bits each so that when you imagine them being combined,</p>



<p class="wp-block-paragraph">it&#8217;s a 128-bit register or 128-bit value.</p>



<p class="wp-block-paragraph">Once I have something that is sufficiently large, then I can divide it by something else.</p>



<p class="wp-block-paragraph">Then the result will show up in the A and D registers.</p>



<p class="wp-block-paragraph">the actual quotient, you know, the actual result of dividing without a fractional part at the end,</p>



<p class="wp-block-paragraph">just, you know, truncated data. Then the remainder will be in the D register, the RDX register. So at</p>



<p class="wp-block-paragraph">least for this form, I just want you to know you can do it with all these other forms if you want</p>



<p class="wp-block-paragraph">to. But in this example, I&#8217;m just going to take 128 bits, divide it by 64 bits, and then I&#8217;ll get</p>



<p class="wp-block-paragraph">So that&#8217;s the basic idea. Here&#8217;s some sample code in the book. I love it. It&#8217;s wonderful.</p>



<p class="wp-block-paragraph">But I&#8217;m going to go ahead and start writing my own program now.</p>



<p class="wp-block-paragraph">Okay, so I got a solution up here. I&#8217;m going to just copy paste my data section and explain it briefly.</p>



<p class="wp-block-paragraph">Okay, so here is, oh, maybe I should first say, here&#8217;s a sample program that I&#8217;m using.</p>



<p class="wp-block-paragraph">You don&#8217;t have to worry too much about what&#8217;s actually inside of it.</p>



<p class="wp-block-paragraph">The C module and the make file, they&#8217;re standard stuff that I usually use in all of my videos.</p>



<p class="wp-block-paragraph">all of my videos this is not a make file video see my other videos if you want to learn how to</p>



<p class="wp-block-paragraph">make your own make files but long story short i&#8217;m just going to type you know make run in order to</p>



<p class="wp-block-paragraph">just get this program to run and then for the c program i&#8217;m compiling a hybrid program so i mean</p>



<p class="wp-block-paragraph">it&#8217;s just easier for me to just kind of get started this way so i have my main entry point sitting</p>



<p class="wp-block-paragraph">inside of a c source code file that gets compiled and linked into my main program and it just calls</p>



<p class="wp-block-paragraph">a function called math right here which is just going to be you know this source code right here</p>



<p class="wp-block-paragraph">so if you want to make hybrid programs you want to learn the basics of assembly you want to learn</p>



<p class="wp-block-paragraph">anything that i&#8217;m not really talking about in this video see my other videos</p>



<p class="wp-block-paragraph">for today or for this video it&#8217;s just going to be integer division anyway i&#8217;m going to paste in</p>



<p class="wp-block-paragraph">some starter code here for my math module i&#8217;ve got a data section and i&#8217;ve got some strings</p>



<p class="wp-block-paragraph">strings or anything but just keep in mind I&#8217;m going to eventually print the division result</p>



<p class="wp-block-paragraph">quotient is and then I&#8217;m going to actually print it and then I&#8217;m going to print the division</p>



<p class="wp-block-paragraph">remainder is and then actually print it so I&#8217;m just I&#8217;m just setting up strings to make my</p>



<p class="wp-block-paragraph">results look a little bit prettier that&#8217;s all I&#8217;m really doing crlf that&#8217;s just new line stuff</p>



<p class="wp-block-paragraph">system calls here&#8217;s the system service code for writing to something writing to a file or standard</p>



<p class="wp-block-paragraph">standard output again that&#8217;s in another video file descriptor one another video and then return</p>



<p class="wp-block-paragraph">value I&#8217;m just choosing to return the number seven from this module I don&#8217;t know why I just</p>



<p class="wp-block-paragraph">I like to reuse code and I&#8217;m not going to change it and then we&#8217;re going to take two different</p>



<p class="wp-block-paragraph">integers here and uh 56 and oh I think in my starter code I actually hard-coded those numbers</p>



<p class="wp-block-paragraph">after everything is working so um i&#8217;m going to start my text section here by just copy pasting</p>



<p class="wp-block-paragraph">a little thing that says okay here&#8217;s where my code is going to go now all my instructions</p>



<p class="wp-block-paragraph">and then uh here&#8217;s my entry point which is just my function called math you want to write functions</p>



<p class="wp-block-paragraph">see my other videos but this is basically it you just kind of have something enter and then return</p>



<p class="wp-block-paragraph">from it when you&#8217;re done and if you mess up any registers you have to preserve them with push and</p>



<p class="wp-block-paragraph">you have to preserve them with push and pop pairs.</p>



<p class="wp-block-paragraph">Another thing that I&#8217;m going to add that I&#8217;m not going to explain in this video</p>



<p class="wp-block-paragraph">is I&#8217;m going to do calls to one of my own personal libraries.</p>



<p class="wp-block-paragraph">And basically I&#8217;m just going to mark two functions as external.</p>



<p class="wp-block-paragraph">They&#8217;re sitting in a shared object that I made.</p>



<p class="wp-block-paragraph">Don&#8217;t worry about that. This video is not about printing.</p>



<p class="wp-block-paragraph">It&#8217;s just about dividing. But I want to be able to easily print.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to use that library.</p>



<p class="wp-block-paragraph">So don&#8217;t worry about that.</p>



<p class="wp-block-paragraph">for this video is my function called divide test because notice how in the entry point of math</p>



<p class="wp-block-paragraph">remember the c module the driver it calls on this math function the math function eventually or</p>



<p class="wp-block-paragraph">immediately calls on divide test and so here&#8217;s the prototype basically for my divide test function</p>



<p class="wp-block-paragraph">I&#8217;m just going to say it&#8217;s void takes no arguments that way it can just do everything by itself</p>



<p class="wp-block-paragraph">as a little demo and it&#8217;s a little bit easier so that means I don&#8217;t really need to preserve any</p>



<p class="wp-block-paragraph">I am using them and if you see here well I guess if it was going to take arguments you don&#8217;t need</p>



<p class="wp-block-paragraph">to preserve the argument register so I should have just said yeah we are going to use some</p>



<p class="wp-block-paragraph">registers and therefore we&#8217;re going to preserve them so our 12 14 and 15 oh why am I not using</p>



<p class="wp-block-paragraph">13 I must have erased that at an earlier date whatever we&#8217;re going to use these three registers</p>



<p class="wp-block-paragraph">make this an official function and because I know I&#8217;m going to use r12 14 and 15 I&#8217;m just going to</p>



<p class="wp-block-paragraph">preserve them with a push pop pair so I&#8217;m going to say this is the prologue whoops and I&#8217;m going to</p>



<p class="wp-block-paragraph">push r12 push r14 push r15 respect the abi if you don&#8217;t other pro other parts of the program</p>



<p class="wp-block-paragraph">will probably crash epilogue is just popping all those in reverse order so that I can basically</p>



<p class="wp-block-paragraph">basically preserve them for the caller.</p>



<p class="wp-block-paragraph">Okay, so now we&#8217;re good with all our registers.</p>



<p class="wp-block-paragraph">Next thing I want to do is I want to load up, let&#8217;s see,</p>



<p class="wp-block-paragraph">I want to load up RAX with a number.</p>



<p class="wp-block-paragraph">Now maybe I&#8217;ll fix this right now.</p>



<p class="wp-block-paragraph">What was 256?</p>



<p class="wp-block-paragraph">It was the my integer B.</p>



<p class="wp-block-paragraph">So I&#8217;m going to hit main memory,</p>



<p class="wp-block-paragraph">which is a little slower than using an immediate,</p>



<p class="wp-block-paragraph">but hey, it&#8217;s more flexible.</p>



<p class="wp-block-paragraph">but hey it&#8217;s more flexible and so basically i&#8217;m going to set up rax which is the numerator if we</p>



<p class="wp-block-paragraph">go back to the book real fast if our intention is to divide a 128 bit uh uh like combined register</p>



<p class="wp-block-paragraph">by a 64 bit something then uh well rax is the lower 64 bit so that&#8217;s why i&#8217;m loading that up</p>



<p class="wp-block-paragraph">first i&#8217;m just saying well i&#8217;ve got 64 bits worth of stuff a quad word worth of stuff sitting in</p>



<p class="wp-block-paragraph">so i&#8217;m just going to load that into rax then i just have to somehow take care of rdx so obviously</p>



<p class="wp-block-paragraph">if you wanted to you could just you know take rdx and just set a bunch of zeros to it but that</p>



<p class="wp-block-paragraph">would probably only work if you were guaranteed that rax was a positive number so that its sign</p>



<p class="wp-block-paragraph">bit was zero at the very left if it was a negative number its sign bit would be a one at the very</p>



<p class="wp-block-paragraph">And that wouldn&#8217;t be very good if RDX plus RAX, you know, lined up are supposed to be a negative number because you would want ones all the way through to RDX.</p>



<p class="wp-block-paragraph">So there&#8217;s a special instruction we can use called CQO.</p>



<p class="wp-block-paragraph">I&#8217;m just going to paste that in here just to make things a little bit easier because I&#8217;m really trying to divide a 64-bit integer by something.</p>



<p class="wp-block-paragraph">The system wants a 128-bit integer.</p>



<p class="wp-block-paragraph">basically this instruction right here CQO whoops what happened how come it&#8217;s not lined up</p>



<p class="wp-block-paragraph">CQO basically just takes RAX whatever is in there and stretches it onto the 128 bit combination of</p>



<p class="wp-block-paragraph">RDX colon RAX so remember when you see this kind of thing remember when you see RDX colon RAX or</p>



<p class="wp-block-paragraph">just like a register colon another register that&#8217;s usually telling you that the system will think of</p>



<p class="wp-block-paragraph">in a combined way as one longer register so that&#8217;s really what it is cqo is going to say</p>



<p class="wp-block-paragraph">let&#8217;s look at rex and let&#8217;s uh you know fix it up and then also fix up uh rdx so that when they&#8217;re</p>



<p class="wp-block-paragraph">combined they make sense as just one number um and you know that that would work pretty easily</p>



<p class="wp-block-paragraph">like i said before if it was a positive number but not necessarily in all cases so we&#8217;ll just use</p>



<p class="wp-block-paragraph">is, let&#8217;s see, set up the denominator. So let&#8217;s see, here, I&#8217;m going to say set up denominator.</p>



<p class="wp-block-paragraph">And you know, we can hard code the 233 there, but I think I&#8217;ve already decided,</p>



<p class="wp-block-paragraph">let&#8217;s just use variables. So the A, I think has 233. Where the heck&#8217;s my A? There we go.</p>



<p class="wp-block-paragraph">So I&#8217;m going to say my integer A. And here I&#8217;m going to put the pointer to the A variable,</p>



<p class="wp-block-paragraph">to the a variable and then i&#8217;m going to dereference it and um oh one thing that we have to remember is</p>



<p class="wp-block-paragraph">when the system is using your registers like rax here it knows rax is the 64-bit form of the a</p>



<p class="wp-block-paragraph">register so if you use different forms of your register then it knows how many bits you want</p>



<p class="wp-block-paragraph">like for example here we just kind of go back up uh this eax that&#8217;s still the a register but the e</p>



<p class="wp-block-paragraph">that it&#8217;s a you know 32 bits instead of 64 bits but there&#8217;s no there&#8217;s no way for</p>



<p class="wp-block-paragraph">the system to really know that that memory location is 64 bits how does it</p>



<p class="wp-block-paragraph">how does it know if I&#8217;m if I&#8217;m trying to load from memory a one byte integer or</p>



<p class="wp-block-paragraph">two byte integer or four bytes or eight bytes it doesn&#8217;t really know so I kind</p>



<p class="wp-block-paragraph">of have to specify what the data size is of the integer at that place in memory</p>



<p class="wp-block-paragraph">this probably wouldn&#8217;t even compile. But if it did compile, you probably should be nervous that</p>



<p class="wp-block-paragraph">it would compile and do something wrong. So I&#8217;m just going to specify exactly what the data size</p>



<p class="wp-block-paragraph">is for that. So now we&#8217;ve set up our numerator and also set up our denominator. And the next step is</p>



<p class="wp-block-paragraph">to just actually divide. Let me copy paste that real fast. So now we actually divide. Why is my</p>



<p class="wp-block-paragraph">Oh, I know what&#8217;s going on.</p>



<p class="wp-block-paragraph">I have the tab size set up differently between my two editors.</p>



<p class="wp-block-paragraph">I got to fix that.</p>



<p class="wp-block-paragraph">Or how about never fix it so I can always complain and you can laugh at me.</p>



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



<p class="wp-block-paragraph">So on line 85, we&#8217;re going to divide the numerator by the denominator.</p>



<p class="wp-block-paragraph">And we&#8217;ll say the numerator is, you know, RAX with RDX in front of it.</p>



<p class="wp-block-paragraph">the denominator is just one register which is uh what have I done wrong here oh no no I&#8217;m okay I&#8217;m</p>



<p class="wp-block-paragraph">okay r12 so if you look back at the I got a little confused here because uh the book you know it</p>



<p class="wp-block-paragraph">wants uh the d and the a registers as the numerator but then it says op64 as the denominator you can</p>



<p class="wp-block-paragraph">just kind of use a bunch of different stuff for that but I&#8217;m choosing to divide by another register</p>



<p class="wp-block-paragraph">So basically I&#8217;m taking the denominator and loading it up into R12.</p>



<p class="wp-block-paragraph">And then when I call my idiv instruction, I&#8217;m just saying integer division.</p>



<p class="wp-block-paragraph">And I want you to divide whatever&#8217;s in the RDX, RAX set as I want you to divide it by R12.</p>



<p class="wp-block-paragraph">At that point, RAX and RDX will be overwritten.</p>



<p class="wp-block-paragraph">So that data is now destroyed, but they contain the answer.</p>



<p class="wp-block-paragraph">So RAX now contains the answer.</p>



<p class="wp-block-paragraph">I&#8217;m just gonna say quotient and the RAX register contains the remainder so for</p>



<p class="wp-block-paragraph">example if you are familiar with the let me pull this up real fast is that the</p>



<p class="wp-block-paragraph">calculator no there is if you&#8217;re familiar with the modulo instruction like if we</p>



<p class="wp-block-paragraph">said let&#8217;s see what was it again 233 okay so let&#8217;s do 256 divided by 233 this</p>



<p class="wp-block-paragraph">this gives us the number one and then some sort of like a fractional remainder</p>



<p class="wp-block-paragraph">with integer division we&#8217;re not going to get the fractional remainder it&#8217;s just literally going to</p>



<p class="wp-block-paragraph">be the number one sitting in rax and then in rdx it&#8217;s going to give us the result of using the</p>



<p class="wp-block-paragraph">modulo operator which is going to be 23 so it&#8217;s going to be one remainder 23</p>



<p class="wp-block-paragraph">is what we should see unless i&#8217;m totally wrong so then right away</p>



<p class="wp-block-paragraph">of our division which is a good practice you know like the rex and rdx those are registers that</p>



<p class="wp-block-paragraph">could be quickly destroyed especially if you called another function or a system call or</p>



<p class="wp-block-paragraph">something so i&#8217;m just going to save them right away so that&#8217;s why we saved 14 and 15</p>



<p class="wp-block-paragraph">in addition to r12 so r12 we were using for the denominator and then we&#8217;re using 14 and 15 to</p>



<p class="wp-block-paragraph">save them real fast and now I&#8217;m just going to do easy stuff that I&#8217;ve talked about in other videos</p>



<p class="wp-block-paragraph">just to sort of print out an answer so I&#8217;m just going to I&#8217;m just going to write this up real</p>



<p class="wp-block-paragraph">fast copy paste this and then just kind of briefly talk about oh look I&#8217;m using a system call</p>



<p class="wp-block-paragraph">explained in another video to print out that little prefix message you know the result of your</p>



<p class="wp-block-paragraph">division here&#8217;s the quotient and then a colon and then I can actually print the number</p>



<p class="wp-block-paragraph">personal library to print a 64-bit integer and so that way it&#8217;s easy for me and notice how I&#8217;m</p>



<p class="wp-block-paragraph">printing r14 I&#8217;m giving it to the call to the function as the first argument which is rdi</p>



<p class="wp-block-paragraph">what is r14 again that&#8217;s just the quotient so here I&#8217;m printing the quotient and then I do the</p>



<p class="wp-block-paragraph">same thing for the remainder and then we&#8217;re going to do a paste here and I&#8217;m just going to print my</p>



<p class="wp-block-paragraph">prefix of the remainder you know here&#8217;s your remainder and then I&#8217;m going to actually print</p>



<p class="wp-block-paragraph">And then I&#8217;m going to actually print it.</p>



<p class="wp-block-paragraph">And the remainder was in R15.</p>



<p class="wp-block-paragraph">So 14 and 15 had the answer.</p>



<p class="wp-block-paragraph">And then the other register just kind of had, or the R12 just kind of had the denominator.</p>



<p class="wp-block-paragraph">So I think that&#8217;s pretty much all I need.</p>



<p class="wp-block-paragraph">Oh, there&#8217;s one more function that I forgot to put in there.</p>



<p class="wp-block-paragraph">I&#8217;m fond of writing a little CRLF function.</p>



<p class="wp-block-paragraph">This is not part of the video, really.</p>



<p class="wp-block-paragraph">But it&#8217;s just a special function that I can call on to do a carriage return line feed.</p>



<p class="wp-block-paragraph">And I&#8217;m just, I just can never stop using this.</p>



<p class="wp-block-paragraph">stop using this. I know I could just prefix or suffix my strings, you know, with like a CRLF,</p>



<p class="wp-block-paragraph">but I like to be able to call every time I want to see our lab. Okay. So we have the program now.</p>



<p class="wp-block-paragraph">Let&#8217;s see if it actually works. Is that my terminal? Yeah. Okay. I&#8217;m going to go clear</p>



<p class="wp-block-paragraph">and make, hopefully it actually works. So, oh yeah, there we are. Okay. So everything seemed</p>



<p class="wp-block-paragraph">It printed hello from the driver.</p>



<p class="wp-block-paragraph">And then when we went inside of the assembly module,</p>



<p class="wp-block-paragraph">this is really what we&#8217;re interested in.</p>



<p class="wp-block-paragraph">It said the division result, the quotient is the number one</p>



<p class="wp-block-paragraph">and the division remainder is the number 23.</p>



<p class="wp-block-paragraph">That&#8217;s what we said before, right?</p>



<p class="wp-block-paragraph">So it&#8217;s going to be 23 is the remainder.</p>



<p class="wp-block-paragraph">If you just do the modular operator or if you&#8217;re dividing,</p>



<p class="wp-block-paragraph">whoops, if you&#8217;re dividing, then you&#8217;ll just get a one.</p>



<p class="wp-block-paragraph">Remember like the quotient is just going to, you know,</p>



<p class="wp-block-paragraph">truncate or remove or throw away all the extra data.</p>



<p class="wp-block-paragraph">all the extra data it&#8217;s not going to round up or down or anything it&#8217;s just going to say</p>



<p class="wp-block-paragraph">anything past the decimal point is gone and that&#8217;s why the uh the remainder is pretty important</p>



<p class="wp-block-paragraph">then of course you know just for fun we could i don&#8217;t know put like some other numbers in here</p>



<p class="wp-block-paragraph">if we wanted to maybe i&#8217;ll copy paste this just put some other numbers let&#8217;s put like a giant</p>



<p class="wp-block-paragraph">number and see what happens because these are 64-bit numbers we can do it um i probably should</p>



<p class="wp-block-paragraph">have switched a and b right because doesn&#8217;t it kind of feel like a&#8217;s on top so it should be the</p>



<p class="wp-block-paragraph">top so it should be the numerator or whatever so I&#8217;m just gonna hit a bunch of stuff here and then</p>



<p class="wp-block-paragraph">divide that by some other stuff see if this comes up with a more fun answer so one more time clear</p>



<p class="wp-block-paragraph">and make and it&#8217;s telling me that uh whoa that&#8217;s the result I guess probably should have printed</p>



<p class="wp-block-paragraph">out the original numbers but let me just put that into the calculator just for fun we&#8217;ll say this</p>



<p class="wp-block-paragraph">just sort of go back to the calculator it enters so uh oh the squiggly lines</p>



<p class="wp-block-paragraph">that means that&#8217;s not exactly the result i got to figure out how to change the mode on</p>



<p class="wp-block-paragraph">this calculator so i can get precise let&#8217;s see can i do that right here</p>



<p class="wp-block-paragraph">exact no can i try it again with exact all right this is not a tutorial on the</p>



<p class="wp-block-paragraph">it as is maybe i&#8217;ll do can i do a slightly smaller number yeah there we go okay i&#8217;ll take</p>



<p class="wp-block-paragraph">off three digits so that the calculator will at least show me something okay run it one more time</p>



<p class="wp-block-paragraph">and then so now we have smaller stuff the answer should have been 38677662 38677662 yeah that looks</p>



<p class="wp-block-paragraph">the decimal point so the point six is not on there and then we run the modulo</p>



<p class="wp-block-paragraph">to see what the remainder is and it should say one two eight six three yeah</p>



<p class="wp-block-paragraph">okay and then the seven is getting returned I don&#8217;t know do you want to</p>



<p class="wp-block-paragraph">return the remainder for some reason we could do it let&#8217;s just return the</p>



<p class="wp-block-paragraph">remainder might as well make this video a little bit more fun so recall that in</p>



<p class="wp-block-paragraph">order to return something if the return value is an integer and not a float or</p>



<p class="wp-block-paragraph">float or if it&#8217;s a pointer then you&#8217;ll just use RAX as the return register so I&#8217;m just going to do</p>



<p class="wp-block-paragraph">move return the remainder to the color okay I&#8217;m going to move something into RAX it&#8217;s going to be</p>



<p class="wp-block-paragraph">R15 nice now this is a function with a return type I&#8217;m going to say along here and then</p>



<p class="wp-block-paragraph">up here, I guess, RAX is already going to be pre-filled now after that call.</p>



<p class="wp-block-paragraph">But you could imagine if you were doing more stuff between the call and the return,</p>



<p class="wp-block-paragraph">you would probably want to save the return value from RAX in some other register like R12.</p>



<p class="wp-block-paragraph">And then that means you got to add a push pop pair to preserve it.</p>



<p class="wp-block-paragraph">And then we&#8217;ll definitely just, well, maybe I&#8217;ll say move RAX into RAX</p>



<p class="wp-block-paragraph">totally useless. That&#8217;s why I&#8217;m commenting it out. So we&#8217;ll do one less instruction.</p>



<p class="wp-block-paragraph">So basically after we get back from divide test, RAX is going to have the</p>



<p class="wp-block-paragraph">return value and math, this function would also use RAX for its return value. So if we just sort</p>



<p class="wp-block-paragraph">of don&#8217;t do anything, we should now see that the driver receives the remainder as its return value.</p>



<p class="wp-block-paragraph">Let me open that up again real fast just to make sure we understand what&#8217;s going on with</p>



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



<p class="wp-block-paragraph">Even though this is not a driver video, it says external long math.</p>



<p class="wp-block-paragraph">It names that as a long function.</p>



<p class="wp-block-paragraph">And then when we call the function, we just kind of grab a long result, 64 bit int, and</p>



<p class="wp-block-paragraph">then we print it with printf because this is a C program, not C++.</p>



<p class="wp-block-paragraph">integers, assigned integers. Let&#8217;s see in assembly. Let&#8217;s see. Where&#8217;s that? Yeah, there we go.</p>



<p class="wp-block-paragraph">So just keep in mind what I&#8217;ve been showing you is idiv, which is a signed integer, which is what</p>



<p class="wp-block-paragraph">you probably want, which means the integer can be positive or negative. And, you know, if you know</p>



<p class="wp-block-paragraph">for sure that you&#8217;re going to be dividing two positive numbers, and there&#8217;s no chance that</p>



<p class="wp-block-paragraph">that the numerator or denominator or the quotient or you know any of those things are going to be</p>



<p class="wp-block-paragraph">negative then you could you could divide unsigned integers and then you can use larger values</p>



<p class="wp-block-paragraph">because remember when we have signed values we basically lose half of the I guess maximum</p>



<p class="wp-block-paragraph">possible value that we can represent because we lose one bit to the fact that the integer is signed</p>



<p class="wp-block-paragraph">is for signed sorry i div is for signed and regular div is for unsigned and then</p>



<p class="wp-block-paragraph">yeah i guess that&#8217;s basically all i really needed to show you there&#8217;s a bunch of nice code in this</p>



<p class="wp-block-paragraph">book i&#8217;m not gonna i&#8217;m not gonna look at it right now so okay thank you so much for watching this</p>



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">So please do me a kindness and subscribe.</p>



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



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



<p class="wp-block-paragraph">It just wakes me up and I get filled with joy.</p>



<p class="wp-block-paragraph">That&#8217;s exactly what happens every single time.</p>



<p class="wp-block-paragraph">So you could do it as a nice favor to me</p>



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



<p class="wp-block-paragraph">Just subscribe and then I&#8217;ll just wake up.</p>



<p class="wp-block-paragraph">I promise that&#8217;s what will happen.</p>



<p class="wp-block-paragraph">Also, if you look at the middle of the screen right now,</p>



<p class="wp-block-paragraph">the screen right now you should see a qr code which you can scan in order to go to the website</p>



<p class="wp-block-paragraph">which i think is also named somewhere at the bottom of this video and it&#8217;ll take you to my</p>



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



<p class="wp-block-paragraph">tutorials and things that i offer and all that good stuff and uh if you have a suggestion for uh</p>



<p class="wp-block-paragraph">clarifications or errata or just future videos that you want to see please leave a comment or</p>



<p class="wp-block-paragraph">what&#8217;s going on you know just send me a comment whatever I also wake up for those in the middle</p>



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



<p class="wp-block-paragraph">to me I would really appreciate it so again thank you so much for watching this video and</p>



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



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



<p class="wp-block-paragraph">you</p>
<p>The post <a href="https://www.NeuralLantern.com/signed-integer-division-in-x86-64-assembly-with-idiv-and-cqo/">Signed Integer Division in x86-64 Assembly with IDIV and CQO</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/signed-integer-division-in-x86-64-assembly-with-idiv-and-cqo/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
