Understanding X86 Assembly dword ptr & 'and' Commands

  • Thread starter Thread starter whitehorsey
  • Start date Start date
  • Tags Tags
    Assembly x86
Click For Summary

Discussion Overview

The discussion revolves around understanding specific x86 assembly instructions, particularly the use of "dword ptr" and the "and" command within a code snippet involving an array of integers. Participants explore the implications of these commands in the context of assembly language programming.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • Some participants explain that "dword ptr" indicates the size of the data being accessed, which is necessary when the destination size isn't implied by the instruction.
  • Others clarify that the instruction "mov eax, dword ptr [esi+4*ebx]" effectively copies a 32-bit integer from memory into the eax register, with the address calculated based on the index stored in ebx.
  • There is a discussion about how the "and" command performs a bitwise operation, specifically "and eax, 1", which is said to check the least significant bit of the value in eax.
  • Some participants express confusion about the results of the "and" operation when applied to different values in eax, particularly in relation to the array elements.
  • Participants discuss how to represent binary numbers of different lengths and the necessity of padding with leading zeroes for proper bitwise operations.

Areas of Agreement / Disagreement

Participants generally agree on the basic functions of the "dword ptr" and "and" commands, but there remains some confusion and differing interpretations regarding their implications and results in specific scenarios.

Contextual Notes

Some participants mention the need for additional documentation or examples to fully understand the behavior of the "and" command and its effects on different values in eax. There is also uncertainty about how to properly represent binary numbers for bitwise operations.

Who May Find This Useful

This discussion may be useful for individuals learning x86 assembly language, particularly those interested in understanding memory addressing, bitwise operations, and the nuances of assembly syntax.

whitehorsey
Messages
188
Reaction score
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
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.
 
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.
 
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:
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:
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.
 
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);.
 
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.
 
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:
 

Similar threads

  • · Replies 19 ·
Replies
19
Views
6K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 7 ·
Replies
7
Views
7K
Replies
19
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 5 ·
Replies
5
Views
4K