Reputation: 1387
I want to write a program using loops that prints the asterisk character '*' five times, so I wrote this:
.data
ast: .word '*'
.text
.globl main
main:
la $a0, ast
add $t0, $zero, $zero #counter
loop:
slti $t1, $t0, 5
beq $t1, $zero, exit
li $v0, 1
syscall
addiu $t0, $t0, 1
j loop
exit:
li $v0, 10
syscall
but instead of printing five asterisks, it gives me this huge number: 268500992268500992268500992268500992268500992
By running step by step I see that $t0 that I use for the counter has the correct value for each loop (1 to the 2 to the 3 to the 4 to the 5). With the slti and beq lines I try to control the loops till the counter reaches the number 5.
What is it missing?
Upvotes: 1
Views: 2273
Reputation: 36
There are two problems with your code. Firstly in data declarations the asterisk should be decaled as .asciiz instead of .word. The .word data type is used for integers usually as it is 32 bits long. Secondly, you have used the wrong syscall to print the aesterick. li $v0, 1 is for printing integers. The correct one to be used here is li $v0, 4.
.data
ast: .asciiz "*"
.text
.globl main
main:
la $a0, ast
add $t0, $zero, $zero #counter
loop:
slti $t1, $t0, 5
beq $t1, $zero, exit
li $v0, 4
syscall
addiu $t0, $t0, 1
j loop
exit:
li $v0, 10
syscall
The above code prints 5 *. Hope this helped. Cheers!
Upvotes: 2