1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

X86 Assembly dword ptr

  1. Oct 28, 2012 #1
    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[])
    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: Oct 28, 2012
  2. jcsd
  3. Oct 28, 2012 #2


    Staff: Mentor

    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.
  4. Oct 28, 2012 #3


    User Avatar
    Homework Helper

    Code (Text):

            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 (Text):

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

    Code (Text):

            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.
  5. Oct 29, 2012 #4
    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: Oct 29, 2012
  6. Oct 29, 2012 #5


    User Avatar
    Homework Helper

    From my previous post:

    Code (Text):

            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: Oct 29, 2012
  7. Oct 29, 2012 #6
    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.
  8. Oct 29, 2012 #7


    User Avatar
    Homework Helper

    and eax,1, is a binary operation that changes the value in eax. The C equivalent would be eax = (eax & 0x01);.
  9. Oct 29, 2012 #8
    Do you know what it's java equivalent would be? I haven't learned C yet.
  10. Oct 29, 2012 #9


    User Avatar
    Homework Helper

    About the same:

    eax = eax & 1
  11. Oct 29, 2012 #10
    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);


    The result is 50.
    What did they do to get this answer?
  12. Oct 29, 2012 #11


    User Avatar
    Homework Helper

  13. Oct 30, 2012 #12
    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?
  14. Oct 30, 2012 #13


    User Avatar
    Homework Helper

    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
  15. Oct 31, 2012 #14
    Thank You for all your help!! :smile:
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: X86 Assembly dword ptr
  1. Assembly Language (Replies: 0)

  2. X86 Assembly Parameters (Replies: 19)