Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Exercise Addition and subtraction are performed on 32 bit integer nu

ID: 3870422 • Letter: P

Question

Programming Exercise Addition and subtraction are performed on 32 bit integer numbers held in the general purpose registers (32 bit-wide each) inside the register file. The result is a 32 bit number itself. Two kinds of instructions are included in the MIPS instruction set to do integer addition and subtraction [5] Instructions for signed arithmetic: the 32 bit numbers are considered to be represented in 2's complement. The execution of the instruction (addition or subtraction) may generate an overflow, such as for add, addi, or an underflow for sub, Instructions for unsigned arithmetic: the 32 bit numbers are considered to be in standard binary representation. Executing the instruction will never generate an overflow error even if there is an actual overflow (the result cannot be represented with only 32 bits), such as, addiu, addu, subu. Some basic arithmetic operations include: Instructions Operation add Sd, Ss, St addi St, Ss, imm sub Sd, Ss, St div Ss, St mult Ss, St Sd=$s + St St = Ss + imm Sd=Ss-St $LO = $s * $t

Explanation / Answer


main:
    li $s0, 5       # a = 5
    li $s1, 5       # b = 5
    li $t0, 0        # i = 0

    LoopI:

        slt $t3, $t0, $s0         #if (i < a) $t3 = 1
        beq $t3, $zero, Exit    #if $t3 != 0, execute next lines
        lw $t1, 0        #j = 0

        LoopJ:
            slt $t2, $t1, $s1       #if (j < b) $t2 = 1
            beq $t2, $zero, LoopI   #if $t2 != 0, execute next lines
            sll $t2, $t0, 2         #$t2 = i * 4
            sll $t2, $t2, 1         #$t2 = $t2 * 2
            mul $t4, $t1, 2  
            add $t2, $t4, $t2       #$t2 = address of C
            lw $t3, 0($t2)     #$t3 = C[2 * j]
            add $t3, $t0, $t4       #C[2 * j] = i+ 2 * j
            addi $t1, $t1, 1        #j++
            j LoopJ   
        addi $t0, $t0, 1        #i++
        j LoopI

    Exit:
        li $v0, 10
                syscall