Saturday, August 17, 2013

Reverso

Here's a program that stores 1,2,3,4 in the registers, and then reverses it to 4,3,2,1.


.model flat, c
.stack 100h
.data
store dword 0
.code
doit proc
mov eax,1
mov ebx,2
mov ecx,3
mov edx,4


mov store,eax
mov eax,edx
mov edx,store
mov store,ebx
mov ebx,ecx
mov ecx,store

Here's another way that you can do it except this way uses the stack.

.model flat, c
.stack 100h
.data
store dword 0
.code
doit proc
mov eax,1
mov ebx,2
mov ecx,3
mov edx,4

push eax
push ebx
push ecx
push edx

pop eax
pop ebx
pop ecx
pop edx


Now the stack is a section of R.A.M . I'm going to give you a little lesson to show you a little example of  what it looks like and what it does.

Imagine your'e inside a restaurant. There is a pile of plates on a table. A neat stack if you please. This stack is an example of what we call LIFO which stands for last in, first out. This means: the last plate that gets put on the stack is the first one to get taken off.

Now that I've told you about that take a look at the program. You see how I've got the words pop and push in there? Let me tell you how that relates with the stack of plates. When I "pop" the stack that means I take a plate off the top. When I "push" the stack that means I put a plate on the top. You notice how I'm popping in the same order I'm pushing? That's what reverses the registers.

No comments:

Post a Comment