home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.asm.x86      Ahh, the lost art of x86 assembly      4,675 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 3,698 of 4,675   
   Rod Pemberton to James Harris   
   Re: String literals in asm source code   
   22 Dec 18 02:34:10   
   
   From: invalid@nospicedham.lkntrgzxc.com   
      
   On Fri, 21 Dec 2018 19:49:50 +0000   
   James Harris  wrote:   
      
   > What's the most readable way to include string literals in asm source   
   > code?   
   >   
   >   
   >   
   > Option 1   
   > ========   
   >   
   > I usually define string literals in a separate block, like this.   
   >   
   > ;**********   
   > ;   
   > ; Data section   
   > ;   
   > ;**********   
   > section .data   
   > msg_started:         db "Operation started", 0   
   > msg_finished_errors: db "Operation finished. Number of errors: ", 0   
   >   
   > Then, later,   
   >   
   > ;**********   
   > ;   
   > ; Text section   
   > ;   
   > ;**********   
   >    mov ebx, msg_started   
   >   
   >   
   > The downside to that is that the message can be separated from its   
   > use by a few screens-worth of scrolling.   
   >   
   >   
   > Option 2   
   > ========   
   >   
   > To avoid a large separation between def and use one could temporarily   
   > drop to the data section as needed in the middle of other code (I'll   
   > use nops to indicate other executable code).   
   >   
   >    nop   
   >    nop   
   >    nop   
   > section .data   
   > msg_started: db "Operation started", 0   
   > section .text   
   >    mov ebx, msg_started   
   >   
   > The downside of that is it is arguably harder to read (and doesn't   
   > deal with duplicate strings well).   
   >   
   >   
   > Option 3   
   > ========   
   >   
   > Or, maybe a macro could effect option 2 - something like the   
   > following. (This is illustrative, not tested code.)   
   >   
   >    mov ebx, string_literal(db "Operation started", 0)   
   >   
   >   
   >   
   > Of course, code layout is not a major issue but it is one of   
   > convenience; and readability is important. So I wondered what other   
   > people do to incorporate strings in code. What have you found to be   
   > the most readable and easiest to work with?   
   >   
      
   I usually just have a single .text section for my assembly code, i.e.,   
   for small amounts of assembly code, as I prefer C to assembly.  The   
   strings are labeled and placed at the end-of-file.  Usually, the label   
   has sufficient meaning, in English or to me personally, as to not   
   require (re)reading the string:   
      
   .text   
       mov ebx, msg_op_start   
       ; ...   
      
    at end-of-file:   
       msg_op_start:       db "Operation started", 0   
       msg_op_fin_errs:    db "Operation finished. Number of errors: ", 0   
      
      
   Some other possible options are:   
      
   1) inline the string, i.e., place it in the code and jump over it:   
      
       jmp nxt   
       msg_started:         db "Operation started", 0   
   nxt:   
       mov ebx, msg_started   
      
      
   2) inline the string another way, i.e., place it inbetween routines,   
   e.g., it could be placed prior to or after any routine, but is more   
   "readable" IMO prior to proc2 below than after proc1 below:   
      
   proc1:   
       mov ebx, msg_started   
       ; ...   
       ret   
      
    after proc1   
    before proc2   
      
       msg_started:         db "Operation started", 0   
      
   proc2:   
       mov ebx, msg_started   
       ; ...   
       ret   
      
      
   3) use a ; comment to describe the string on the code line that uses it:   
      
       mov ebx, msg_started  ; msg_started is "Operation started"   
      
      
   Obviously, I believe there are disadvantages to each of these.  These   
   are left for the reader to identify, and then choose.   
      
      
   Rod Pemberton   
   --   
   Why isn't the SpaceX car considered to be space junk? Elon Musk, space   
   polluter.   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca