How to create a two-dimensional array (NxN) in assembler? The problem is that the dimension must be entered by the user.
2 answers
Given that you need to create an array dynamically, you can connect the msvcrt library (or libc under * nix) and use the scanf and malloc functions from there.
Sample code for fasm (for masm, the gist will be the same):
format PE console entry start include 'win32a.inc' section '.data' data readable writeable scanf_format_string db '%d',0 n dd ? matrix dd ? section '.code' code readable executable start: cinvoke scanf, scanf_format_string, n mov eax, [n] mul eax ; Будем выделяем память на n * n элементов lea eax, [eax*4] ; Домножаем на размер элемента (будем считать, что это dword - 4 байта) cinvoke malloc, eax ; Выделяем память mov [matrix], eax ; Сохраняем полученный адрес ; ... cinvoke free, [matrix] ; Освобождаем память invoke ExitProcess,0 section '.idata' import data readable library kernel, 'kernel32.dll',\ msvcrt, 'msvcrt.dll' import kernel,\ ExitProcess, 'ExitProcess' import msvcrt,\ scanf,'scanf',\ malloc,'malloc',\ free,'free' Addresses inside the array will have to be calculated independently as (i * n + j) * 4 , where i and j are, respectively, the row and column number (counting from 0).
|
How to create a two-dimensional array (NxN) in assembler?
Just like in any other language: allocate NxN memory. Well, or first decide what you mean "two-dimensional array."
section .data dvumernii_massiv db 1, 2, 9, 4 db 5, 14, 7, 8 db 9, 10, 14,14 It'll do?
- this way we determine the dimension in advance, in this case 4 by 3. And the problem is to allocate space for the dimension array that the user will enter. That is, it must be dynamically created - kyoto
- Well, and formulate it in the NORMAL LANGUAGE. First of all for myself. Then on the algorithmic: "allocate memory for the N addresses of the array." "From 0 to N-1 allocate memory for new arrays, size N". "Write the values of the received addresses to the corresponding elements of the first array N". Done? Here then already translate into assembler. Do not forget to refresh the knowledge of your chosen dialect. Or do you think the assembler is such a jumble of instructions where you don’t need to think? - user6550
- Wrote a simple C program that solves this problem ( ideone.com/eeGknC ). It is required to write a program in assembler that implements this algorithm: 1. Received the dimension 2. Allocated space 3. Filled with anything 4. Brought to the screen. I don’t know the assembler, I tried to look for some resources, but most of the examples of working with an array rest on the fact that the array is specified in the .data section and it is always of a certain dimension. - kyoto
- “ I don’t know the assembler, ” so who bothers to find out? Or are you hoping that they will start reading out textbooks out loud with artistic tricks? On the other hand, since you do not correlate the C code and what is actually happening in the computer, it means that you need to go back again. And write on a piece of paper, listing the contents of memory bytes in cells, and each instruction of your C-code in columns. - user6550
- Well, I concretize the question: How to allocate a section of memory of size N * (4 bytes) in a code segment? What instructions for this should be considered? - kyoto
|