15

When I break main it looks like the bold line is where i is being created and initialized. I think I'm going at this all wrong, I'm trying to examine x86_64 assembly from a book that is explaining x86. This seems weird and I'm pretty sure I just don't understand seeing as in this book he says he'll refer to a word and dword as 4-bytes. If I could get an explanation to aid my incognisance it would be greatly appreciated.

    (gdb) list
    1   #include <stdio.h>
    2   
    3   int main()
    4   {
    5       int i;
    6       for(i=0; i < 10; i++)
    7       {
    8           printf("Hello, world!\n");
    9       }
    10      return 0;
    (gdb) disassemble main
    Dump of assembler code for function main:
       0x0000000100000f10 <+0>: push   rbp
       0x0000000100000f11 <+1>: mov    rbp,rsp
       0x0000000100000f14 <+4>: sub    rsp,0x10
       0x0000000100000f18 <+8>: mov    DWORD PTR [rbp-0x4],0x0
       0x0000000100000f1f <+15>:    mov    DWORD PTR [rbp-0x8],0x0
       0x0000000100000f26 <+22>:    cmp    DWORD PTR [rbp-0x8],0xa
       0x0000000100000f2d <+29>:    jge    0x100000f54 <main+68>
       0x0000000100000f33 <+35>:    lea    rdi,[rip+0x48]        # 0x100000f82
       0x0000000100000f3a <+42>:    mov    al,0x0
       0x0000000100000f3c <+44>:    call   0x100000f60
       0x0000000100000f41 <+49>:    mov    DWORD PTR [rbp-0xc],eax
       0x0000000100000f44 <+52>:    mov    eax,DWORD PTR [rbp-0x8]
       0x0000000100000f47 <+55>:    add    eax,0x1
       0x0000000100000f4c <+60>:    mov    DWORD PTR [rbp-0x8],eax
       0x0000000100000f4f <+63>:    jmp    0x100000f26 <main+22>
       0x0000000100000f54 <+68>:    mov    eax,0x0
       0x0000000100000f59 <+73>:    add    rsp,0x10
       0x0000000100000f5d <+77>:    pop    rbp
       0x0000000100000f5e <+78>:    ret    
    End of assembler dump. </code>
John Holly
  • 391
  • 1
  • 2
  • 12
  • How did you figuure out from this that a word size is 8 bytes? – ScarletAmaranth Nov 28 '13 at 18:32
  • As the processor is 64 bit I would say that word is 64 bit long, as confirmed by addresses on the left. Then, yes, a word is 8 bytes. Maybe your book refers to a 32 bit processor. – HAL9000 Nov 28 '13 at 18:35
  • @HAL9000 word is 64 bit long? nope.jpg – ScarletAmaranth Nov 28 '13 at 18:37
  • @ScarletAmaranth in a 64 bit architecture word is 64 bit long: http://en.wikipedia.org/wiki/64-bit_computing http://stackoverflow.com/questions/5295903/how-many-bits-does-a-word-contain-in-32-64-bit-os-respectively – HAL9000 Nov 28 '13 at 18:41
  • Sorry, forgot to bold the line, but why is this second line going back 8 from the base pointer and the significance of the one going back 4 @ScarletAmaranth? `<+8>: mov DWORD PTR [rbp-0x4],0x0` `<+15>: mov DWORD PTR [rbp-0x8],0x0` – John Holly Nov 28 '13 at 18:42
  • @HAL9000 I stand corrected, looks like one way of looking at the word does allow for this interpretation, thank you. – ScarletAmaranth Nov 28 '13 at 18:44
  • 3
    The term "word" meant something in the olden days, back when the data bus size was a match with the ALU and register size. Those days are long gone and the term got hopelessly ambiguous. A 32-bit processor like the original Pentium already used a 64-bit data bus. And a 64-bit processor has a 48-bit address bus. If this book is talking about "word" a lot then you definitely ought to look for another one. – Hans Passant Nov 28 '13 at 18:50
  • It was talking more in reference to the gdb arguments used to examine sizes. It's not that old of a book. I was just curious and just for some closure and so I can mark this answered... an int is 8 bytes because this is 64 bit assembly, that makes sense to me. Maths.jpg – John Holly Nov 28 '13 at 18:57
  • 2
    Well, no, there is no x86_64 compiler that uses 8 bytes for an *int*. Much too expensive. A 64-bit architecture doesn't buy you twice the amount of cache. – Hans Passant Nov 28 '13 at 19:05

1 Answers1

32

The terms used to describe sizes in the x86 architecture are:

  • byte: 8 bits
  • word: 2 bytes
  • dword: 4 bytes (stands for "double word")
  • qword: 8 bytes (stands for "quad word")

This is somewhat at odds with the usual meaning of "word": the 16-bit nature of word is a result of the evolution of x86 machines from their 16 bit origins, not a reflection of the natural word size of the machine. For compatibility reasons the size of a word operand must always remain the same, even on a 64-bit machine.

Note that the variable i in your program is 32 bits: you can see dword size annotations in the relevant stack accesses. It may be instructive to recompile your program with the type of i changed to long int.

gsg
  • 9,167
  • 1
  • 21
  • 23