Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

assembly - addressing array elements in 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...