| Win32 Assembly Chapter 12 | |
|---|---|
| Topic Started: Jan 10 2008, 03:42 PM (401 Views) | |
| astropirate | Jan 10 2008, 03:42 PM Post #1 |
![]()
Not Lurking
![]() ![]() ![]()
|
Accessing Arrays Welcome Hi! Welcome to the twelveth chapter of this series. Now, I'm going to explain about how we can access arrays in assembly. Actually, there is nothing really new in accessing array. Most of the things are already discussed in the second chapter. In this chapter I just explain on how we can access array effectively. Intel x86 assembly has two handy instructions: xlat and xchg which will be explained shortly. Array Revisited To refresh our mind, declaring array a ten-byte array is like this:
To load the first element of the array into register al is like this:
Accessing the second, the third, and the forth element is like this:
Now, how can we access this through a loop? You probably do the loop like this:
This loop is perfectly fine. You can access the array this way. There is another way to access the array:
This loop does exactly the same thing as the first one. However, this one may be preferable because it doesn't move the array's base pointer (which is pointed by BX), but instead it use the register SI as index increment. The first approach uses what we called pointer arithmetic. It is called pointer arithmetic because we increment BX, which at that time acted as a pointer. This approach is somehow disliked because it will mess up debugging or analysis. To me, it's just a matter of personal taste. Anyway, the second approach has a slight advantage over the first. Suppose you'd like to reverse the array. See the snippet below:
See? So, the beginning of the array is pointed by BX+SI, and the end is by BX+DI. SI is incremented and DI is decremented. The process goes on until they meet in the middle. If we increment the BX register, it would be hard to mark up the other end. Notice that this indexing capability can only be carried out by BX, DX, SI and DI. BX and DX act as the base registers, whereas SI and DI act as the indices. This limitation is further lifted in 80386 or above processor (but it still doesn't mean that you can jumble up any registers). Note: Because of this, BX is nicked as 'base register', SI as 'source index' and DI as 'destination index'. DX is nicked as 'data register' because it always used to do input and output between peripherals (which is somehow irrelevant here). XLAT Instruction Intel x86 processor has a nice instruction called xlat, which is now largely forgotten because using index registers are much nicer. Suppose we'd like to access the third element of the array:
Nice huh? Bubble Sort Let's see how we can apply this into a nice bubble sort program:
MASM lovers can modify this program pretty easily. XCHG: A Nice Extra Intel x86 processor does have an instruction to swap things. It is xchg (exchange). Let's look into the bubble sort above:
You can use xchg to exchange values. See the change below:
These two programs are equal. Closing OK, I think that's all for now. You've seen the bubble sort in assembly. How about quick sort? Try that as an exercise. See you next time. Roby Joehanes © 2001 1 |
You will Bow before me!![]() | |
![]() |
|
| 1 user reading this topic (1 Guest and 0 Anonymous) | |
| « Previous Topic · Tutorials · Next Topic » |






![]](http://z3.ifrm.com/static/1/pip_r.png)




1:08 AM Feb 7