Click to See Complete Forum and Search --> : assembler help?


gmueller
August 15th, 2006, 08:13 PM
<I'm compiling this code using VS 2005 on Windows XP.>

I'm not really sure if this is a C++ topic or an assembler project, but since assembler is usually somewhat of an art that not all C++ people know, I'll post it here instead of the C++ thread.

What im trying to do is be able to make command lists to speed up some basic functions...

I was just practicing making add(int a, int b) as a list that could be called.

For some reason I'm getting an access violation, and can't figure out what to change.
can someone look at this to see what's going wrong?


--------------------------------------------------------------
CODE
--------------------------------------------------------------

#include <iostream>
#include <stdio.h>

using namespace std;



int add(const int& a, const int& b) {
return a+b;
}

void printhex(const char x) {
for (int i= 2*sizeof(char) - 1; i >= 0; i--) {
cout << "0123456789ABCDEF"[((x >> i*4) & 0xF)];
}
}


int main(void) {
#include <iostream>

int x;
int three;
int five;
three = 15;
five = 5;
typedef int (__cdecl *functptr1) (int , int);




char *funct;

funct = new char[48];
//funct contains the same assembler codes of the add function above

funct[0] = 0x55;//push
funct[1] = 0x8B;//mov
funct[2] = 0xEC;
funct[3] = 0x81;//sub
funct[4] = 0xEC;
funct[5] = 0xC0;
funct[6] = 0x00;
funct[7] = 0x00;
funct[8] = 0x00;
funct[9] = 0x53;//push
funct[10] = 0x56;//push
funct[11] = 0x57;//push
funct[12] = 0x8D;//lea
funct[13] = 0xBD;
funct[14] = 0x40;
funct[15] = 0xFF;
funct[16] = 0xFF;
funct[17] = 0xFF;
funct[18] = 0xB9;//mov ecx 30h
funct[19] = 0x30;
funct[20] = 0x00;
funct[21] = 0x00;
funct[22] = 0x00;
funct[23] = 0xB8;//mov eax CCCCCCCCh
funct[24] = 0xCC;
funct[25] = 0xCC;
funct[26] = 0xCC;
funct[27] = 0xCC;
funct[28] = 0xF3;//rep stos
funct[29] = 0xAB;
funct[30] = 0x8B;//mov eax
funct[31] = 0x45;
funct[32] = 0x08;
funct[33] = 0x8B;//mov eax
funct[34] = 0x00;
funct[35] = 0x8B;//mov ecx
funct[36] = 0x4D;
funct[37] = 0x0C;
funct[38] = 0x03;//add eax, [ecx]
funct[39] = 0x01;
funct[40] = 0x5F;//pop edi
funct[41] = 0x5E;//pop ebi
funct[42] = 0x5B;//pop esi
funct[43] = 0x8B;//mov esp, ebp
funct[44] = 0xE5;
funct[45] = 0x5D;
funct[46] = 0xC3;//ret
funct[47] = 0xCC;//int 3


functptr1 fun = (functptr1)funct;
x = fun(three,five);
delete funct;


cout << x;
return 0;
}

wildfrog
August 15th, 2006, 08:42 PM
Your function prototype differs from the original:

int add(const int& a, const int& b) {

typedef int (__cdecl *functptr1) (int , int);

And if you new[] then you delete[]:

delete[] funct;

Anyway I'm not sure this is the best way to optimize code.

- petter

gmueller
August 15th, 2006, 08:48 PM
wow, it works, thank you so much!!

amazing that I was so close