Question

Here's a simple assembly language program i have written:

section .text
    global main     ;must be declared for linker (ld)
main:               ;tells linker entry point
    mov edx,len     ;message length
    mov ecx,msg     ;message to write
    mov ebx,1       ;file descriptor (stdout)
    mov eax,4       ;system call number (sys_write)
    int 0x80        ;call kernel

    mov eax,1       ;system call number (sys_exit)
    int 0x80        ;call kernel

section .data
msg db 'Hello, world!', 0xa  ;our dear string
len equ $ - msg              ;length of our dear string

now i dont know what is happening in this line: msg db 'Hello, world!', 0xa
i know the meaning of msg db 'Hello, world!' but the problem rises when i see the comma and 0xa after that..

After some research i cam to know that the 0xa causes a line break but i just dont understand why is a comma used there?

Is it some sort of concatenation or something? like the c(++) syntax: cout << "asdfsdf" << var; or perhaps the Java syntax: System.out.println("Hello,"+var+"!"); or python syntax: print 'Yo '+var

please help...

Was it helpful?

Solution

That line is simply declaring a label named msg in the data segment. msg is pointing to a byte buffer initialized with the characters Hello, world!, immediately followed by a newline character (0x0A). The line after that is calculating the length of the string by taking the difference between the current address and the msg label. The len symbol will hold that length. The use of the comma is not an operator, it's simply just a separator between the values you are using to initialize the buffer.

Strings are treated specially in most (if not all) assembly languages to make it more convenient to write. That way you don't have to write out every individual character in that string.

The following declarations are equivalent:

str1 db 'abcde'
str2 db 0x61, 'bc', 'd', 101
; 'a' is 0x61 hex
; 'e' is 101 decimal

Just be warned that they usually don't automatically include a null terminator. This is why you would typically see a string declared like this:

message db 'Hello world', 0

OTHER TIPS

The comma operator, when used with db, is a concatenation.

The reason that the newline (0xa) is concatenated to the end of the 'Hello, world!' is because when displaying the string, it is a good idea to include a newline afterwards.

Without it, if you looped this code, you would get:

Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!

But with a newline:

Hello, world!
Hello, world!
Hello, world!
Hello, world!
Hello, world!
Hello, world!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top