Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Two basic questions about generated assembly

Two basic questions about generated assembly

Scheduled Pinned Locked Moved C / C++ / MFC
comdata-structuresquestion
35 Posts 7 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Stephen Hewitt

    George_George wrote:

    Where to look for documents for cycles needed for a specific instruction (e.g. LEA and MUL)?

    Download the datasheet for the CPU.

    George_George wrote:

    "it assumes a linear address space" -- it you mean low layer CPU/register or high layer C/C++?

    It's complicated and very low level. I suggest you start reading something like this[^].

    Steve

    G Offline
    G Offline
    George_George
    wrote on last edited by
    #26

    Thanks Steve, 1. I have read the link you referred. It talks about how protected mode is using segment based accessment model. But during the whole article, it never mentioned what means linear and non-lnear -- and this is my question. 2. Could you provide a link for the data sheet you means please? Sorry I am new to this area. regards, George

    S 1 Reply Last reply
    0
    • C cp9876

      You need to understand how the effective address is calculated in x86 CPUs. I'm not an expert, but from here[^] The offset part of the memory address can be specified either directly as a static value (called a displacement) or through an address computation made up of one or more of the following components: Displacement—An 8-, 16-, or 32-bit value. Base—The value in a general-purpose register. Index—The value in a general-purpose register except EBP. Scale factor—A value of 2, 4, or 8 that is multiplied by the index value. An effective address is computed by: Offset = Base + (Index * Scale) + displacement So the address generation logic provides a rapid way to compute B + I*S + D, (and this has to be very fast as it is done before the memory access) all the article was saying was that the LEA instruction allows the programmer to access the result of this instruction that clearly executes in one cycle, which back when the article was written, was presumably faster than using the integer ALU. Note that as the Scale factor is restricted to being 1,2,4 or 8 there are limited equations that can be calculated. To multiply by 5 you would calculate base + 4*base etc.. It may not be relevant today.

      Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

      G Offline
      G Offline
      George_George
      wrote on last edited by
      #27

      Thanks Peter, I like the article and read through it last evening. Three more comments, 1. I found two places in the document are conflicting, they are - "Modern operating system and applications use the (unsegmented) memory model¾ all the segment registers are loaded with the same segment selector so that all memory references a program makes are to a single linear-address space." -- looks like segment is useless since unsegment model is used? - "The offset which results from adding these components is called an effective address of the selected segment. Each of these components can have either a positive or negative (2's complement) value, with the exception of the scaling factor." -- why still needs segment selector to calculate? Conflicting with last statement, which is unsegment model? 2. "Note that the value of the EIP may not match with the current instruction because of instruction prefetching. The only way to read the EIP is to execute a CALL instruction and then read the value of the return instruction pointer from the procedure stack." -- my confusion is, EIP is next instruction to execute, and why return address is the same as EIP? Are they related? 3. "you generally create segment selectors with assembler directives and symbols. The assembler and/or linker then creates the actual segment selectors associated with these directives and symbols." -- what does this mean? Does it mean all segment related instruction will be ignored or modified by linker before execution? regards, George

      1 Reply Last reply
      0
      • G George_George

        Thanks Steve, 1. I have read the link you referred. It talks about how protected mode is using segment based accessment model. But during the whole article, it never mentioned what means linear and non-lnear -- and this is my question. 2. Could you provide a link for the data sheet you means please? Sorry I am new to this area. regards, George

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #28

        George_George wrote:

        I have read the link you referred. It talks about how protected mode is using segment based accessment model. But during the whole article, it never mentioned what means linear and non-lnear -- and this is my question.

        This is an oversimplification, but here goes. In protected mode a number of segment register are used: CS : Code segment DS : Data segment SS : Stack segment ES : Extra segment FS : Another extra segment GS : Yet another extra segment Different instructions use different segments for different purposes (some instructions also allow for the default segment used to be overridden). Each segment can represent a physically distinct linearly addressable memory space; it’s possible to set things up so that memory addressable in one segment is not addressable in another. In Windows most of the segment registers map to the same memory, which is good as languages such as C/C++ have no concept of segments (or you could think of it as only supporting one segment). The FS segment is an exception however and serves a special purpose: the memory in it is the TIB. So languages like C/C++ can access the TIB the same memory is also mapped into the other segments. The address in the other segments where it’s mapped is stored at FS:[0x18].

        George_George wrote:

        Could you provide a link for the data sheet you means please? Sorry I am new to this area.

        See here[^]. Makes good bedtime reading¿

        Steve

        G 1 Reply Last reply
        0
        • J jhwurmbach

          Yes, LEA means "Load effective address". But lead can not only load an address, it can compute it on the fly. LEA EAX,[ESP+14] puts the result of (value of ESP) plus 14 into EAX. And it can compute more complicated calculations: LEA EAX,[EAX*4+EAX] works, and as I got from the text you have given, it does not actually use a calculation (involving things like cache access and multiply units). Instead, it uses a table lookup. But that does only work for for some multipliers. So, in effect, for certain multipliers, LEA is faster than MUL is.

          Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
          Douglas Adams, "Dirk Gently's Holistic Detective Agency"

          G Offline
          G Offline
          George_George
          wrote on last edited by
          #29

          Thanks jhwurmbach, You bring good point. My further question, in my previous experience with table looking or similar implementation, it only deals with constants' lookup with limited numbers (e.g. lookup one's ID by its name in a large table). So, both input and output are finite. How could LEA use table to deal with multiplication? My confusion is, since multiplication has infinite possible input and output, how could a constant size table be used to deal with all values? :-) regards, George

          J 1 Reply Last reply
          0
          • S Stephen Hewitt

            George_George wrote:

            I have read the link you referred. It talks about how protected mode is using segment based accessment model. But during the whole article, it never mentioned what means linear and non-lnear -- and this is my question.

            This is an oversimplification, but here goes. In protected mode a number of segment register are used: CS : Code segment DS : Data segment SS : Stack segment ES : Extra segment FS : Another extra segment GS : Yet another extra segment Different instructions use different segments for different purposes (some instructions also allow for the default segment used to be overridden). Each segment can represent a physically distinct linearly addressable memory space; it’s possible to set things up so that memory addressable in one segment is not addressable in another. In Windows most of the segment registers map to the same memory, which is good as languages such as C/C++ have no concept of segments (or you could think of it as only supporting one segment). The FS segment is an exception however and serves a special purpose: the memory in it is the TIB. So languages like C/C++ can access the TIB the same memory is also mapped into the other segments. The address in the other segments where it’s mapped is stored at FS:[0x18].

            George_George wrote:

            Could you provide a link for the data sheet you means please? Sorry I am new to this area.

            See here[^]. Makes good bedtime reading¿

            Steve

            G Offline
            G Offline
            George_George
            wrote on last edited by
            #30

            Thanks Steve, 1. I understand the concept of segment. My question is what means linear and non-linear address? My confusion is, I did some search, but can not find any concept like linear or non-linear. Any comments or ideas? 2. Sorry I am new to Intel manual, and looks like there is quite a few. To look up instruction machine cycle for x64, I should look at the following two manuals? Intel® 64 and IA-32 Architectures Software Developer's Manual Volume 2A: Instruction Set Reference, A-M Describes the format of the instruction and provides reference pages for instructions (from A to M). This volume also contains the table of contents for both Volumes 2A and 2B. Download › (PDF 2.99MB) Order a printed copy › (SKU #253666) Intel® 64 and IA-32 Architectures Software Developer's Manual Volume 2B: Instruction Set Reference, N-Z Provides reference pages for instructions (from N to Z). VMX instructions are treated in a separate chapter. This volume also contains the appendices and index support for Volumes 2A and 2B. Download › (PDF 5.60MB) Order a printed copy › (SKU #253667) regards, George

            1 Reply Last reply
            0
            • R Rajesh R Subramanian

              George_George wrote:

              "it probably must execute in a single cycle and therefore would surely be faster than mul" -- do you have any documents to support this statement?

              With the LEA instruction, the x86 processor can now perform a 3-number add, with something like a C expression "a = b + c + 10;" translating into EAX = EBX+ECX+10 and being coded into one instruction: LEA EAX,[EBX+ECX+10] Notice that no memory is actually referenced. LEA is used merely to calculate values by performing the addition of a base register (EBX) with an index register (ECX) with some constant displacement (10). This is what the address generation unit (AGU) does, allowing the processor to quickly calculate addresses of array elements, screen pixel locations, and do some basic arithmetic in one clock cycle. Source: http://www.emulators.com/docs/pentium_1.htm[^] Refer to pages 6 & 7 in this PDF (Pentium: Not the same old song)[^]. This too supports my earlier statement. Also, read "Handy info on speeding up integer instructions" in this page[^]

              Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

              G Offline
              G Offline
              George_George
              wrote on last edited by
              #31

              Thanks Rajesh, My question item 1 is answered. Any ideas to my question item 2? About what mean linear and non-linear address? regards, George

              1 Reply Last reply
              0
              • G George_George

                Thanks jhwurmbach, You bring good point. My further question, in my previous experience with table looking or similar implementation, it only deals with constants' lookup with limited numbers (e.g. lookup one's ID by its name in a large table). So, both input and output are finite. How could LEA use table to deal with multiplication? My confusion is, since multiplication has infinite possible input and output, how could a constant size table be used to deal with all values? :-) regards, George

                J Offline
                J Offline
                jhwurmbach
                wrote on last edited by
                #32

                Hmm. Here, at least the input is limited to the multiplicants mentioned. But the remaining number of results would still be very high.

                Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
                Douglas Adams, "Dirk Gently's Holistic Detective Agency"

                G 1 Reply Last reply
                0
                • J jhwurmbach

                  Hmm. Here, at least the input is limited to the multiplicants mentioned. But the remaining number of results would still be very high.

                  Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
                  Douglas Adams, "Dirk Gently's Holistic Detective Agency"

                  G Offline
                  G Offline
                  George_George
                  wrote on last edited by
                  #33

                  Thanks jhwurmbach, Could you explain or describe how multiplication could be implemented as table look-up solution please? :-) regards, George

                  1 Reply Last reply
                  0
                  • R Rajesh R Subramanian

                    I'll be happy to hear your feedback on "why" you voted the post down. Correct me if I said something wrong there, please. :)

                    Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                    T Offline
                    T Offline
                    ThatsAlok
                    wrote on last edited by
                    #34

                    whoso ever downvoted you .. will be beaten with stick! any ways le me also square it of!

                    "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
                    Never mind - my own stupidity is the source of every "problem" - Mixture

                    cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/xml>

                    R 1 Reply Last reply
                    0
                    • T ThatsAlok

                      whoso ever downvoted you .. will be beaten with stick! any ways le me also square it of!

                      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
                      Never mind - my own stupidity is the source of every "problem" - Mixture

                      cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/xml>

                      R Offline
                      R Offline
                      Rajesh R Subramanian
                      wrote on last edited by
                      #35

                      Thanks man. You're kind. :)

                      Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups