0

I decided to learn Assembly. And I think I'm in big trouble. ( I am using NASM )

section .data
    character_x DB 'x'

section .text
    global _start

_start:
    mov eax,4
    mov ebx,1
    mov ecx, character_x
    mov edx,1
    int 0x80

    mov eax,1
    int 0x80

The code above prints the character x. And he system call required to print something on the screen is 4 for eax. For example, how do I put the integer value of 4 in the eax register?

for example:

    mov eax, 4h
    ; OR
    mov eax, '4'

And How do I define an integer value as a bit? Or hexadimal. Example

    integer_value1 DB 00100010 ; Decimal = 34
    integer_value2 DD AF3  ; Decimal = 2803

I want to ask another question like the other stupid questions above,

cx register is Count Register. dx register is Data Register.

    mov ecx, character_x
    mov edx, 1

Why ecx register got the character itself? And Why did the edx register take the length of the character?

I think the code should have been like the one below

    mov ecx, 1
    mov edx, character_x

Thanks.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 2
    _"how do I put the integer value of 4 in the eax register?"_ `mov eax, 4`. _"Why ecx register got the character itself?"_ It doesn't. It contains the address where the character is stored. – Michael May 27 '21 at 19:53
  • 3
    1. Try searching for how to format and display a number in assembly. 2. Append a "b" for binary literals, append a "h" for hexadecimal literals. Additionally if the first hexadecimal digit is a letter you need to prepend a zero. Eg, `00100010b` or `0AF3h`. 3. The meanings as "counter" or such are only for old instructions or some special cases (eg `loop`, `rep movsw`, shift count). Linux's `int 80h` interface does not use `ecx` for "counter", just as one of several parameters (in a certain order). – ecm May 27 '21 at 19:54

1 Answers1

3

For example, how do I put the integer value of 4 in the eax register?

for example:

mov eax, 4h
; OR
mov eax, '4'

The first one moves the number 4 into eax, which is the correct number for the write syscall. The second moves the number 52 (ASCII code for the character '4') into eax, which will result in the umount2 syscall being called when you do int 80h. So I think you want the first.

And How do I define an integer value as a bit? Or hexadimal. Example

integer_value1 DB 00100010 ; Decimal = 34
integer_value2 DD AF3  ; Decimal = 2803

The NASM manual clearly states how to use binary and hexadecimal literals here. To use binary literals, simply add a 0b prefix followed by a binary number (though this is not the only way to do it). For hex, add a 0x prefix followed by a hex number (again, this is not the only way to write a hex literal). To see all the literals that NASM supports, take a look at the link.

cx register is Count Register. dx register is Data Register.

mov ecx, character_x
mov edx, 1

Why ecx register got the character itself? And Why did the edx register take the length of the character?

ecx gets the address of the character, not "the character itself". As for the actual question, because the x86 syscall calling convention applies for other functions too. The "intended meanings" of the registers are not taken into account. ebx, ecx, edx, esi, edi and ebp are just used as general parameter slots for any system call. Besides, the meanings of the registers are rarely kept in mind in most cases. As far as I know, only ecx is one which is still used in some cases as a counter, and eax is sometimes used as an accumulator for instructions like mul (and of course esp used as the stack pointer in push/pop).

mediocrevegetable1
  • 4,086
  • 1
  • 11
  • 33
  • 2
    [What are the calling conventions for UNIX & Linux system calls (and user-space functions) on i386 and x86-64](https://stackoverflow.com/q/2535989) explains how `write(int fd, void *buf, size_t len)` maps to the i386 Linux calling convention. [Why are rbp and rsp called general purpose registers?](//stackoverflow.com/a/51347294) shows implicit uses in modern compiler-generated code for all 8 legacy registers. (EBX is the rarest; only lock cmpxchg8b, and it doesn't particularly match the "base" original meaning of the reg name the way things like `rep movsb` / `rep stosb` do for ESI/EDI) – Peter Cordes May 28 '21 at 00:33