Exercises

These are the implementations of the exercises presentend in course's slides and notations.

; pdf 3 slide 10

.data

.text
    li $s0, 2 #; a = 2
    li $s1, 5 #; b = 5
    li $s2, 9 #; c = 9
    li $s3, 4 #; d = 4
    li $s4, 12 #; e = 12
    
    #; a = ( b - c ) + ( d - e )

    sub $t0, $s1, $s2 #; t0 = b - c
    sub $t1, $s3, $s4 #; t1 = d - e
    add $s0, $t0, $t1 #; s0 = t0 + t1
; pdf 3 slide 11

.data
    variable: 10
    vector: .word 12, 4, 59, 9, 19, 8, 6, 18, 9, 19, 28, 12, 100

.text
    la $s6, variable #; s6 = address of variable
    la $s5, vector #; s5 = address of vector

    #; vector[12] = vector[6] + variable

    lw $t0, ($s6) #; t0 = *s6 (value at address s6, which is the value of the variable)
    lw $t1, 24($s5) #; t1 = s5[6] ; s5[6] = *(s5 + 6), but 6 rappresents words, not bytes, so 6 * 4 = 24 (which corresponds to vector[6])

    add $t0, $t0, $t1 #; t0 += t1 (which is vector[5])

    sw $t0, 48($s5) #; stores result of sum (from t0) to vector[12], but 12 is words, so 48 is bytes
; pdf 3 slide 17

.data

.text
    li $s0, 1 #; u = 1
    li $s1, 0 #; v = 0

    #; v = u * 256

    sll $s1, $s0, 8 #; s1 = s0 * 256

    #; which is the first value that breaks?

    #; a word ha 32 bits, multiplying by 256 means shifting by 8 bits
    #; this means that as soon as we have the 25 bit set to 1, the 1 is shifted out of 32
    #; 2^24 = 16777216

    li $s2, 16777216 #; 16777215 will work normally
    sll $s0, $s2, 8 #; becomes 0


; pdf 4 slide 34

.data 
    vector: .word 11, 35, 2, 7, 29, 95
    size: .word 6

.text
    #; find max value in vector

    la $t0, vector #; t0 = current address
    la $t1, vector #; t1 = end of vector

    la $t2, size #; t2 = address of vector size
    lw $t2, ($t2) #; t2 = vector size
    sll, $t2, $t2, 2 #; t2 *= 4, to accomodate words

    add $t1, $t1, $t2 #; t1 = end of vector + vector size

    lw $t2, ($t0) #; t2 = max value

    for:
        bgt $t0, $t1, endFor #; if current address > end of vector, end for 

        lw $t3, ($t0) #; load value from current address
        ble $t3, $t2, elseSmaller #; if current value <= max value, continue

        ifBigger:
            move $t2, $t3 #; max value = current value

        elseSmaller:

        addi $t0, $t0, 4 #; current address = next address
        j for #; repeat cycle

    endFor:
; pdf 4 slide 35

.data 
    vector: .word 4, -1, 5, 500, 0, 10000, -256
    size: .word 5
    sums: .word 0, 0

.text
    #; find max value in vector

    la $t0, vector #; t0 = current address
    la $t1, vector #; t1 = end of vector

    la $t2, size #; t2 = address of vector size
    lw $t2, ($t2) #; t2 = vector size
    sll, $t2, $t2, 2 #; t2 *= 4, to accomodate words

    add $t1, $t1, $t2 #; t1 = end of vector + vector size

    li $t2, 0 #; t2 = parity 
    li $t4, 0 #; t4 = even 
    li $t5, 0 #; t5 = odd 

    for:
        bgt $t0, $t1, endFor #; if current address > end of vector, end for 

        lw $t3, ($t0) #; load value from current address
        beq $t2, 1, ifIsOdd #; check if current parity is odd

        ifIsEven:
            add $t4, $t4, $t3 #; even += current value
            li $t2, 1 #; parity = odd
            j nextIteration
        ifIsOdd:
            add $t5, $t5, $t3 #; odd += current value
            li $t2, 0 #; parity = even

        nextIteration:
            addi $t0, $t0, 4 #; current address = next address
            j for #; repeat cycle

    endFor:

    la $t6, sums #; t6 = address of the result
    sw $t4, ($t6) #; t6[0] = even 
    sw $t5, 4($t6) #; t6[1] = odd; 4 is used instead of 1 because a word is 4 bytes long
; pdf 5 slide 10 

.data
    vector: .byte 1, 2, 3, 4 
.text
    #; the vector corresponds to the word 0x04030201
    #; which basically is 4, 3, 2, 1
    #; as it's rappresented in memory using little-endian

; pdf 6 slide 7 

.globl main

.data
    matrix: .word 2, -10, -10, -10, 2, -10, -10, -10, 2 
    length: .word 3 

.text
    main: 
        la $t0, matrix #; t0: matrix_address = matrix
        la $t1, length #; t1: matrix_length_address = length 
        lw $t1, ($t1) #; t1: matrix_length = *length
        move $t2, $t1 #; t2: jumps_to_do = matrix_length (to count how many jumps are needed to reach the end)

        addi $t1, $t1, 1 #; t1: jump_length = matrix_length += 1 (to jump to next diagonal cell)
        sll $t1, $t1, 2 #; t1: jump_length = jump_length * 4 (because words are 4 bytes long)

        li $t3, 0 #; t3: sum = 0 

        while:
            beq $t2, 0, end #; if jumps_to_do == 0 { end loop }

            lw $t4, ($t0) #; t4: value = *matrix_address
            add $t3, $t3, $t4 #; sum += value

            subi $t2, $t2, 1 #; jumps_to_do -= 1
            add $t0, $t0, $t1 #; matrix_address = matrix_address = jump_length
            j while
        end:

        print: 
            li $v0, 1 #; print integer
            move $a0, $t3 #; integer = sum
            syscall

        return: 
            li $v0, 17 #; exit
            li $a0, 0 #; result = 0
            syscall
; pdf 7 slide 22

.globl main

.data

.text
    main:

        li $a0, 5
        li $a1, -3
        li $a2, 9
        li $a3, 2
        jal avgOfSquareAbsSub
        
        print:
            move $a0, $v0 #; integer = formula 
            li $v0, 1 #; print integer
            syscall

        return:
            li $v0, 17 #; exit
            li $a0, 0 #; result = 0
            syscall

    avgOfSquareAbsSub:
        subi $sp, $sp, 8 #; ra, first result
        sw $ra, ($sp) 

        jal squareAbsSub #; x, y 
        sw $v0, 4($sp) #; t0: first = (|x|-|y|)^2

        move $a0, $a2
        move $a1, $a3
        jal squareAbsSub #; w, z
        move $t1, $v0 #; t1: second = (|w|-|z|)^2
        lw $t0, 4($sp) #; t0: first = (|x|-|y|)^2

        add $t0, $t0, $t1 #; first += second
        srl $v0, $t0, 1 #; numerator /= 2
        
        returnAvg:
            lw $ra, ($sp)
            addi $sp, $sp, 8
            jr $ra

    squareAbsSub:
        subi $sp, $sp, 4 #; ra
        sw $ra, ($sp)

        jal abs #; |x|
        move $t0, $v0 #; t0: abs_x = |x|

        move $a0, $a1 #; number = y
        jal abs #; |y|
        move $t1, $v0 #; t1: abs_y = |y|

        sub $t0, $t0, $t1 #; difference = |x| - |y|
        mul $v0, $t0, $t0 #; result = (|x| - |y|)^2
        
        returnSub:
            lw $ra, ($sp)
            addi $sp, $sp, 4
            jr $ra

    abs:
        bge $a0, 0, returnAbs #; if number >= 0, return 

        ca2:
            nor $a0, $a0, $zero #; number = bitwise not of number
            addi $a0, $a0, 1 #; number += 1
        
        returnAbs:
            move $v0, $a0 #; result = |number|
            jr $ra