i
The length of a null-terminated string is defined as the number of characters it contains not counting the null. To calculate length, start the count at zero. Then increment the count for each successive non-null byte. When you hit the null, stop counting.
The structured flow chart shows this procedure. It describes the algorithm in general terms. Assembly language details are left to the coding stage. Here is an outline of the program:
## strlen.asm ## ## Count the characters in a string ## ## Registers: ## $8 -- count ## .text .globl main # Initialize main: ori $8,$0,0 # count = 0 # while not ch==null do loop: . . . . . . . . . j loop sll $0,$0,0 # branch delay slot # finish done: sll $0,$0,0 # target for branch .data string: .asciiz "Time is the ghost of space."
Why is the null not counted in the length of a string? (Hint: think about what happens when two strings are concatenated.)