Colour Theme | Font Size Options | |
In this lab, we are going to explore how to make loops and how to implement if-then-else statement in SPIM.
In this section, the control structure instructions and examples are given for your reference.
The simplest control instruction jump j addr means jump to address addr which makes the processor to fetch the next instruction from the word at address addr. Actually, the implementation is just copying the value of the address to the program counter. There are another two jump instructions jal addr and jr Rsrc They are used for procedure/function calls. We will talk more about them later.
Here are some examples of MIPS jump, branch and compare instructions:
beq $t0, $t1, addr means branch to addr if $t0 == $t1 beqz $t0 addr means branch to addr if $t0 == 0 bne $t0, $t1, addr means branch to addr if $t0 != $t1 blt $t0, $t1, addr means branch to addr if $t0 < $t1 ble $t0, 5, loop means branch to loop if $t0 <= 5 bgt $t0, $t1, addr means branch to addr if $t0 > $t1 bge $t0, 5, loop means branch to loop if $t0 >= 5
## ## The program --- length.s ## ## - will print out the length of character ## string "str". ## ## ## - $t0 - holds each byte from string in turn ## - $t1 - contains count of characters ## - $t2 - points to the string ## ################################################# # # # text segment # # # ################################################# .text .globl __start __start: # execution starts here la $t2,str # t2 points to the string li $t1,0 # t1 holds the count nextCh: lb $t0,($t2) # get a byte from the string beqz $t0,strEnd # zero means end of string addi $t1, $t1, 1 # increment count addi $t2, $t2, 1 # move pointer one character j nextCh # go round the loop again strEnd: la $a0,ans # System call li $v0,4 # to print out syscall # the string message move $a0,$t1 # copy the count to a0 li $v0,1 # System call 1 syscall # to print the length worked out la $a0,endl # syscall to print out li $v0,4 # a newline syscall li $v0,10 syscall # Bye! ################################################# # # # data segment # # # ################################################# .data str: .asciiz "hello world" ans: .asciiz "Length is " endl: .asciiz "\n" ## ## end of file length.s
## ## The program --- loop1.s ## ## - will replace all occurrences of 'a' with ## 'A' in the string "chararray" and ## print the resulting string. ## ## - chararray: "abbbaabbbabababab\n" ## - output: "AbbbAAbbbAbAbAbAb" ## ## ################################################# # # # text segment # # # ################################################# .text .globl __start __start: # execution starts here la $t2,chararray # t2 points to the string li $t1,'A' # t1 holds the char 'A' nextCh: lb $t0,($t2) # get a byte from the string beqz $t0,strEnd # zero means end of string bne $t0,'a',nota # if not 'a' go to nota sb $t1,($t2) # store upper case 'A' nota: addi $t2, $t2, 1 # move pointer one character j nextCh # go round the loop again strEnd: la $a0,chararray # System call li $v0,4 # to print out syscall # the modified string li $v0,10 syscall # Bye! ################################################# # # # data segment # # # ################################################# .data chararray: .asciiz "abbbaabbbabababab\n" ## ## end of file loop1.s