PDA

View Full Version : سوال: حرکت بر روی آرایه



sinasafaee
جمعه 13 آبان 1390, 19:47 عصر
سلام

در cpp یک آرایه تعریف کردم و می خوام با استفاده از asm روی آرایه حرکت کنم

کسی نظر خاصی نداره؟

#target
جمعه 13 آبان 1390, 22:32 عصر
خوب اسم آرایه اشاره گر به اولین آیتمه . حالا اگه هر آیتم چهار بایت باشه !


MOV EAX,ArrayName
MOV ? , [EAX] // Item 1
MOV ? , [EAX+4] // Item 2
MOV ? , [EAX+8] // Item 3
.
.

xman_1365_x
شنبه 14 آبان 1390, 00:42 صبح
یک برنامه نمونه نوشتم برای کار با ارایه و فراخوانی توابع
دسترسی به عناصر هم که در پست قبل اشاره شده به صورت 4 بایتی هست که شما میتونید بازه رو خودتون مشخص کنید. اما این شکل اشتباه هستش که باید آدرس رو قرار بدین با offset یا lea

//in the name of god the compassionate the merciful
//programmer: xman_1365_x
//date: 11/04/2011
//inline asm in c/C++‎ project: call standard function in c/C++‎ (printf,getch) & work on array

#include <stdio.h> //printf display your value(any type)
#include <conio.h> //getch(); wait for press any key and no echo

#define ARRAYSIZE 5
/*
public var use(lea is faster):
mov eax,offset world
or
lea eax, world
*/

char hello[] = "press ";

void main( void )
{
//private var only use:
//lea eax, world

int intArrTest[ARRAYSIZE] = {1,2,3,4,5};
char format1[] = "search for input number in intArrTest[]\nSuccessfully search, your number:%d exist in array \n";
char format2[] = "search for input number in intArrTest[]\nyour number:%c not exist in array \n";

char format3[] = "%s %s\n";
char world[] = "any key...";

char format0[] = "Enter a number for search in array :";
char num;

//search
__asm
{
lea eax,format0
push eax
call dword ptr printf
pop ebx
call dword ptr getchar
mov num,al
sub eax,48
mov edx,eax
xor edi,edi;
mov ecx,ARRAYSIZE
l1:
cmp intArrTest[edi],edx
je existnum
add edi,4
loop l1
jmp notexistnum

existnum:
mov eax,intArrTest[edi]
push eax
lea eax,format1
push eax
call dword ptr printf
pop ebx ; Pop what was pushed on the stack.
pop ebx
jmp endshowint

notexistnum:
xor eax,eax
mov al,num
push eax
lea eax,format2
push eax
call dword ptr printf
pop ebx
pop ebx
endshowint:

}



//display string
__asm
{
//begin printf
lea eax, world
push eax
lea eax, hello
push eax
lea eax, format3
push eax
call dword ptr printf
pop ebx
pop ebx
pop ebx
//end printf
call dword ptr _getch
}
}