如何使用 Irvine 库打印格式化输出?

问题描述 投票:0回答:0

所以这些是我的代码的目标: 本作业的目的是让您体验使用循环、数组以及创建和使用您自己的过程的经验。您需要创建一个程序,允许用户输入一个字符串并在该字符串中搜索用户指定的字符。程序将返回该字符在字符串中出现的次数、该字符的第一次出现和最后一次出现。

要求: 允许用户输入要在搜索中使用的字符。 您必须创建一个搜索过程,该过程接收对用户输入的字符串的引用(指针)、字符串的大小以及要在搜索中使用的字符。此过程无法获得用户的输入或将输出显示到屏幕。它必须简单地查看字符串,找到必要的信息,并使用寄存器返回输出(出现次数、第一次出现和最后一次出现)。 您必须使用变量来存储所有用户输入和值。 您必须在代码中使用注释和良好的编程习惯(适当的间距、适当的代码缩进和良好的命名)。

示例程序:

Enter a string: CSC203 RCSJ
Enter a character to use in the search: C

Performing search...
Search completed.

Search results:
The character 'C' was found 3 times in the string.
First occurrence: 1
Last occurrence: 9

我的代码是这样的:

INCLUDE Irvine32.inc

.DATA
inputString BYTE 100 DUP(0) ; Define a buffer for the user's input string
searchChar BYTE ? ; Define a variable to store the character to search for
;strLength DWORD ? ; Define a variable to store the length of the string
count DWORD 0 ; Define a variable to store the count of occurrences
firstIndex DWORD 0 ; Define a variable to store the index of the first
lastIndex DWORD 0 ; Define a variable to store the index of the last occurrence
promptString BYTE "Enter a string: ",0
promptChar BYTE "Enter a character to use in the search: ",0
searchStart BYTE "Performing search...",0
searchEnd BYTE "Search completed.",0
resultsTitle BYTE "Search results:",0
foundCountString BYTE "The character '%c' was found %d times in the string.",0dh, 0ah, 0
firstOccurString BYTE "First occurrence: %d",0
lastOccurString BYTE "Last occurrence: %d",0

.CODE
main PROC
    ; prompt the user for input string
    mov edx, OFFSET promptString
    call WriteString
    mov edx, OFFSET inputString
    mov ecx, SIZEOF inputString
    call ReadString




    ; prompt the user for search character
    mov edx, OFFSET promptChar
    call WriteString
    call ReadString
    call Crlf
    mov searchChar, al
    call Crlf


    ; perform the search
    mov edx, OFFSET searchStart
    call WriteString
    mov count, 0 ; initialize count to 0
    call SearchForChar
    call Crlf
    call Crlf
    mov edx, OFFSET searchEnd
    call WriteString
    call Crlf
    call Crlf
    ; display the results
    mov edx, OFFSET resultsTitle
    call WriteString
    call Crlf

   mov eax, count
movzx ecx, searchChar
mov edx, OFFSET foundCountString
call WriteString
call Crlf
call WriteChar ; add this line to print the searchChar value
call WriteDec ; add this line to print the count value

mov edx, OFFSET firstOccurString
call WriteString
mov eax, firstIndex
call WriteDec
call Crlf

mov edx, OFFSET lastOccurString
call WriteString
mov eax, lastIndex
call WriteDec
call Crlf


    exit
main ENDP

SearchForChar PROC
    ; initialize variables
    mov ecx, edx
    mov esi, OFFSET inputString
    movzx ebx, searchChar
    mov count, 0
    mov firstIndex, -1
    mov lastIndex, -1

    ; loop through the string
    mov edi, 0
    searchLoop:
        cmp BYTE PTR [esi+edi], 0 ; check for end of string
        je endSearchLoop
        cmp BYTE PTR [esi+edi], bl ; compare current character with
        jne nextChar
        add count, 1 ; increment count
        cmp firstIndex, -1 ; check if first occurrence has been found
        jne nextChar
        mov firstIndex, edi ; set first occurrence
        nextChar:
        mov lastIndex, edi ; update last occurrence
        inc edi ; move to next character
        jmp searchLoop
    endSearchLoop:

    ; return the results in registers
    mov eax, count
    mov ebx, firstIndex
    mov ecx, lastIndex
    ret
SearchForChar ENDP
END main

调试屏幕上显示的结果是:

Enter a string: teddy
Enter a character to use in the search: d


Performing search...

Search completed.

Search results:
The character '%c' was found %d times in the string.

0First occurrence: %d4294967295
Last occurrence: %d4 

我需要帮助来格式化代码并将“%c”和 %d 替换为正确的值。所以它会说“字符‘d’在字符串中被找到了 2 次。”

assembly masm irvine32
© www.soinside.com 2019 - 2024. All rights reserved.