【问题标题】:Reading to and from arrays in Assembly?在Assembly中读取和读取数组?
【发布时间】:2014-02-20 04:03:32
【问题描述】:

在汇编中读取和读取数组时遇到了一些问题。

这是一个相当简单的程序(尽管此时还远未完成)。我现在要做的就是读取一串(我们假设是数字),将其转换为十进制数,然后打印出来。这是我到目前为止所得到的。截至目前,它打印 str1。输入数字并按 Enter 后,它会再次打印 str1 并冻结。谁能提供一些关于我做错了什么的见解?

INCLUDE Irvine32.inc

.data

buffersize equ 80
buffer DWORD buffersize DUP (0)

str1 BYTE "Enter numbers to be added together. Press (Q) to Quit.", 0dh, 0ah,0;
str2 BYTE "The numbers entered were: ", 0dh, 0ah, 0
str3 BYTE "The total of numbers entered is: ", 0dh, 0ah, 0
error BYTE "Invalid Entry. Please try again.", 0dh, 0ah,0
value DWORD 0

.code
main PROC
mov edx, OFFSET str1
call Writestring

Input:
 call readstring
 mov buffer[edi], eax

 cmp buffer[edi], 0
 JL NOTDIGIT
 cmp buffer[edi], 9
 JG NOTDIGIT

call cvtDec
mov edx, buffer[edi]
call WriteString
jmp endloop

Notdigit:
 mov edx, OFFSET error
 call writestring
 exit

 cvtDec:
    mov eax, buffer[edi]
    AND eax,0Fh
    mov buffer[edi],edx
    ret

endloop:
main ENDP
END MAIN

【问题讨论】:

  • 请尝试调试,一步一步来。

标签: string assembly x86 masm irvine32


【解决方案1】:

首先,Irvine 先生创建了名为 WriteString 的函数,但您使用了 2 个变体 - writestring 和 Writestring;您确实在一个地方使用了该函数的正确大小写。现在养成使用正确函数名的习惯,以后会减少bug。

其次,您创建了一个名为Notdigit 的标签,但您在代码中使用了JL NOTDIGITJG NOTDIGIT。再次,使用正确的拼写。 MASM 应该给你一个 A2006 错误“未定义的符号”

您还将入口点声明为main,但您使用END MAIN 而不是END main 关闭代码部分。

如果您正确设置了 MASM(通过在源代码顶部添加 option casemap:none。或者只需打开 irvine32.inc 并取消注释显示 OPTION CASEMAP:NONE 的行)

我们看一下irvine32.asm中的ReadString过程注释:

; Reads a string from the keyboard and places the characters
; in a buffer.
; Receives: EDX offset of the input buffer
;           ECX = maximum characters to input (including terminal null)
; Returns:  EAX = size of the input string.
; Comments: Stops when Enter key (0Dh,0Ah) is pressed. If the user
; types more characters than (ECX-1), the excess characters
; are ignored.

ReadString 使用缓冲区的地址来保存edx 中的输入字符串,您使用的是提示符str1 的地址,也许您打算使用buffer?您也没有将缓冲区的大小放入ecx

您使用edi 作为缓冲区的索引,edi 包含什么值?您试图将eax 的值放入其中,eax 包含什么? edieax 都可能包含垃圾;不是你想要的。

仔细看这个:

cvtDec:
    mov     eax, buffer[edi]
    AND     eax,0Fh
    mov     buffer[edi],edx

您将一个值(您认为是数字的 ASCII 值)放入 eax 然后转换为十进制值...好的...接下来,您将 edx 中的任何内容放回您的缓冲。这就是你想要的吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-09
    • 2020-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    相关资源
    最近更新 更多