## strlen.asm
##
## Count the characters in a string
##
## Registers:
## $8 -- count
## $9 -- pointer to the char
## $10 -- the char (in low order byte)
.text
.globl main
# Initialize
main: ori $8,$0,0 # count = 0
lui $9,0x1000 # point at first char
# while not ch==null do
loop: lbu $10,0($9) # get the char
sll $0,$0,0 # branch delay
beq $10,$0,done # exit loop if char == null
sll $0,$0,0 # branch delay
addiu $8,$8,1 # count++
addiu $9,$9,1 # point at the next char
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."
Program di atas sangat dekat dengan saudaranya di standar library C function
int strlen(char*). Program assembly ini setelah ditulis hanya akan memiliki datanya sendiri dan tidak bisa sebagai function yang bisa dipanggil, kita nanti akan menulis program function yang bisa dipanggil dan memiliki parameter.
Tidak ada komentar:
Posting Komentar