Transcript for:
Understanding Computer Registers and Assembly

hello everyone I'm Dan in this tutorial I'll give you an introduction to registers let's begin by opening up your web browser to my website the GPU comm and will scroll down here to assembly language select the introduction to registers option so in this tutorial I'm going to introduce you to the first four general-purpose registers and I'll go over what that means in later videos but there's a lot to take in now also I will introduce you to the concept of disassembly of the final executable now the most common processors found in servers desktops laptops and many other things for that matter are the x86 and x86 64 processors now the industry is dominated by Intel and AMD and they both support the x86 instruction set the x86 architecture is the most widespread now registers are locations in the processor where it can access data perform operations and then store resulting data data contained in registers can be moved and manipulated extremely quickly registers are by far the fastest form of quote unquote memory in the entire computer now each core in a processor has its own registers and each core can perform multiple operations on a register per clock cycle now the typical memory hierarchy ordered by speed in a modern computer looks at works like this so registers registers they are super fast they perform operations are located on the chip and each core has its own set of registers l1 cache now it's the fastest of true memory storage located on the chip and each core has its own and it's located just outside the registers okay and registers level-1 level-2 there they're really expensive as far as that the pricing of this memory goes now the l2 cache the second fastest the true memory storage located on the chip and each core has its own and it's just outside of the l1 cache the the l3 cache is the third fastest of true memory storage and it's located on the chip and each core shares this memory okay so if you have like an eight core processor then you'll have each each core will have its own registers its own l1 cache its own l2 cache but then all eight of them would share the l3 cache okay now let's talk about RAM RAM varies in speed but it's really it's slow compared to registers or cache and it's located on the motherboard you know yeah obviously if you're taking my tutorial probably familial you have to clip it in and so on and so forth and it must communicate over a bus an SSD slow compared to all of the above right and magnetic you know your old platter platter hard drives you know they're crazy slow compared to all the above so let's talk a little bit about register sizes now the very first x86 processor started out as 8-bit and subsequent generations expanded the 16-bit 32-bit and 64-bit and I imagine some day this tutorial be obsolete when they moved 128-bit but anyway this progression led to a certain overlapping in the names of the registers okay so the very first register it's called the accumulator right and notice how it starts off with an A here okay so in the 8-bit there's an 8-bit high and an 8-bit low and so it's called a H and al for the accumulator low an accumulator high now 16-bit is the ax register once again accumulator and it contains just basically the high bits plus the low bits total of 16 bits right but it's referred to as the 8 ax register ok so these two comprise this now the 32-bit register comprises the ax register which comprises that right and it's called the e ax right and then the 64-bit register is called our ax with the r prefix on there but it's comprised of the 32 the 16 and which is comprised of course these two registers here so then of course there's eight bytes in a bit so a 64-bit register would be eight bytes long and the thirty-two would be four sixteen would be two and obviously here's the byte and here's a byte right so talking about this right they just kind of overlap and encompass each other okay now with that being said the second one is called the counter notice to see our CX e CX c XC HCL so it follows some sort of logic here so it's interesting anyway the third one is called the data register notice the drdx e DX DX d HDL and the fourth one is called the base our BX for the 64-bit EBX for the 32-bit BX for the 16-bit and BH and BL now sometimes when you see these registers you might be like oh wow we can actually spell it ABCD and maybe that's the order they go in there some of them and that's kind of a common start misconception there but that's not the case at all just happens to be that and this is the order they actually go in but sometimes you'll actually I prefer to actually code when like if I'm moving and stuff into the registers I'll actually do like the you know the EAX first followed by the eb x the e CX but anyway that'll be a little bit more down the road now what we're going to do today is we're just gonna kind of explore how this all works there so h XD for windows is by far my favorite hex editor you can download it here pause the video I'm not gonna go through that there is fairly simple there and for Ubuntu I'm going to use WX hex editor right here's the command you can install there but up terminal install that and then when you start I recommend starting it from the applications window alright let's go ahead and dive in and do some code here I'm gonna go ahead and basically just minimize this here actually what I'm gonna do is I'm going to copy this here okay first so let's go ahead and copy this code and then we're gonna paste it over to the virtual machine here the Ubuntu virtual machine alright so the first thing we'll do is we'll just go ahead and open up terminal we're going to make der assembly now I already have that folder but if you don't know create it for you and then we'll change directories to the assembly folder and then we're gonna make a folder here today called rig intro and let's mix up my windows commands and Linux commands there alright and let's go ahead and change directories to the Reg intro folder here and we're going to leafpad ring intro ASM ok and then we'll just simply come over here and paste that stuff in there so this is like a bare bare minimum program it just starts up and exits out this is the equivalent of saying like C or C++ of you know basically doing int main and then returning 0 this is exactly what that does and I'll go over that in just a second here so let's go ahead and just save this let's open up another terminal reason why we're opening up another terminal is because you can't access the command-line there while that's running we'll change directories to the assembly folder and change directories to the Reg intro folder and now we'll assemble it using NASM okay we've got our dot o file there and now we'll link it there - in l3 86 oh and our output file will be rig intro okay do an LS and there we've got our Gutter executable there let's go ahead and just run it there of course it's going to just basically do absolutely nothing alright so if you've got the hopefully you've got the hex editor installed if not pause the video get that installed looks good and open it up there it's a little funky when you open it up because you'll have to like kind of adjust these panels accordingly but the first thing you want to do is come up here and go to view and do disassembler panel and make sure that's checked okay and then let's go ahead and open up the Reg intro here alright and you'll have to kind of you know if you if you play with this this window here it's a little funky to get it kind of aligned there but basically what we want to do is we want to have the hex numbers 0 through F listed up here on this column just to make stuff just a little bit easier okay there we go now it's all aligned so we basically have two bytes across each one there all right so MOV is is an operation code or in other words an OP code right and what what I'm going to show you here is is kind of how this works on the raw level since we're looking at at you know the final executable this is this is machine language right here right the assembler you know NASM assembles that LD links it and we end up with machine language these are the raw instructions for the x86 architecture here okay the when we run this program basically the you know the CPU basically just starts at the top here and executes everything from top to finish there but let's go ahead and and take a look at some certain things right here ok so if I come down here to b8 right happen to know that b8 is the move instruction for e8 for the EAX register and so we're moving one into EAX and that happens to be this right here b8 followed by the next since b8 we're dealing with 32-bit architecture here right this is the value for one and never mind that you might think 1 will be over here I'll explain why one is over here in a future video but this is the value right here so b8 says hey we're going to move the value 1 which is like this is the operation code here and this is the actual like argument right and you can see over here in the disassembler panel move EAX and then you know it's got the hex value of 1 there I could have put the hex value of 1 up there as well to move EBX now EBX the operation code for that there right is BB right and then int you might be looking at this going alright you know chances are for you tackling assembly language you having some knowledge of other languages there and this almost looks like you know hey C or you know Java or whatnot there C++ were declaring an int 80 but that's not the case at all IMT and assembly is short for interrupt okay so we're calling interrupt 80 right which is basically we're going to execute a system call here right when we move e1 into EAX this is actually the equivalent and by the way comments are actually they have a semicolon in front of oppose the C which ends line there so this essentially is a system call right sis exit all right and this right here is basically its argument right so when we call interrupt ATS is oh what's in the EAX register well that's this exit and then we're gonna pass the argument a little parameter whatever you want to call it and depending on where it's going into but anyway whatever this is the equivalent of return and this is zero for its parameter and this tells it to actually execute this D whatever command is loaded into here right for example if we had four in the EAX register that would be the colon like sis right right we're gonna write something rather there anyway I will go into that and future tutorials probably maybe recognize that from the Welcome tutorial there but what we want to do at this point in time is just leave our one there right which is our sis exit and let's take a look at some of these other registers here all right so before we do that we're just gonna do some silly little stuff here and I'm gonna move ex1 and I'm gonna move e DX to and I'm gonna move C X 3 and move EDX for alright let's go ahead and save this come back down here and we'll just stop arrow and this again there assemble it link it and you know we can run it it won't actually still do anything but what we are looking for looking to do here is let's go ahead and we'll just close this and reopen it rig intro right and looking at our first b8 right here right there's our opcode and followed by the parameter one EBX is BB right - you can see over here on the disassembler panel - benign right is ECX ba followed by the four right there is EDX and then then you can see the next thing we do is we move one back into b8 right we move 0 into BB and then we do this cdat right here which calls the interrupt 80 ok so if you as you as you get familiar with this right you'll you'll be able to go into stuff and look at it and it really is critical understanding this really machine language stuff here if you want to get get really you know I don't know hardcore on this stuff you could get to the point where you can just basically open up a hex editor and you can just write machine language if you wanted to and executable and then run the thing so without any sort of the simpler linker compiler you name it right so anyway um let's go ahead and pop over to masam to finish this tutorial out here open up visual studio and we'll do a new project C++ we're into an empty project just call this one rang intro on the assembly folder there okay alright so we're gonna do is come in here we have to set our build dependencies check NASM and now we set some properties here just popped over in this window here first of all we're gonna pop over to system and we're gonna set the subsystem as a console and we're gonna go into advanced we're gonna set our entry point and start and we're gonna scroll down to oh wait we're gonna cancel on this first thing we're gonna do right let's back that up is we're going to change the this to release okay and we got to come back here and do all this stuff again build dependencies okay that's still good properties let's go over to system subsystem console advanced our entry point it's gonna be start and down here image has safe exception handlers we're gonna select no on that and apply alright alright source files add new item while just going to call this reg intro dot ASM okay and let's pop back over to here and we'll just cut and paste this stuff right in there okay we can for example stick a breakpoint right there and run this thing will pop up into the window here there's absolutely nothing to this right if we continue on boom we're done right but we want to come up here to build and then build reg intro okay succeeded boom bada bing all right I've got a hxd shortcut here down on my taskbar I'm going to just pop that open here and let's go ahead and open up that that executable it'll be under assembly reg intro and the release folder and here it is as you can see it's got significantly larger than you know running running looking at it on Ubuntu there so it's just got a lot of lot of bloat in here but let's go ahead and search for the hex value of b8 which we know is the EAX right move EAX opcode there and let's hit f3 to find the next instance of it and f3 again okay so now we've got the BB 0 2 so over here in the source code there right same sort of stuff move EAX EBX edx right so but then of course you know the exit process is a little bit different over here on on windows there but you can see the same same stuff in the machine language over here I'm on memory there are in the hex editor there for Windows there anyway um that basically about concludes this tutorial there I'm going to basically on the next video I'm gonna go in a little bit more about the you know not just the EAX registers but the ax registers the ALS and so on and so forth there anyway that concludes this tutorial thanks for watching