.data
StringOriginal: .asciiz "Frase Introduzida:\n" #ex_msg_og
StringFinal: .asciiz "\nFrase Encriptada:\n"
String: .asciiz "Hello World"
.text
Main:
la $v1,String #load addr of string into $v0
la $t1,($v1) #copy addr into $t0 for later access
lb $a1,($v1) #load byte from string addr
Loop1Original: #strlen_loop
beqz $a1,Memoria #alloc_mem
addi $a0,$a0,1 #increment strlen_counter
addi $v1,$v1,1 #increment ptr
lb $a1,($v1) #load the byte
j Loop1Original
Memoria:
li $v0, 9 #alloc memory, $a0 is arg for how many bytes to allocate
#result is stored in $v0
syscall
la $t0, ($v0) #$v0 is static, $t0 is the moving ptr
la $v1, ($t1) #get a copy we can increment
add $t1, $t1, $a0 #add strlen to our original, static addr to equal last char
subi $t1, $t1, 1 #previous operation is on NULL byte, i.e. off-by-one error.
#this corrects.
copy_str:
lb $a1, ($t1) #copy first byte from source
Loop2Original:
beq $a0,0,Final
sb $a1,($t0) #store the byte at the target pointer
addi $t0,$t0,1 #increment target ptr
subi $t1,$t1,1
subi $a0,$a0,1
lb $a1,($t1) #load next byte from source ptr
j Loop2Original
Final:
la $a1,($v0) #store our string at $v0 so it doesn't get overwritten
li $v0,4 #set syscall to PRINT
la $a0,StringOriginal #PRINT("original string:")
syscall
la $a0,($v1) #PRINT(original string)
syscall
la $a0,StringFinal #PRINT("copied string:")
syscall
la $a0,($a1) #PRINT(strcopy)
syscall
li $v0,10 #EXIT(0)
syscall