Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the syntax to define a string constant in assembly?

I am learning assembly I see two examples of defining a string:

msg db 'Hello, world!',0xa

  • what does the 0xa mean here?

message DB 'I am loving it!', 0

  • why we have a 0 here?
  • is it a trailing null character?
  • why we have 0xa the above example but 0 here? (doesn't seem they are relating to string length)

If the above examples are two ways of defining an assembly string, how could the program differentiate them?

Thanks ahead for any help :)

like image 356
DuoD Avatar asked Jan 01 '26 04:01

DuoD


2 Answers

The different assemblers have different syntax, but in the case of db directive they are pretty consistent.

db is an assembly directive, that defines bytes with the given value in the place where the directive is located in the source. Optionally, some label can be assigned to the directive.

The common syntax is:

[label]  db  n1, n2, n3, ..., nk

where n1..nk are some byte sized numbers (from 0..0xff) or some string constant.

As long as the ASCII string consists of bytes, the directive simply places these bytes in the memory, exactly as the other numbers in the directive.

Example:

db 1, 2, 3, 4

will allocate 4 bytes and will fill them with the numbers 1, 2, 3 and 4

string  db 'Assembly', 0, 1, 2, 3

will be compiled to:

string:  41h, 73h, 73h, 65h, 6Dh, 62h, 6Ch, 79h, 00h, 01h, 02h, 03h

The character with ASCII code 0Ah (0xa) is the character LF (line feed) that is used in Linux as a new line command for the console.

The character with ASCII code 00h (0) is the NULL character that is used as a end-of-string mark in the C-like languages. (and probably in the OS API calls, because most OSes are written in C)

Appendix 1: There are several other assembly directives similar to DB in that they define some data in the memory, but with other size. Most common are DW (define word), DD (define double word) and DQ (define quadruple word) for 16, 32 and 64 bit data. However, their syntax accepts only numbers, not strings.

like image 59
johnfound Avatar answered Jan 03 '26 22:01

johnfound


0 is a trailing null, yes. 0xa is a newline. They don’t define the same string, so that’s how you would differentiate them.

like image 44
Ry- Avatar answered Jan 04 '26 00:01

Ry-



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!