Jump to content
xisto Community
Sign in to follow this  
HannahI

Assemby Programming Intel Syntax Chapter 2: Arrays, And Stack Instructions

Recommended Posts

Welcome to chapter to of my tutorials. If you are reading this you should have also read [a]http://forums.xisto.com/topic/97893-topic/?findpost=1064410842[/a] If you did not, you should have a working knowledge of the registers ax, bx, cx, dx, instructions: add, sub, mul, div, and mov. In chapter 2, you will learn about arrays and the stack instructions. Lets start with arrays. In order to understand how arrays work, you have to understand how computer memory works. In Diagram 2.1 is an example of an array called foo and a variable called bar. In the example foo starts at memory address 675, and the variable bar starts at address 680. The assembler knows that when you refer to the word foo, it means the memory address 675, and when you reference the word bar, you are reffering to the memory address 680. If you wanted the access the nth element of foo, the equation the computer executes is 675 (address of foo) + n (index). If n was 3, the answer would be 678, which is a memory address. If you were to refer to that in the register side of the move instruction, and putting 5 on the other side, the assembler would say, "Okay, put the value 5 at memory address 678". This is the indepth explanation of the move instruction. But unfortunately, in assembly it isn't that easy.
Diagram 2.1 0 0 0 0 0 5675 676 677 678 679 680foo foo foo foo foo bar
In assembly it is actually pretty easy, but it requires a few steps. The first step would be to put the memory address 675 (or just type foo) into ax, lets say. Then put the element you want to access into bx, lets say. Now add bx to ax. Now you would use a special move instructiont to put the value in a specified memory address that you precalcualted when you set ax. This is shown in Example 2.1.
Example 2.1

mov ax, 675;or you could just say foomov bx, 3;to access the third elementadd ax, bx;get the destination memory addressmov byte ptr [ax], 5;put five into the address pointed by ax - we'll talk more with this in the next tut on what this means
That is basic arrays. Not to complicated. But now for stacks. The stack is actually just a very special array in assembly. When you call a c function, you put its parameters on the stack. This way you are able to hold multiple things in the same, global, array that every program has access to. There are two main stack instructions - push and pop. The push instruction puts something on the top of the stack. The pop instruction takes the thing at the top of the stack. The only problem with this is that everything is read in reverse order. You have to be very careful when dealing with stacks. The syntax for push is shown in Example 2.2. The syntax for pop is shown in Example 2.3. The register or value part of push is where you put what you want to put on the top of stack. The register part of pop is the destination of where the popped element will be stored. Thats all to basic stacks. But there are also 4 more stack instruction - pusha, popa, pushf, popf. For pushf and popf, the only differences from these registers and the other stack regster they originate from is the destination/register or value type. They use something called flags instead of registers. Pusha and popa go on their own line and do not have any parameters unlike all of the other instructions I've showed you. Pusha puts these register on the stack in the following order: AX, CX, DX, BX, SP, BP, SI, DI. Popa does the same thing as pop except for that it pops its values off the stack in the following order: DI, SI, BP, SP, BX, DX, CX, AX.
Example 2.2
push [i]register of value[/i]
Example 2.3
pop [i]register[/i]
Excersises1. What did we compare the stack to?Answer:

A global array

2. What are all the stack instructions?Answer:

push, pop, pusha, popa, pushf, popf

3. How do you compute the memory address to write to when using arrays?Answer:

Array + index


Thanks for learning about Assembly's Arrays and Stacks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.