yulaf_ve_abant
yulaf_ve_abant

Reputation: 37

Understanding pointers in assembly language

Are we enclosing the variable or register in brackets to specify a pointer in assembly?

Example1;

    MOV eax, array+4
    LEA eax, [array+4]

Example2;

section .data
    array DB 116,97

section .bss
    variable RESB 0

section .text
    global _start:

_start:
    mov eax,[array]

    ;exit
    mov eax,1
    int 0x80

I am not getting any errors while compiling or running the above code. Is the address of the zero index of the array placed in the EAX register?

Example3;

    INC [variable]

When compiling the above code, I am getting the "operation size not specified" error. And why can't the command be used as INC variable?

Example4;

section .data
    array DB 116,97

section .bss
    variable RESB 97

section .text
    global _start:

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

    ;exit
    mov eax,1
    int 0x80

And this code is not working.

Upvotes: 1

Views: 1688

Answers (2)

keijeizei
keijeizei

Reputation: 80

array            ; variable address
byte[array]      ; value of first byte of array
word[array]      ; value of first word of array
byte[array + 1]  ; value of second byte of array

Think of the variable names as pointers, and using size[name] gets the value being pointed (similar to *name in C where name is a pointer)

Upvotes: 3

mediocrevegetable1
mediocrevegetable1

Reputation: 4217

Are we enclosing the variable or registrar in brackets to specify a pointer in assembly?

Example1;

MOV eax, array+4
LEA eax, [array+4]

The brackets are like the dereference operator in C (*ptr). They get the value at the resulting address inside the square brackets. As for the example, both of these essentially do the same thing. The first moves the address of the array label + 4 into eax. The second uses lea, which loads the effective address of its source operand. So you get array + 4, dereference it, and get the address again with lea and load it into eax.

Example2;

section .data
    array DB 116,97

section .bss
    variable RESB 0

section .text
    global _start:

_start:
    mov eax,[array]

    ;exit
    mov eax,1
    int 0x80

I am not getting any errors while compiling or running the above code. Is the address of the zero index of the array placed in the eax register?

Kind of. Since you're moving it into eax, a 32-bit register, it is assumed that you want to move the first 4 bytes at the address array into eax. But there are only 2 bytes at array: 116 and 97. So this is probably not what you intended. To load the first byte at array into eax, do movzx eax, BYTE [array], which will move array[0] into the LSByte of eax and zero out the higher bytes. mov al, [array] will also work, though it won't zero out the upper bytes.

Example3;

INC [variable]

When compiling the above code, I am getting the "operation size not specified" error. And why can't the command be used as INC variable.

The error says it all. variable is just an address. When you use [], how many bytes should it take? You need to specify a size. For example to get the first byte, you would do inc BYTE [variable]. However, from the previous example, it seems like you've reserved nothing at variable, so trying to access any bytes at it may cause some issue. As for "And why can't the command be used as INC variable", as I just said, variable is just a label which translates to some address. You can't change the address which variable translates to.

Example4;

section .data
    array DB 116,97

section .bss
    variable RESB 97

section .text
    global _start:

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

    ;exit
    mov eax,1
    int 0x80

And this code is not working.

It may seem to not be printing anything, but it actually is. .bss zero-initializes any memory that you reserve. That means when you print the first byte at variable, it just prints the NUL character. However, this doesn't seem to be visible for you when you print it, so it seems like nothing has been printed.

(By the way, are you certain that you know what resb does? In one example, you reserve 0 bytes, and in another, you reserve 97 bytes for no apparent reason. You might want to take another look at what resb actually does.)

Upvotes: 4

Related Questions