简体   繁体   English

为什么我可以移动到eax,移动到ax,但是不能移动到al,甚至可能移动到ah?

[英]Why can I mov into eax, into ax, but not into al, or even maybe into ah?

If I mov, eax 12345 and later mov var, eax (assuming var is a 32bit int etc..etc..) and output var later it'll output correctly. 如果我mov, eax 12345和更高版本的mov var, eax (假设var是一个32bit的int等..等。)然后输出var它将正确输出。

Same with ax . ax相同。 mov ax, 65535 -> mov var16, ax will output correctly. mov ax, 65535 > mov var16, ax将正确输出。

But if I mov into al or even ah it won't output anything. 但是,如果我moval ,甚至ah也不会输出任何东西。

Perhaps moving into ah not working makes sense. 也许搬进去ah不行是有道理的。 But why is that so for both? 但是为什么两者都这样呢?

typedef int int32;
typedef unsigned char int8;
typedef short int16;

int _tmain(int argc, _TCHAR* argv[])
{
    int8  c1 = 0;
    int8  c2 = 0;
    __asm
    {
        mov al, 255
        mov c1, al
    }

    cout << c1 << endl; //outputs nothing, same if I used ah

    //whereas

    int32 n = 0;
    __asm 
    {
        mov eax, 10000
        mov n, eax
    }

    cout << n << endl; // works fine, well, you get the picture

    return 0;
}

The problem is not (only) with your assembly code, it's the way you print your value: 问题不仅仅与汇编代码有关,而是您打印值的方式:

cout << c1 << endl;

Even though c1 is unsigned char (aka uint8), C++ iostreams treat it the same way they treat an ordinary char. 即使c1是无符号字符(也称为uint8),C ++ iostream对待它们的方式也与对待普通字符相同。 In other words, that code is roughly equivalent to: 换句话说,该代码大致等效于:

printf("%c\n", c1);

Just cast it to unsigned int and it should work fine. 只需将其转换为unsigned int,它就可以正常工作。

BTW, on some platforms (eg Linux PowerPC) the ordinary char is actually unsigned. 顺便说一句,在某些平台(例如Linux PowerPC)上,普通字符实际上是未签名的。

256 equals to 2^8 256等于2^8

when you use int8 , it uses twos complement approach , so the number is between [-128 , 127] 当您使用int8 ,它使用二进制补码方法,因此数字在[-128 , 127]

I think if you change its type to unsigned integer , it must work 我认为如果将其类型更改为无符号 整数 ,则它必须可以工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 RAX/EAX/AX/AL/AH 寄存器作为目标时,进位加法是否更快? - Is an addition with carry faster with RAX/EAX/AX/AL/AH registers as destination? MOV EAX, DWORD PTR SS:[EBP+8h] 是什么意思,如何翻译成 AT&amp;T 格式? - What's the meaning of MOV EAX, DWORD PTR SS:[EBP+8h] and how can I translate it into AT&T format? XOR AL,AL + MOVZX EAX,AL优于XOR EAX,EAX的任何优势? - Any advantage of XOR AL,AL + MOVZX EAX, AL over XOR EAX,EAX? 编译器在eax上来回生成一个mov - complier generating a mov back and forth on eax 为什么在C ++中追加 <eax> 在变量之后 - Why in C++ append <eax> after a variable 如何解决稀疏矩阵的线性方程AX = b - How can I solve linear equation AX=b for sparse matrix 为什么即使删除了对象也可以访问成员函数? - Why I can access member functions even after the object was deleted? 为什么即使没有调用CoInitializeEx,我也可以调用StringFromCLSID? - Why I can call StringFromCLSID even without the calling of CoInitializeEx before? 即使删除了析构函数,为什么我仍可以进行匿名联合? - Why can I make an anonymous union even though the destructor is deleted? 为什么即使它是右值,我也可以在 begin() 上使用赋值运算符? - Why can I use assignment operator on begin() even if it is an rvalue?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM