如何在nasm中声明一个用户指定大小的数组?
How to declare an array with size indicated by the user in nasm?
想用 nasm 使用 x86 架构创建一个示例,它可以创建一个大小为 "n" 的数组,其中 "n" 将是用户想要的数字运行 时间
的数组大小
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
你的意思是像BSS中的resd n
一样,用户可以用nasm -felf -Dn=1024
构建程序来将NASM宏设置为常量?您可以使用 %ifdef
.
提供默认值
如果你想要一个运行时可变的数组大小,它显然不能在静态存储中(除非你大量过度分配并且只使用需要的数组的任何部分。这在进行延迟分配的系统上很好对于 BSS。)
如果您对 Windows 的示例感到满意,请参阅 EuroAssembler 中的以下程序。它创建 3128 字节长 AllocArray.exe,它将在 BSS 部分中保留和初始化请求的大小。
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
想用 nasm 使用 x86 架构创建一个示例,它可以创建一个大小为 "n" 的数组,其中 "n" 将是用户想要的数字运行 时间
的数组大小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
你的意思是像BSS中的resd n
一样,用户可以用nasm -felf -Dn=1024
构建程序来将NASM宏设置为常量?您可以使用 %ifdef
.
如果你想要一个运行时可变的数组大小,它显然不能在静态存储中(除非你大量过度分配并且只使用需要的数组的任何部分。这在进行延迟分配的系统上很好对于 BSS。)
如果您对 Windows 的示例感到满意,请参阅 EuroAssembler 中的以下程序。它创建 3128 字节长 AllocArray.exe,它将在 BSS 部分中保留和初始化请求的大小。
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