macro to allocate space in memory

时光怂恿深爱的人放手 提交于 2019-12-24 01:54:59

问题


i need to make an assembly programmer to calculate pascal triangle . so that every line of pascal triangle stored in memory place separate from other line i want to make one but i have no idea how to do it in assembly using macro .

macro take an number and allocate that number of dword in memory

i did a try but i don't know if it is the correct way to do it

%macro Malloc 2
%2 : resd %1
%endmacro

i want to know 2 thing :

first i want the second arg ( %2 ) to have a string name automatically for example

first line name :"line1" and next line to be "line2" "line3" ... and so on so i don't need to put my self ?

second thing is this is a good idea to use macro in this case ?


回答1:


For NASM: What you want here is the %+ operator to concatenate things (after expanding single-line %assign macros), inside a %rep block.

section .bss

%assign i 1
%rep    64
    line %+ i:  resd  i
%assign i i+1
%endrep

This assembles identically to

section .bss
line1:    resd 1     # reserve space for 1 DWORD
line2:    resd 2     # reserve space for 2 DWORDs
line3:    resd 3     # reserve space for 3 DWORDs
...

Testing:

$ nasm -felf64  pascal-triangle-macro.asm
$ nm -n pascal-triangle-macro.o        # sort by numeric address, not lexicographic

0000000000000000 b line1
0000000000000004 b line2
000000000000000c b line3
0000000000000018 b line4
0000000000000028 b line5
000000000000003c b line6
0000000000000054 b line7
0000000000000070 b line8
0000000000000090 b line9
00000000000000b4 b line10
00000000000000dc b line11
0000000000000108 b line12
...

As expected, the label addresses are in a geometric progression. (Starting from 0 because this is an object file, not a linked executable).



来源:https://stackoverflow.com/questions/49159846/macro-to-allocate-space-in-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!