2

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 Answers1

2

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).

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • Just a nitpick: the series of addresses is not a geometric progression (e.g. 24:12 != 12:4), it is more of a scaled triangular progression. The differences form an arithmetic progression with common difference 4 though. – Margaret Bloom Mar 08 '18 at 13:21
  • @MargaretBloom: I think I meant that it grows approximately geometrically, because `n*(n+1)` is O(n^2). – Peter Cordes Mar 08 '18 at 13:23