Search code examples
stringassemblyx86nasmstack-memory

Should we store long strings on stack in assembly?


The general way to store strings in NASM is to use db like in msg: db 'hello world', 0xA. I think this stores the string in the bss section. So the string will occupy the storage for the duration of the entire program. Instead, if we store it in the stack, it will be alive only during the local frame. For small strings (less than 8 bytes), this can be done using mov dword [rsp], 'foo'. But for longer strings, the string has to be split and be stored using multiple instructions. So this would increase the executable size (I thought so).

So now, which is better in large programs with multiple strings? Are any of the assumptions I made above, wrong?


Solution

  • mov dword [rsp] 'foo' assembles to C70424666F6F00, it takes 7 bytes to encode 4 payload characters.
    In comparison with standard static way DB 'foo',0 the definition of string as immediate operand in code section increases the code size by 75 %.

    Your dynamic method may be profitable only if you could eliminate the .rodata or .data section entirely (which is seldom the case of large programs). Each additional section takes more space in executable program than its netto contents because of its file-alignment (in PE format it is 512 bytes). And even when your program has no other static data in data sections beside long strings, you could spare more space with static declaration in .text (code) section.