Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

addressing array elements in nasm

Tags:

assembly

nasm

I'm very new to assembly and NASM and there is a 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 through GDB eax register is set to value 123 as intended, but in ebx there is some mess instead of the array elements value.

like image 321
bvk256 Avatar asked Oct 06 '11 14:10

bvk256


People also ask

Are there arrays in assembly language?

An array is implemented at the assembly language level by a block of memory in which the variables of the array are stored, contiguously, one right after the other. When the length value stored as well, it's generally stored just before the beginning of the block, so that it can be easily found.

What is MOV in NASM?

mov dest,src. Move data between registers, load immediate data into registers, move data between registers and memory.


1 Answers

Since you're loading 32-bit values from memory, you should declare array and wordvar using dd rather than db/dw so that each entry gets four bytes:

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

Also, the indexing in the following is wrong:

mov ebx, [array+1]

You probably meant:

mov ebx, [array+1*4]
like image 65
NPE Avatar answered Sep 26 '22 01:09

NPE