0

I'm new on assembly languages and I'm trying to learn the intel 32bit assembly language on linux using NASM and following the assembly tutorial of tutorialspoint. I've got some questions on this example:

section .text
    global _start       ;must be declared for using gcc
_start:                     ;tell 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

I know that name db 'Zara Ali' it's a shorthand of name db 'Z','a','r','a'.... but

  1. How 'name' variable is stored in the ecx register if it doesn't fit in it? (I mean when we want to write Zara Ali on output)
  2. Is the system call (0x80) repeated for each character?
lurker
  • 56,987
  • 9
  • 69
  • 103
GJCode
  • 1,959
  • 3
  • 13
  • 30
  • 7
    `mov ecx, name` moves the *address* of `name` to `ecx`, not the content of what's at that address. The system call, `sys_write`, writes out the whole string. That's why the length is passed as an argument to `sys_write` (the `mov edx, len` instruction in the linked example). You can look up the system calls online for descriptions. Just do a Google search on "linux sys_write". – lurker Mar 12 '19 at 11:03
  • oh yes you're right thanks! – GJCode Mar 12 '19 at 11:18

2 Answers2

1

To answer your second question. No, the system call is made only once with the 'int 0x80' instruction. Then control is transferred to sys_write and it writes all those characters to stdout. There are no jmp instructions or loops in this code, from which you can see that code is executed sequentially with no jumps (excluding the syscall itself which is called only once).

rmn
  • 81
  • 1
  • 11
1

The instruction mov ecx, msg does not move the array contents rather, it simply puts a pointer to the variable into the ecx register. If you would like to load 4 bytes from that address into ecx, you'd use square brackets around the address: mov ecx, [msg]. (Normally you wouldn't want that for string data, since that's only part of the value of a longer string, and the write(int fd, void *buf, size_t len) system call needs a pointer.)

Secondly, the system call 0x80 and all other system calls made with the int instruction immediately transfer control to the kernel which decides what to do with it, in this case write to standard out.

So in a nutshell, mov ecx, msg moves the pointer to the variable into ecx not the actual thing. And secondly, int 0x80 is only called once.

If you did want to load one byte from the string,movzx eax, byte [ecx] is a standard way, zero-extending it into a dword register.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
Markian
  • 322
  • 1
  • 12