1

我正在研究一个相当于插入函数的程序集,我想知道我将如何准确地分隔列表的元素,以便我可以将我的数据放入其中。我已经找到了应该插入我的信息的正确地址,但是对于如何写入我的数据而不覆盖那里的数据我有点困惑。我的想法是

    set givespace, %l1
    !Next part is at the bottom in data section
    .section ".data"
    givespace:
          .align 48

其中 48 是我的元素的大小。这给了我一个错误无效的对齐边界。有任何想法吗?

4

1 回答 1

0

If I correctly understand the question and you need statically allocate space for some data structure with predefined size, you must replace line

      .align 48

by the next one

      times 48 db 0

This is true way at least for the NASM. In any case I would recomend you to look at your assembler manual to ensure that this construct is supported by you assembly compiler. It is highly possible that your compiler can support not exactly the same notation as in the above example, but in any case it will support some similar analog. 'times' command have the next sintax:

    times <number of repeats> <body>

where specifies how many times body will be inserted into the resulting code and contain some standard assembly instruction. In the example above it is allocation of byte of the memory with initialization to null. So, for example, the next two code sections are equal

     times 5 dd 012345678h

and

     dd 012345678h
     dd 012345678h
     dd 012345678h
     dd 012345678h
     dd 012345678h

.align directive isn't correct for memory space allocation. It have another purpose - aligning of the following chunk of code or data to some predefined value. For example, in this case

    VAR1 db 0
    .align 48
    VAR2 db 0

if address of the VAR1 will be 0x1234, then the compiler will insert 44 unused bytes just to ensure that address of VAR2 will be alligned to the 48 bytes boundary. In our case it will be address - 0x1260 (0x1260 = 0x62 * 30 + 0). If be honest, compilers usually allow aligning only to the value that is power of two, and it is highly possible that on compilation of the above example assembler will rise an error, because 48 isn't a power of two. So, it is hard to control how many unused bytes will be allocated by .align directive. It can be any value from 0 to directive parameter. And this value will change in reply to the changes in the source code of your program.

But NOTE! This is an approach for the statical memory allocation during compilation time! If you need dynamical memory allocation in the run time, you need to write your own or find and adopt some third party memory allocator. If you program will run in the OS environment, you can request dynamical memory allocation by using OS API. (for example VirtualAlloc in WinAPI).

于 2012-12-06T05:55:58.857 回答