gpt4 book ai didi

string - 如何在mips程序集中迭代字符串

转载 作者:行者123 更新时间:2023-12-01 09:03:58 26 4
gpt4 key购买 nike

所以我现在在一个项目上工作,我们必须输入一个字符串并将其转换为所有大写字母,然后再转换为小写字母(如果toUpper或toLower字节不是0的话)。部分(猜测小写字母几乎是相同的),但是当我遍历字符串以查找大写字母或调用syscall打印时,我陷入了困境。任何帮助都会很棒。这是我到目前为止的内容:

    .data

toUpper: .byte 1
toLower: .byte 0

string:
.asciiz " A long time ago in a Galaxy far, far away...."

# Your code goes below this line

origString:
.asciiz "Original string:\n"

toUpForwardStr:
.asciiz "\nConverted to upper-case:\nForward:\n"

toLowerForwardStr:
.asciiz "\nConverted to lower-case:\nForward:\n"

backwardStr:
.asciiz "Backward:"

.text



main:
# Function prologue
subu $sp, $sp, 24 # allocate stack space -- default of 24 here
sw $fp, 0($sp) # save caller's frame pointer
sw $ra, 4($sp) # save return address
addiu $fp, $sp, 20 # setup main's frame pointer

la $a0, origString #Print "Original String:"
addi $v0, $zero, 4
syscall

la $a0, string #Print string
addi $v0, $zero, 4
syscall

la $s0, toUpper
sb $s1, 0($s0) #toUpper stored in $s1

la $s0, toLower
sb $s2, 0($s0) #toLower stored in $s2

bne $s1, $zero, toUpperCase #Jump toUpperCase if toUpper ≠ 0



toUpperCase:
la $a0, toUpForwardStr #Print "Converted to upper-case:"
addi $v0, $zero, 4 # "Forward:"
syscall

la $s3, string #$s3 holds address to string

addi $s1, $zero, 0 #$s1 = i = 0

j upperCaseLoop #Goto upperCaseLoop

upperCaseLoop:

# Compute address of string[i]

add $t2, $s3, $s1 # $t2 = address of string[i]
lb $t3, 8($t2) # $t3 = elements[i]

beq $t3, $zero, upperDone # test if for loop is done

addi $t6, $zero, 96 #$t6 = 96 (lowercase letters)
bgt $t3, $t6, isLowercase1#If letter is lowercase goto isLowercase1

comeBackFromLowercaseIfs:

move $t3, $a0
addi $v0, $zero, 11
syscall




addi $s1, $s1, 1 # i++
j upperCaseLoop

upperDone:

bne $s2, $zero, toLowerCase #Jump toLowerCase if toLower ≠ 0


toLowerCase:

la $a0, toLowerForwardStr #Print "Converted to lower-case:"
addi $v0, $zero, 4 # "Forward:"
syscall
j done #The END!!







isLowercase1:
addi $t7, $zero, 123 #$t7 = 123
blt $t3, $t7, isLowercase2 #Goto isLowercase2
j comeBackFromLowercaseIfs #Go back to uppercaseLoop

isLowercase2:
addi $t3, $zero, -30 #changes letter to lowercase
j comeBackFromLowercaseIfs #Go back to uppercaseLoop



done:
# Epilogue for main -- restore stack & frame pointers and return
lw $ra, 4($sp) # get return address from stack
lw $fp, 0($sp) # restore the caller's frame pointer
addiu $sp, $sp, 24 # restore the caller's stack pointer
jr $ra # return to caller's code

最佳答案

1个

move    $t3, $a0

应该
move    $a0, $t3

2这个
lb      $t3, 8($t2)

应该
lb      $t3, 0($t2)

3这个
addi    $t3, $zero, -30         #changes letter to lowercase

应该
addi    $t3, $t3, -32         #changes letter to lowercase

关于string - 如何在mips程序集中迭代字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12739463/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com