yulaf_ve_abant
yulaf_ve_abant

Reputation: 37

Assembly language define integer variable

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.

Upvotes: 0

Views: 5464

Answers (1)

mediocrevegetable1
mediocrevegetable1

Reputation: 4207

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).

Upvotes: 4

Related Questions