Declaring a fixed-length filled string in GNU assembler

I am trying to define some data in a fixed size structure in assembler. I would like to declare string data of a fixed number of bytes initialized by a string. In C, it will be:

char my_string[32] = "hello";

This is 32 bytes and is filled with the fact that in the end you need the number of zeros.

What will be the equivalent in assembler? I know that I can manually calculate the length of my string and declare the required number of zero bytes for laying to 32, for example:

my_string:
  .asciz "hello"
  .zero 26

But how can I do this if the string is determined dynamically, for example, from an external define or include?

+3
source share
3 answers

I think for this I would use a macro with local labels. For instance:

    .macro padded_string string, max
1:
    .ascii "\string"
2:
    .iflt \max - (2b - 1b)
    .error "String too long"
    .endif

    .ifgt \max - (2b - 1b)
    .zero \max - (2b - 1b)
    .endif

    .endm

... :

my_string:
    padded_string "Hello world!", 16

( C- , 0, max long. , , .ascii .asciz.)

+4

, , - , . my, ($). , , :

my_string:
    .ascii "mystery string"
    .zero (maxsize-($-my_string)) ;maxsize-actualsize

:

my_string:
    .ascii "mystery string"
    .zero (maxsize+my_string-$)
+3

, . .zero (32+my_string-$), Error: .space specifies non-absolute value.

:

my_string:
  .asciz "mystery string"
  .org my_string + 32
other_stuff:
  ...

, , 32 (.. , ), .asciz , .

0
source

Source: https://habr.com/ru/post/1790659/


All Articles