1

我用汇编语言编写了一个程序,它执行以下操作:

-instructs user
-prompts the user to enter a character
-reads the user's input into a char variable
-the program terminates if the user enters 'q' (lower-case)
-if user enters a numeral, program echoes that numeral N+1 times
-any other character gets echoed just once

这是我的代码和示例输入/输出:

# Constants
        .equ    STDIN,0         #Input
        .equ    STDOUT,1        #Output
# Stack Frame
        .equ    count,-8
        .equ    response,-4
        .equ    localSize,-16
# Read only data
        .section   .rodata
instructions:   # Displays instructions for user
        .ascii  "\nA single numeral, N, will be echoed N+1 times."
        .asciz  "\nOther characters are echoed once. 'q' ends the program.\n\n"
prompt:
        .string "Enter a single character: "
msg:
        .string "You entered: "
terminate:
        .string "End of program.\n\n"
newline:
        .byte   '\n'
    # Code
            .text
            .globl  main
            .type   main, @function
    main:
            pushq   %rbp            # save caller's base pointer
            movq    %rsp, %rbp      # establish our base pointer
            addq    $localSize, %rsp   # local variables

            movl    $instructions, %esi     # shows instructions for user
    instructionLoop:
            cmpb    $0, (%esi)      # check if end of string?
            je      runLoop         # yes, run program
            movl    $1, %edx        # no, one character
            movl    $STDOUT, %edi
            call    write
            incl    %esi                    # next character
            jmp     instructionLoop         # check at top of loop
    runLoop:
            movl    $prompt, %esi   # prompt user
    promptLoop:
            cmpb    $0, (%esi)      # check if end of string?
            je      getChar         # yes, get user input
            movl    $1, %edx        # no, just one character
            movl    $STDOUT, %edi
            call    write
            incl    %esi            # next character
            jmp     promptLoop      # check at top of loop
    getChar:
            leaq    response(%rbp), %rsi    # place to store the user input
            movl    $2, %edx                # include a newline
            movl    $STDIN, %edi
            call    read

            movb    response(%rbp), %al     # get the input character
            cmpb    $'q', %al               # if 'q'
            je      allDone                 # end program
       # Otherwise, set up a count loop for N
            movl    $1, count(%rbp)         # assume not numeral
            cmpb    $'0', %al               # check for numeral
            jb      echoLoop
            cmpb    $'9', %al
            ja      echoLoop
            andl    $0xf, %eax              # convert numeral to int
            incl    %eax                    # echo N+1 times
            movl    %eax, count(%rbp)       # save counter
    echoLoop:
            movl    $msg, %esi              # pointer to the string
    msgLoop:
            cmpb    $0, (%esi)              # check if end of string?
            je      doChar                  # yes, show the character
            movl    $1, %edx                # no, one character
            movl    $STDOUT, %edi
            call    write
            incl    %esi                    # next character
            jmp     msgLoop                 # check at top of loop
    doChar:
            movl    $1, %edx                # just one character
            leaq    response(%rbp), %rsi    # store in this memory location
            movl    $STDOUT, %edi
            call    write

            movl    $1, %edx                # and a newline
            movl    $newline, %esi
            movl    $STDOUT, %edi
            call    write

            decl    count(%rbp)             # count--
            jne     echoLoop                # continue if more to do
            jmp     runLoop                 # else get the next character
    allDone:
            movl    $terminate, %esi        # ending message
    doneLoop:
            cmpb    $0, (%esi)              # check if end of string?
            je      cleanUp                 # yes, get user input
            movl    $1, %edx                # no, one character
            movl    $STDOUT, %edi
            call    write
            incl    %esi                    # next character
            jmp     doneLoop                # check at top of loop

Enter a single character: a
You entered: a
Enter a single character: A
You entered: A
Enter a single character: Z
You entered: Z
Enter a single character: 0
You entered: 0
Enter a single character: 1
You entered: 1
You entered: 1
Enter a single character: 4
You entered: 4
You entered: 4
You entered: 4
You entered: 4
You entered: 4
Enter a single character:
You entered:
Enter a single character: *
You entered: *
Enter a single character: .
You entered: .
Enter a single character: 11
You entered: 1
You entered: 1
Enter a single character: You entered:

我的问题是: 我将如何计算输入字符串的长度,然后打印出该计数?

例如...

如果我要输入“11”,我希望我的程序确定输入的长度为 2,然后打印出:“您输入了长度为 <2> 的字符串,因此只会显示第一个字符。”

然后我的其余输出将保持不变

4

0 回答 0