# COMP2421 Assignment 2

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

## Question 1

    # Get value of A
lw $t3, 80($s2)       # load word at address $t2 to$t3

# Get value of B[A]
mul $t4,$t3, 4     # get index
add $t5,$s3, $t4 # calculate offset lw$t6, ($t5) # load word at that address # Add y to value of B[A] add$s0, $s1,$t6


Full executable program for verification is attached below.

## Question 2

loop:
# Check condition for this while loop
slti $t8,$s0, 10   # t8 is 1 if the condition satisfies, 0 if not
beq $t8,$zero, exit # if condition doesn't hold ,exit without getting into the loop

add $t0,$s0, $s1 # add x and y and save to$t0

mul $t3,$s0, 4     # $t3 is the offset add$t4, $s2,$t3   # $t4 is the address of A sw$t0, ($t4) # store the value to address addi$t1, $s0, 1 # add 1 to x value move$s0, $t1 j loop # run the loop body again exit: # program to be run after the while loop  Full executable program for verification is attached below. ## Question 3 // Assume variable x and y are already been declaimed with values int i; i = 0; while (y != 0){ i += x; y -= 1; } i += 1; return i;  This function calculate the value of $$x \times y+1$$ and return the value. ## Question 4  # Check condition in if statement slt$t5, $t1,$t2       # compare values in $t1 and$t2
beq $t5,$zero, L1      # if $t5 is zero, x<y not satisfied, exit bne$t3, $zero, L1 # if condition doesn't hold ,exit without getting into the loop li$t4, 1               # set value of w to 1

j L1
L1:
# program after this instruction


## Full programs for verification

### Program for question 1

# assign2_1.s

.data
arrayA:   .word  0:20       # assume array A has length of 20
arrayB:   .word  0:20       # assume array A has length of 20

.globl main # Global variable: the entry point of the prog.

.text
main:
# Pre-process: load all values to registers
li $s1, 5 # assume y is 5 la$s2, arrayA # load base address of array A
la $s3, arrayB # load base address of array B # Get value of A lw$t3, 80($s2) # load word at address$t2 to $t3 # Print value of A move$a0, $t3 li$v0, 1
syscall

# Get value of B[A]
mul $t4,$t3, 4     # get index
add $t5,$s3, $t4 # calculate offset lw$t6, ($t5) # load word at that address # Print value of B[A] move$a0, $t6 li$v0, 1
syscall

# Add y to value of B[A]
add $s0,$s1, $t6 # Print final value for x move$a0, $s0 li$v0, 1
syscall

# Terminate the program
li $v0, 10 syscall  ### Program for question 2 Following verification programs do not simulate function call part, just logical operations part. # assign2_2.s .data array: .word 0:10 # assume array A has length of 20 .globl main .text main: # Pre-process: load all values to registers li$s0, 1           # assume x is 1
li $s1, 5 # assume y is 5 la$s2, array       # load base address of array

loop:
# Check condition for this while loop
slti $t8,$s0, 10   # t8 is 1 if the condition satisfies, 0 if not
beq $t8,$zero, exit # if condition doesn't hold ,exit without getting into the loop

add $t0,$s0, $s1 # add x and y and save to$t0

mul $t3,$s0, 4     # $t4 is the offset add$t4, $s2,$t3   # $t5 is the address of A sw$t0, ($t4) # store the value to address addi$t1, $s0, 1 # add 1 to x value move$s0, $t1 # Print out the result for verification move$a0, $t0 li$v0, 1
syscall

move $a0,$s0
li $v0, 1 syscall j loop # run the loop body again exit: # Terminate the program li$v0, 10
syscall


### Programs for question 3

Following verification programs do not simulate function call part, just logical operations part.

# assign2_3.s

.data
str1: .asciiz "------\n"

.globl main # Global variable: the entry point of the prog.

.text
main:
li $a3, 3 li$a1, 17
add $t0,$zero, $zero loop: beq$a1, $zero, Done move$a0, $t0 li$v0, 1
syscall

move $a0,$a3
li $v0, 1 syscall add$t0, $t0,$a3

move $a0,$t0
li $v0, 1 syscall sub$a1, $a1, 1 move$a0, $a1 li$v0, 1
syscall

j loop

Done:
la $a0, str1 # load string address into$a0 and I/O code into $v0 li$v0, 4
syscall
addi $t0,$t0, 1

move $a0,$t0
li $v0, 1 syscall #add$v0, $t0,$zero
li $v0, 10 syscall  // assign2_3.c #include <stdio.h> int main () { int x, y; x = 3; y = 17; // Assume variable x and y are already been declaimed with values int i; i = 0; while (y != 0){ i += x; y -= 1; } i += 1; // return i; printf("%d", i); }  ### Program for question 4 # assign2_4.s .data str1: .asciiz "Getting in the if body\n" .globl main # Global variable: the entry point of the prog. .text main: li$t1, 3 # x
li $t2, 4 # y li$t3, 0 # z
li $t4, 0 slt$t5, $t1,$t2 # compare values in $t1 and$t2
beq $t5,$zero, L1 # if $t5 is zero, x<y not satisfied, exit bne$t3, $zero, L1 # if condition doesn't hold ,exit without getting into the loop la$a0, str1
li $v0, 4 syscall li$t4, 1 # set value of w to 1
j L1
L1:
move $a0,$t4
li $v0, 1 syscall li$v0, 10
syscall


## References

http://pages.cs.wisc.edu/~smoler/cs354/onyourown/arrays.html

http://logos.cs.uic.edu/366/notes/mips%20quick%20tutorial.htm

https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Mips/cond.html

https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Mips/pseudojump.html