使用 nasm 将数字移动到内存中

moving numbers into memory using nasm

我是 nasm 编程新手。我想在变量中存储整数值

SECTION .bss
    temp:      RESB    8

SECTION .text

global  _start

_start:

    mov eax,4
    mov [temp],eax

这会将我的 integer 值移动到临时位置的开始。但是我想把它移到2nd location。由于整数占用 2 个字节,因此我不想将 4 存储在开头,而是存储在下一个位置,即 temp+2。我怎样才能做到这一点?此外,当取回值时,假设我在 temp 中有 4 个整数,每个占用 2 个字节,我将如何能够仅从 temp+2 位置检索。

要存储 2 字节整数,请使用 ax 寄存器而不是 eaxax 对应于 eax 的 2 个最低字节)。

要存储在 temp+2,请存储在 temp+2 :)

所以:

mov [temp+2], ax

您可以类似地将值检索到 ax 寄存器中:

mov ax, [temp+2]

或者您可以使用零扩展名或符号扩展名移动到 eax:

movzx eax, word [temp+2]
movsx eax, word [temp+2]

(如果值是无符号的,则使用第一个,如果是有符号的,则使用第二个)。