How to print the length of a string in assembly

Viewed 29750

I am learning assembly using the following hello world program

section .text
    global _start     ;must be declared for linker (ld)

_start:             ;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 string
    len equ $ - msg              ;length of our string

The original question I had was what is meant by the length of the string. Does it mean the number of chars or the length in memory (number of bytes)? To check this I wanted to print the variable len. How can I do that? I naively tried to define the new variable

    len2 equ $ - len

and then instead run

   mov  edx,len2     ;message length
   mov  ecx,len     ;message to write
   mov  ebx,1       ;file descriptor (stdout)
   mov  eax,4       ;system call number (sys_write)
   int  0x80        ;call kernel

to try to print len, but this printed nothing. How can I print the number that is represented by len?

1 Answers
Related