Addressing array elements in nasm

I am very new to assembly and NASM, and there is code:

SECTION .data array db 89, 10, 67, 1, 4, 27, 12, 34, 86, 3 wordvar dw 123 SECTION .text global main main: mov eax, [wordvar] mov ebx, [array+1] mov ebx,0 mov eax,1 int 0x80 

Then I run the executable via GDB. The eax register has a value of 123, as was supposed, but in ebx there is some mess instead of the value of the array elements.

+4
source share
1 answer

Since you are loading 32-bit values ​​from memory, you must declare array and wordvar with dd , not db / dw , so that each record has four bytes:

 array dd 89, 10, 67, 1, 4, 27, 12, 34, 86, 3 wordvar dd 123 

Additionally, indexing in the following is incorrect:

 mov ebx, [array+1] 

You probably meant:

 mov ebx, [array+1*4] 
+5
source

All Articles