希望使用x86架构的nasm示例,该示例可以创建大小为“ 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
答案 0 :(得分:1)
您的意思是否像BSS中的import React from 'react';
import { Route, Redirect } from 'react-router-dom';
import auth from '../_helpers/auth';
class PrivateRoute extends React.Component {
getComponent = () => {
const { component: Component, location } = this.props;
if (auth.isAuthenticated) {
return (
<Component {...this.props} />
);
}
return (
<Redirect
to={{
pathname: '/public',
state: {
from: location,
},
}}
/>
);
}
render() {
const { ...rest } = this.props;
return (
<Route {...rest} render={this.getComponent()} />
);
}
}
,用户可以使用resd n
来构建程序以将NASM宏设置为常量?您可以使用nasm -felf -Dn=1024
提供默认值。
如果您想要运行时可变的数组大小,则显然不能将其存储在静态存储中(除非您大量过度分配并且仅使用需要的数组任何部分。在为延迟分配空间的系统上很好) BSS。)
答案 1 :(得分:0)
如果您满意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