我正在使用Visual Studio2012,在windows32解决方案中编辑.asm文件。
这是需要更改为汇编的伪代码:
Declare a 32-bit integer array A[10] in memory
repeat
Prompt for and input user's array length L
until 0 <= L <= 10
for i := 0 to (L-1)
Prompt for and input A[i]
end for
while (First character of prompted input string for searching = 'Y' or 'y')
Prompt for and input value V to be searched
found := FALSE
for i := 0 to (L-1)
if V = A[i] then
found := TRUE
break
end if
end for
if (found) then
Display message that value was found at position i
else
Display message that value was not found
end if
end while我可以很好地管理输入、循环和跳转,但最让我头疼的是让数组具有用户输入的长度,并遍历数组以比较这些值。对我最有帮助的是,如果有人能制作并解释一段代码,帮助我理解我无法理解的部分。我试着在网上搜索它,但我遇到的几乎所有东西都使用不同的汇编程序,这使得它很难剖析。
到目前为止我的代码是:
.586
.MODEL FLAT
INCLUDE io.h ; header file for input/output
.STACK 4096
.DATA
lengthA DWORD ?
promptL BYTE "Please enter a length of the array between 0 and 10: ", 0
string BYTE 40 DUP (?)
A DWORD 0 DUP (?)
.CODE
_MainProc PROC
Reread: input promptL, string, 40 ; read ASCII characters
atod string ; convert to integer
;while (promptL < 0 or > 10)
cmp eax, 0
jl Reread
cmp eax, 10
jg Reread
;end while
mov lengthA, eax ; store in memory
_MainProc ENDP
END ; end of source code在检查用户输入是否在范围内之后,我遇到了麻烦,我不确定如何将数组A设置为具有指定的长度,甚至不确定我是否正确地声明了A。
发布于 2017-11-14 05:03:18
不能在运行时修改程序集时间常数。DUP指令是汇编时间指令,它通过在生成的机器码中多次发出"init“值来预留内存空间。这形成了一个固定的可执行文件,它被限制在汇编过程中使用的大小。
由于您的最大L是10,您可以保留最大可能大小的数组:
A DWORD 10 DUP (?)在后面的代码中,您必须获取[lengthA]以了解使用了多少元素(以便使其看起来像是动态调整大小的数组,并且不处理地址A之后保留的未使用的剩余部分)。
另一种选择是在运行时动态保留内存,或者通过调用OS服务来分配堆内存,或者在堆栈上保留数组。但这两种选择都比使用上述固定大小的内存要先进得多。
我没有在你的伪代码中看到任何动态内存分配的请求,固定大小数组的解决方案对我来说还可以,特别是如果你已经在为它而苦苦挣扎,那么你可能还没有准备好编写自己的动态内存管理器/分配器。
编辑:实际上伪代码确实指定你应该保留固定大小的内存,我在第一次阅读时不知何故忽略了它:
Declare a 32-bit integer array A[10] in memory所以使用[lengthA]的伪代码示例
; for i := 0 to (L-1)
xor edi,edi ; index i
input_loop:
cmp edi,[lengthA]
jge input_loop_ends ; if (i >= L), end for
; TODO: Prompt for and input A[i]
; eax = input value, preserve edi (!)
mov [A + 4*edi],eax ; store value into A[i]
; end for
inc edi ; ++i
jmp input_loop
input_loop_ends:
; memory at address A contains L many DWORD values from userhttps://stackoverflow.com/questions/47273206
复制相似问题