What is .word, .data and .text in assembly?

2

I am trying to learn to get a better understanding with assembler

Could someone explain what .data, .word and .text means does in the following code??

I don't get what this is for and what it does in this case

   .data
    array:
    .word 0x12121212, 0x23232323, 0x34343434, 0x4, 0x5
    .text
    add_array:
    li x1, 0x10000000
    add x3, x0, x0
    lw x2, 0(x1)
    add x3, x3, x2
    lw x2, 4(x1)
    add x3, x3, x2
    lw x2, 8(x1)
    add x3, x3, x2
    lw x2, 12(x1)
    add x3, x3, x2
    lw x2, 16(x1)
    add x3, x3, x2
assembly
asked on Stack Overflow Feb 25, 2020 by Rapiz • edited Feb 25, 2020 by Ondrej Tucny

2 Answers

3

.word is essentially creating some space in memory to hold data. Each item following it is 4 bytes long. It's similar to creating an array:

Assembly

array:
.word 0x12121212, 0x23232323, 0x34343434, 0x4, 0x5

C

int array[] = {0x12121212, 0x23232323, 0x34343434, 0x4, 0x5};

.text tells the assembler to switch to the text segment (where code goes), and .data tells the assembler to switch to the data segment (where data goes).

answered on Stack Overflow Feb 25, 2020 by Michael Bianconi
0
.data

The .data directive starts series of variable declarations. This is sometimes called a “data segment”.

array:
.word 0x12121212, 0x23232323, 0x34343434, 0x4, 0x5

Here a variable named array is being declared with five elements, each sized to the target CPU's word. A word typically denotes 16 bits (2 bytes) or 32 bits (4 bytes) of memory, depending of the specific CPU's convention. In other words, the .word directive allocates memory.

.text
add_array:

The .text directive starts the actual program code, sometimes called a “text segment” or “code segment”.

Please note different flavors of assembly terminology exist, depending on the CPU family you are working with.

answered on Stack Overflow Feb 25, 2020 by Ondrej Tucny

User contributions licensed under CC BY-SA 3.0