1
"안녕하세요, 당신 이름이 무엇입니까?"와 같은 문자열이 주어졌습니다.
재귀 함수를 사용하여 단어를 역 분개해야합니다.
예제 문자열의 결과는 "name? your hello입니다"
언어는 MIPS 어셈블리입니다.문장의 밉스 단어를 역순으로 바꾸십시오.
.macro print_int(%arg)
li $v0, 1
add $a0, %arg, $zero
syscall
.end_macro
.macro print_string(%arg)
move $t9, $a0
li $v0, 4
add $a0, %arg, $zero
syscall
move $a0, $t9
.end_macro
.text
la $s0, string
li $s1, 32 # space
la $t8, space
sub $s0, $s0, 1
sb $s1, 0($s0)
# find the length of the string
move $t0, $s0 # $t0 = i = the iterator
L1: lb $t1, 0($t0) # $t1 = i'th char of the string
beq $t1, 0, Exit # if string[i] == null, Exit
addi $t0, $t0, 1 # i++
j L1
Exit:
sub $s3, $t0, $s0 # $s3 is the length of the string
# Set arguements
move $a0, $s0
move $a1, $t0 # endFlag = length of the string
jal reverse # call the function
li $v0, 10
syscall # exit
reverse:
# save registers
sub $sp, $sp, 12
sw $ra, 0($sp)
sw $a0, 4($sp)
sw $a1, 8($sp)
bgt $a1, $s0, L2 # base case
add $sp, $sp, 12
jr $ra
# find a word in the string
L2:
add $t0, $zero, $a1
add $t3, $a0, $s3 # address of last character of the string
Loop:
lb $t4, 0($t3) # chracter from the string
seq $v0, $s1, $t4 # if space
ble $t3, $a0, Exit_Loop # if first of string
beq $v0, 1, Exit_Loop # if character was space
sub $t3, $t3, 1
j Loop
Exit_Loop:
sb $zero, 0($t3)
add $t3, $t3, 1
print_string($t3)
print_string($t8)
#recursive call
move $a1, $t3
jal reverse
# load registers
lw $ra, 0($sp)
lw $a0, 4($sp)
lw $a1, 8($sp)
add $sp, $sp, 12 # release the stack
jr $ra
.data
string: .asciiz "hello what is your name?"
newline: .asciiz "\n"
space: .asciiz " "
감사합니다. 나는 당신이 괜찮다면 또 다른 Q를 얻었습니다. 이제, 그 반전 된 문자열을 메모리 어딘가에 저장하고 싶습니다. 그러나 나는 아무 곳에도 가지 않는다. 나는 그 자리에있는 메모리의 반전 된 문장의 각 부분을 저장하는 코드 줄이 "print_int (..)"를 어디에 두 었는지 알고 있습니다. – user3100883
http://stackoverflow.com/questions/20591641/reverse-a-string-and-save-the-reversed-sentence-in-memory – user3100883
@ user3100883 내 반응이 늦어서 죄송합니다. 나는 나의 연구 프로젝트의 진행을 밀어 내기 위해 미친 불면의 한 주를 보내고 있었고, 심지어 오후의 차를 마실 시간조차 없었습니다. 나는 그것을 곧 보게 될 것입니다. 불편을 끼쳐 드려 죄송합니다. – starrify