Reverse Engineering for Beginners

(avery) #1

CHAPTER 10. POINTERS CHAPTER 10. POINTERS


Chapter 10


Pointers


Pointers are often used to return values from functions (recallscanf()case (7 on page 58)). For example, when a function
needs to return two values.


10.1 Global variables example


#include <stdio.h>


void f1 (int x, int y, int sum, int product)
{
sum=x+y;
product=x*y;
};


int sum, product;


void main()
{
f1(123, 456, &sum, &product);
printf ("sum=%d, product=%d\n", sum, product);
};


This compiles to:


Listing 10.1: Optimizing MSVC 2010 (/Ob0)

COMM _product:DWORD
COMM _sum:DWORD
$SG2803 DB 'sum=%d, product=%d', 0aH, 00H


_x$ = 8 ; size = 4
_y$ = 12 ; size = 4
_sum$ = 16 ; size = 4
_product$ = 20 ; size = 4
_f1 PROC
mov ecx, DWORD PTR _y$[esp-4]
mov eax, DWORD PTR _x$[esp-4]
lea edx, DWORD PTR [eax+ecx]
imul eax, ecx
mov ecx, DWORD PTR _product$[esp-4]
push esi
mov esi, DWORD PTR _sum$[esp]
mov DWORD PTR [esi], edx
mov DWORD PTR [ecx], eax
pop esi
ret 0
_f1 ENDP


_main PROC
push OFFSET _product
push OFFSET _sum
push 456 ; 000001c8H

Free download pdf