|
-
Mar 3rd, 2008, 12:43 AM
#1
Thread Starter
Addicted Member
quick question
how to check a length of a given string.
I put the following sentence into buffer1
"Hi there."
I wanna know the length of that string. how would I do that?
Thanks a lot.
EDIT: I use PCSpim (if it even matter)
effort effort effort and still effort
-
Mar 6th, 2008, 11:17 PM
#2
Thread Starter
Addicted Member
Re: quick question
here is the code what I have so far. It didn't print the correct result. i need to put a termination charachter at the end of the string. I tried to but failed.
can anybody help me out?
Code:
.data
prompt:
.asciiz "Please enter a line of text:\n"
name :
.asciiz "whata: "
input :
.asciiz "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW"
output:
.asciiz "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ\n"
nl:
.asciiz "\n"
.text
.globl main
main:
li $v0, 4
la $a0, input
syscall #cout << input;
li $v0, 4
la $a0, output
syscall #cout << output;
li $v0, 4
la $a0, name
syscall #cout << name;
la $a0, prompt # Starting address of string.
li $v0, 4
syscall
la $a0, input #input # Starting address of buffer.
li $a1, 42 # Length of buffer.
li $v0, 8
syscall
li $v0, 4
la $a0, input
syscall #cout << input;
li $v0, 4
la $a0, output
syscall #cout << output;
li $v0, 4
la $a0, input
syscall #cout << input;
# Now, let's reverse the string.
la $s0, output # $s0 points to beginning of line.
la $s1, input # So does $s1, for now.
# We want $s1 pointing to the '\n' at the
# end of the line.
while1:
lb $t0, 0($s1)
beq $t0, 10, while2
addi $s1, $s1, 1
b while1
while2:
addi $s1, $s1, -1 # Now, $s1 points to the last character
# of the line.
# Ok, $s0 points to the beginning of the
# line and $s1 points to the end. Start
# swapping characters to reverse the string.
while3:
beq $t1, 10, while4
lb $t0, 0($s0)
lb $t1, 0($s1)
sb $t0, 0($s1)
sb $t1, 0($s0)
addi $s0, $s0, 1
addi $s1, $s1, -1
b while3
while4:
lb $t0, 0($s0)
sb $t0, 10
# Print the reversed string.
li $v0,4
la $a0, nl
syscall
li $v0,4
la $a0, output
syscall
li $v0,4
la $a0, input
syscall
li $v0,4
la $a0, nl
syscall
li $v0,4
la $a0, output
syscall
li $v0, 4
la $a0, name
syscall #cout << name;
# Return to startup code.
li $v0, 0 # Return value is zero.
jr $ra
Last edited by ayahnabunda; Feb 28th, 2010 at 04:40 AM.
effort effort effort and still effort
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|