1

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.

3
  • 3
    [] denotes a memory reference. Without it it's a literal number which can be an address. nasm doesn't keep track of variable types so you need to tell it the size if it can't be deduced. So inc byte [variable] will work. inc variable will not work as it would try to increment a literal constant (the address of variable). The final example #4 is working but you are printing a zero byte from variable. Maybe you meant mov ecx, array. Commented May 28, 2021 at 13:32
  • I don't understand what you mean about #4. Where exactly is my fault? Thank you for the other things. @Jester Commented May 28, 2021 at 13:41
  • 2
    It's unclear what you want example #4 to do. It does work. It prints the first byte of variable which is a binary zero. Maybe you wanted db 97 and not resb 97? That can only work in .data as .bss is all zeroes. resb 97 allocates 97 zeroes. Commented May 28, 2021 at 13:44

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for everything. What should I do so that I can print the letter "a" in the last code? (using variable)
@yulaf_ve_abant RESB reserves bytes. Like I said, you'll want to take another look at what RESB does. For printing 'a', you'll probably want to replace your variable resb 97 with a variable db 97 in .data or .rodata.
Note that mov eax, BYTE [array] is invalid. You might have meant movzx.
Oh my god. I thought the data in the .data section was unchangeable. ok i got it, thank you. It was written at the address TutorialPoint that the data in the .data section cannot be changed.
@yulaf_ve_abant I don't know what's up about that, because you can :/ Though as I previously mentioned, .rodata is specifically there for data that cannot be changed.
3
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)

1 Comment

Asm labels work a lot like extern char foo[] in C - using the bare label name gives the address, unlike with other kinds of C variables where there's an implicit access to the memory. char *foo in C would mean there's some data memory somewhere whose bytes hold the address; that's not the case with asm labels.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.