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

Swap1.s: .text main: la $a0,n1 la $a1,n2 jal swap li $v0,1 # print n1 and n2; sh

ID: 3706428 • Letter: S

Question

Swap1.s:

.text

main:

la $a0,n1

la $a1,n2

jal swap

li $v0,1 # print n1 and n2; should be 27 and 14

lw $a0,n1

syscall

li $v0,11

li $a0,' '

syscall

li $v0,1

lw $a0,n2

syscall

li $v0,11

li $a0,' '

syscall

li $v0,10 # exit

syscall

swap:

addi $sp, $sp, -16

sw $ra, 12($sp)

sw $a0, 8($sp)

sw $a1, 4($sp)

lw $s0,0($a0)

sw $s0,0($sp)

lw $t0,0($a1)

sw $t0,0($a0)

lw $t0,0($sp)

sw $t0,0($a1)

lw $ra, 12($sp)

addi $sp, $sp, 16

jr $ra

.data

n1: .word 14

n2: .word 27

(Exercise) Create swap2.s Now modify your solution (swap1.s) to implement the following buggy version of the swap procedure. void swap (int *px, int *py) int *temp; temp-px py*temp; Q1. The bug in swap2.s is that the temp pointer is dereferenced without being initialized. Why might a programmer not notice this even after testing the buggy swap? In other words: what situation would allow buggy swap to seem to work correctly?

Explanation / Answer

CODING

.data
n1: .word 14
n2: .word 27
temp: .word 0
.text
main:
la $a0,n1
la $a1,n2
jal swap
li $v0,1 # print n1 and n2; should be 27 and 14
lw $a0,n1
syscall
li $v0,11
li $a0,' '
syscall
li $v0,1
lw $a0,n2
syscall
li $v0,11
li $a0,' '
syscall
li $v0,10 # exit
syscall
swap:
la $t1,temp
lw $t0,0($a0)
sw $t0,0($t1)
lw $t0,0($a1)
sw $t0,0($a0)
lw $t0,0($t1)
sw $t0,0($a1)
jr $ra