简单加法:
add eax, ebx ; Adds eax and ebx and stores the result in eax
Run Code Online (Sandbox Code Playgroud)
从内存中加载,然后添加,然后存储在内存中:
mov eax, DWORD PTR [esi] ; Load a double word from memory into eax
mov ebx, DWORD PTR [edi] ; Load a double word from memory into ebx
add eax, ebx ; Adds eax and ebx and stores the result in eax
mov DWROD PTR[esi], eax ; Store a double word in eax into memory
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,DWORD PTR 并不是严格需要的,但这是一个值得练习的好习惯,因为它消除了任何歧义并使代码更易于阅读。
请务必记住,您只能添加相同大小的寄存器(上面示例中的 DWORD)。如果要添加两个不同大小的寄存器:
mov al, BYTE PTR [esi] ; Loads a single byte from memory into al
mov bx, WORD PTR [edi] ; Loads a word from memory into bx
movzx eax, al ; Zero extends al into the entire eax register
movzx ebx, bx ; Zero extends bx into the entire ebx register
add eax, ebx ; Adds eax and ebx and stores the result in eax
Run Code Online (Sandbox Code Playgroud)
这不是一个非常好的例子,因为有更简单的方法可以做同样的事情,但希望它能展示一些您可以使用的技巧。
您还可以将内存值添加到寄存器:
mov eax, DWORD PTR [esi] ; Load a double word from memory into eax
add eax, DWORD PTR [edi] ; Add a double word in memory to eax
Run Code Online (Sandbox Code Playgroud)
这是一个可以用作参考的好网站:http ://ref.x86asm.net/