将大寄存器移入小内存 [英] move large register into small memory

查看:106
本文介绍了将大寄存器移入小内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解PTR运算符时遇到了一些问题,我可以轻松地将大内存移到下面的小寄存器示例中:

I have a little problem understanding PTR Operator, I can easily move large memory into small register example below:

ax = small register which is 2 byte
memory = variable which is 4 byte


.data
variable dword 05010h

.code
mov ax,WORD PTR variable

它将正常工作. 现在我的问题是我不明白为什么我不能将像eax这样的大寄存器移到一个2字节的小内存变量中?

It will works fine. Now my problem is I don't understand why I can't move large register like eax into a small memory variable which is 2 byte?

示例:

.data
variable word ?

.code

mov eax, 01050h
mov variable, word ptr eax

它说:Error invalid use of register

注意:如果大小匹配,它会很好地工作

Note: it works fine if the size is match

推荐答案

问题是您显然不知道以下指令的作用:

The problem is that you are obviously not aware what the following instruction does:

mov ax, word ptr variable

该指令不会读取32位变量并将变量写入16位寄存器!

This instruction does not read the 32-bit variable and write the variable to a 16-bit register!

但是它有什么作用?

通常,CPU不知道哪种数据类型存储在某个存储位置中.地址variable上的内存可能包含以下字节:

In general the CPU does not know which data type is stored in a certain memory location. Memory at the address variable may contain the following bytes:

01h 02h 03h 04h

这可以是四个8位变量,两个16位变量(值分别为201h和403h),一个32位变量(值分别为4030201h)或什至一个16位和两个8位变量.

This can be four 8-bit variables, two 16-bit variables (with the values 201h and 403h), one 32-bit variable with the value 4030201h or even one 16-bit and two 8-bit variables.

由于CPU不知道存储器中存储了哪种变量,因此由程序员决定(或由编译器)以16位变量的方式编写汇编程序作为16位变量进行访问.

Because the CPU does not know which kind of variables is stored in the memory it is up to the programmer (or the compiler) to write an assembler program in a way that a 16-bit variable is accessed as 16-bit variable.

指令mov ax, variable的意思是:"RAM中的位置(地址)variable中有一个16位变量.对其进行读取并将其写入ax寄存器."

The instruction mov ax, variable means: "There is a 16-bit variable in the RAM at the location (address) variable. Read it and write it into the ax register."

CPU将执行此操作.因为在我们的示例中内存中包含字节01h 02h 03h 04h,所以它将读取两个字节01h 02h并将其解释为16位变量(具有值201h).

The CPU will do this. Because the memory contains the bytes 01h 02h 03h 04h in our example it will read the two bytes 01h 02h and interpret them as 16-bit variable (having the value 201h).

使用所谓的"little-endian" CPU(如x86),当32位变量具有"small"值时,这将导致正确"值,因为该指令将有效地读取存储器的低16位. 32位变量.

Using so-called "little-endian" CPUs (like x86) this will result in the "correct" value when the 32-bit variable has a "small" value because the instruction will effectively read the low 16 bits of the 32-bit variable.

顺便说一句:使用"big-endian" CPU(如PowerPC)将无法再工作,因为将读取32位变量的高(而不是低)16位.

By the way: Using "big-endian" CPUs (like PowerPC) even this would not work any more because the high (and not the low) 16 bits of the 32-bit variable would be read.

当然,您也可以将包含16位变量的内存位置读取到32位寄存器中:

Of course you could also read a memory location containing a 16-bit variable into a 32-bit register:

mov eax, variable

在上面的示例中,01h 02h 03h 04h是两个具有值201h和403h的16位变量.如果您以32位值读取此RAM内存,则会得到4030201h,而不是201h!

Let's say in the example above 01h 02h 03h 04h are two 16-bit variables having the values 201h and 403h. If you read this RAM memory as 32-bit value you'll get 4030201h as result, not 201h!

x86 CPU(自80386开始)具有两条指令movsx(高位符号扩展)和movzx(将高位设置为零),用于将较小的值复制到较大的寄存器中.

x86 CPUs (since the 80386) have two instructions movsx (sign-extend high bits) and movzx (set high bits to zero) which are used to copy a smaller value into a larger register.

这些指令允许您正确"将16位值读入32位寄存器.

These instructions allow you to "correctly" read a 16-bit value into a 32-bit register.

编辑

根据彼得·科德斯(Peter Cordes)的说法,MASM在mov eax, dword ptr variable中明确要求dword ptr,因为MASM否则将检查variable是否真的是32位并打印错误消息.

According to Peter Cordes MASM explicitly requires a dword ptr in mov eax, dword ptr variable because MASM will otherwise check if variable is really 32-bit and print an error message.

对于CPU,指令mov eax, variablemov eax, dword ptr variable是完全相同的指令.

For the CPU the instructions mov eax, variable and mov eax, dword ptr variable are however exactly the same instruction.

mov variable, word ptr eax

它说:Error invalid use of register

该指令可能会被命名为(我不确定,因为我不使用MASM):

The instruction would probably (I'm not sure as I don't use MASM) be named:

mov dword ptr variable, eax

但是,您应该知道该指令的作用:它会覆盖内存中的32位:variable和内存中紧随variable的数据.我怀疑这是您真正想要做的.

However you should be aware what this instruction will do: It will overwrite 32 bits in memory: The variable and the data which is following variable in memory. I doubt that this is what you really want to do.

如果要像下面的C代码一样将32位值转换"为16位值:

If you want to "convert" a 32-bit value into a 16-bit value like the following C code:

variable16 = (unsigned short)variable32;

...以下汇编代码将完成此工作:

... the following assembler code will do the job:

mov eax, variable32
mov variable16, ax

...,因为axeax的低16位.

... because ax is the low 16 bits of eax.

这篇关于将大寄存器移入小内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆