From e0b47cc83d77c9ea73461085420ef0a5e94fa3e1 Mon Sep 17 00:00:00 2001 From: hailin cai Date: Fri, 19 Feb 2016 11:58:11 -0500 Subject: [PATCH] Update Booting/linux-bootstrap-2md.md --- Booting/linux-bootstrap-2md.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Booting/linux-bootstrap-2md.md b/Booting/linux-bootstrap-2md.md index 8bc43b0..4e4328d 100644 --- a/Booting/linux-bootstrap-2md.md +++ b/Booting/linux-bootstrap-2md.md @@ -320,4 +320,8 @@ GLOBAL(memset) ENDPROC(memset) ``` -As you can read above, it uses the `fastcall` calling conventions like the `memcpy` function, which means that the function gets parameters from `ax`, `dx` and `cx` registers. \ No newline at end of file +首先你会发现,`memset`函数和`memcpy`函数一样使用了`fastcall`调用规则,因此函数的参数是通过`ax`,`dx`以及`cx`寄存器传入函数内部的。 + +就像memcpy函数一样,`memset`函数一开始将`di`寄存器入栈,然后将`biosregs`结构的地址从`ax`寄存器拷贝到`di`寄存器。记下来,使用`movzbl`指令将`dl`寄存器的内容拷贝到`ax`寄存器的滴字节,到这里`ax`寄存器就包含了需要拷贝到`di`寄存器所指向的内存的值。 + +接下来的`imull`指令将`eax`寄存器的值乘上`0x01010101`。这么做的原因是代码每次将尝试4个字节内存的内容。下面让我们来看一个具体的例子,假设我们需要将`0x7`这个数值放到内存中,在执行`imull`指令之前,`eax`寄存器的值是`0x7`,在`imull`指令被执行之后,`eax`寄存器的内容变成了`0x07070707`。The next instruction multiplies `eax` with `0x01010101`. It needs to because `memset` will copy 4 bytes at the same time. For example, we need to fill a structure with `0x7` with memset. `eax` will contain `0x00000007` value in this case. So if we multiply `eax` with `0x01010101`, we will get `0x07070707` and now we can copy these 4 bytes into the structure. `memset` uses `rep; stosl` instructions for copying `eax` into `es:di`. \ No newline at end of file