宏以分配内存中的空间 [英] macro to allocate space in memory
问题描述
我需要让一个程序集程序员来计算 帕斯卡三角形 强>. 这样存储在内存中的每行Pascal三角形都与另一行分开 我想制造一个,但是我不知道如何使用宏在组装中做到这一点.
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 .
宏获取一个数字并在内存中分配该数字的dword
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
我想知道2件事:
第一我希望第二个参数(%2 )自动具有字符串名称,例如
first i want the second arg ( %2 ) to have a string name automatically for example
第一行名称:"line1",下一行为"line2","line3" ...,依此类推,所以我不需要放下自己?
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 ?
推荐答案
对于NASM :您想要的是一个%rep
块.
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
它的组装方式与
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
...
如预期的那样,标签地址呈几何级数. (因为它是一个目标文件,而不是链接的可执行文件,所以从0开始).
As expected, the label addresses are in a geometric progression. (Starting from 0 because this is an object file, not a linked executable).
这篇关于宏以分配内存中的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!