COMP2421 Assignment 3

MA Mingyu (Derek) | Oct 17, 2017
derek.ma | derek.ma@connect.polyu.hk

Question 1

# Assume the function name is len
#  base address of the string is in $a0 already
#  return value is saved in $v0
len:  li   $v0, 0       # int len = 0
loop: lb   $t0, 0($a0)  # load the character
      bne  $t0, $zero, upt # if $t0 is not 0, update the counters
      jr   $ra          # go back to caller
upt:  addi $v0, $v0, 1  # update length counter
      addi $a0, $a0, 1  # update address
      j    loop         # go next round loop

Full executable program for verification is attached below.

Question 2

a

slt $t0, $t5, $t3
beq $t0, $zero, NEXT

b

slt $t0, $t5, $t4
bne $t0, $zero, NEXT

c

slt $t0, $t5, $t3
beq $t0, $zero, NEXT

d

lui   $at, upper(big)
ori   $at, $at, lower(big)
addu  $t0, $t2, $at

Question 3

000000 00000 10010 10001 11110 000010

Question 4

Assumption: all $t3 register in the following code are aimed to make the program executable on QtSpim. For simulating what happens in assembler, please replace all $t3 with at.

a

lui $t3, 18838
ori $t3, $t3, 722
addu $v0, $zero, $t3

b

lui $t3, 18838
ori $t3, $t3, 722
addu $v0, $zero, $t3

slt $t1, $t0, $v0
bne $t1, $zero, NEXT

c

Please note that $t1 is overflow as a signed number if add these two 32-bit number together. If output the result in $t1 as unsigned number using commmand 36, the result is right.

lui $t3, 18838
ori $t3, $t3, 722
addu $v0, $zero, $t3

lui $t3, 15070
ori $t3, $t3, 26801
addu $v1, $zero, $t3

addu $t1, $v0, $v1

Full Executable Program for Question 1

This is the executable .s program for verification for question 1.

       .data
str0: .asciiz "stringexample"

       .globl main

       .text
main:
      la $a0, str0      # load sample string
      jal len           # call function

      move $a0, $v0     # get ready to print the output result from function
      li $v0, 1         # print integer
      syscall

      li $v0 ,10        # terminate the program
      syscall

len:  li   $v0, 0       # int len = 0
loop: lb   $t0, 0($a0)  # load the character

      move $t3, $a0     # debug:print the character out
      move $t4, $v0
      move $a0, $t0
      li $v0, 11
      syscall
      move $a0, $t3
      move $v0, $t4

      bne  $t0, $zero, upt # if $t0 is not 0, update the counters
      jr   $ra          # go back to caller
upt:  addi $v0, $v0, 1  # update length counter
      addi $a0, $a0, 1  # update address
      j    loop         # go next round loop