Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Help with memory addressing

  1. #1
    Senior Member
    Join Date
    Feb 2003
    Posts
    282

    Help with memory addressing

    Im having dificulty understanding memory addressing during function calls in c/c++

    An article states that a frame pointer inside the stack sits between the function parameters and the local variables. So that using the frame pointer, adding and subtracting from it alows the function to access any paramters or local variables.

    It states the top of the stack is the lowest memory address and the bottom is the highest, and that paramteters are a positive refernce from frame pointer, and local variables are negitive refernce from frame pointer.

    I don't understand is how 0xfffffffb(%ebp) is a negitave refernce and 0x8(%ebp) is a positive refernce.

    What makes the fffffffb negative ?

    Also acording to one site (http://www.cee.hw.ac.uk/~alison/SysL...1/topic11.html) using the gcc -S option the assembly produced resembles the ebp refernces in the format -1(%ebp), +4(%ebp).

    Where another site (http://www.phrack.org/show.php?p=49&a=14) produces asembly where ebp refernces are in the format 0x8(%ebp), 0xfffffffd(%ebp)

    Also I am useing djgpp compiler for windows since it gives me the gcc compiler. Its output format resembles that of the second site.

    How can I produce the refernces in format -2(%ebp) instead of 0xfffffffd(%ebp)

    Is there any gcc port for windows that will give the assembly format -2(%ebp) ?

    Any guidence in this matter is apreciated.

  2. #2
    Senior Member
    Join Date
    Jul 2002
    Posts
    339
    journy101,

    What makes the fffffffb negative ?
    FFFF FFFB (a double word, or a quad byte) can be represented as unsigned long integer or signed long integer. As a unsigned longint it is 4294967291. As a signed longint it is -5.

    The first half signed longint numbers (0000 0000 to 7FFF FFFF) are positive.
    The second half signed longint numbers (FFFF FFFF to 8000 000) are negative.

    Hex FFFF FFFF, as unsigned longint is 4294967295 (2^16 - 1) and as signed longint is -1.
    Hex FFFF FFFE, as unsigned longint is 4294967294 (2^16 - 2) and as signed longint is -2.
    Hex FFFF FFFD, as unsigned longint is 4294967293 (2^16 - 3) and as signed longint is -3.
    etc...

    How can I produce the refernces in format -2(%ebp) instead of 0xfffffffd(%ebp)

    Is there any gcc port for windows that will give the assembly format -2(%ebp) ?
    I've just tested gcc -S with MinGW's gcc (www.mingw.org) and Cygwin's gcc (www.cygwin.com). Both produce the format that you want (signed decimal).

    Hope this helps.

    Peace always,
    <jdenny>
    Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
    I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds


  3. #3
    Senior Member
    Join Date
    Feb 2003
    Posts
    282
    Thanks for explaining this, it still confuses me though. I downloaded mingw's msys and it works nicely. The output is in the format I wanted. This saves me confusion. Many thanks.

    Your explanation was great but I have still some questions I don't understand, Do you hapen to know of any articles or tutorials dealing spasificaly with unsigned/signedint notation.

    Ive been searching google and found many bug reports but nothing realy in theory describeing how it works.

  4. #4
    Senior Member
    Join Date
    Jul 2002
    Posts
    339
    I knew that I would confuse you as I'm bad in explaining things. Specifically which parts that you still don't understand? I'll try to explain more... errr... maybe not...

    Have a look at the explanation other people has made:
    http://www.phim.unibe.ch/comp_doc/c_...ata_types.html

    Unsigned data types number have no sign (positive or negative), i.e. are always positive. All bits are used to store the real value.

    Signed data types (short integer, integer or long integer) numbers have sign (positive or negative), hence the name "signed". The MSB (Most Significant Bit or highest position bit) is used for indicating the sign (positive or negative).
    If the MSB=0 then it's a positive number. The real value is the value of all remaining bits.
    If the MSB=1 then it's a negative number. The real value (in negative sign) is the value that you get from [ (the maximum value for unsigned) - (the value of all bits, including MSB) + 1 ].

    Code:
    Hex:            F    F    F    F     F    F    F    B
    Bin:         1111 1111 1111 1111  1111 1111 1111 1011
    
    Unsigned:    1111 1111 1111 1111  1111 1111 1111 1011
                =                              4294967291
    
    Signed:    (-)111 1111 1111 1111  1111 1111 1111 1011
                =       (-) (4294967295 - 4294967291 + 1)
                =                                      -5
    I'm sure that this will confuse you even more Oh well...

    Peace always,
    <jdenny>
    Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
    I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds


  5. #5
    Senior Member
    Join Date
    Jun 2003
    Posts
    772
    It states the top of the stack is the lowest memory address and the bottom is the highest
    That is only true in the case of a stack that grows down (like 80x86 architecture), a stack can also grow up.
    The above sentences are produced by the propaganda and indoctrination of people manipulating my mind since 1987, hence, I cannot be held responsible for this post\'s content - me

    www.elhalf.com

  6. #6
    Senior Member
    Join Date
    Feb 2003
    Posts
    282
    Wouldent say you are bad at explaining things, just I sometimes take a while to see things.

    I understood that signed long int would have a + or - sign in front, and that unsigned int would use some other means to tell it is negitave.

    Above where you explain the MSB indicateing the sign of an unsigned int makes sence. I understand how you made the conversion from FFFFFFFB to binary. And can understand that since the MSB in this case is 1 it is negative So I understand where you got this

    (-)111 1111 1111 1111 1111 1111 1111 1011

    but now where does the math (-) (4294967295 - 4294967291 + 1) come from? I can see the MSB being 1 was replaced with - sign. but why do we add these numbers together to get the signed equivalent.

    I am reading the article you provided, it explains the data type sizes and range.

    I greatly aprecuiate the help you have provided and now I have some things to search google for.

  7. #7
    Senior Member
    Join Date
    Feb 2003
    Posts
    282
    Things started clicking when you shoed the MSB sign bit and I remembered a book from college that talked about this, I managed to find the book.

    I was in the section signed numbers of "Digital Fundementals"

    Thanks for pointing me in the right direction

  8. #8
    Senior Member
    Join Date
    Jul 2002
    Posts
    339
    Digital Fundamentals! That's it. I forgot where I got this knowledge from, so I couldn't show any references. I just knew it! Thanks for bring back my college memory. Back then I had two full semesters for Digital Fundamentals 1 and 2. BTW, did I explain it right as in the official definition?

    Peace always,
    <jdenny>
    Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
    I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds


  9. #9
    Senior Member
    Join Date
    Feb 2003
    Posts
    282
    me too, i also had two semesters of Digital Fundementals, what was your major? my major was EET

    Yes yopu explained nicely, the book gives many examples, and further discussions, but many thanks for the help you did provide.

  10. #10
    Senior Member
    Join Date
    Jul 2002
    Posts
    339
    EET is Electrical Engineering Technology right? My major was in the dept of Computer Engineering (when I studied) but a couple years ago it has been changed to dept of Informatics Engineering. Personally I see both are very similar as well as Computer Science or other varians. The difference is that CE has more hardware topics but IE/CS has more software topics.

    K C U. Or should we continue this conversation in other forum?

    Peace always,
    <jdenny>
    Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
    I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •