How is a string represented in IA32 assembly?

1

A string is represented as an array of char. For example, if I have a string "abcdef" at address 0x80000000, is the following correct?

0x80000008
0x80000004: 00 00 46 45
0x80000000: 44 43 42 41

(In stack, it grows down so I have address decreasing)

c
assembly
asked on Stack Overflow Oct 14, 2015 by Dzung Nguyen

2 Answers

5
  1. The lower addresses are always first - even in the stack. So your example should be:

    80000000: 41 42 43 44 
    80000004: 45 46 00 00
    
  2. Your example is actually the string: "ABCDEF". The string "abcdef" should be:

    80000000: 61 62 63 64
    80000004: 65 66 00 00 
    

Also, in memory dumps, the default radix is 16 (hexadecimal), so "0x" is redundant. Notice that the character codes are also in hexadecimal. For example the string "JKLMNOP" will be:

   80000000: 4A 4B 4C 4D
   80000000: 4E 4F 50 00
  1. No strings are usually placed in the stack. Only in data memory. Sometimes in the stack are placed pointers to strings, i.e. the start address of the string.

  2. Your (and my) examples concerns so called ASCII encoding. But there are many possible character encoding schemes possible. For example EBCDIC also uses 8bit codes, but different than ASCII.

But the 8 bit codes are not mandatory. UTF-32 for example uses 32 bit codes. Also, it is not mandatory to have fixed code size. UTF-8 uses variable code size from 1 to 6 bytes, depending on the characters encoded.

answered on Stack Overflow Oct 14, 2015 by johnfound
0

That isn’t actually assembly. You can get an example of that by running gcc-S. Traditionally in x86 assembly, you would declare a label followed by a string, which would be declared as db (data bytes). If it were a C-style string, it would be followed by db 0. Modern assemblers have an asciiz type that adds the zero byte automatically. If it were a Pascsl-style string, it would be preceded by an integer containing its size. These would be laid out contiguously in memory, and you would get the address of the string by using the label, similarly to how you would get the address of a branch target from its label.

Which option you would use depends on what you’re going to do with it. If you’re passing to a C standard library function, you probably want a C-style string. If you’re going to be writing it with write() or send() and copying it to buffers with bounds-checking, you might want to store its length explicitly, even though no system or library call uses that format any more. Good, secure code shouldn’t use strcpy() either. However, you can both store the length and null-terminate the string.

Some old code for MS-DOS used strings terminated with $, a convention copied from CP/M for compatibility with 8-bit code on the Z80. There were a bunch of these legacies in OSes up to Windows ME.

answered on Stack Overflow Oct 14, 2015 by Davislor • edited Oct 14, 2015 by Davislor

User contributions licensed under CC BY-SA 3.0