Kap*_*arg 1 arrays assembly x86-64 nasm
我想在初始化的数据部分创建一个包含 5 个字符串的数据数组。每个字符串正好有 4 个字符。每个字符串都有一些初始数据,例如第一个字符串的“abcd”,第二个字符串的“efgh”等等。\0任何字符串都不需要空字符。如何用汇编语言初始化字符串数组?
这是我目前能想到的:
string db "abcdefghijklmnopqrst"
Run Code Online (Sandbox Code Playgroud)
是否有一些干净的语法或方法?
我正在使用nasm64 位代码。
第一:在汇编代码级别没有“数组”的概念,它只是由您(开发人员)设置解释的位和字节。
为您的示例实现数组的最直接方法是将字符串分解为它们自己的块:
string1: db "abcd"
string2: db "efgh"
string3: db "ijkl"
string4: db "mnop"
string5: db "qrst"
Run Code Online (Sandbox Code Playgroud)
您现在已经创建了可以作为一个单元单独引用的单个字符串块。最后一步是通过一个包含 5 个字符串中每一个的起始地址的新数据元素来声明“数组”:
string_array: dq string1, string2, string3, string4, string5
Run Code Online (Sandbox Code Playgroud)
上面现在有 5 个地址(每个占用 64 位)。
将数组的地址放入代码段中某处的寄存器中。以下是遍历数组并获取每个字符串本身的一种相当残酷的方法:
xor rdx, rdx ; Starting at offset zero
lea rdi, [string_array] ; RDI now has the address of the array
mov rsi, [rdi+rdx] ; Get the address of string1
; Process String1
; Get next string
add rdx, 8 ; Get the next offset which is 64 bits
mov rsi, [rdi+rdx] ; Get the address of string2
; Process String2
; etc.
Run Code Online (Sandbox Code Playgroud)
在不知道您对数组做什么的情况下,您的代码方法可能会有所不同。