Reputation: 792
wanted to make an example with nasm using the x86 architecture, that can create an array with size "n", where "n" will be the number that the user wants to have the size of the array at run time
extern _printf
extern _scanf
extern _scanf
global _main
section .data
array: 10,5,4
msg: db "enter the size of the array: ",10,0
size: db 10
format: db "%i",0
section .text
_main:
push msg
call _printf
add esp, 4
push size
push format
call _scanf
add esp, 8
ret
Upvotes: 1
Views: 1701
Reputation: 5775
If you'll satisfy with an example for Windows, see the following program in EuroAssembler. It creates 3128 bytes long AllocArray.exe which will reserve and initialize the requested size in BSS section.
AllocArray PROGRAM Format=PE, IconFile=, Entry=Main:
INCLUDE winapi.htm, cpuext32.htm
%MaxPossibleLength %SETA 1_000_000 ; Specify the maximal acceptable allocation here.
[.text]
Main: StdOutput ="Enter the size of the array (1..%MaxPossibleLength): "
StdInput EnterredLength ; Let the user to set array length.
LodD EnterredLength ; Convert the enterred decimal number to integer in EAX.
CMP EAX,%MaxPossibleLength
JA Main:
MOV [LengthOfTheArray],EAX
StoD AllocatedLength ; Convert the integer EAX to decimal.
XOR EAX,EAX
STOSB ; Zero-terminate the decimal number.
MOV EDI,TheArray:
MOV ECX,[LengthOfTheArray]
MOV EAX,0x5A5A5A5A
REP STOSD ; Initialize the array with 5A.
StdOutput ="The array of ", AllocatedLength, =" DWORDs is now allocated statically."
TerminateProgram
[.bss]
EnterredLength: DB 16 * BYTE
AllocatedLength: DB 16 * BYTE
LengthOfTheArray: DD DWORD
TheArray: DD %MaxPossibleLength * DWORD
ENDPROGRAM AllocArray
Upvotes: 0
Reputation: 363980
Do you mean like resd n
in the BSS, and the user can build the program with nasm -felf -Dn=1024
to set the NASM macro as a constant? You could provide a default with %ifdef
.
If you want a runtime-variable array size, it obviously can't be in static storage (unless you massively over-allocate and only use whatever part of the array is needed. That's fine on systems that do lazy allocation for the BSS.)
Upvotes: 1