سؤال

I'm trying to understand the difference between these two commands in AT&T assembly language.

movl    %edx, %eax
movl    (%ebx), %eax

I understand that the first command takes the value in %edx and puts that value in %eax. My understanding is that the second one is ...something... to do with pointers. But wouldn't %ebx be the address and whatever value it contains be the value at that address?

I've looked a lot of different tutorials, wiki articles, powerpoints, and stack overflow posts, but I can't find anyone that's clearly explained it. The best I've seen is at http://www.scs.stanford.edu/nyu/04fa/notes/l2.pdf.

They give the example

movl    %edx, %eax    //edx = eax
movl    (%ebx), %eax  //edx = *((int32_t*) ebx)

Which confirms I was right about the first, but the second they are typecasting ebx as a pointer or..? It's part of an assignment for class which I accidentally came across the answer while searching for existing questions. The assignment is almost identical to C, Assembly : understanding the switch condition, edx eax ecx and Pointers in Assembly, but honestly I didn't read those because I don't want to learn what the answers are from the posts, I want to learn how to do it and check my work against them.

Thanks if you can help!!

هل كانت مفيدة؟

المحلول

http://en.wikipedia.org/wiki/X86_assembly_language#Syntax

The ATT syntax uses source before destination, so

movl %edx, %eax

is equivalent to

eax = edx

The more complicated example

movl (%ebx), %eax

is equivalent to

eax = *((int32 *) ebx;

The reason for the int32 is that the instruction has the letter l at the end (that's a lower case L). Different letters specify different types, but l specifies a signed 32-bit number. The parentheses around (%ebx) indicate that an effective address is being specified. An effective address has only one mandatory element (the BASE address), and 3 optional elements. In your example, only the mandatory base address is supplied. When given an instruction with an effective address, the address is computed as follows

address = base + index * scale + displacement

In the C version of the statement, casting ebx to an (int32 *) converts the value in ebx to a pointer that points to an int32 at the effective address, and then dereferencing that pointer reads the 32-bit number at that address.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top