#########################################
.text
main:
# Prompt for the string to enter
li $v0, 4
la $a0, prompt
syscall
# Read the string.
li $v0, 8
la $a0, word
lw $a1, inputSize
syscall
la $t0, word #set it so that word is in t0
li $t1, 0 #length of string
jal string_length #find length of stirng and store it in string_length
li $t2, 0 #counter for print_loop
jal print_loop
j end
string_length:
lb $s0, ($t0) #set byte to be checked to s0
bnez $s0, not_end_string_length #if not at end of string then return to start of loop
jr $ra #return to main, or wherever function was called
not_end_string_length:
addiu $t0, $t0, 1 #increment pointer
addiu $t1, $t1, 1 #add 1 to counter with length of string
j string_length #go back to string_length
print_loop: #print out for the amount of times that is the length of word entered
la $t0, word #put word in t0
li $t3, 0 #counter for inner loop
addi $t4, $1, -1 #set length of string to be printed to be one less than previous length printed
# print out a new line (formatting)
li $v0, 4
la $a0, newLine
syscall
#works until here
blt $t2, $t1, print_out #if counter is less than string length then print_out
jr $ra #return to where called (end loop)
print_out:
bge $t3, $t4, print_loop #go back to print loop if amount of character has reached amount that should be printed
j not_end_print_out
not_end_print_out: ######################################## Errors are here
#print out char num t3#
addi $t0, $t0, 1
#set char to be value t3
lb $a0, 0($t0)
li $v0, 11
syscall
addi $t3, $t3, 1
j print_out #go back to print_out
####################################################################
end:
# Output end message
li $v0,8
la $a0, endMessage
syscall
#
.data
prompt: .asciiz "\nEnter a string: "
word: .space 31
char: .space 2
inputSize: .word 30
charSize: .word 1
newLine: .asciiz "\n"
endMessage: .asciiz "\nYou have reached the end. Goodbye"
我需要我的代码来打印用户输入的字符串,其中每次删除最后一个值。例如,如果输入“hi”则
嗨
h
将被打印出来。
我尝试用打印字符的代码更改一些内容,但老实说我真的不知道该怎么做。我尝试更改它,以便有几种不同的方法来打印内容,但什么也没有。由于某种原因,第一个字符没有被打印,然后只打印了一行。该行有一些垃圾值。
如果有人想知道我设法通过改变一些小错误来解决这个问题。事实证明,大多数问题都是小问题,比如写 $1 而不是 $t1。希望下次我写代码时我会多检查几次。