Search code examples
c++assemblycpu-registersmov

MOV Instruction for Registers


So I am running _asm with C++ using Visual Studio.

So I am new to assembly programming, I am following a textbook and know that the general register EAX holds 32 bits and AX is 16 with AL, AH being low/high.

So how would I move multiple commands using only the MOV instruction. I tried doing

MOV AL, 'a', 'c' , 'e'

but I get illegal amount of operands. Thats only 3 so shouldn't AL be able to hold that value?

I tried after doing

MOV EAX, 0
MOV AL, 'a', 'c' , 'e'

So how would I move three different values or even more like 12 letters to 8 bit? Shouldn't me moving 0 to eax clear out both the low and high of the 8 bit allowing those 3 variables to be moved?? I only want to use 8-bit register to move multiple values into it, in this case which is 3. Also I like to know how to do more like 12 letters into 8 Bit. I read you would make MOV EAX, 0 but I had no luck.

Note: This isn't the full program, I only included the problem with MOV.

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

using namespace std;
using std::cout;
using std::cin;


int main(void)
{

    char test
    _asm
    {
        MOV EAX, 0
        MOV AL, 'a', 'c' , 'e'
    }
}

Solution

  • In case you only got literals to push up into EAX you can use the following code:

    MOV EAX, 'ace'
    

    If you want a piece of code that can use three variables to compose the value that goes on EAX, use the following code as a template and replace literals with variable names.

    MOV EAX, 'a' * 0x10000 + 'c' * 0x100 + 'e'
    

    And finally if you'd like a solution that pushes a character by character you can use the following:

    void init()
    {
        _asm
        {
           XOR EAX, EAX
        }
    }
    
    void push(char c)
    {
        _asm
        {
            SHL EAX, 8
            MOV AL, c
        }
    }
    

    Also I like to know how to do more like 12 letters into 8 Bit. I read you would make MOV EAX, 0 but I had no luck.

    I cannot get what you mean by this, do you mean 12 characters inside EAX. EAX is 32-bit which means only 4 ASCII characters. But if what you were trying to fit into EAX are hex digits (0-9 and a-f) this means you can fit 8 of these into EAX no more.