Understanding X86 Assembly dword ptr & 'and' Commands

In summary, the mov eax, dword ptr [esi+4*ebx] instruction copies the contents of memory address esi + 4*ebx into the eax register, where esi is the address of the array and ebx is the index. This allows for the manipulation of specific elements within the array. The and eax, 1 instruction performs a bitwise and of the value in eax with the constant 1, altering the value stored in eax.
  • #1
whitehorsey
192
0
1. I'm stuck on understand two lines of code because I don't really understand how the command (dword ptr and 'and') works.

array = {40, 86, 10, 14, 68, 25, 50, 7, 9}

int method(int array[])
{
__asm{
mov ebx, 0
mov esi, array

(there is a loop that goes around ten times before exiting in the body there is this line of code ( written below))

mov eax, dword ptr [esi+4*ebx] What is happening in this line of code?
and eax, 1 Also, what does the and command do?

...
}
}



3. I know that dword ptr stands for size directive but I don't understand how it works in this context.
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
whitehorsey said:
1. I'm stuck on understand two lines of code because I don't really understand how the command (dword ptr and 'and') works.

array = {40, 86, 10, 14, 68, 25, 50, 7, 9}

int method(int array[])
{
__asm{
mov ebx, 0
mov esi, array

(there is a loop that goes around ten times before exiting in the body there is this line of code ( written below))

mov eax, dword ptr [esi+4*ebx] What is happening in this line of code?
and eax, 1 Also, what does the and command do?

...
}
}



3. I know that dword ptr stands for size directive but I don't understand how it works in this context.

In the mov eax, dword ptr [esi+4*ebx] instruction, the expression esi + 4*ebx represents a value that is to be treated as an address, making the value at esi + 4*ebx effectively a pointer. Further, it is a pointer to a dword (32 bits). The effect of this instruction is to copy the contents of memory address esi + 4*ebx into the eax register.

The and performs a bitwise and of the value in eax with the constant 1. If you are writing assembly code, you should have some documentation for the various x86 instructions. Look up the and instruction to see which flag registers it sets for different combinations of arguments.
 
  • #3
Code:
        mov     eax, dword ptr [esi+4*ebx]

In this case "dword ptr" is redundant and not needed, since the destination register, eax, is a 32 bit register. "dword ptr" just tells the assembler the operand size and is only needed when the destination size isn't directly implied by the instruction. Some examples:

Code:
        mul     dword ptr [...]         ;mul can be dx:ax, edx:eax, rdx:rax
        mov     dword ptr [...],012h    ;move immediate to memory

- - -

Code:
        mov     eax, dword ptr [esi+4*ebx]

So getting back to this instruction, esi is the address of (pointer to) the array, and ebx is the index, multiplied by 4 since each element of the array is 4 bytes in size.
 
  • #4
rcgldr;4135758 [code said:
mov eax, dword ptr [esi+4*ebx]
[/code]

So getting back to this instruction, esi is the address of (pointer to) the array, and ebx is the index, multiplied by 4 since each element of the array is 4 bytes in size.

Mark44 said:
In the mov eax, dword ptr [esi+4*ebx] instruction, the expression esi + 4*ebx represents a value that is to be treated as an address, making the value at esi + 4*ebx effectively a pointer. Further, it is a pointer to a dword (32 bits). The effect of this instruction is to copy the contents of memory address esi + 4*ebx into the eax register.

Thank You! I'm still confused on what the result would be if ebx = 1?

I'm thinking that when ebx = 0 then
eax = esi = array

However, when ebx = 1 then it would be esi+4. By adding 4 to esi what happens to esi (since it stores an array does it go to index 4?) and what would be stored in eax. I'm confused on how this would change/affect the array...

Also to make sure, ebx is the index in the array so when it is equal to 0 it is pointing to 40?
 
Last edited:
  • #5
whitehorsey said:
I'm thinking that when ebx = 0 ... it is pointing to 40 ... ebx = 1 ...

From my previous post:

Code:
        mov     eax, dword ptr [esi+4*ebx]

So getting back to this instruction, esi is the address of (pointer to) the array, and ebx is the index, multiplied by 4 since each element of the array is 4 bytes in size.

- - - continuing with the explanation

only eax is modified by this instruction. esi and ebx are used to calculate an address, but they are not modified. Assume the array of 32 bit integers is in memory, starting at location 0x200:

[0x200] = 40
[0x204] = 86
[0x208] = 10

The instruction mov esi,array will set esi = 0x200, the address of array.

The intruction mov ebx,0 wil set ebx = 0.

The instruction mov eax, dword ptr [esi+4*ebx], will move the 32 bit integer in memory located at the address of esi + 4*ebx which equals 0x200 + 4*ebx into eax. If ebx = 0, then eax = [0x200 + 4*0] = [0x200], setting eax = 40. If ebx = 1, then eax = [0x200 + 4*1] = [0x204], setting eax = 86.

If you write working code, some source level debuggers have an assembly window and a register window, which will let you step through the assembly instructions one at a time, so you can see what is happening. If you're running windows, microsoft visual c / c++ express (free) includes a source level debugger with these features.
 
Last edited:
  • #6
rcgldr said:
...The instruction mov eax, dword ptr [esi+4*ebx], will move the 32 bit integer in memory located at the address of esi + 4*ebx which equals 0x200 + 4*ebx into eax. If ebx = 0, then eax = [0x200 + 4*0] = [0x200], setting eax = 40. If ebx = 1, then eax = [0x200 + 4*1] = [0x204], setting eax = 86...

Thank You for the clear explanation! Could you also explain the and eax, 1? I looked up the instructions on x86 Assembly Guide but the example is not that clear.

I'm thinking that since and is a logical operation if you compare it to the array of integers ex. 'and eax(= 40), 1' wouldn't that just return false for every number in the array?

I'm not sure if this would affect the outcome of the and eax, 1 code,but whatever is stored into eax I have to change it into hexadecimal or decimal form.
 
  • #7
whitehorsey said:
Could you also explain the and eax, 1?
and eax,1, is a binary operation that changes the value in eax. The C equivalent would be eax = (eax & 0x01);.
 
  • #8
rcgldr said:
and eax,1, is a binary operation that changes the value in eax. The C equivalent would be eax = (eax & 0x01);.

Do you know what it's java equivalent would be? I haven't learned C yet.
 
  • #9
whitehorsey said:
Do you know what it's java equivalent would be? I haven't learned C yet.
About the same:

eax = eax & 1
 
  • #10
rcgldr said:
About the same:

eax = eax & 1

Could you explain what it does? I looked up some examples online but they didn't explain how they solved it. One of the examples I saw was this:
byte x = 50;
byte y = 51;
byte result = (byte) (x&y);

00110010
00110011
-------------
00110010

The result is 50.
What did they do to get this answer?
 
  • #12
rcgldr said:

Thanks! Out of all the websites, I forgot to check wiki...
Wiki says that the binary has to be of equal length however 40 and 1 are not equal in length
40 = 101000
1 = 1

How would this work? Do you I just add extra 0's like 000001?
 
  • #13
whitehorsey said:
Thanks! Out of all the websites, I forgot to check wiki...
Wiki says that the binary has to be of equal length however 40 and 1 are not equal in length
40 = 101000
1 = 1
How would this work? Do you I just add extra 0's like 000001?
Yes, just prefix the shorter number with leading zeroes. Remember that the program is working with 32 bit integers so the numbers are actually:

40 = 00000000000000000000000000101000
01 = 00000000000000000000000000000001
 
  • #14
rcgldr said:
Yes, just prefix the shorter number with leading zeroes. Remember that the program is working with 32 bit integers so the numbers are actually:

40 = 00000000000000000000000000101000
01 = 00000000000000000000000000000001

Thank You for all your help! :smile:
 

Related to Understanding X86 Assembly dword ptr & 'and' Commands

1. What is X86 Assembly?

X86 Assembly is a low-level programming language used to write instructions for the x86 family of processors. It is often used for system-level programming and is known for its speed and efficiency.

2. What does the term "dword ptr" mean in X86 Assembly?

"dword ptr" is a keyword used to specify a double-word (32-bit) memory location in X86 Assembly. It is commonly used to store and manipulate 32-bit data values.

3. What is the purpose of the "and" command in X86 Assembly?

The "and" command is a bitwise logical operator used to perform a logical AND operation on two operands. It is often used for data masking and bit manipulation in X86 Assembly.

4. How is data represented in X86 Assembly?

Data in X86 Assembly is represented in binary form, using 0s and 1s. This binary data is stored in memory locations and can be manipulated using various instructions and commands.

5. What are some benefits of using X86 Assembly?

X86 Assembly offers a high level of control and optimization, making it useful for tasks such as operating system development and embedded systems programming. It also allows for direct access to hardware, resulting in faster and more efficient code execution.

Similar threads

  • Programming and Computer Science
Replies
19
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
Back
Top