Assembly Q, MOVS, and register DI not working

This is a discussion on Assembly Q, MOVS, and register DI not working within the C++ Programming forums, part of the General Programming Boards category; This is my first night playing with x86 ASM and I can store data in variables, however I have two ...

  1. #1
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719

    Assembly Q, MOVS, and register DI not working

    This is my first night playing with x86 ASM and I can store data in variables, however I have two problems, I cannot store data from an array index into a register or another variable, and I cannot copy bytes/words from and to variables. The following code is what Im trying, and it returns operand type errors:
    Code:
    int y[5]];
    y[2] = 10;
    char *a=new char[5];
    char *b=new char[5];
    a="a";
    b="b";
    __asm
    {
    //First I am trying to copy one string, to the other:
    mov SI, OFFSET a;
    mov DI, OFFSET b; //Returns error
    movs; //Returns error
    //Second I want to place y, into a register or another variable, neither will work:
    mov AX, y[2]; //Returns error
    }
    It would be greatly appreciated if someone could tell me why this inline function does not work, and if you can, please list any good ASM tutorial sites which may be good for Inline ASM like this. Thanks.

  2. #2
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    The logic of your code is all wrong. You really need to understand c better.
    int y[5]]; // ]
    y[2] = 10;
    char *a=new char[5];
    char *b=new char[5];
    a="a"; // assign a to static storage, leaking the new storage
    b="b"; // assign b to static storage, leaking the new storage

    This looks like a pretty good primer on
    intel instructions but uses nasm
    http://www.users.csbsju.edu/~cburch/...tfolio/x86.pdf

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>please list any good ASM tutorial sites
    I've a feeling someone has asked that before...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Yor error is the size of the registers....

    Most compilers are 32 bit these days.........

    So for a 32 bit pointer.....swap DI and SI for ESI and EDI....

    Also for movs, you need to specify the amount of chars to coby in the ECX register before hand...


    Same size problem with the array assignment...and instead of []...use the array name and add a constant offset (2 in this case)

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    :: Assuming VC++6 ::

    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    
    int main(){
    
    
    	char a[6]="Hello";
    	char b[6];
    	int x = strlen(a);
    	x++;
    
    	__asm{
    		
    		lea EDI, b //address of b
    		lea ESI, a //address of a
    		mov ECX, x //bytes to copy
    		movsloop: //loop point
    		movs 
    		loop movsloop //loop while cx != 0
    	}
    
    	cout << b << endl;
    
    	int y[6];
    	y[2] = 10;
    	int z;
    
    	__asm{
    		mov EAX, [y+(TYPE int * 2)]//size int  * 2nd offset
    		mov z, EAX
    	}
    
    	cout << z << endl;
    
    
    
    	return 0;
    }
    Last edited by Fordy; 09-10-2002 at 06:38 PM.

  6. #6
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719

    Thanks, works great!

    Thanks Fordy for showing me what I was doing wrong, it makes sense now. I have reviewed the LEA command from some 69 page x86 ASM tutorial from the University Guadalajara(strange name) that I printed at school today. No, this is not homework in any way, this is all just personal education so nobody has to worry. However, I do have one more question about the loop sequence you used: Does the movs command subtract 1 from the ECX register, or does the loop command subtract 1 from the ECX register? Thats all I want to know. Thanks Nick for the PDF reference, and thanks Fordy for your help, its definately appreciated. Thanks.

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789

    Re: Thanks, works great!

    Originally posted by Xei
    Thanks Fordy for showing me what I was doing wrong, it makes sense now. I have reviewed the LEA command from some 69 page x86 ASM tutorial from the University Guadalajara(strange name) that I printed at school today. No, this is not homework in any way, this is all just personal education so nobody has to worry. However, I do have one more question about the loop sequence you used: Does the movs command subtract 1 from the ECX register, or does the loop command subtract 1 from the ECX register? Thats all I want to know. Thanks Nick for the PDF reference, and thanks Fordy for your help, its definately appreciated. Thanks.
    MOVS subtracts from ECX every time its used....LOOP then just keeps looping until ECX == 0....

    If you want some good ASM sources for Win32 platforms, look up Iczelion....that guy has been writting full Win32 tutorials for years...it will also link you to a free MASM package if you want it

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21