2

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?

This is what i can think of so far:

string    db    "abcdefghijklmnopqrst"

Is there some clean syntax or way?

I am using nasm for 64 bit code.

4
  • Are you coding for 16, 32 or 64 bit? What assembler are you using? More detail would provide a more accurate response. Commented Apr 6, 2017 at 9:52
  • 1
    What is wrong with the solution you've shown? What kind of "clean syntax" are you looking for? An array is just a sequence of bytes, nothing magical. You can choose to interpret that data as a string if you like. Commented Apr 6, 2017 at 10:03
  • Its just that my approach just not seem to show intuitively that we are initialising an array of strings. It looks like a single array of chars. Commented Apr 6, 2017 at 10:15
  • 1
    There is no such thing in assembly language as an array of strings. That's just an interpretation of the data that you, the programmer, choose to have. All there is is data. Commented Apr 6, 2017 at 10:16

1 Answer 1

8

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"

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

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.

Sign up to request clarification or add additional context in comments.

2 Comments

can we use mov rdi, string_array to move base array address ?
@KapilGarg - I suggest you read the various ways you can do things in nasm in their documentation found here-> nasm.us

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.