Difference between revisions of "Stack Example"

From SkullSecurity
Jump to navigation Jump to search
m
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Infobox assembly}}
[[Category: Assembly Examples]]
This code should compile and run in Visual Studio (I've tested it):
<pre>
#include <stdio.h>
#include <stdio.h>


Line 5: Line 11:
__asm
__asm
{
{
push ebp     ; Preserve ebp.
push ebp             ; Preserve ebp.
mov ebp, esp ; Set up the frame pointer.
mov ebp, esp         ; Set up the frame pointer.
sub esp, 8   ; Make room for two local variables.
sub esp, 8           ; Make room for two local variables.
push esi     ; Preserve esi on the stack.
push esi             ; Preserve esi on the stack.
push edi     ; Preserve edi on the stack.
push edi             ; Preserve edi on the stack.


mov ecx, [ebp+8]   ; Put the first parameter (a pointer) into ecx.
mov ecx, [ebp+8]     ; Put the first parameter (a pointer) into ecx.
mov edx, [ebp+12] ; Put the second parameter (a pointer) into edx.
mov edx, [ebp+12]   ; Put the second parameter (a pointer) into edx.


mov esi, [ecx] ; Dereference the pointer to get the first parameter.
mov esi, [ecx]       ; Dereference the pointer to get the first parameter.
mov edi, [edx] ; Dereference the pointer to get the second parameter.
mov edi, [edx]       ; Dereference the pointer to get the second parameter.


mov [ebp-4], esi ; Store the first as a local variable
mov [ebp-4], esi     ; Store the first as a local variable
mov [ebp-8], edi ; Store the second as a local variable
mov [ebp-8], edi     ; Store the second as a local variable
mov esi, [ebp-8] ; Retrieve them in reverse
mov esi, [ebp-8]     ; Retrieve them in reverse
mov edi, [ebp-4]
mov edi, [ebp-4]


mov [ecx], esi ; Put the second value into the first address.
mov [ecx], esi       ; Put the second value into the first address.
mov [edx], edi ; Put the first value into the second address.
mov [edx], edi       ; Put the first value into the second address.
pop edi       ; Restore the edi register
pop edi             ; Restore the edi register
pop esi       ; Restore the esi register
pop esi             ; Restore the esi register
add esp, 8     ; Remove the local variables from the stack
add esp, 8           ; Remove the local variables from the stack
pop ebp       ; Restore ebp
pop ebp             ; Restore ebp
ret           ; Return (eax isn't set, so there's no return value)
ret                 ; Return (eax isn't set, so there's no return value)
}
}
}
}
Line 48: Line 54:
return 0;
return 0;
}
}
</pre>

Latest revision as of 01:50, 16 January 2012

Assembly Language Tutorial
Please choose a tutorial page:

This code should compile and run in Visual Studio (I've tested it):

#include <stdio.h>

void __declspec(naked) swap(int *a, int *b)
{
	__asm
	{
		push ebp             ; Preserve ebp.
		mov ebp, esp         ; Set up the frame pointer.
		sub esp, 8           ; Make room for two local variables.
		push esi             ; Preserve esi on the stack.
		push edi             ; Preserve edi on the stack.

		mov ecx, [ebp+8]     ; Put the first parameter (a pointer) into ecx.
		mov edx, [ebp+12]    ; Put the second parameter (a pointer) into edx.

		mov esi, [ecx]       ; Dereference the pointer to get the first parameter.
		mov edi, [edx]       ; Dereference the pointer to get the second parameter.

		mov [ebp-4], esi     ; Store the first as a local variable
		mov [ebp-8], edi     ; Store the second as a local variable
		
		mov esi, [ebp-8]     ; Retrieve them in reverse
		mov edi, [ebp-4]

		mov [ecx], esi       ; Put the second value into the first address.
		mov [edx], edi       ; Put the first value into the second address.
		
		pop edi              ; Restore the edi register
		pop esi              ; Restore the esi register
		add esp, 8           ; Remove the local variables from the stack
		pop ebp              ; Restore ebp
		ret                  ; Return (eax isn't set, so there's no return value)
	}
}

int main(int argc, char* argv[])
{
	int a = 3; 
	int b = 4;

	printf("a = %d, b = %d\n", a, b);
	swap(&a, &b);
	printf("a = %d, b = %d\n", a, b);

	while(1)
		;

	return 0;
}