This video tutorial explains how to access and manipulate memory in x86 assembly language, covering direct memory addressing using labels with square brackets, data types (DB for bytes, DW for 16-bit words, DD for 32-bit double words), and stack operations where the stack pointer (ESP) manages a LIFO structure that grows downward in memory; the instructor demonstrates writing to specific memory locations using move operations with byte offsets and shows how to allocate and write strings directly on the stack for output operations.
x86 Assembly Programming: Memory Access and Stack Operations
Added:welcome back in this video I'll be covering memory access and stack operations in assembly technically we've already seen one example of addressing memory and that was in the hello world program from the first video so let's run a similar example so we can play with this memory access a bit more well create a new file and add the start declaration and then add a dot data section you should be familiar with all this then we'll create a new label in this section named a DDR and that's short for address I'm naming it that to highlight the fact that this label is really just a pointer to some memory address oh and the memory at points 2 is just a string that contains the word yellow then switch back to a dot text section for our code and create the start label next set EI x to 4 so we can do a system write call and set B X to 1 so we're writing to standard out set e CX to our address label because that's where the bytes we want to write our story set e DX to 6 to specify how many bytes we want to write because yellow is 6 bytes long and finally make the system call by using an interrupt then we can exit the program with the status of 0 meaning that there was no error and if we do all the usual assembling linking and executing stuff we get the output yellow cool but what if we want to alter the contents of the string at runtime well that's actually pretty simple here I'm using a move operation where the first operand is our address surrounded by square brackets this means that we're moving some data into the address the second operand is byte H meaning that the byte representation of the character H is what we're moving into the memory at our address the bike keyword is important here because this move operation can also be used for larger integers than bytes and the assembler needs to know how much data is being moved since addr points to the beginning of our string that's where this H will be moved to but we can also use an offset to access other parts of the data now we're moving an exclamation point into the memory at our address plus an offset of five so if our memory without an offset is where the Y was then moving five bytes to the right puts us at the address where the W is see where I'm going with this if we assemble a link and execute will be greeted by hello followed by an exclamation point and that's how you're able to write to specific locations in memory so far we've been working with contiguous bytes in the data section but there are other data types here are some examples of common data types we've been using this DB for storing byte strings like this but you can also a store byte literals by the way whenever a number begins with 0x like this it means that it's a hex value FX or FF in hexadecimal is 255 but you can also use decimal literals if you don't start them with 0x sometimes you want to encode a larger integer so this DW data type is 2 bytes long in other words 16 bits long and this DD data type is 4 bytes long or 32 there are larger types but since we've been focusing on 32-bit assembly I'll stop here for now plus now let the perfect point to focus on the stack you might recall that this is the last in first out or LIFO data structure available to us meaning that it behaves like that a stack of books we're pushing is like putting a book on top of the stack and popping is like taking a book off of the top of the stack and you should also recall that beyond pushing and popping the stack is just an array with a pointer to where the top is and also remember that we do have random access to this memory meaning that we can read and write from arbitrary locations within it but even if you do remember all of that it probably still helps to see a visual representation so on the Left I'll be adding some assembly code and on the right we have a visual representation of the stack after that code has executed ESP is our stack pointer this is the actual register that holds the current location of the top of the stack below this is a visual representation of the stack on the left side we have the memory addresses you might notice that these count by fours and I'll explain that here shortly and on the right we'll have the value of each of these memory addresses so the value stored at that address you might be wondering why I'm starting with the stack pointer at the highest memory address and that's because this is exactly how x86 machines work the stack pointer starts at a higher address in memory and moves down with pushes and back up with pops so what does it look like to push a value well the push operation causes the stack pointer to decrease by 4 and then the value being pushed is written to that location in memory this is why I've labeled the memory address is counting by fours but why does the stack move by fours in the first place well because it's pushing for by integers remember we're using 32-bit assembly and each integer is 32 bits or 4 bytes so each number being stored is actually 4 bytes long now if we push another value the same thing happens again and again and well you get the point but instead of doing these as push operations we could actually work with the ESP register directly so that last push could be re-written as subtract 4 from ESP and then move 357 into the location that ESP points to the purpose of this d-word keyword is to tell NASM that we're moving four bytes into this memory this memory location remember that this move operation is exactly how we were able to turn the word yellow into hello by moving individual bytes at a time but now we're moving four byte integers at a time so we have to tell NASA own you know what size of data we want to move and that's exactly what this D word key word does but let's change it back to the push version of that operation since it's easier to read and then add this pop operation so what did that just do well it moved the value at ESP the top of the stack into the EAX register in this example of that means EAX is now 357 and then it added four to ESP removing 357 from the top of the stack I mean technically the 357 value is still there in memory it wasn't erased or anything but if we do another push operation it'll be written over of course we could implement this pop operation using a move and in addition it has the exact same effect which is to move those four bytes stored at the location ESP points to which was 300 57 into the EAX register and then add four to ESP removing 357 from the top of the set now let's take everything that we've learned so far and write a program that allocates a string on the stack and then writes that to standard out first we start with the usual entry point stuff then we create some space on the stack by subtracting 4 from it effectively we've just allocated 4 bytes on the stack that we can use however we want now move the byte H into the first the first byte allocated in this space move into the next byte using the same offset that we use when we turned yellow into hello then we move Y and an exclamation point then we can do the usual system right call but this time we're setting ECX to be the address that ESP points to the value of the ESP register and we're setting edx to 4 because that's how many bytes we want to write finally we do a system exit call and in the program and now if we assemble link and execute it says hey well that's it for this video and the next one I'll show you how to combine the stack operations and the jump operations we've covered so far to create functions and function calls by
Up Next

x86 Operating Systems: BIOS Interrupt 0x10 to Print a Boot Message
@olivestemlearning
7.3K views•2023-08-27

BitTorrent Protocol Explained: Piece Selection & Peer Choking
@StevenGordonAU
481 views•2013-02-22

HTTP Requests Explained: GET, POST, PUT, DELETE
@codecademy
103.1K views•2021-10-07

Enigma Machine Mechanics: WWII Encryption Explained
@JaredOwen
13.2M views•2021-12-11
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Computer Science

















![[Part 1] Unit 3.3 - Memory Units](https://i.ytimg.com/vi/YmBIJ-4Mzv4/hqdefault.jpg)






![[CS161 FA25] Lectures 3-4: Memory Safety Vulnerabilities](https://i.ytimg.com/vi/CqAOqTA54IA/maxresdefault.jpg)













