# print a string one-char-at-a-time using a loop
#
# code uses registers a0, a7, t0, t1, t2
#
# Note: string contains only ASCII characters. UTF-8 has many more
# multi-byte characters.
.data
string: .asciz "Wow, does it work?"
.text
main:
# step 1. setup to print null terminated string chars (ASCII)
la t0, string # address of the first char (byte) of a string
li t1, 0 # initialize string char index
start_print_loop:
# step 2. load string's (ASCII) char into a0
add t2, t1, t0 # set t2 to address of string char (t0 + t1)
lbu a0, 0(t2) # load char (byte) unsigned
# step 3. end of string? (a0 == null char)
beq a0, zero, end_print_loop # If string char is null, exit the loop
# step 4. print string char
li a7, 11 # service code 11 = Print character
ecall
# step 5. Advance to the next char (byte)
addi t1, t1, 1 # Increment string char index
# step 6. loop
j start_print_loop # Jump back to the start of the loop
end_print_loop:
# step 7. exit program
li a0, 0 # returned value
li a7, 93 # exit program
ecall # Trigger exit system call