在程序集中初始化字符串数组 [英] Initialise array of strings in assembly

查看:43
本文介绍了在程序集中初始化字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在初始化的数据部分创建一个包含 5 个字符串的数据数组.每个字符串正好有 4 个字符.每个字符串都有一些初始数据,例如第一个字符串的abcd",第二个字符串的efgh"等等.任何字符串都不需要空 \0 字符.如何用汇编语言初始化字符串数组?

I want to create a data array holding 5 strings in the initialised data section. Each string has exactly 4 characters. Each string has some initial data like "abcd" for first string, "efgh" for second string and so on. Null \0 character is not needed for any string. How can i initialise array of strings in assembly language?

这是我目前能想到的:

string    db    "abcdefghijklmnopqrst"

有一些干净的语法或方法吗?

Is there some clean syntax or way?

我将 nasm 用于 64 位代码.

I am using nasm for 64 bit code.

推荐答案

第一:在汇编代码级别,没有数组"的概念,它只是由您(开发人员)设置解释的位和字节.

First: At the assembly code level there is no notion of an "array", it is just bits and bytes to be setup interpreted by you, the developer.

为您的示例实现数组的最直接方法是将字符串分解为它们自己的块:

The most straight forward way to achieve an array for your example would be to break up the strings into their own block:

string1: db "abcd"
string2: db "efgh"
string3: db "ijkl"
string4: db "mnop"
string5: db "qrst"

您现在已经创建了可以作为一个单元单独引用的单个字符串块.最后一步是通过一个包含 5 个字符串中每一个的起始地址的新数据元素来声明数组":

You've now created individual string blocks which can be referenced as a unit individually. The final step would be to declare the "array" through a new data element that contains the starting address of each of the 5 strings:

string_array: dq string1, string2, string3, string4, string5

上面现在有 5 个地址(每个占用 64 位).

The above now holds 5 addresses (each occupying 64 bits).

将数组的地址放入代码段中某处的寄存器中.以下是遍历数组并获取每个字符串本身的一种相当粗暴的方法:

One gets the address of the array into a register somewhere in your code segment. The following is a rather brutal way to go about traversing the array and getting each string itself:

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.

在不知道您对数组做什么的情况下,您的代码方法可能会有所不同.

Without knowing what you are doing with the array your code approach may vary.

这篇关于在程序集中初始化字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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