1

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 
5
  • There are no declarations in assembly language. Can you elaborate what you try to do? Commented Apr 6, 2019 at 17:08
  • 1
    You will need to allocate at runtime then. Either stack or heap. Same as in C. Commented Apr 6, 2019 at 17:10
  • the array should not have the size burned, but should be indicated when captured from the console Commented Apr 6, 2019 at 17:30
  • 1
    You can't do that in assembly. You will have to get the size(s) from the user first, and then use an external memory management function to allocate that amount of memory from the heap. or reserve that much on the stack (but not too much -- stacks are relatively small). Which function you call depends on the environment the code will run in. In C, you would call malloc, I guess. In a standalone application, you would call an OS function. etc. Commented Apr 6, 2019 at 18:06
  • thanks, I lean more for the second part, but reserving enough memory space for the vector would not be bad practice Commented Apr 6, 2019 at 18:20

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

could you give me an example
0

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 

5 Comments

This isn't NASM syntax. I think this is FASM syntax (definitely not MASM, MOV EDI,TheArray: would be a load in MASM, or invalid because of the :). And in case anyone else missed it, StoD isn't stosd, it's a macro or something that this answer doesn't define, but which apparently leaves EDI pointing past the end of a string. The majority of this code is printing output, not the actual allocation and `memset(TheArray, 0x5A, LenthOfTheArray).
The OP asked for an example of massively ovet-allocate and only use whatever part of the array is needed
Indeed, StoD is not a mavhine instruction, neither is printf or memset . StoD is a formated-print macro shipped with €ASM: euroassembler.eu/maclib/cpuext32.htm#StoD
Right, but normally you'd use call printf so there's no room to confuse it with a machine instruction. And you use StoD very close to STOSB, so it's easy to get confused about what it is. But more importantly, until your comment, this answer didn't even say what assembler it requires. It doesn't work with NASM, which is what the OP was asking for an example in.
Actually the code must be in nasm, because between languages, many things change. But the truth is, I made a fixed size array and specified the user when I entered the size of the array, the maximum it has to create it, because as they commented above that you can not create an array dynamically, you always have to have a size and also someone else told me outside of this community.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.