views:

33

answers:

2

Hello, I am having a lot of trouble accessing a value in an array of chars at a specific location. I am using inline-assembly in C++ and using visual studio (if that is of any help). Here is my code:

char* addTwoStringNumbers(char *num1)
{
    // here is what I have tried so far:
    movzx eax, num1[3];
    mov al, [eax]
}

When I debug, I can see that num1[3] is the value I want but I can't seem to make either al or eax equal that value, it seems to always be some pointer reference. I have also played around with Byte PTR with no luck.

+1  A: 

I'm not good neither at inline assembly, neither at MASM syntax, but here are some hints:

1) Try this:

mov   eax, num1 ;// eax points to the beggining of the string
movsx eax, [eax + some_index] ;// movsx puts the char num1[some_index] in eax with sign extend.

(movzx is for unsigned char, so we used movsx)

2) You need to pass the value from eax to C. The simpliest way is to declare a variable and to put results there: int rez; __asm { mov rez, eax; };

3) If you want to write the whole function in assembly, you should consider using the naked keyword (and read about calling conventions). If not, make sure you preserve registers and don't damage the stack.

ruslik
works perfectly! Thank you so much for the help. I will look into the naked keyword
Quentin
@Quentin Also try NASM without any VS around. It will help better to understand what's going on.
ruslik
A: 

Looks like someone's doing their ICS 51 homework! Follow ruslik's advice and you'll be done in no time.

Jack Black
Yup, reading out of arrays is confusing in assembly...
Quentin