Assembly memory allocation

后端 未结 3 912
情书的邮戳
情书的邮戳 2020-12-30 06:02

I am trying to learn assembly language and I need clarification on something. Please correct me if I am wrong on any of this since I don\'t know much about assembly.

<
相关标签:
3条回答
  • 2020-12-30 06:42

    This is not exactly an answer but in this book there is the answer. I can only recommend it. It will teach you the basics, like the name says it's Programming from ground up.

    ProgrammingGroundUp

    0 讨论(0)
  • 2020-12-30 06:49

    Depends on the target and the type of memory (RAM, ROM, etc) you are talking about. If you are talking about RAM a small embedded project you may just have a few files to keep track of, and the data sheets for the micro will tell you the various memory region addressing. In the case where there are several 'modules,' you would use a linker to link the object files into an executable. The linker can re-assign memory address so they don't overlap, or you can have a central file where all the memory locations are defined, and the other modules use this as a resource. Sorry. Its a big question with a lot of answers.

    0 讨论(0)
  • 2020-12-30 06:56

    The answer to the second part of you question (on most modern OSs) is virtual memory.

    You start at the hardware layer with physical memory. That's the stuff you can actually poke with your finger. This is what the operating system sees. The operating system lets you run processes on an abstraction called virtual memory.

    Each process gets its own virtual memory space. So it can pretend that it's the only process running, and it has tons of memory. Then each time you access memory, you supply a virtual address, which gets mapped to a physical address. The operating system keeps a table of which virtual address gets mapped to which actual physical addresses in RAM. Typically this is done with some special hardware as well (an MMU, memory management unit) for performance reasons, but you could do it 100% in software too.

    So when you say 0x000 in your program, that's a virtual address. It gets translated into a physical address by the computer when you read or write. So in another process, the same virtual address 0x000 maps to a different physical address. This system lets you write your program without knowing exactly how much RAM is available, or what address your program will be loaded into. It also prevents your program from trashing memory that belongs to another program.

    As for the first part, absolutely. Different types of data take different amounts of memory. You have to know how much space you need when you lay out your data structures. There are also byte-alignment issues to keep in mind. Multi-byte data types (eg floating point numbers) often have to start at an address that is divisible by 2 or 4 or the number of bytes it takes to store a float -- it's a requirement of the processor or the RAM. So you can't just crunch all your data together, one byte after the next, you have to lay it out in a specific order like fitting together the pieces of a puzzle if you want to minimize unused memory.

    0 讨论(0)
提交回复
热议问题