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

前端 未结 3 1512
南方客
南方客 2021-02-19 10:51

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

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-19 11:00

    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.

提交回复
热议问题