请帮我理解这个.... [英] Please help me understand this....

查看:142
本文介绍了请帮我理解这个....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的疑问,如果它被清除会很好。



我只是在写我所知道的,如果我错了请纠正我。



32位寄存器只能容纳32位或4字节的数据,但是当我编写并执行语句

I have a major doubt it would be good if it is cleared.

I am just writing what I know, please correct me if I am wrong.

A 32bit register can hold 32bits or 4 bytes of data only, but when I write and execute the statement

msg db 'Hello, world!',0xa
mov ecx,msg



它是如何有效的?字符串'hello world!',即使是ASCII表示也是11个字节,然后最后还有一个空字节(如果我错了请纠正我)。

那么这是怎么发生的?只有指向这个字符串所在的内存地址的指针放入ecx吗?



之前我从来没有注意到这一点,如果你真的很棒可以帮帮我。



谢谢。



编辑:我也想知道关键字是什么


How is it that it is valid? The string 'hello world!', even in ASCII representation is 11 bytes and then there is a null byte also at the end (please correct me if I am wrong).
So how does this happen? Is it that only the pointer to the memory address where this string is located is put into ecx?

I never paid attention to this before, it will be great if you can help me.

Thanks.

I also want to know what does the keyword

db

做。我所知道的是它意味着'定义字节',如何将11字节数据存储到1字节存储器中?

do. What I know is that it means 'define byte', how do you store a 11 byte data into 1 byte memory?

推荐答案

你如何存储11字节数据到1字节存储器?



:笑:

你没有。



db 分配字节,而不只是一个字节 - 所以如果你使用

"how do you store a 11 byte data into 1 byte memory?"

:laugh:
You don't.

db allocates bytes, not just a byte - so if you use
msg db 'Hello, world!',0xa

然后它将为填入数据的十四个字符串分配一个区域,而不是单个字节。标签 msg 将包含该字符串的第一个字节的地址。

所以当你执行

then it will allocate an area for an fourteen character string with the data filled in, not a single byte. The label msg will contain the address of the first byte of that string.
So when you execute

mov ecx,msg

第一个字节的地址放在寄存器中。





我忘了问的另一件事是,如果'db'分配字节,'dw','dd'等的目的是什么?





它们分配不同的大小:

db以一个字节为单位分配,并与单字节地址对齐。 />
dw以两个字节为单位进行分配,并与下一个两字节地址对齐。

dd以四个字节为单位进行分配,并与下一个四字节地址对齐。 />


对齐很重要:当您访问一个字节时,您可以使用任何地址。当您访问一个字时,忽略该地址的最低有效位,因为字总是应该在字边界上。如果您为字取指令或存储指令提供奇数地址,它将在错误的位置读取或写入。

假设地址从十六进制100开始:

The address of the first byte is placed in the register.


"Another thing that I forgot to ask is that if 'db' allocates bytes, what is the purpose of having 'dw', 'dd', etc.?"


They assign different sizes:
db allocates in units of one byte, and aligns to a single byte address.
dw allocates in units of two bytes, and aligns to the next two byte address.
dd allocates in units of four bytes, and aligns to the next four byte address.

The alignment is important: when you access a byte, you can use any address. When you access a word, the least significant bit of the address is ignored, because words are always supposed to be on a word boundary. If you provide an odd-numbered address to a word fetch or store instruction, it will read or write at the wrong location.
Suppose the address starts at hex 100:

x   db   1
y   dw   1001



如果没有发生对齐,那么x将是0x100而y会是0x101(我们将忽略段,并在此处进行32/64位寻址)。

因此,当您写入y时,处理器中的硬件执行基于字的指令并舍入该地址通过忽略最低有效位来写下来。因此,对y的写入也会覆盖x!



dw通过使x保持0x100和y0x102来强制对齐对于字操作是正确的 - dd对4字节地址执行相同操作等等。



有意义吗?


If the alignment didn't happen, then x would be "0x100" and y would be "0x101" (We'll ignore segments, and 32 / 64 bit addressing here).
So when you write to y the hardware in the processor executes a word based instruction and "rounds" the address to write to down by ignoring the least significant bit. As a result the write to y overwrites x as well!

dw forces the alignment to be correct for word operations by making x hold "0x100" and y "0x102" - dd does the same for 4 byte addresses and so forth.

Make sense?


msg dbABC





上面的ABC位于该内存地址,该内存地址标记为msg。所以,如果我的代码在内存地址10000,那么下面的程序集



msg dbABC

NOP



将导致



10000 A

10002 B

10004 C

10006 00(00是NOP)



内存地址10000现在称为msg,我的NOP操作存储在10006.如果我的代码是



msg dbABCD

NOP



我会有



10000 A

10002 B

10004 C
10006 D

10008 00(00为NOP)



mov ecx,msg表示进行注册ecx等于值msg。记住msg是10000,所以ECX是10000,因为10000是我的字符串可以找到的地址。所以我的字符串有多长并不重要,我的所有代码都需要知道它是从哪里开始的,它从10000开始。它如何知道字符串有多长?它没有。在汇编中,您需要进行自己的内存管理。处理字符串的某些函数可能会在遇到特殊值时将字符串视为结束,例如0或
msg db "ABC"


the above places "ABC" at that memory address and that memory address is labelled as "msg". So if my code is at memory address 10000 then the following assembly

msg db "ABC"
NOP

will result in

10000 A
10002 B
10004 C
10006 00 (00 being the NOP)

Memory address "10000" is now known as "msg", and my "NOP" operation is stored at 10006. If my code was

msg db "ABCD"
NOP

I would have

10000 A
10002 B
10004 C
10006 D
10008 00 (00 being the NOP)

the "mov ecx, msg" means make the register ecx equal the value msg. Remember msg is 10000, so ECX is 10000, as 10000 is the address where my string can be found. So it doesn't matter how long my string is, all my code needs to know is where it starts, and it starts at 10000. How does it know how long the string is? It doesn't. In assembly you need to do your own memory management. Some functions that process the string might consider the string to be ended when it encounters a special value, like "0" or "


,或者该函数是否已编码。某些函数可能需要字符串的地址和字符串的长度,因此它知道要处理多少个字符,并且您必须告诉该函数该地址是10000而且长度是3 \\\\whatever 。在进行汇编时,你必须记住内存分配,知道它们有多长的字符串变量,知道它们有多大的类,都是更高级语言提供的所有功能,所以你不必担心它是一个编码器。使用汇编程序时,没有这样的细节,你必须自己进行变量和内存管理。
", or however that function is coded. Some functions might require both the address of the string and the length of the string so it knows how many characters to process, and you'll have to tell that function that the address is 10000 and the length if 3\4\whatever. When doing assembler you have to remember that memory allocation, and string variables knowing how long they are, classes knowing how big they are, are all features provided by the higher level language so you don't need to worry about it as a coder. With assembler there are no such niceties and you have to do your own variable and memory management.


这篇关于请帮我理解这个....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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