MIPS Assembly 中的电话目录,打印条目出现问题

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

我正在 MIPS 中制作电话目录,但在获取每次递归时输入的元素时遇到问题。当选择模式 [I](查询)来检索我之前所做的输入时,将使用 Print_Entry 函数。但是,当我创建新条目时,前一个条目的元素会被我插入的最新元素覆盖。每个元素占用 20 个字符的内存。谁能解释为什么该程序不保留以前条目的元素?

    .data

menu: .asciiz "\nPlease determine operation, Entry[E],Inquiry[I] ,Quit[Q]: \n"
get_first: .asciiz "\nPlease enter first name: \n"
get_last: .asciiz "\nPlease enter last name: \n"
get_num: .asciiz "\nPlease enter phone number: \n"
entry_submission: .asciiz "\nThank you, the new entry is the following: "
find_contact: .asciiz "\nPlease enter the entry number you wish to retrieve: "
not_found: .asciiz "\nThere is no such entry in the phonebook"
print_contact: .asciiz "\nThe number is: "
error_message: .asciiz "\nWrong input, please try again\n"
space1: .asciiz "\n"
space2: .asciiz ".\n"
.align 2
last_name: .space 20
first_name: .space 20
phone_num: .space 20
phonebook: .space 600

.text

main:

    la $s1,phonebook    #Set address to phonebook in $s1
    addu $s4, $zero, $s1
    ori $s2, $zero, 0 #Index for how many times we have executed Get_Entry
    sub_main:

        #E=69   I=73    Q=81 (ascii codes)
        #Call function Prompt_User
        jal Prompt_User

        beq $s0,69,x
        beq $s0,73,y
        beq $s0,81,z

        #Print "Wrong input try again" if it isn't one of the 3 letters
        ori $v0, $zero, 4
        la $a0, error_message
        syscall

        j sub_main

        x:
        #Call function Get_Entry
        jal Get_Entry
        j sub_main

        y:
        #Call function Print_Entry
        jal Print_Entry
        j sub_main

        z:
        #Terminate the programm
        ori $v0, $zero, 10
        syscall




Prompt_User:

    #print "Please determine operation, Entry[E],Inquiry [I] ,Quit [Q]: \n"
    ori $v0, $zero, 4
    la $a0, menu
    syscall

    #Get users input
    ori $v0, $zero, 12 #call=12=read character
    syscall

  #Save user's choice in $s0
    addu $s0, $zero, $v0

    jr $ra

Get_Entry:

    addi $sp, $sp, -4   # enlarge the stack, to make room for one word
  sw $ra, 0($sp)        # save return address to stack

    addu $s5, $zero, $s4

    jal Last_Name       #Asks for the Last Name from user
    addi $s4,20
    jal First_Name      #Asks for the First Name from user
    addi $s4,20
    jal Phone_Number    #Asks for the Phone Number from user
    addi $s4,20

    #Prints "Thank you, the new entry is the following: "
    ori $v0, $zero, 4
    la $a0, entry_submission
    syscall

    ori $v0, $zero, 4
    la $a0, space1
    syscall


    #Prints index
    ori $v0, $zero, 1
    addu $a0, $zero, $s2
    syscall

    ori $v0, $zero, 4
    la $a0, space2
    syscall

    #Prints Last Name
    ori $v0, $zero, 4
    lw $a0, 0($s5)
    syscall

    #Prints first name
    ori $v0, $zero, 4
    lw $a0, 20($s5)
    syscall

    #Prints phone number
    ori $v0, $zero, 4
    lw $a0, 40($s5)
    syscall


    addi $s2,$s2,1      #Progress counter of Get_Entry executions

    lw $ra, 0($sp)      # load return address to stack

    addi $sp, $sp, 4    # shrink the stack to its former size
    jr $ra

Last_Name:

    #Ask for last name
    ori $v0, $zero, 4
    la $a0, get_last
    syscall

    #Read user's input
    ori $a1, $zero, 20
    ori $v0, $zero, 8
    la $a0,last_name
    syscall

    la $t0, last_name       #Load address of user's input in $t0
    sw $t0, 0($s4)          #Store user's string in phonebook


    jr $ra

First_Name:

    #Ask for first name
    ori $v0, $zero, 4
    la $a0, get_first
    syscall

    #Read user's input
    ori $a1, $zero, 20
    ori $v0, $zero, 8
    la $a0,first_name
    syscall

    la $t0, first_name      #Load address of user's input in $t0
    sw $t0, 0($s4)          #Store user's string in phonebook

    jr $ra

Phone_Number:

    #Ask for Phone Number
    ori $v0, $zero, 4
    la $a0, get_num
    syscall

    #Read user's input
    ori $a1, $zero, 20
    ori $v0, $zero, 8
    la $a0,phone_num
    syscall

    la $t0,phone_num        #Load address of user's input in $t0
    sw $t0, 0($s4)          #Store user's string in phonebook

    jr $ra

Print_Entry:

    #Print "Please enter the entry number you wish to retrieve:"
    ori $v0, $zero, 4
    la $a0, find_contact
    syscall

    #Get user's choice
    ori $v0, $zero, 5
    syscall
    addu $t8, $zero, $v0

    #Check if user's input is less than 0
    slt $t7, $t8, $0
    bne $t7, $zero, exit
    #Check if user's input is greater than the number of entries in the catalog
    slt $t7, $s2, $t8
    bne $t7, $zero, exit

    addi $t1,$zero,60
  mul $t9, $t8, $t1
    addu $s3, $s1, $t9

    #Print "The number is:"
    ori $v0, $zero, 4
    la $a0, print_contact
    syscall

    #Print Last name of registration
    ori $v0, $zero, 4
    lw $a0, 0($s3)
    syscall

    #Print First name of registration
    ori $v0, $zero, 4
    lw $a0, 20($s3)
    syscall

    #Print phone number of registration
    ori $v0, $zero, 4
    lw $a0, 40($s3)
    syscall


    jr $ra

    exit:

        #Print "There is no such entry in the phonebook"
        ori $v0, $zero, 4
        la $a0, not_found
        syscall

    jr $ra
assembly mips
1个回答
0
投票

图??? xreiazomai thn voitheia su

© www.soinside.com 2019 - 2024. All rights reserved.